Skip to content

Commit

Permalink
🚧 test generator
Browse files Browse the repository at this point in the history
  • Loading branch information
homersimpsons committed Apr 1, 2024
1 parent 4d3e2d2 commit cbe7d14
Show file tree
Hide file tree
Showing 14 changed files with 535 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"image": "mcr.microsoft.com/devcontainers/universal:2",
"features": {
"ghcr.io/devcontainers/features/php:1": {
"version": "8.3",
"installComposer": true
}
}
}
20 changes: 10 additions & 10 deletions exercises/practice/list-ops/ListOpsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function setUpBeforeClass(): void
/**
* @testdox append entries to a list and return the new list -> empty lists
*/
public function testAppendEmptyLists()
public function testAppendEntriesToAListAndReturnTheNewListEmptyLists()
{
$listOps = new ListOps();
$this->assertEquals([], $listOps->append([], []));
Expand All @@ -45,25 +45,25 @@ public function testAppendEmptyLists()
/**
* @testdox append entries to a list and return the new list -> list to empty list
*/
public function testAppendNonEmptyListToEmptyList()
public function testAppendEntriesToAListAndReturnTheNewListListToEmptyList()
{
$listOps = new ListOps();
$this->assertEquals([1, 2, 3, 4], $listOps->append([1, 2, 3, 4], []));
$this->assertEquals([1, 2, 3, 4], $listOps->append([], [1, 2, 3, 4]));
}

/**
* @testdox append entries to a list and return the new list -> empty list to list
*/
public function testAppendEmptyListToNonEmptyList()
public function testAppendEntriesToAListAndReturnTheNewListEmptyListToList()
{
$listOps = new ListOps();
$this->assertEquals([1, 2, 3, 4], $listOps->append([], [1, 2, 3, 4]));
$this->assertEquals([1, 2, 3, 4], $listOps->append([1, 2, 3, 4], []));
}

/**
* @testdox append entries to a list and return the new list -> non-empty lists
*/
public function testAppendNonEmptyLists()
public function testAppendEntriesToAListAndReturnTheNewListNonEmptyLists()
{
$listOps = new ListOps();
$this->assertEquals([1, 2, 2, 3, 4, 5], $listOps->append([1, 2], [2, 3, 4, 5]));
Expand All @@ -72,16 +72,16 @@ public function testAppendNonEmptyLists()
/**
* @testdox concatenate a list of lists -> empty list
*/
public function testConcatEmptyLists()
public function testConcatenateAListOfListsEmptyList()
{
$listOps = new ListOps();
$this->assertEquals([], $listOps->concat([], []));
$this->assertEquals([], $listOps->concat());
}

/**
* @testdox concatenate a list of lists -> list of lists
*/
public function testConcatLists()
public function testConcatenateAListOfListsListOfLists()
{
$listOps = new ListOps();
$this->assertEquals([1, 2, 3, 4, 5, 6], $listOps->concat([1, 2], [3], [], [4, 5, 6]));
Expand All @@ -90,7 +90,7 @@ public function testConcatLists()
/**
* @testdox concatenate a list of lists -> list of nested lists
*/
public function testConcatNestedLists()
public function testConcatenateAListOfListsListOfNestedLists()
{
$listOps = new ListOps();
$this->assertEquals([[1], [2], [3], [], [4, 5, 6]], $listOps->concat([[1], [2]], [[3]], [[]], [[4, 5, 6]]));
Expand Down
226 changes: 226 additions & 0 deletions exercises/practice/list-ops/ListOpsTest.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php
/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/
declare(strict_types=1);
use PHPUnit\Framework\ExpectationFailedException;
class ListOpsTest extends PHPUnit\Framework\TestCase
{
public static function setUpBeforeClass(): void
{
require_once 'ListOps.php';
}
{% set case0 = cases[0] -%}
{% for case in case0.cases -%}
/**
* @testdox {{ case0.description }} -> {{ case.description }}
*/
public function {{ testfn(case0.description ~ ' ' ~ case.description) }}()
{
$listOps = new ListOps();
$this->assertEquals({{ export(case.expected) }}, $listOps->{{ case.property }}({{ export(case.input.list1) }}, {{ export(case.input.list2) }}));
}
{% endfor -%}
{% set case1 = cases[1] -%}
{% for case in case1.cases -%}
/**
* @testdox {{ case1.description }} -> {{ case.description }}
*/
public function {{ testfn(case1.description ~ ' ' ~ case.description) }}()
{
$listOps = new ListOps();
$this->assertEquals({{ export(case.expected) }}, $listOps->{{ case.property }}({{ case.input.lists | map(l => export(l)) | join(', ') }}));
}
{% endfor -%}
/**
* @testdox filter list returning only values that satisfy the filter function -> empty list
*/
public function testFilterEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
[],
$listOps->filter(static fn ($el) => $el % 2 === 1, [])
);
}
/**
* @testdox filter list returning only values that satisfy the filter function -> non empty list
*/
public function testFilterNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
[1, 3, 5],
$listOps->filter(static fn ($el) => $el % 2 === 1, [1, 2, 3, 5])
);
}
/**
* @testdox returns the length of a list -> empty list
*/
public function testLengthEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(0, $listOps->length([]));
}
/**
* @testdox returns the length of a list -> non-empty list
*/
public function testLengthNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(4, $listOps->length([1, 2, 3, 4]));
}
/**
* @testdox returns a list of elements whose values equal the list value transformed by the mapping function -> empty list
*/
public function testMapEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
[],
$listOps->map(static fn ($el) => $el + 1, [])
);
}
/**
* @testdox returns a list of elements whose values equal the list value transformed by the mapping function -> non-empty list
*/
public function testMapNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
[2, 4, 6, 8],
$listOps->map(static fn ($el) => $el + 1, [1, 3, 5, 7])
);
}
/**
* @testdox folds (reduces) the given list from the left with a function -> empty list
*/
public function testFoldlEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
2,
$listOps->foldl(static fn ($acc, $el) => $el * $acc, [], 2)
);
}
/**
* @testdox folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list
*/
public function testFoldlDirectionIndependentNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
15,
$listOps->foldl(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5)
);
}
/**
* @testdox folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list
*/
public function testFoldlDirectionDependentNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
64,
$listOps->foldl(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24)
);
}
/**
* @testdox folds (reduces) the given list from the right with a function -> empty list
*/
public function testFoldrEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
2,
$listOps->foldr(static fn ($acc, $el) => $el * $acc, [], 2)
);
}
/**
* @testdox folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list
*/
public function testFoldrDirectionIndependentNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
15,
$listOps->foldr(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5)
);
}
/**
* @testdox folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list
*/
public function testFoldrDirectionDependentNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
9,
$listOps->foldr(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24)
);
}
/**
* @testdox reverse the elements of a list -> empty list
*/
public function testReverseEmptyList()
{
$listOps = new ListOps();
$this->assertEquals([], $listOps->reverse([]));
}
/**
* @testdox reverse the elements of a list -> non-empty list
*/
public function testReverseNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals([7, 5, 3, 1], $listOps->reverse([1, 3, 5, 7]));
}
/**
* @testdox reverse the elements of a list -> list of lists is not flattened
*/
public function testReverseNonEmptyListIsNotFlattened()
{
$listOps = new ListOps();
$this->assertEquals([[4, 5, 6], [], [3], [1, 2]], $listOps->reverse([[1, 2], [3], [], [4, 5, 6]]));
}
}
1 change: 1 addition & 0 deletions test-generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
Loading

0 comments on commit cbe7d14

Please sign in to comment.