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