Skip to content

Commit 76bb99b

Browse files
committed
test: add ProblemSink unit tests
1 parent 528a74a commit 76bb99b

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package dev.objz.commandbridge.scripting.validation;
2+
3+
import dev.objz.commandbridge.logging.Log;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.Test;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.util.List;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertFalse;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
14+
class ProblemSinkTest {
15+
16+
@BeforeAll
17+
static void installLog() {
18+
try {
19+
Log.install(LoggerFactory.getLogger("test"));
20+
} catch (IllegalStateException e) {
21+
// Log already installed, ignore
22+
}
23+
}
24+
25+
@Test
26+
void errorAddsToList() {
27+
ProblemSink sink = new ProblemSink();
28+
sink.error("field", "msg");
29+
assertEquals(1, sink.count());
30+
}
31+
32+
@Test
33+
void hasErrorsTrueAfterError() {
34+
ProblemSink sink = new ProblemSink();
35+
sink.error("field", "msg");
36+
assertTrue(sink.hasErrors());
37+
}
38+
39+
@Test
40+
void hasErrorsFalseWhenEmpty() {
41+
ProblemSink sink = new ProblemSink();
42+
assertFalse(sink.hasErrors());
43+
}
44+
45+
@Test
46+
void countReturnsCorrectNumber() {
47+
ProblemSink sink = new ProblemSink();
48+
sink.error("field1", "msg1");
49+
sink.error("field2", "msg2");
50+
sink.error("field3", "msg3");
51+
assertEquals(3, sink.count());
52+
}
53+
54+
@Test
55+
void problemsReturnsImmutableCopy() {
56+
ProblemSink sink = new ProblemSink();
57+
sink.error("field", "msg");
58+
List<ProblemSink.Problem> problems = sink.problems();
59+
assertEquals(1, problems.size());
60+
// Verify it's a copy by trying to modify it
61+
try {
62+
problems.add(new ProblemSink.Problem("field2", "msg2"));
63+
// If we get here, the list is mutable (test should fail)
64+
assertTrue(false, "problems() should return an immutable copy");
65+
} catch (UnsupportedOperationException e) {
66+
// Expected: the returned list is immutable
67+
assertTrue(true);
68+
}
69+
}
70+
71+
@Test
72+
void toBulletedListFormatsWithHeader() {
73+
ProblemSink sink = new ProblemSink();
74+
sink.error("field", "msg");
75+
String output = sink.toBulletedList("Errors");
76+
assertTrue(output.contains("Errors"));
77+
assertTrue(output.contains(" - field: msg"));
78+
}
79+
80+
@Test
81+
void toBulletedListWithNullPath() {
82+
ProblemSink sink = new ProblemSink();
83+
sink.error(null, "msg");
84+
String output = sink.toBulletedList("Errors");
85+
assertTrue(output.contains(" - msg"));
86+
assertFalse(output.contains("null:"));
87+
}
88+
89+
@Test
90+
void toStringFormatsAllProblems() {
91+
ProblemSink sink = new ProblemSink();
92+
sink.error("field", "msg");
93+
String output = sink.toString();
94+
assertTrue(output.contains("field: msg"));
95+
}
96+
97+
@Test
98+
void problemRecordToString() {
99+
ProblemSink.Problem withPath = new ProblemSink.Problem("field", "msg");
100+
assertEquals("field: msg", withPath.toString());
101+
102+
ProblemSink.Problem withoutPath = new ProblemSink.Problem(null, "msg");
103+
assertEquals("msg", withoutPath.toString());
104+
}
105+
}

0 commit comments

Comments
 (0)