Skip to content

Commit 04bca91

Browse files
committed
tests
1 parent 880c5ab commit 04bca91

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

src/test/java/org/jamplate/glucose/spec/ExamplesTest.java

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22

33
import org.jamplate.glucose.spec.tool.DebugSpec;
44
import org.jamplate.impl.document.PseudoDocument;
5+
import org.jamplate.impl.unit.Action;
56
import org.jamplate.impl.unit.UnitImpl;
67
import org.jamplate.model.Document;
78
import org.jamplate.unit.Unit;
89
import org.junit.jupiter.api.Test;
910

11+
import java.io.ByteArrayOutputStream;
12+
import java.io.OutputStream;
13+
import java.io.PrintStream;
14+
15+
import static org.jamplate.glucose.internal.util.Values.number;
16+
import static org.jamplate.util.Specs.listener;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
1018
import static org.junit.jupiter.api.Assertions.fail;
1119

1220
public class ExamplesTest {
1321
@Test
1422
public void _99bottlesOfBeerOnTheWall() {
23+
//manual test :)
1524
Document document = new PseudoDocument(
1625
"Test",
1726
"#declare $i 99",
@@ -55,4 +64,119 @@ public void _99bottlesOfBeerOnTheWall() {
5564
fail("Test not completed");
5665
}
5766
}
67+
68+
@Test
69+
public void _fibonacci() {
70+
for (
71+
int[] arr : new int[][]{
72+
{0, 0},
73+
{1, 1},
74+
{2, 1},
75+
{3, 2},
76+
{4, 3},
77+
{5, 5},
78+
{6, 8},
79+
{7, 13},
80+
{8, 21},
81+
{9, 34},
82+
{10, 55}
83+
}
84+
) {
85+
Document fibonacci = new PseudoDocument(
86+
"fibonacci.jh",
87+
" #if $p == 0",
88+
" #declare $r 0",
89+
" #elif $p == 1",
90+
" #declare $r 1",
91+
" #else",
92+
" #for $ort [$rt]",
93+
" #declare $p $p - 1",
94+
" #include __PATH__",
95+
" #declare $rt $r",
96+
"",
97+
" #declare $p $p - 1",
98+
" #include __PATH__",
99+
" #declare $rt $rt + $r",
100+
"",
101+
" #declare $p $p + 2",
102+
" #declare $r $rt",
103+
" #declare $rt $ort",
104+
" #endfor",
105+
" #endif"
106+
);
107+
Document main = new PseudoDocument(
108+
"main.jamplate",
109+
" #declare $p $input",
110+
" #include __DIR__ 'fibonacci.jh'",
111+
" #message $r"
112+
);
113+
114+
Unit unit = new UnitImpl(new GlucoseSpec());
115+
116+
unit.getSpec().add(listener(event -> {
117+
if (event.getAction().equals(Action.PRE_EXEC))
118+
event.getMemory().set(
119+
"$input",
120+
number(arr[0])
121+
);
122+
}));
123+
124+
OutputStream outBuffer = new ByteArrayOutputStream();
125+
PrintStream out = System.out;
126+
System.setOut(new PrintStream(outBuffer));
127+
128+
if (
129+
!unit.initialize(fibonacci, main) ||
130+
!unit.parse(fibonacci, main) ||
131+
!unit.analyze(fibonacci, main) ||
132+
!unit.compile(fibonacci, main) ||
133+
!unit.execute(main)
134+
) {
135+
unit.diagnostic();
136+
fail("Test not completed");
137+
}
138+
139+
assertEquals(
140+
Integer.toString(arr[1]),
141+
outBuffer.toString(),
142+
"Fibonacci seq"
143+
);
144+
System.setOut(out);
145+
}
146+
}
147+
148+
@Test
149+
public void _quine() {
150+
Document document = new PseudoDocument(
151+
//name
152+
"Main",
153+
//content
154+
"#declare input '#message \"#declare input \" \"\\'\" input \"\\'\" \"\\n\" input'",
155+
"#message \"#declare input \" \"\\'\" input \"\\'\" \"\\n\" input"
156+
);
157+
158+
Unit unit = new UnitImpl(new GlucoseSpec());
159+
160+
OutputStream outBuffer = new ByteArrayOutputStream();
161+
PrintStream out = System.out;
162+
System.setOut(new PrintStream(outBuffer));
163+
164+
if (
165+
!unit.initialize(document) ||
166+
!unit.parse(document) ||
167+
!unit.analyze(document) ||
168+
!unit.compile(document) ||
169+
!unit.execute(document)
170+
) {
171+
unit.diagnostic();
172+
fail("Test not completed");
173+
}
174+
175+
assertEquals(
176+
document.read(),
177+
outBuffer.toString(),
178+
"Weird Quine"
179+
);
180+
System.setOut(out);
181+
}
58182
}

0 commit comments

Comments
 (0)