LinesOfCodeTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/lines-of-code.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\LinesOfCode;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * @covers \SebastianBergmann\LinesOfCode\LinesOfCode
  14. *
  15. * @small
  16. */
  17. final class LinesOfCodeTest extends TestCase
  18. {
  19. /**
  20. * @testdox Has Lines of Code (LOC)
  21. */
  22. public function testHasLinesOfCode(): void
  23. {
  24. $this->assertSame(1, $this->linesOfCode()->linesOfCode());
  25. }
  26. /**
  27. * @testdox Has Comment Lines of Code (CLOC)
  28. */
  29. public function testHasCommentLinesOfCode(): void
  30. {
  31. $this->assertSame(1, $this->linesOfCode()->commentLinesOfCode());
  32. }
  33. /**
  34. * @testdox Has Non-Comment Lines of Code (NCLOC)
  35. */
  36. public function testHasNonCommentLinesOfCode(): void
  37. {
  38. $this->assertSame(0, $this->linesOfCode()->nonCommentLinesOfCode());
  39. }
  40. /**
  41. * @testdox Has Logical Lines of Code (LLOC)
  42. */
  43. public function testHasLogicalLinesOfCode(): void
  44. {
  45. $this->assertSame(0, $this->linesOfCode()->logicalLinesOfCode());
  46. }
  47. public function testLinesOfCodeCannotBeNegative(): void
  48. {
  49. $this->expectException(NegativeValueException::class);
  50. $this->expectExceptionMessage('$linesOfCode must not be negative');
  51. new LinesOfCode(-1, 0, 0, 0);
  52. }
  53. public function testCommentLinesOfCodeCannotBeNegative(): void
  54. {
  55. $this->expectException(NegativeValueException::class);
  56. $this->expectExceptionMessage('$commentLinesOfCode must not be negative');
  57. new LinesOfCode(0, -1, 0, 0);
  58. }
  59. public function testNonCommentLinesOfCodeCannotBeNegative(): void
  60. {
  61. $this->expectException(NegativeValueException::class);
  62. $this->expectExceptionMessage('$nonCommentLinesOfCode must not be negative');
  63. new LinesOfCode(0, 0, -1, 0);
  64. }
  65. public function testLogicalLinesOfCodeCannotBeNegative(): void
  66. {
  67. $this->expectException(NegativeValueException::class);
  68. $this->expectExceptionMessage('$logicalLinesOfCode must not be negative');
  69. new LinesOfCode(0, 0, 0, -1);
  70. }
  71. /**
  72. * @testdox Lines of Code = Comment Lines of Code + Non-Comment Lines of Code
  73. */
  74. public function testNumbersHaveToMakeSense(): void
  75. {
  76. $this->expectException(IllogicalValuesException::class);
  77. $this->expectExceptionMessage('$linesOfCode !== $commentLinesOfCode + $nonCommentLinesOfCode');
  78. new LinesOfCode(1, 2, 2, 0);
  79. }
  80. public function testTwoInstancesCanBeAdded(): void
  81. {
  82. $a = new LinesOfCode(2, 1, 1, 1);
  83. $b = new LinesOfCode(4, 2, 2, 2);
  84. $sum = $a->plus($b);
  85. $this->assertSame(6, $sum->linesOfCode());
  86. $this->assertSame(3, $sum->commentLinesOfCode());
  87. $this->assertSame(3, $sum->nonCommentLinesOfCode());
  88. $this->assertSame(3, $sum->logicalLinesOfCode());
  89. }
  90. private function linesOfCode(): LinesOfCode
  91. {
  92. return new LinesOfCode(1, 1, 0, 0);
  93. }
  94. }