1 module TTSListenerTest;
2 
3 import std.stdio;
4 import std.conv;
5 
6 version(unittest) {
7 
8     import RuleTranslatorLexer;
9     import RuleTranslatorParser;
10     import TTSListener;
11     import antlr.v4.runtime.ANTLRInputStream;
12     import antlr.v4.runtime.CommonToken;
13     import antlr.v4.runtime.CommonTokenStream;
14     import antlr.v4.runtime.DiagnosticErrorListener;
15     import antlr.v4.runtime.LexerNoViableAltException;
16     import antlr.v4.runtime.Token;
17     import antlr.v4.runtime.atn.ParserATNSimulator;
18     import antlr.v4.runtime.tree.ParseTreeWalker;
19     import dshould : be, equal, not, should;
20     import dshould.thrown;
21     import std.conv : to;
22     import std.file;
23     import unit_threaded;
24 
25     class Test {
26 
27         @Tags("ANTLRInputStream")
28         @("input file missing")
29         unittest {
30             try
31                 auto antlrInput = new ANTLRInputStream(File("simple.rule1"));
32             catch (Exception e)
33                 e.msg.should.equal("Cannot open file `simple.rule1' in " ~
34                                    "mode `rb' (No such file or directory)");
35         }
36 
37         @Tags("Lexer")
38         @("rule")
39         unittest {
40             auto antlrInput = new ANTLRInputStream(File("unittest/complex/complex.rule", "r"));
41             auto lexer = new RuleTranslatorLexer(antlrInput);
42             auto cts = new CommonTokenStream(lexer);
43             cts.getNumberOfOnChannelTokens.should.equal(405);
44             auto f = File("unittest/complex/tokens.cmp", "r");
45             auto charRange = f.byLine();
46             string s;
47             int i;
48             foreach (t; charRange) {
49                 s = cts.get(i++).to!string;
50                 t.should.equal(s);
51             }
52         }
53 
54         @Tags("Parser")
55         @("simple_rule")
56         unittest {
57             auto antlrInput = new ANTLRInputStream(File("unittest/complex/simple.rule", "r"));
58             auto lexer = new RuleTranslatorLexer(antlrInput);
59             auto cts = new CommonTokenStream(lexer);
60             cts.getNumberOfOnChannelTokens.should.equal(35);
61             auto f = File("unittest/complex/simple_tokens.cmp");
62             auto charRange = f.byLine();
63             string s;
64             int i;
65             foreach (t; charRange) {
66                 s = cts.get(i++).to!string;
67                 t.should.equal(s);
68             }
69             auto parser = new RuleTranslatorParser(cts);
70             // Specify entry point
71             auto rootContext = parser.file_input;
72             parser.getNumberOfSyntaxErrors.should.equal(0);
73         }
74 
75         @Tags("Parser")
76         @("simple_rule_syntax_error")
77         unittest {
78             auto antlrInput = new ANTLRInputStream(File("unittest/complex/simple_error.rule", "r"));
79             auto lexer = new RuleTranslatorLexer(antlrInput);
80             auto cts = new CommonTokenStream(lexer);
81             cts.getNumberOfOnChannelTokens.should.equal(36);
82             auto f = File("unittest/complex/simple_tokens_error.cmp");
83             auto charRange = f.byLine();
84             string s;
85             int i;
86             foreach (t; charRange) {
87                 s = cts.get(i++).to!string;
88                 t.should.equal(s);
89             }
90             auto parser = new RuleTranslatorParser(cts);
91             parser.addErrorListener(new DiagnosticErrorListener!(Token, ParserATNSimulator));
92             // Specify entry point
93             auto rootContext = parser.file_input;
94             parser.getNumberOfSyntaxErrors.should.equal(3);
95         }
96 
97     }
98 }