Skip to content

Commit cbe7d14

Browse files
committed
🚧 test generator
1 parent 4d3e2d2 commit cbe7d14

14 files changed

+535
-10
lines changed

.devcontainer/devcontainer.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"image": "mcr.microsoft.com/devcontainers/universal:2",
3+
"features": {
4+
"ghcr.io/devcontainers/features/php:1": {
5+
"version": "8.3",
6+
"installComposer": true
7+
}
8+
}
9+
}

exercises/practice/list-ops/ListOpsTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function setUpBeforeClass(): void
3636
/**
3737
* @testdox append entries to a list and return the new list -> empty lists
3838
*/
39-
public function testAppendEmptyLists()
39+
public function testAppendEntriesToAListAndReturnTheNewListEmptyLists()
4040
{
4141
$listOps = new ListOps();
4242
$this->assertEquals([], $listOps->append([], []));
@@ -45,25 +45,25 @@ public function testAppendEmptyLists()
4545
/**
4646
* @testdox append entries to a list and return the new list -> list to empty list
4747
*/
48-
public function testAppendNonEmptyListToEmptyList()
48+
public function testAppendEntriesToAListAndReturnTheNewListListToEmptyList()
4949
{
5050
$listOps = new ListOps();
51-
$this->assertEquals([1, 2, 3, 4], $listOps->append([1, 2, 3, 4], []));
51+
$this->assertEquals([1, 2, 3, 4], $listOps->append([], [1, 2, 3, 4]));
5252
}
5353

5454
/**
5555
* @testdox append entries to a list and return the new list -> empty list to list
5656
*/
57-
public function testAppendEmptyListToNonEmptyList()
57+
public function testAppendEntriesToAListAndReturnTheNewListEmptyListToList()
5858
{
5959
$listOps = new ListOps();
60-
$this->assertEquals([1, 2, 3, 4], $listOps->append([], [1, 2, 3, 4]));
60+
$this->assertEquals([1, 2, 3, 4], $listOps->append([1, 2, 3, 4], []));
6161
}
6262

6363
/**
6464
* @testdox append entries to a list and return the new list -> non-empty lists
6565
*/
66-
public function testAppendNonEmptyLists()
66+
public function testAppendEntriesToAListAndReturnTheNewListNonEmptyLists()
6767
{
6868
$listOps = new ListOps();
6969
$this->assertEquals([1, 2, 2, 3, 4, 5], $listOps->append([1, 2], [2, 3, 4, 5]));
@@ -72,16 +72,16 @@ public function testAppendNonEmptyLists()
7272
/**
7373
* @testdox concatenate a list of lists -> empty list
7474
*/
75-
public function testConcatEmptyLists()
75+
public function testConcatenateAListOfListsEmptyList()
7676
{
7777
$listOps = new ListOps();
78-
$this->assertEquals([], $listOps->concat([], []));
78+
$this->assertEquals([], $listOps->concat());
7979
}
8080

8181
/**
8282
* @testdox concatenate a list of lists -> list of lists
8383
*/
84-
public function testConcatLists()
84+
public function testConcatenateAListOfListsListOfLists()
8585
{
8686
$listOps = new ListOps();
8787
$this->assertEquals([1, 2, 3, 4, 5, 6], $listOps->concat([1, 2], [3], [], [4, 5, 6]));
@@ -90,7 +90,7 @@ public function testConcatLists()
9090
/**
9191
* @testdox concatenate a list of lists -> list of nested lists
9292
*/
93-
public function testConcatNestedLists()
93+
public function testConcatenateAListOfListsListOfNestedLists()
9494
{
9595
$listOps = new ListOps();
9696
$this->assertEquals([[1], [2], [3], [], [4, 5, 6]], $listOps->concat([[1], [2]], [[3]], [[]], [[4, 5, 6]]));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
use PHPUnit\Framework\ExpectationFailedException;
28+
29+
class ListOpsTest extends PHPUnit\Framework\TestCase
30+
{
31+
public static function setUpBeforeClass(): void
32+
{
33+
require_once 'ListOps.php';
34+
}
35+
36+
{% set case0 = cases[0] -%}
37+
{% for case in case0.cases -%}
38+
/**
39+
* @testdox {{ case0.description }} -> {{ case.description }}
40+
*/
41+
public function {{ testfn(case0.description ~ ' ' ~ case.description) }}()
42+
{
43+
$listOps = new ListOps();
44+
$this->assertEquals({{ export(case.expected) }}, $listOps->{{ case.property }}({{ export(case.input.list1) }}, {{ export(case.input.list2) }}));
45+
}
46+
47+
{% endfor -%}
48+
49+
{% set case1 = cases[1] -%}
50+
{% for case in case1.cases -%}
51+
/**
52+
* @testdox {{ case1.description }} -> {{ case.description }}
53+
*/
54+
public function {{ testfn(case1.description ~ ' ' ~ case.description) }}()
55+
{
56+
$listOps = new ListOps();
57+
$this->assertEquals({{ export(case.expected) }}, $listOps->{{ case.property }}({{ case.input.lists | map(l => export(l)) | join(', ') }}));
58+
}
59+
60+
{% endfor -%}
61+
62+
/**
63+
* @testdox filter list returning only values that satisfy the filter function -> empty list
64+
*/
65+
public function testFilterEmptyList()
66+
{
67+
$listOps = new ListOps();
68+
$this->assertEquals(
69+
[],
70+
$listOps->filter(static fn ($el) => $el % 2 === 1, [])
71+
);
72+
}
73+
74+
/**
75+
* @testdox filter list returning only values that satisfy the filter function -> non empty list
76+
*/
77+
public function testFilterNonEmptyList()
78+
{
79+
$listOps = new ListOps();
80+
$this->assertEquals(
81+
[1, 3, 5],
82+
$listOps->filter(static fn ($el) => $el % 2 === 1, [1, 2, 3, 5])
83+
);
84+
}
85+
86+
/**
87+
* @testdox returns the length of a list -> empty list
88+
*/
89+
public function testLengthEmptyList()
90+
{
91+
$listOps = new ListOps();
92+
$this->assertEquals(0, $listOps->length([]));
93+
}
94+
95+
/**
96+
* @testdox returns the length of a list -> non-empty list
97+
*/
98+
public function testLengthNonEmptyList()
99+
{
100+
$listOps = new ListOps();
101+
$this->assertEquals(4, $listOps->length([1, 2, 3, 4]));
102+
}
103+
104+
/**
105+
* @testdox returns a list of elements whose values equal the list value transformed by the mapping function -> empty list
106+
*/
107+
public function testMapEmptyList()
108+
{
109+
$listOps = new ListOps();
110+
$this->assertEquals(
111+
[],
112+
$listOps->map(static fn ($el) => $el + 1, [])
113+
);
114+
}
115+
116+
/**
117+
* @testdox returns a list of elements whose values equal the list value transformed by the mapping function -> non-empty list
118+
*/
119+
public function testMapNonEmptyList()
120+
{
121+
$listOps = new ListOps();
122+
$this->assertEquals(
123+
[2, 4, 6, 8],
124+
$listOps->map(static fn ($el) => $el + 1, [1, 3, 5, 7])
125+
);
126+
}
127+
128+
/**
129+
* @testdox folds (reduces) the given list from the left with a function -> empty list
130+
*/
131+
public function testFoldlEmptyList()
132+
{
133+
$listOps = new ListOps();
134+
$this->assertEquals(
135+
2,
136+
$listOps->foldl(static fn ($acc, $el) => $el * $acc, [], 2)
137+
);
138+
}
139+
140+
/**
141+
* @testdox folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list
142+
*/
143+
public function testFoldlDirectionIndependentNonEmptyList()
144+
{
145+
$listOps = new ListOps();
146+
$this->assertEquals(
147+
15,
148+
$listOps->foldl(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5)
149+
);
150+
}
151+
152+
/**
153+
* @testdox folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list
154+
*/
155+
public function testFoldlDirectionDependentNonEmptyList()
156+
{
157+
$listOps = new ListOps();
158+
$this->assertEquals(
159+
64,
160+
$listOps->foldl(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24)
161+
);
162+
}
163+
164+
/**
165+
* @testdox folds (reduces) the given list from the right with a function -> empty list
166+
*/
167+
public function testFoldrEmptyList()
168+
{
169+
$listOps = new ListOps();
170+
$this->assertEquals(
171+
2,
172+
$listOps->foldr(static fn ($acc, $el) => $el * $acc, [], 2)
173+
);
174+
}
175+
176+
/**
177+
* @testdox folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list
178+
*/
179+
public function testFoldrDirectionIndependentNonEmptyList()
180+
{
181+
$listOps = new ListOps();
182+
$this->assertEquals(
183+
15,
184+
$listOps->foldr(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5)
185+
);
186+
}
187+
188+
/**
189+
* @testdox folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list
190+
*/
191+
public function testFoldrDirectionDependentNonEmptyList()
192+
{
193+
$listOps = new ListOps();
194+
$this->assertEquals(
195+
9,
196+
$listOps->foldr(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24)
197+
);
198+
}
199+
200+
/**
201+
* @testdox reverse the elements of a list -> empty list
202+
*/
203+
public function testReverseEmptyList()
204+
{
205+
$listOps = new ListOps();
206+
$this->assertEquals([], $listOps->reverse([]));
207+
}
208+
209+
/**
210+
* @testdox reverse the elements of a list -> non-empty list
211+
*/
212+
public function testReverseNonEmptyList()
213+
{
214+
$listOps = new ListOps();
215+
$this->assertEquals([7, 5, 3, 1], $listOps->reverse([1, 3, 5, 7]));
216+
}
217+
218+
/**
219+
* @testdox reverse the elements of a list -> list of lists is not flattened
220+
*/
221+
public function testReverseNonEmptyListIsNotFlattened()
222+
{
223+
$listOps = new ListOps();
224+
$this->assertEquals([[4, 5, 6], [], [3], [1, 2]], $listOps->reverse([[1, 2], [3], [], [4, 5, 6]]));
225+
}
226+
}

test-generator/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

0 commit comments

Comments
 (0)