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");
45             auto charRange = f.byLine();
46             string s;
47             int i;
48             foreach (t; charRange) {
49                 s = cts.get(i++).to!string;
50                 s.should.equal(t);
51             }
52             f.close;
53         }
54 
55         @Tags("Parser")
56         @("simple_rule")
57         unittest {
58             auto antlrInput = new ANTLRInputStream(File("unittest/complex/simple.rule", "r"));
59             auto lexer = new RuleTranslatorLexer(antlrInput);
60             auto cts = new CommonTokenStream(lexer);
61             cts.getNumberOfOnChannelTokens.should.equal(35);
62             auto f = File("unittest/complex/simple_tokens.cmp");
63             auto charRange = f.byLine();
64             string s;
65             int i;
66             foreach (t; charRange) {
67                 s = cts.get(i++).to!string;
68                 s.should.equal(t);
69             }
70             f.close;
71             auto parser = new RuleTranslatorParser(cts);
72             // Specify entry point
73             auto rootContext = parser.file_input;
74             parser.numberOfSyntaxErrors.should.equal(0);
75         }
76 
77         @Tags("Parser")
78         @("simple_rule_syntax_error")
79         unittest {
80             auto antlrInput = new ANTLRInputStream(File("unittest/complex/simple_error.rule", "r"));
81             auto lexer = new RuleTranslatorLexer(antlrInput);
82             auto cts = new CommonTokenStream(lexer);
83             cts.getNumberOfOnChannelTokens.should.equal(36);
84             auto f = File("unittest/complex/simple_tokens_error.cmp");
85             auto charRange = f.byLine();
86             string s;
87             int i;
88             foreach (t; charRange) {
89                 s = cts.get(i++).to!string;
90                 s.should.equal(t);
91             }
92             f.close;
93             auto parser = new RuleTranslatorParser(cts);
94             parser.addErrorListener(new DiagnosticErrorListener!(Token, ParserATNSimulator));
95             // Specify entry point
96             auto rootContext = parser.file_input;
97             parser.numberOfSyntaxErrors.should.equal(1);
98         }
99 
100     }
101 }