1 #include "UserTestTraits.hpp"
2 #include "t012lexerXMLLexer.hpp"
3
4 #include <sys/types.h>
5
6 #include <iostream>
7 #include <sstream>
8 #include <fstream>
9
10 using namespace Antlr3Test;
11 using namespace std;
12
13 int testValid(string const& in, string const& out);
14 int testMalformedInput1(string const& data);
15 int testMalformedInput2(string const& data);
16 int testMalformedInput3(string const& data);
17 string slurp(string const& fileName);
18
19 static t012lexerXMLLexer *lxr;
20
main(int argc,char * argv[])21 int main (int argc, char *argv[])
22 {
23 testValid("t012lexerXML.input", "t012lexerXML.output");
24 testMalformedInput1("<?xml version='1.0'?>\n<document d>\n</document>\n");
25 testMalformedInput2("<?tml version='1.0'?>\n<document>\n</document>\n");
26 testMalformedInput3("<?xml version='1.0'?>\n<docu ment attr=\"foo\">\n</document>\n");
27
28 return 0;
29 }
30
testValid(string const & inFilename,string const & outFilename)31 int testValid(string const& inFilename, string const& outFilename)
32 {
33 string data = slurp(inFilename);
34 t012lexerXMLLexerTraits::InputStreamType* input = new t012lexerXMLLexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
35 ANTLR_ENC_8BIT,
36 data.length(), //strlen(data.c_str()),
37 (ANTLR_UINT8*)inFilename.c_str());
38 if (lxr == NULL)
39 lxr = new t012lexerXMLLexer(input);
40 else
41 lxr->setCharStream(input);
42
43 std::cout << "testValid: \"" << inFilename << '"' <<std::endl;
44 for(;;)
45 {
46 t012lexerXMLLexerTraits::CommonTokenType *token = lxr->nextToken();
47 if( token->getType() == t012lexerXMLLexerTokens::EOF_TOKEN)
48 break;
49 }
50
51 string expOutput = slurp(outFilename);
52 string lxrOutput = lxr->outbuf.str();
53
54 ofstream out("t012.lxr.output");
55 out << lxrOutput;
56
57 std::cout << inFilename << '\t' << (expOutput == lxrOutput ? "OK" : "Fail") << std::endl;
58
59 delete lxr; lxr = NULL;
60 delete input;
61 return 0;
62 }
63
testMalformedInput1(string const & data)64 int testMalformedInput1(string const& data)
65 {
66 t012lexerXMLLexerTraits::InputStreamType* input = new t012lexerXMLLexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
67 ANTLR_ENC_8BIT,
68 data.length(), //strlen(data.c_str()),
69 (ANTLR_UINT8*)"t012");
70 if (lxr == NULL)
71 lxr = new t012lexerXMLLexer(input);
72 else
73 lxr->setCharStream(input);
74
75 std::cout << "testMalformedInput1: \"" << data << '"' <<std::endl;
76
77 t012lexerXMLLexerTraits::CommonTokenType *token;
78 token = lxr->nextToken();
79 std::cout << token->getText() << std::endl;
80 token = lxr->nextToken();
81 std::cout << token->getText() << std::endl;
82 token = lxr->nextToken();
83 std::cout << token->getText() << std::endl;
84
85 // try:
86 // while True:
87 // token = lexer.nextToken()
88 // # Should raise NoViableAltException before hitting EOF
89 // if token.type == antlr3.EOF:
90 // self.fail()
91 //
92 // except antlr3.NoViableAltException as exc:
93 // self.assertEqual(exc.unexpectedType, '>')
94 // self.assertEqual(exc.charPositionInLine, 11)
95 // self.assertEqual(exc.line, 2)
96
97 delete lxr; lxr = NULL;
98 delete input;
99 return 0;
100 }
101
testMalformedInput2(string const & data)102 int testMalformedInput2(string const& data)
103 {
104 t012lexerXMLLexerTraits::InputStreamType* input = new t012lexerXMLLexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
105 ANTLR_ENC_8BIT,
106 data.length(), //strlen(data.c_str()),
107 (ANTLR_UINT8*)"t012");
108 if (lxr == NULL)
109 lxr = new t012lexerXMLLexer(input);
110 else
111 lxr->setCharStream(input);
112
113 std::cout << "testMalformedInput2: \"" << data << '"' <<std::endl;
114
115 t012lexerXMLLexerTraits::CommonTokenType *token;
116 token = lxr->nextToken();
117 std::cout << token->getText() << std::endl;
118 token = lxr->nextToken();
119 std::cout << token->getText() << std::endl;
120 token = lxr->nextToken();
121 std::cout << token->getText() << std::endl;
122
123 // try:
124 // while True:
125 // token = lexer.nextToken()
126 // # Should raise NoViableAltException before hitting EOF
127 // if token.type == antlr3.EOF:
128 // self.fail()
129 //
130 // except antlr3.MismatchedSetException as exc:
131 // self.assertEqual(exc.unexpectedType, 't')
132 // self.assertEqual(exc.charPositionInLine, 2)
133 // self.assertEqual(exc.line, 1)
134
135 delete lxr; lxr = NULL;
136 delete input;
137 return 0;
138 }
139
testMalformedInput3(string const & data)140 int testMalformedInput3(string const& data)
141 {
142 t012lexerXMLLexerTraits::InputStreamType* input = new t012lexerXMLLexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
143 ANTLR_ENC_8BIT,
144 data.length(), //strlen(data.c_str()),
145 (ANTLR_UINT8*)"t012");
146 if (lxr == NULL)
147 lxr = new t012lexerXMLLexer(input);
148 else
149 lxr->setCharStream(input);
150
151 std::cout << "testMalformedInput3: \"" << data << '"' <<std::endl;
152
153 t012lexerXMLLexerTraits::CommonTokenType *token;
154 token = lxr->nextToken();
155 std::cout << token->getText() << std::endl;
156 token = lxr->nextToken();
157 std::cout << token->getText() << std::endl;
158 token = lxr->nextToken();
159 std::cout << token->getText() << std::endl;
160
161 // try:
162 // while True:
163 // token = lexer.nextToken()
164 // # Should raise NoViableAltException before hitting EOF
165 // if token.type == antlr3.EOF:
166 // self.fail()
167 //
168 // except antlr3.NoViableAltException as exc:
169 // self.assertEqual(exc.unexpectedType, 'a')
170 // self.assertEqual(exc.charPositionInLine, 11)
171 // self.assertEqual(exc.line, 2)
172
173 delete lxr; lxr = NULL;
174 delete input;
175 return 0;
176 }
177
slurp(string const & fileName)178 string slurp(string const& fileName)
179 {
180 ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);
181 ifstream::pos_type fileSize = ifs.tellg();
182 ifs.seekg(0, ios::beg);
183
184 stringstream sstr;
185 sstr << ifs.rdbuf();
186 return sstr.str();
187 }
188