Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JUnit @Nested classes for tests instead of comment sections #2960

Draft
wants to merge 55 commits into
base: version/1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
9260254
Add toolchain
bvkatwijk Dec 28, 2024
967a676
Add mapTry to Option
bvkatwijk Dec 28, 2024
088a2b3
Update OptionTest.java
bvkatwijk Dec 28, 2024
390b8db
Update Option.java
bvkatwijk Dec 28, 2024
b416dd9
Revert "Add toolchain"
bvkatwijk Dec 29, 2024
8e235c7
Create nested.sh
bvkatwijk Dec 30, 2024
690e6b4
Update nested.sh
bvkatwijk Dec 30, 2024
5162880
Update nested.sh
bvkatwijk Dec 30, 2024
843ffb1
Update nested.sh
bvkatwijk Dec 30, 2024
9c86b23
Update nested.sh
bvkatwijk Dec 30, 2024
233e8e3
Update nested.sh
bvkatwijk Dec 30, 2024
c77e0fa
Update nested.sh
bvkatwijk Dec 30, 2024
b4d3829
Update nested.sh
bvkatwijk Dec 30, 2024
4c4b05f
Update nested.sh
bvkatwijk Dec 30, 2024
d3c06e6
Update nested.sh
bvkatwijk Dec 30, 2024
e04ff86
Update nested.sh
bvkatwijk Dec 30, 2024
ca6be88
Update nested.sh
bvkatwijk Dec 30, 2024
d1eb943
Update nested.sh
bvkatwijk Dec 30, 2024
28c5ec3
Update nested.sh
bvkatwijk Dec 30, 2024
a0c066a
Update nested.sh
bvkatwijk Dec 30, 2024
ae95b4f
Update nested.sh
bvkatwijk Dec 30, 2024
6a4a981
Update nested.sh
bvkatwijk Dec 30, 2024
f056d6f
Create dock.sh
bvkatwijk Dec 30, 2024
54d3dfe
Update nested.sh
bvkatwijk Dec 30, 2024
2657ddc
Update nested.sh
bvkatwijk Dec 30, 2024
d79f15e
Update nested.sh
bvkatwijk Dec 30, 2024
eef0745
Update nested.sh
bvkatwijk Dec 30, 2024
c9ea9a0
Update nested.sh
bvkatwijk Dec 31, 2024
4f60de3
Update nested.sh
bvkatwijk Dec 31, 2024
2834ab6
Update nested.sh
bvkatwijk Dec 31, 2024
bb84724
Update nested.sh
bvkatwijk Dec 31, 2024
8367deb
Update nested.sh
bvkatwijk Dec 31, 2024
f1a082c
Update nested.sh
bvkatwijk Dec 31, 2024
4c64244
Update nested.sh
bvkatwijk Dec 31, 2024
ff39540
Update nested.sh
bvkatwijk Dec 31, 2024
af70e96
Update nested.sh
bvkatwijk Dec 31, 2024
4d102bf
Update CheckedConsumerTest.java
bvkatwijk Dec 31, 2024
fcdf049
Update API.java
bvkatwijk Dec 31, 2024
06ff407
Update CheckedPredicateTest.java
bvkatwijk Dec 31, 2024
94df49d
Update CheckedRunnableTest.java
bvkatwijk Dec 31, 2024
3a42cd1
Update Generator.sc
bvkatwijk Jan 1, 2025
28c95fd
Update Generator.sc
bvkatwijk Jan 1, 2025
8d6dfb5
Update Generator.sc
bvkatwijk Jan 1, 2025
d637330
Update Generator.sc
bvkatwijk Jan 1, 2025
4d11a54
Update APITest.java
bvkatwijk Jan 1, 2025
a074c5d
Update ValidationTest.java
bvkatwijk Jan 1, 2025
c7a780d
Update ArrayTest.java
bvkatwijk Jan 1, 2025
7a6073c
Update PredicatesTest.java
bvkatwijk Jan 1, 2025
3f4b02f
Update MatchTest.java
bvkatwijk Jan 1, 2025
6adb5ca
Update IterableTest.java
bvkatwijk Jan 1, 2025
ad34127
Delete dock.sh
bvkatwijk Jan 3, 2025
f0bbb9e
Revert "Update Option.java"
bvkatwijk Jan 3, 2025
7a7a534
Revert "Update OptionTest.java"
bvkatwijk Jan 3, 2025
8dec840
Revert "Add mapTry to Option"
bvkatwijk Jan 3, 2025
1feab74
Revert "Update API.java"
bvkatwijk Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 147 additions & 131 deletions generator/Generator.sc
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,7 @@ def generateTestClasses(): Unit = {
genVavrFile("io.vavr", s"APITest", baseDir = TARGET_TEST)((im: ImportManager, packageName, className) => {

val assertThat = im.getStatic("org.assertj.core.api.Assertions.assertThat")
val nested = im.getType("org.junit.jupiter.api.Nested")
val test = im.getType("org.junit.jupiter.api.Test")

val API = im.getType("io.vavr.API")
Expand Down Expand Up @@ -2914,9 +2915,13 @@ def generateTestClasses(): Unit = {
$AssertionsExtensions.assertThat($API.class).isNotInstantiable();
}

// -- shortcuts
@Nested
class Shortcuts {

${genShortcutsTests(im, packageName, className)}

}

${genShortcutsTests(im, packageName, className)}

//
// Alias should return not null.
Expand All @@ -2925,157 +2930,168 @@ def generateTestClasses(): Unit = {

${genAliasesTests(im, packageName, className)}

// -- run

@$test
public void shouldRunUnitAndReturnVoid() {
int[] i = { 0 };
Void nothing = run(() -> i[0]++);
$assertThat(nothing).isNull();
$assertThat(i[0]).isEqualTo(1);
}
@Nested
class Run {

// -- For
@$test
public void shouldRunUnitAndReturnVoid() {
int[] i = { 0 };
Void nothing = run(() -> i[0]++);
$assertThat(nothing).isNull();
$assertThat(i[0]).isEqualTo(1);
}

@$test
public void shouldIterateFor1UsingSimpleYield() {
final $ListType<Integer> list = List.of(1, 2, 3);
final $ListType<Integer> actual = For(list).yield().toList();
$assertThat(actual).isEqualTo(list);
}

${(1 to N).gen(i => xs"""
@$test
public void shouldIterateFor$ListType$i() {
final $ListType<Integer> result = For(
${(1 to i).gen(j => s"$ListType.of(1, 2, 3)")(",\n")}
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}).toList();
$assertThat(result.length()).isEqualTo((int) Math.pow(3, $i));
$assertThat(result.head()).isEqualTo($i);
$assertThat(result.last()).isEqualTo(3 * $i);
}
""")("\n\n")}

${monadicTypesFor.gen(mtype => (1 to N).gen(i => { xs"""
@$test
public void shouldIterateFor$mtype$i() {
final $mtype<Integer> result = For(
${(1 to i).gen(j => s"$mtype.of($j)")(",\n")}
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")});
$assertThat(result.get()).isEqualTo(${(1 to i).sum});
}
"""})("\n\n"))("\n\n")}
@Nested
class For {

${monadicFunctionTypesFor.gen(mtype => (1 to N).gen(i => { xs"""
@$test
public void shouldIterateFor$mtype$i() {
final ${if(mtype == "Either") mtype + "<Object, Integer>" else mtype + "<Integer>"}result = For(
${(1 to i).gen(j => s"${if(mtype == "Either") mtype + s".right($j)" else mtype + s".of(() -> $j)"}")(",\n")}
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")});
$assertThat(result.get()).isEqualTo(${(1 to i).sum});
}
"""})("\n\n"))("\n\n")}
@$test
public void shouldIterateFor1UsingSimpleYield() {
final $ListType<Integer> list = List.of(1, 2, 3);
final $ListType<Integer> actual = For(list).yield().toList();
$assertThat(actual).isEqualTo(list);
}

@$test
public void shouldIterateNestedFor() {
final $ListType<String> result =
For(${im.getType("java.util.Arrays")}.asList(1, 2), i ->
For(${im.getType("io.vavr.collection.List")}.of('a', 'b')).yield(c -> i + ":" + c)).toList();
assertThat(result).isEqualTo($ListType.of("1:a", "1:b", "2:a", "2:b"));
}
${(1 to N).gen(i => xs"""
@$test
public void shouldIterateFor$ListType$i() {
final $ListType<Integer> result = For(
${(1 to i).gen(j => s"$ListType.of(1, 2, 3)")(",\n")}
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}).toList();
$assertThat(result.length()).isEqualTo((int) Math.pow(3, $i));
$assertThat(result.head()).isEqualTo($i);
$assertThat(result.last()).isEqualTo(3 * $i);
}
""")("\n\n")}

// -- Match
${monadicTypesFor.gen(mtype => (1 to N).gen(i => { xs"""
@$test
public void shouldIterateFor$mtype$i() {
final $mtype<Integer> result = For(
${(1 to i).gen(j => s"$mtype.of($j)")(",\n")}
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")});
$assertThat(result.get()).isEqualTo(${(1 to i).sum});
}
"""})("\n\n"))("\n\n")}

@$test
public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndSupplier() {
final Match.Case<Object, Integer> _case = Case($$(ignored -> true), ignored -> 1);
assertThat(_case.isDefinedAt(null)).isTrue();
assertThat(_case.apply(null)).isEqualTo(1);
}
${monadicFunctionTypesFor.gen(mtype => (1 to N).gen(i => { xs"""
@$test
public void shouldIterateFor$mtype$i() {
final ${if(mtype == "Either") mtype + "<Object, Integer>" else mtype + "<Integer>"}result = For(
${(1 to i).gen(j => s"${if(mtype == "Either") mtype + s".right($j)" else mtype + s".of(() -> $j)"}")(",\n")}
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")});
$assertThat(result.get()).isEqualTo(${(1 to i).sum});
}
"""})("\n\n"))("\n\n")}

@$test
public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndSupplier() {
assertThat(Case($$(ignored -> false), ignored -> 1).isDefinedAt(null)).isFalse();
}
@$test
public void shouldIterateNestedFor() {
final $ListType<String> result =
For(${im.getType("java.util.Arrays")}.asList(1, 2), i ->
For(${im.getType("io.vavr.collection.List")}.of('a', 'b')).yield(c -> i + ":" + c)).toList();
assertThat(result).isEqualTo($ListType.of("1:a", "1:b", "2:a", "2:b"));
}

@$test
public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndValue() {
final Match.Case<Object, Integer> _case = Case($$(ignored -> true), 1);
assertThat(_case.isDefinedAt(null)).isTrue();
assertThat(_case.apply(null)).isEqualTo(1);
}

@$test
public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndValue() {
assertThat(Case($$(ignored -> false), 1).isDefinedAt(null)).isFalse();
}
@Nested
class MatchTest {

@$test
public void shouldPassIssue2401() {
final $SeqType<String> empty = $StreamType.empty();
try {
Match(empty).of(
Case($$($ListType.empty()), ignored -> "list")
);
fail("expected MatchError");
} catch (MatchError err) {
// ok!
@$test
public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndSupplier() {
final Match.Case<Object, Integer> _case = Case($$(ignored -> true), ignored -> 1);
assertThat(_case.isDefinedAt(null)).isTrue();
assertThat(_case.apply(null)).isEqualTo(1);
}
}

@$test
public void shouldCatchClassCastExceptionWhenPredicateHasDifferentType() {
try {
final Object o = "";
Match(o).of(
Case($$((Integer i) -> true), "never")
);
fail("expected MatchError");
} catch (MatchError err) {
// ok!
@$test
public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndSupplier() {
assertThat(Case($$(ignored -> false), ignored -> 1).isDefinedAt(null)).isFalse();
}
}

// -- Match patterns

static class ClzMatch {}
static class ClzMatch1 extends ClzMatch {}
static class ClzMatch2 extends ClzMatch {}
@$test
public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndValue() {
final Match.Case<Object, Integer> _case = Case($$(ignored -> true), 1);
assertThat(_case.isDefinedAt(null)).isTrue();
assertThat(_case.apply(null)).isEqualTo(1);
}

${(1 to N).gen(i => {
@$test
public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndValue() {
assertThat(Case($$(ignored -> false), 1).isDefinedAt(null)).isFalse();
}

im.getStatic("io.vavr.API.*")
im.getStatic("io.vavr.Patterns.*")
@$test
public void shouldPassIssue2401() {
final $SeqType<String> empty = $StreamType.empty();
try {
Match(empty).of(
Case($$($ListType.empty()), ignored -> "list")
);
fail("expected MatchError");
} catch (MatchError err) {
// ok!
}
}

xs"""
@$test
public void shouldMatchPattern$i() {
final Tuple$i<${(1 to i).gen(j => s"Integer")(", ")}> tuple = Tuple.of(${(1 to i).gen(j => s"1")(", ")});
final String func = Match(tuple).of(
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "fail"),
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "okFunc")
);
assertThat(func).isEqualTo("okFunc");
final String supp = Match(tuple).of(
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), () -> "fail"),
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), () -> "okSupp")
);
assertThat(supp).isEqualTo("okSupp");
final String val = Match(tuple).of(
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), "fail"),
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), "okVal")
);
assertThat(val).isEqualTo("okVal");

final ClzMatch c = new ClzMatch2();
final String match = Match(c).of(
Case(Match.Pattern$i.of(ClzMatch1.class, ${(1 to i).gen(j => s"$d()")(", ")}, t -> Tuple.of(${(1 to i).gen(j => s"null")(", ")})), "fail"),
Case(Match.Pattern$i.of(ClzMatch2.class, ${(1 to i).gen(j => s"$d()")(", ")}, t -> Tuple.of(${(1 to i).gen(j => s"null")(", ")})), "okMatch")
);
assertThat(match).isEqualTo("okMatch");
public void shouldCatchClassCastExceptionWhenPredicateHasDifferentType() {
try {
final Object o = "";
Match(o).of(
Case($$((Integer i) -> true), "never")
);
fail("expected MatchError");
} catch (MatchError err) {
// ok!
}
}
"""
})("\n\n")}

}

@Nested
class MatchPatterns {

class ClzMatch {}
class ClzMatch1 extends ClzMatch {}
class ClzMatch2 extends ClzMatch {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed these to non-static since static inner classes are not supported in JDK 8


${(1 to N).gen(i => {

im.getStatic("io.vavr.API.*")
im.getStatic("io.vavr.Patterns.*")

xs"""
@$test
public void shouldMatchPattern$i() {
final Tuple$i<${(1 to i).gen(j => s"Integer")(", ")}> tuple = Tuple.of(${(1 to i).gen(j => s"1")(", ")});
final String func = Match(tuple).of(
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "fail"),
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "okFunc")
);
assertThat(func).isEqualTo("okFunc");
final String supp = Match(tuple).of(
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), () -> "fail"),
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), () -> "okSupp")
);
assertThat(supp).isEqualTo("okSupp");
final String val = Match(tuple).of(
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), "fail"),
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), "okVal")
);
assertThat(val).isEqualTo("okVal");

final ClzMatch c = new ClzMatch2();
final String match = Match(c).of(
Case(Match.Pattern$i.of(ClzMatch1.class, ${(1 to i).gen(j => s"$d()")(", ")}, t -> Tuple.of(${(1 to i).gen(j => s"null")(", ")})), "fail"),
Case(Match.Pattern$i.of(ClzMatch2.class, ${(1 to i).gen(j => s"$d()")(", ")}, t -> Tuple.of(${(1 to i).gen(j => s"null")(", ")})), "okMatch")
);
assertThat(match).isEqualTo("okMatch");
}
"""
})("\n\n")}
}
}
"""
})
Expand Down
9 changes: 9 additions & 0 deletions nested.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/zsh
set -euo pipefail

find ./src -name "*Test.java" -type f -exec sed -E -i '' \
's|// -- ([a-zA-Z0-9]+)$|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} +

find ./src -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} +

#./gradlew test
Loading
Loading