Skip to content

Commit 6aeea13

Browse files
authored
Use @nested in APITest (generated via Generator.scala) (#2999)
1 parent 8b0f051 commit 6aeea13

File tree

2 files changed

+1560
-1516
lines changed

2 files changed

+1560
-1516
lines changed

vavr/generator/Generator.scala

Lines changed: 151 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,6 +2620,7 @@ def generateTestClasses(): Unit = {
26202620
genVavrFile("io.vavr", s"APITest", baseDir = TARGET_TEST)((im: ImportManager, packageName, className) => {
26212621

26222622
val assertThat = im.getStatic("org.assertj.core.api.Assertions.assertThat")
2623+
val nested = im.getType("org.junit.jupiter.api.Nested")
26232624
val test = im.getType("org.junit.jupiter.api.Test")
26242625
val assertThrows = im.getStatic("org.junit.jupiter.api.Assertions.assertThrows")
26252626

@@ -2892,168 +2893,188 @@ def generateTestClasses(): Unit = {
28922893
$AssertionsExtensions.assertThat($API.class).isNotInstantiable();
28932894
}
28942895

2895-
// -- shortcuts
2896+
@Nested
2897+
class ShortcutTests {
28962898

2897-
${genShortcutsTests(im, packageName, className)}
2899+
${genShortcutsTests(im, packageName, className)}
2900+
2901+
}
28982902

28992903
//
29002904
// Alias should return not null.
29012905
// More specific test for each aliased class implemented in separate test class
29022906
//
29032907

2904-
${genAliasesTests(im, packageName, className)}
2908+
@Nested
2909+
class AliasTests {
29052910

2906-
// -- run
2911+
${genAliasesTests(im, packageName, className)}
29072912

2908-
@$test
2909-
public void shouldRunUnitAndReturnVoid() {
2910-
int[] i = { 0 };
2911-
Void nothing = run(() -> i[0]++);
2912-
$assertThat(nothing).isNull();
2913-
$assertThat(i[0]).isEqualTo(1);
29142913
}
29152914

2916-
// -- For
2915+
@Nested
2916+
class RunTests {
29172917

2918-
@$test
2919-
public void shouldIterateFor1UsingSimpleYield() {
2920-
final $ListType<Integer> list = List.of(1, 2, 3);
2921-
final $ListType<Integer> actual = For(list).yield().toList();
2922-
$assertThat(actual).isEqualTo(list);
2923-
}
2918+
@$test
2919+
public void shouldRunUnitAndReturnVoid() {
2920+
int[] i = { 0 };
2921+
Void nothing = run(() -> i[0]++);
2922+
$assertThat(nothing).isNull();
2923+
$assertThat(i[0]).isEqualTo(1);
2924+
}
29242925

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

2937-
${monadicTypesFor.gen(mtype => (1 to N).gen(i => { xs"""
2938-
@$test
2939-
public void shouldIterateFor$mtype$i() {
2940-
final $mtype<Integer> result = For(
2941-
${(1 to i).gen(j => s"$mtype.of($j)")(",\n")}
2942-
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")});
2943-
$assertThat(result.get()).isEqualTo(${(1 to i).sum});
2944-
}
2945-
"""})("\n\n"))("\n\n")}
2928+
@Nested
2929+
class ForTests {
29462930

2947-
${monadicFunctionTypesFor.gen(mtype => (1 to N).gen(i => { xs"""
2948-
@$test
2949-
public void shouldIterateFor$mtype$i() {
2950-
final $mtype<Integer> result = For(
2951-
${(1 to i).gen(j => s"$mtype.of(() -> $j)")(",\n")}
2952-
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")});
2953-
$assertThat(result.get()).isEqualTo(${(1 to i).sum});
2954-
}
2955-
"""})("\n\n"))("\n\n")}
2931+
@$test
2932+
public void shouldIterateFor1UsingSimpleYield() {
2933+
final $ListType<Integer> list = List.of(1, 2, 3);
2934+
final $ListType<Integer> actual = For(list).yield().toList();
2935+
$assertThat(actual).isEqualTo(list);
2936+
}
29562937

2957-
@$test
2958-
public void shouldIterateNestedFor() {
2959-
final $ListType<String> result =
2960-
For(${im.getType("java.util.Arrays")}.asList(1, 2), i ->
2961-
For(${im.getType("io.vavr.collection.List")}.of('a', 'b')).yield(c -> i + ":" + c)).toList();
2962-
assertThat(result).isEqualTo($ListType.of("1:a", "1:b", "2:a", "2:b"));
2963-
}
2938+
${(1 to N).gen(i => xs"""
2939+
@$test
2940+
public void shouldIterateFor$ListType$i() {
2941+
final $ListType<Integer> result = For(
2942+
${(1 to i).gen(j => s"$ListType.of(1, 2, 3)")(",\n")}
2943+
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}).toList();
2944+
$assertThat(result.length()).isEqualTo((int) Math.pow(3, $i));
2945+
$assertThat(result.head()).isEqualTo($i);
2946+
$assertThat(result.last()).isEqualTo(3 * $i);
2947+
}
2948+
""")("\n\n")}
29642949

2965-
// -- Match
2950+
${monadicTypesFor.gen(mtype => (1 to N).gen(i => { xs"""
2951+
@$test
2952+
public void shouldIterateFor$mtype$i() {
2953+
final $mtype<Integer> result = For(
2954+
${(1 to i).gen(j => s"$mtype.of($j)")(",\n")}
2955+
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")});
2956+
$assertThat(result.get()).isEqualTo(${(1 to i).sum});
2957+
}
2958+
"""})("\n\n"))("\n\n")}
29662959

2967-
@$test
2968-
public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndSupplier() {
2969-
final Match.Case<Object, Integer> _case = Case($$(ignored -> true), ignored -> 1);
2970-
assertThat(_case.isDefinedAt(null)).isTrue();
2971-
assertThat(_case.apply(null)).isEqualTo(1);
2972-
}
2960+
${monadicFunctionTypesFor.gen(mtype => (1 to N).gen(i => { xs"""
2961+
@$test
2962+
public void shouldIterateFor$mtype$i() {
2963+
final $mtype<Integer> result = For(
2964+
${(1 to i).gen(j => s"$mtype.of(() -> $j)")(",\n")}
2965+
).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")});
2966+
$assertThat(result.get()).isEqualTo(${(1 to i).sum});
2967+
}
2968+
"""})("\n\n"))("\n\n")}
29732969

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

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

2986-
@$test
2987-
public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndValue() {
2988-
assertThat(Case($$(ignored -> false), 1).isDefinedAt(null)).isFalse();
2989-
}
2980+
@Nested
2981+
class MatchTests {
29902982

2991-
@$test
2992-
public void shouldPassIssue2401() {
2993-
final $SeqType<String> empty = $StreamType.empty();
2994-
try {
2995-
Match(empty).of(
2996-
Case($$($ListType.empty()), ignored -> "list")
2997-
);
2998-
fail("expected MatchError");
2999-
} catch (MatchError err) {
3000-
// ok!
2983+
@$test
2984+
public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndSupplier() {
2985+
final Match.Case<Object, Integer> _case = Case($$(ignored -> true), ignored -> 1);
2986+
assertThat(_case.isDefinedAt(null)).isTrue();
2987+
assertThat(_case.apply(null)).isEqualTo(1);
30012988
}
3002-
}
30032989

3004-
@$test
3005-
public void shouldCatchClassCastExceptionWhenPredicateHasDifferentType() {
3006-
try {
3007-
final Object o = "";
3008-
Match(o).of(
3009-
Case($$((Integer i) -> true), "never")
3010-
);
3011-
fail("expected MatchError");
3012-
} catch (MatchError err) {
3013-
// ok!
2990+
@$test
2991+
public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndSupplier() {
2992+
assertThat(Case($$(ignored -> false), ignored -> 1).isDefinedAt(null)).isFalse();
30142993
}
3015-
}
3016-
3017-
// -- Match patterns
30182994

3019-
static class ClzMatch {}
3020-
static class ClzMatch1 extends ClzMatch {}
3021-
static class ClzMatch2 extends ClzMatch {}
2995+
@$test
2996+
public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndValue() {
2997+
final Match.Case<Object, Integer> _case = Case($$(ignored -> true), 1);
2998+
assertThat(_case.isDefinedAt(null)).isTrue();
2999+
assertThat(_case.apply(null)).isEqualTo(1);
3000+
}
30223001

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

3025-
im.getStatic("io.vavr.API.*")
3026-
im.getStatic("io.vavr.Patterns.*")
3007+
@$test
3008+
public void shouldPassIssue2401() {
3009+
final $SeqType<String> empty = $StreamType.empty();
3010+
try {
3011+
Match(empty).of(
3012+
Case($$($ListType.empty()), ignored -> "list")
3013+
);
3014+
fail("expected MatchError");
3015+
} catch (MatchError err) {
3016+
// ok!
3017+
}
3018+
}
30273019

3028-
xs"""
30293020
@$test
3030-
public void shouldMatchPattern$i() {
3031-
final Tuple$i<${(1 to i).gen(j => s"Integer")(", ")}> tuple = Tuple.of(${(1 to i).gen(j => s"1")(", ")});
3032-
final String func = Match(tuple).of(
3033-
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "fail"),
3034-
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "okFunc")
3035-
);
3036-
assertThat(func).isEqualTo("okFunc");
3037-
final String supp = Match(tuple).of(
3038-
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), () -> "fail"),
3039-
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), () -> "okSupp")
3040-
);
3041-
assertThat(supp).isEqualTo("okSupp");
3042-
final String val = Match(tuple).of(
3043-
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), "fail"),
3044-
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), "okVal")
3045-
);
3046-
assertThat(val).isEqualTo("okVal");
3047-
3048-
final ClzMatch c = new ClzMatch2();
3049-
final String match = Match(c).of(
3050-
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"),
3051-
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")
3052-
);
3053-
assertThat(match).isEqualTo("okMatch");
3021+
public void shouldCatchClassCastExceptionWhenPredicateHasDifferentType() {
3022+
try {
3023+
final Object o = "";
3024+
Match(o).of(
3025+
Case($$((Integer i) -> true), "never")
3026+
);
3027+
fail("expected MatchError");
3028+
} catch (MatchError err) {
3029+
// ok!
3030+
}
30543031
}
3055-
"""
3056-
})("\n\n")}
3032+
3033+
}
3034+
3035+
@Nested
3036+
class MatchPatternTests {
3037+
3038+
class ClzMatch {}
3039+
class ClzMatch1 extends ClzMatch {}
3040+
class ClzMatch2 extends ClzMatch {}
3041+
3042+
${(1 to N).gen(i => {
3043+
3044+
im.getStatic("io.vavr.API.*")
3045+
im.getStatic("io.vavr.Patterns.*")
3046+
3047+
xs"""
3048+
@$test
3049+
public void shouldMatchPattern$i() {
3050+
final Tuple$i<${(1 to i).gen(j => s"Integer")(", ")}> tuple = Tuple.of(${(1 to i).gen(j => s"1")(", ")});
3051+
final String func = Match(tuple).of(
3052+
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "fail"),
3053+
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "okFunc")
3054+
);
3055+
assertThat(func).isEqualTo("okFunc");
3056+
final String supp = Match(tuple).of(
3057+
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), () -> "fail"),
3058+
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), () -> "okSupp")
3059+
);
3060+
assertThat(supp).isEqualTo("okSupp");
3061+
final String val = Match(tuple).of(
3062+
Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), "fail"),
3063+
Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), "okVal")
3064+
);
3065+
assertThat(val).isEqualTo("okVal");
3066+
3067+
final ClzMatch c = new ClzMatch2();
3068+
final String match = Match(c).of(
3069+
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"),
3070+
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")
3071+
);
3072+
assertThat(match).isEqualTo("okMatch");
3073+
}
3074+
"""
3075+
})("\n\n")}
3076+
3077+
}
30573078
}
30583079
"""
30593080
})

0 commit comments

Comments
 (0)