5 import org.junit.contrib.java.lang.system.SystemErrRule;
6 import org.junit.contrib.java.lang.system.SystemOutRule;
8 import java.io.IOException;
9 import java.nio.file.Files;
10 import java.nio.file.Paths;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
18 @Rule
public final SystemOutRule
sysOut =
new SystemOutRule().enableLog().muteForSuccessfulTests();
19 @Rule
public final SystemErrRule
sysErr =
new SystemErrRule().enableLog().muteForSuccessfulTests();
26 String actual =
sysOut.getLog();
28 assertEquals(expected,actual);
30 assertTrue(
sysErr.getLog().isEmpty());
46 testerr(
"y*y",
"y*y",
"Unknown ref 'y'",4,0);
48 testerr(
"x+x",
"x=4",
"Cannot re-assign final val 'x'",4,0);
50 test(
"sq={x->x*x}",
"[sq=*{x -> }]");
52 testerr(
"sq \"abc\"",
"sq={x->x*x}",
"*\"abc\" is none of (flt64,int64)", 6,7);
53 testerr(
"x",
"x=4",
"Cannot re-assign final val 'x'",4,0);
58 test(
"do={pred->{body->pred()?body():^; do pred body}}",
"[do=*{pred -> }]");
59 test(
"sum:=0; i:=0; do {i++ < 100} {sum:=sum+i}; sum",
"int64");
64 test (
"hash := {@{ tab = [3]; get = { key -> idx = key.hash(); tab[idx] } } }",
"[hash=*{ -> }]");
65 testerr(
"junk := hash:int",
"junk := hash:int",
"[hash=*{ -> }] is not a int64",2,12);
66 testerr(
"hash.tab",
"hash.tab",
"Unknown field '.tab'",2,5);
67 test (
"x := hash()",
"@{tab==*[3]0/obj?; get==[get=*{key -> }]}");
68 testerr(
"x.#tab",
"x.#tab",
"Unknown ref 'tab'",3,3);
73 String hash_src =
new String(Files.readAllBytes( Paths.get(
"test/java/com/cliffc/aa",
"HashTable.aa")));
74 test(hash_src,
"[HashTable=*{ -> }]");
75 test(
"htab = HashTable()",
"@{_tab=*[7]0/obj; get=[get=*{key -> }]; put=[put=*{key val -> }]}");
76 test(
"htab.put(\"Monday\",1)",
"0");
80 private void test( String partial, String expected ) {
82 String actual =
sysOut.getLog();
83 String exp = expected+System.lineSeparator()+
REPL.
prompt;
84 assertEquals(exp,actual);
86 assertTrue(
sysErr.getLog().isEmpty());
90 private void testerr( String partial, String parerr, String expected,
int line,
int cur_off ) {
92 String actual =
sysOut.getLog();
93 String cursor =
new String(
new char[cur_off]).replace(
'\0',
' ');
95 assertEquals(exp,actual);
97 assertTrue(
sysErr.getLog().isEmpty());