aa
TestREPL.java
Go to the documentation of this file.
1 package com.cliffc.aa;
2 
3 import com.cliffc.aa.util.SB;
4 import org.junit.*;
5 import org.junit.contrib.java.lang.system.SystemErrRule;
6 import org.junit.contrib.java.lang.system.SystemOutRule;
7 
8 import java.io.IOException;
9 import java.nio.file.Files;
10 import java.nio.file.Paths;
11 
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14 
15 
16 public class TestREPL {
17  // Replace STDIN/STDOUT and track them
18  @Rule public final SystemOutRule sysOut = new SystemOutRule().enableLog().muteForSuccessfulTests();
19  @Rule public final SystemErrRule sysErr = new SystemErrRule().enableLog().muteForSuccessfulTests();
20 
21  private String _prog;
22  @Before public void open_repl() {
23  REPL.init();
24  _prog = "";
25  // Drain the initial prompt string so tests do not expect one
26  String actual = sysOut.getLog();
27  String expected = REPL.prompt;
28  assertEquals(expected,actual);
29  sysOut.clearLog();
30  assertTrue(sysErr.getLog().isEmpty());
31  }
32 
33  @After public void close_repl() {
34  }
35 
36  // Does the REPL test work?
37  @Test public void testREPL00() {
38  test("2", "2");
39  }
40 
41  // Basic REPL, with errors & recovery
42  @Test public void testREPL01() {
43  test("2+3", "5");
44  test("x=3", "3");
45  test("x*x", "9");
46  testerr("y*y", "y*y", "Unknown ref 'y'",4,0);
47  test("x=4", "4");
48  testerr("x+x", "x=4", "Cannot re-assign final val 'x'",4,0);
49  test("3+2", "5");
50  test("sq={x->x*x}", "[sq=*{x -> }]");
51  test("sq 5","25");
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);
54  }
55 
56  // Requires multi-pass type inference.
57  @Test public void testREPL02() {
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");
60  }
61 
62  // Start of a HashTable class.
63  @Test public void testREPL03() {
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);
69  test ("#x.tab","3");
70  }
71 
72  @Test public void testREPL04() throws IOException {
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");
77  }
78 
79  // Jam the code into STDIN, run the REPL one-step, read the STDOUT and compare.
80  private void test( String partial, String expected ) {
81  _prog = REPL.go_one(_prog,partial);
82  String actual = sysOut.getLog();
83  String exp = expected+System.lineSeparator()+REPL.prompt;
84  assertEquals(exp,actual);
85  sysOut.clearLog();
86  assertTrue(sysErr.getLog().isEmpty());
87  }
88  // Jam the code into STDIN, run the REPL one-step, read the STDOUT and compare.
89  // Includes the lengthy error message.
90  private void testerr( String partial, String parerr, String expected, int line, int cur_off ) {
91  _prog = REPL.go_one(_prog,partial);
92  String actual = sysOut.getLog();
93  String cursor = new String(new char[cur_off]).replace('\0', ' ');
94  String exp = new SB().p("stdin:").p(line).p(":").p(expected).nl().p(parerr).p(';').nl().p(cursor).p('^').nl().p(REPL.prompt).toString();
95  assertEquals(exp,actual);
96  sysOut.clearLog();
97  assertTrue(sysErr.getLog().isEmpty());
98  }
99 }
com.cliffc
com.cliffc.aa.TestREPL._prog
String _prog
Definition: TestREPL.java:21
com.cliffc.aa.util
Definition: AbstractEntry.java:1
com.cliffc.aa.REPL.prompt
static final String prompt
Definition: REPL.java:12
com.cliffc.aa.TestREPL.testREPL02
void testREPL02()
Definition: TestREPL.java:57
com.cliffc.aa.REPL
an implementation of language AA
Definition: REPL.java:11
com.cliffc.aa.REPL.go_one
static String go_one(String prog, String line)
Definition: REPL.java:27
com.cliffc.aa.TestREPL.open_repl
void open_repl()
Definition: TestREPL.java:22
com.cliffc.aa.TestREPL.testREPL03
void testREPL03()
Definition: TestREPL.java:63
com.cliffc.aa.TestREPL.testerr
void testerr(String partial, String parerr, String expected, int line, int cur_off)
Definition: TestREPL.java:90
com.cliffc.aa.util.SB
Tight/tiny StringBuilder wrapper.
Definition: SB.java:8
com.cliffc.aa
Definition: AA.java:1
com.cliffc.aa.util.SB.nl
SB nl()
Definition: SB.java:48
com.cliffc.aa.TestREPL
Definition: TestREPL.java:16
com.cliffc.aa.util.SB.p
SB p(String s)
Definition: SB.java:13
com.cliffc.aa.TestREPL.testREPL04
void testREPL04()
Definition: TestREPL.java:72
com.cliffc.aa.TestREPL.testREPL01
void testREPL01()
Definition: TestREPL.java:42
com.cliffc.aa.TestREPL.sysOut
final SystemOutRule sysOut
Definition: TestREPL.java:18
com.cliffc.aa.TestREPL.close_repl
void close_repl()
Definition: TestREPL.java:33
com
com.cliffc.aa.TestREPL.test
void test(String partial, String expected)
Definition: TestREPL.java:80
com.cliffc.aa.REPL.init
static void init()
Definition: REPL.java:22
com.cliffc.aa.util.SB.toString
String toString()
Definition: SB.java:62
com.cliffc.aa.TestREPL.sysErr
final SystemErrRule sysErr
Definition: TestREPL.java:19
com.cliffc.aa.TestREPL.testREPL00
void testREPL00()
Definition: TestREPL.java:37