ILIAS  release_8 Revision v8.23
RangeTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
7 
11 class RangeTest extends TestCase
12 {
13  public function testFactory(): Range
14  {
15  $f = new ILIAS\Data\Factory();
16  $range = $f->range(1, 2);
17  $this->assertInstanceOf(Range::class, $range);
18  return $range;
19  }
20 
24  public function testValues(Range $range): void
25  {
26  $this->assertEquals(1, $range->getStart());
27  $this->assertEquals(2, $range->getLength());
28  }
29 
33  public function testEndCalculation(Range $range): void
34  {
35  $this->assertEquals(3, $range->getEnd());
36  }
37 
41  public function testWithLength(Range $range): Range
42  {
43  $range = $range->withLength(3);
44  $this->assertEquals(1, $range->getStart());
45  $this->assertEquals(3, $range->getLength());
46  $this->assertEquals(4, $range->getEnd());
47  return $range;
48  }
49 
53  public function testWithStart(Range $range): Range
54  {
55  $range = $range->withStart(3);
56  $this->assertEquals(3, $range->getStart());
57  $this->assertEquals(3, $range->getLength());
58  $this->assertEquals(6, $range->getEnd());
59  return $range;
60  }
61 
65  public function testUnpack(Range $range): void
66  {
67  $this->assertEquals(
68  [3,3],
69  $range->unpack()
70  );
71  }
72 
76  public function testNegativeStart(Range $range): void
77  {
78  $this->expectException(InvalidArgumentException::class);
79  $range = $range->withStart(-5);
80  }
81 
85  public function testNegativeLength(Range $range): void
86  {
87  $this->expectException(InvalidArgumentException::class);
88  $range = $range->withLength(-1);
89  }
90 
91  public function testConstructionWrongStart(): void
92  {
93  $this->expectException(InvalidArgumentException::class);
94  $f = new ILIAS\Data\Factory();
95  $range = $f->range(-1, 2);
96  }
97 
98  public function testConstructionWrongLength(): void
99  {
100  $this->expectException(InvalidArgumentException::class);
101  $f = new ILIAS\Data\Factory();
102  $range = $f->range(1, -2);
103  }
104 
108  public function testCroppedTo($start, $length, $max, $has_changed): void
109  {
110  $f = new ILIAS\Data\Factory();
111  $range = $f->range($start, $length);
112  $cropped = $range->croppedTo($max);
113 
114  if (!$has_changed) {
115  $this->assertEquals($range, $cropped);
116  } else {
117  $this->assertEquals(min($max, $start), $cropped->getStart());
118  $this->assertEquals($max, $cropped->getEnd());
119  }
120  }
121 
122  public function cropCases(): array
123  {
124  return [
125  [0, 100, 1000, false],
126  [0, 1000, 100, true],
127  [50, 100, 75, true],
128  [50, 100, 200, false],
129  [100, 100, 50, true]
130  ];
131  }
132 }
testNegativeStart(Range $range)
testFactory
Definition: RangeTest.php:76
testConstructionWrongStart()
Definition: RangeTest.php:91
testNegativeLength(Range $range)
testFactory
Definition: RangeTest.php:85
testFactory()
Definition: RangeTest.php:13
testEndCalculation(Range $range)
testFactory
Definition: RangeTest.php:33
testConstructionWrongLength()
Definition: RangeTest.php:98
getEnd()
getEnd will return the (excluded) endpoint.
Definition: Range.php:76
testWithStart(Range $range)
testWithLength
Definition: RangeTest.php:53
withLength(int $length)
Definition: Range.php:93
testWithLength(Range $range)
testFactory
Definition: RangeTest.php:41
testCroppedTo($start, $length, $max, $has_changed)
cropCases
Definition: RangeTest.php:108
withStart(int $start)
Definition: Range.php:85
A simple class to express a range of whole positive numbers.
Definition: Range.php:30
testValues(Range $range)
testFactory
Definition: RangeTest.php:24
testUnpack(Range $range)
testWithStart
Definition: RangeTest.php:65