1 /*
2 [The "BSD licence"]
3 Copyright (c) 2007 Johannes Luber
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code MUST RETAIN the above copyright
10    notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form MUST REPRODUCE the above copyright
12    notice, this list of conditions and the following disclaimer in
13    the documentation and/or other materials provided with the
14    distribution.
15 3. The name of the author may not be used to endorse or promote products
16    derived from this software without specific prior WRITTEN permission.
17 4. Unless explicitly state otherwise, any contribution intentionally
18    submitted for inclusion in this work to the copyright owner or licensor
19    shall be under the terms and conditions of this license, without any
20    additional terms or conditions.
21 
22 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #pragma warning disable 219 // No unused variable warnings
35 
36 namespace Antlr.Runtime.Tests {
37 	using System;
38 	using System.Collections.Generic;
39 	using Antlr.Runtime.Tree;
40 
41 	using MbUnit.Framework;
42 
43 	[TestFixture]
44 	public class RewriteRuleXxxxStreamFixture : TestFixtureBase {
45 		#region Check Constructors
46 
47 		[Test]
CheckRewriteRuleTokenStreamConstructors()48 		public void CheckRewriteRuleTokenStreamConstructors() {
49 			RewriteRuleTokenStream tokenTest1 = new RewriteRuleTokenStream(CreateTreeAdaptor(),
50 				"RewriteRuleTokenStream test1");
51 
52 			RewriteRuleTokenStream tokenTest2 = new RewriteRuleTokenStream(CreateTreeAdaptor(),
53 				"RewriteRuleTokenStream test2", CreateToken(1,
54 				"test token without any real context"));
55 
56 			RewriteRuleTokenStream tokenTest3 = new RewriteRuleTokenStream(CreateTreeAdaptor(),
57 				"RewriteRuleTokenStream test3", CreateTokenList(4));
58 		}
59 
60 		[Test]
CheckRewriteRuleSubtreeStreamConstructors()61 		public void CheckRewriteRuleSubtreeStreamConstructors() {
62 			RewriteRuleSubtreeStream subtreeTest1 =
63 				new RewriteRuleSubtreeStream(CreateTreeAdaptor(),
64 				"RewriteRuleSubtreeStream test1");
65 
66 			RewriteRuleSubtreeStream subtreeTest2 =
67 				new RewriteRuleSubtreeStream(CreateTreeAdaptor(),
68 				"RewriteRuleSubtreeStream test2", CreateToken(1,
69 				"test token without any real context"));
70 
71 			RewriteRuleSubtreeStream subtreeTest3 =
72 				new RewriteRuleSubtreeStream(CreateTreeAdaptor(),
73 				"RewriteRuleSubtreeStream test3", CreateTokenList(4));
74 		}
75 
76 		[Test]
CheckRewriteRuleNodeStreamConstructors()77 		public void CheckRewriteRuleNodeStreamConstructors() {
78 			RewriteRuleNodeStream nodeTest1 = new RewriteRuleNodeStream(CreateTreeAdaptor(),
79 				"RewriteRuleNodeStream test1");
80 
81 			RewriteRuleNodeStream nodeTest2 = new RewriteRuleNodeStream(CreateTreeAdaptor(),
82 				"RewriteRuleNodeStream test2", CreateToken(1,
83 				"test token without any real context"));
84 
85 			RewriteRuleNodeStream nodeTest3 = new RewriteRuleNodeStream(CreateTreeAdaptor(),
86 				"RewriteRuleNodeStream test3", CreateTokenList(4));
87 		}
88 		#endregion
89 
90 		#region Method Tests
91 
92 		#region Empty Behaviour
93 		[Test]
94 		[ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleTokenStream test")]
CheckRRTokenStreamBehaviourWhileEmpty1()95 		public void CheckRRTokenStreamBehaviourWhileEmpty1() {
96 			string description = "RewriteRuleTokenStream test";
97 			RewriteRuleTokenStream tokenTest =
98 				new RewriteRuleTokenStream(CreateTreeAdaptor(),	description);
99 
100 			Assert.IsFalse(tokenTest.HasNext, "tokenTest has to give back false here.");
101 			Assert.AreEqual(description.ToString(), tokenTest.Description,
102 				"Description strings should be equal.");
103 			Assert.AreEqual(0, tokenTest.Count, "The number of elements should be zero.");
104 			tokenTest.Reset();
105 			Assert.IsTrue(true, "Reset() shouldn't make any problems here.");
106 			Assert.AreEqual(0, tokenTest.Count,
107 				"The number of elements should be still zero.");
108 			tokenTest.NextNode();
109 		}
110 
111 		[Test]
112 		[ExpectedException(typeof(RewriteEmptyStreamException),
113 			"RewriteRuleSubtreeStream test")]
CheckRRSubtreeStreamBehaviourWhileEmpty1()114 		public void CheckRRSubtreeStreamBehaviourWhileEmpty1() {
115 			string description = "RewriteRuleSubtreeStream test";
116 			RewriteRuleSubtreeStream subtreeTest =
117 				new RewriteRuleSubtreeStream(CreateTreeAdaptor(), description);
118 
119 			Assert.IsFalse(subtreeTest.HasNext, "HasNext has to give back false here.");
120 			Assert.AreEqual(description.ToString(), subtreeTest.Description,
121 				"Description strings should be equal.");
122 			Assert.AreEqual(0, subtreeTest.Count, "The number of elements should be zero.");
123 			subtreeTest.Reset();
124 			Assert.IsTrue(true, "Reset() shouldn't make any problems here.");
125 			Assert.AreEqual(0, subtreeTest.Count,
126 				"The number of elements should be still zero.");
127 			subtreeTest.NextNode();
128 		}
129 
130 		[Test]
131 		[ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleNodeStream test")]
CheckRRNodeStreamBehaviourWhileEmpty1()132 		public void CheckRRNodeStreamBehaviourWhileEmpty1() {
133 			string description = "RewriteRuleNodeStream test";
134 			RewriteRuleNodeStream nodeTest =
135 				new RewriteRuleNodeStream(CreateTreeAdaptor(), description);
136 
137 			Assert.IsFalse(nodeTest.HasNext, "HasNext has to give back false here.");
138 			Assert.AreEqual(description.ToString(), nodeTest.Description,
139 				"Description strings should be equal.");
140 			Assert.AreEqual(0, nodeTest.Count, "The number of elements should be zero.");
141 			nodeTest.Reset();
142 			Assert.IsTrue(true, "Reset() shouldn't make any problems here.");
143 			Assert.AreEqual(0, nodeTest.Count,
144 				"The number of elements should be still zero.");
145 			nodeTest.NextNode();
146 		}
147 
148 		[Test]
149 		[ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleTokenStream test")]
CheckRRTokenStreamBehaviourWhileEmpty2()150 		public void CheckRRTokenStreamBehaviourWhileEmpty2() {
151 			RewriteRuleTokenStream tokenTest = new RewriteRuleTokenStream(CreateTreeAdaptor(),
152 				"RewriteRuleTokenStream test");
153 
154 			tokenTest.NextTree();
155 		}
156 
157 		[Test]
158 		[ExpectedException(typeof(RewriteEmptyStreamException),
159 			"RewriteRuleSubtreeStream test")]
CheckRRSubtreeStreamBehaviourWhileEmpty2()160 		public void CheckRRSubtreeStreamBehaviourWhileEmpty2() {
161 			RewriteRuleSubtreeStream subtreeTest = new RewriteRuleSubtreeStream(
162 				CreateTreeAdaptor(), "RewriteRuleSubtreeStream test");
163 
164 			subtreeTest.NextTree();
165 		}
166 
167 		[Test]
168 		[ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleNodeStream test")]
CheckRRNodeStreamBehaviourWhileEmpty2()169 		public void CheckRRNodeStreamBehaviourWhileEmpty2() {
170 			RewriteRuleNodeStream nodeTest = new RewriteRuleNodeStream(CreateTreeAdaptor(),
171 				"RewriteRuleNodeStream test");
172 
173 			nodeTest.NextTree();
174 		}
175 
176 		[Test]
177 		[ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleTokenStream test")]
CheckRRTokenStreamBehaviourWhileEmpty3()178 		public void CheckRRTokenStreamBehaviourWhileEmpty3() {
179 			RewriteRuleTokenStream tokenTest = new RewriteRuleTokenStream(CreateTreeAdaptor(),
180 				"RewriteRuleTokenStream test");
181 
182 			tokenTest.NextToken();
183 		}
184 
185 		#endregion
186 
187 		#region Behaviour with Elements
188 		[Test]
189 		[ExpectedException(typeof(RewriteCardinalityException), "RewriteRuleTokenStream test")]
CheckRRTokenStreamBehaviourWithElements()190 		public void CheckRRTokenStreamBehaviourWithElements() {
191 			RewriteRuleTokenStream tokenTest = new RewriteRuleTokenStream(CreateTreeAdaptor(),
192 				"RewriteRuleTokenStream test");
193 
194 			IToken token1 = CreateToken(1, "test token without any real context");
195 
196 			// Test Add()
197 			tokenTest.Add(token1);
198 			Assert.AreEqual(1, tokenTest.Count, "tokenTest should have the size 1 (1).");
199 			Assert.IsTrue(tokenTest.HasNext, "HasNext should be true here (1).");
200 
201 			// Test NextNode()
202 			CommonTree tree = (CommonTree) tokenTest.NextNode();
203 			Assert.AreEqual(token1, tree.Token,
204 				"The returned token should be equal to the given token (1).");
205 			Assert.AreEqual(1, tokenTest.Count, "tokenTest should have the size 1 (2).");
206 			Assert.IsFalse(tokenTest.HasNext, "HasNext should be false here (1).");
207 			tokenTest.Reset();
208 			Assert.AreEqual(1, tokenTest.Count, "tokenTest should have the size 1 (3).");
209 			Assert.IsTrue(tokenTest.HasNext, "HasNext should be true here (2).");
210 
211 			// Test NextToken()
212 			IToken returnedToken = tokenTest.NextToken();
213 			Assert.AreEqual(token1, returnedToken,
214 				"The returned token should be equal to the given token (2).");
215 			Assert.AreEqual(1, tokenTest.Count, "tokenTest should have the size 1 (4).");
216 			Assert.IsFalse(tokenTest.HasNext, "HasNext should be false here (2).");
217 			tokenTest.Reset();
218 			Assert.AreEqual(1, tokenTest.Count, "tokenTest should have the size 1 (5).");
219 			Assert.IsTrue(tokenTest.HasNext, "HasNext should be true here (3).");
220 
221 			// Test NextTree()
222 			returnedToken = (IToken) tokenTest.NextTree();
223 			Assert.AreEqual(token1, returnedToken,
224 				"The returned token should be equal to the given token (3).");
225 			Assert.AreEqual(1, tokenTest.Count, "tokenTest should have the size 1 (6).");
226 			Assert.IsFalse(tokenTest.HasNext, "HasNext should be false here (2).");
227 			tokenTest.Reset();
228 			Assert.AreEqual(1, tokenTest.Count, "tokenTest should have the size 1 (7).");
229 			Assert.IsTrue(tokenTest.HasNext, "HasNext should be true here (3).");
230 
231 			// Test, what happens with two elements
232 			IToken token2 = CreateToken(2, "test token without any real context");
233 
234 			tokenTest.Add(token2);
235 			Assert.AreEqual(2, tokenTest.Count, "tokenTest should have the size 2 (1).");
236 			Assert.IsTrue(tokenTest.HasNext, "HasNext should be true here (4).");
237 			returnedToken = tokenTest.NextToken();
238 			Assert.AreEqual(token1, returnedToken,
239 				"The returned token should be equal to the given token (4).");
240 			Assert.AreEqual(2, tokenTest.Count, "tokenTest should have the size 2 (2).");
241 			Assert.IsTrue(tokenTest.HasNext, "HasNext should be true here (5).");
242 			returnedToken = tokenTest.NextToken();
243 			Assert.AreEqual(token2, returnedToken,
244 				"The returned token should be equal to the given token (5).");
245 			Assert.IsFalse(tokenTest.HasNext, "HasNext should be false here (3).");
246 
247 			// Test exception
248 			tokenTest.NextToken();
249 		}
250 
251 		[Test]
252 		[ExpectedException(typeof(RewriteCardinalityException),
253 			"RewriteRuleSubtreeStream test")]
CheckRRSubtreeStreamBehaviourWithElements()254 		public void CheckRRSubtreeStreamBehaviourWithElements() {
255 			RewriteRuleSubtreeStream subtreeTest =
256 				new RewriteRuleSubtreeStream(CreateTreeAdaptor(),
257 				"RewriteRuleSubtreeStream test");
258 
259 			IToken token1 = CreateToken(1, "test token without any real context");
260 			ITree tree1 = CreateTree(token1);
261 
262 			// Test Add()
263 			subtreeTest.Add(tree1);
264 			Assert.AreEqual(1, subtreeTest.Count, "subtreeTest should have the size 1 (1).");
265 			Assert.IsTrue(subtreeTest.HasNext, "HasNext should be true here (1).");
266 
267 			// Test NextNode()
268 			Assert.AreEqual(tree1, (ITree) subtreeTest.NextNode(),
269 				"The returned tree should be equal to the given tree (1).");
270 			Assert.AreEqual(1, subtreeTest.Count, "subtreeTest should have the size 1 (2).");
271 			Assert.IsFalse(subtreeTest.HasNext, "HasNext should be false here (1).");
272 			subtreeTest.Reset();
273 			Assert.AreEqual(1, subtreeTest.Count, "subtreeTest should have the size 1 (3).");
274 			Assert.IsTrue(subtreeTest.HasNext, "HasNext should be true here (2).");
275 
276 			// Test NextTree()
277 			CommonTree returnedTree = (CommonTree) subtreeTest.NextTree();
278 			Assert.AreEqual(token1, returnedTree.Token,
279 				"The returned token should be equal to the given token (3).");
280 			Assert.AreEqual(1, subtreeTest.Count, "subtreeTest should have the size 1 (4).");
281 			Assert.IsFalse(subtreeTest.HasNext, "HasNext should be false here (2).");
282 			subtreeTest.Reset();
283 			Assert.AreEqual(1, subtreeTest.Count, "subtreeTest should have the size 1 (5).");
284 			Assert.IsTrue(subtreeTest.HasNext, "HasNext should be true here (3).");
285 
286 			// Test, what happens with two elements
287 			IToken token2 = CreateToken(2, "test token without any real context");
288 			ITree tree2 = CreateTree(token2);
289 
290 			subtreeTest.Add(tree2);
291 			Assert.AreEqual(2, subtreeTest.Count, "subtreeTest should have the size 2 (1).");
292 			Assert.IsTrue(subtreeTest.HasNext, "HasNext should be true here (4).");
293 			returnedTree = (CommonTree) subtreeTest.NextTree();
294 			Assert.AreEqual(token1, returnedTree.Token,
295 				"The returned token should be equal to the given token (4).");
296 			Assert.AreEqual(2, subtreeTest.Count, "subtreeTest should have the size 2 (2).");
297 			Assert.IsTrue(subtreeTest.HasNext, "HasNext should be true here (5).");
298 			returnedTree = (CommonTree) subtreeTest.NextTree();
299 			Assert.AreEqual(token2, returnedTree.Token,
300 				"The returned token should be equal to the given token (5).");
301 			Assert.IsFalse(subtreeTest.HasNext, "HasNext should be false here (3).");
302 
303 			// Test exception
304 			subtreeTest.NextTree();
305 		}
306 
307 		[Test]
308 		[ExpectedException(typeof(RewriteCardinalityException), "RewriteRuleNodeStream test")]
CheckRRNodeStreamBehaviourWithElements()309 		public void CheckRRNodeStreamBehaviourWithElements() {
310 			RewriteRuleNodeStream nodeTest = new RewriteRuleNodeStream(CreateTreeAdaptor(),
311 				"RewriteRuleNodeStream test");
312 
313 			IToken token1 = CreateToken(1, "test token without any real context");
314 			ITree tree1 = CreateTree(token1);
315 
316 			// Test Add()
317 			nodeTest.Add(tree1);
318 			Assert.AreEqual(1, nodeTest.Count, "nodeTest should have the size 1 (1).");
319 			Assert.IsTrue(nodeTest.HasNext, "HasNext should be true here (1).");
320 
321 			// Test NextNode()
322 			CommonTree returnedTree = (CommonTree) nodeTest.NextNode();
323 			Assert.AreEqual(tree1.Type, returnedTree.Type,
324 				"The returned tree should be equal to the given tree (1).");
325 			Assert.AreEqual(1, nodeTest.Count, "nodeTest should have the size 1 (2).");
326 			Assert.IsFalse(nodeTest.HasNext, "HasNext should be false here (1).");
327 			nodeTest.Reset();
328 			Assert.AreEqual(1, nodeTest.Count, "nodeTest should have the size 1 (3).");
329 			Assert.IsTrue(nodeTest.HasNext, "HasNext should be true here (2).");
330 
331 			// Test NextTree()
332 			returnedTree = (CommonTree) nodeTest.NextTree();
333 			Assert.AreEqual(token1, returnedTree.Token,
334 				"The returned token should be equal to the given token (3).");
335 			Assert.AreEqual(1, nodeTest.Count, "nodeTest should have the size 1 (4).");
336 			Assert.IsFalse(nodeTest.HasNext, "HasNext should be false here (2).");
337 			nodeTest.Reset();
338 			Assert.AreEqual(1, nodeTest.Count, "nodeTest should have the size 1 (5).");
339 			Assert.IsTrue(nodeTest.HasNext, "HasNext should be true here (3).");
340 
341 			// Test, what happens with two elements
342 			IToken token2 = CreateToken(2, "test token without any real context");
343 			ITree tree2 = CreateTree(token2);
344 
345 			nodeTest.Add(tree2);
346 			Assert.AreEqual(2, nodeTest.Count, "nodeTest should have the size 2 (1).");
347 			Assert.IsTrue(nodeTest.HasNext, "HasNext should be true here (4).");
348 			returnedTree = (CommonTree) nodeTest.NextTree();
349 			Assert.AreEqual(token1, returnedTree.Token,
350 				"The returned token should be equal to the given token (4).");
351 			Assert.AreEqual(2, nodeTest.Count, "nodeTest should have the size 2 (2).");
352 			Assert.IsTrue(nodeTest.HasNext, "HasNext should be true here (5).");
353 			returnedTree = (CommonTree) nodeTest.NextTree();
354 			Assert.AreEqual(token2, returnedTree.Token,
355 				"The returned token should be equal to the given token (5).");
356 			Assert.IsFalse(nodeTest.HasNext, "HasNext should be false here (3).");
357 
358 			// Test exception
359 			nodeTest.NextTree();
360 		}
361 
362 		#endregion
363 
364 		#endregion
365 
366 
367 		#region Helper Methods
368 
CreateTreeAdaptor()369 		private ITreeAdaptor CreateTreeAdaptor() {
370 			return new CommonTreeAdaptor();
371 		}
372 
CreateTree(IToken token)373 		private ITree CreateTree(IToken token) {
374 			return new CommonTree(token);
375 		}
376 
CreateToken(int type, string text)377 		private IToken CreateToken(int type, string text) {
378 			return new CommonToken(type, text);
379 		}
380 
CreateTokenList(int count)381 		private IList<IToken> CreateTokenList(int count) {
382 			IList<IToken> list = new List<IToken>();
383 			for (int i = 0; i < count; i++) {
384 				list.Add(new CommonToken((i+1), "test token " + (i+1).ToString() +
385 					" without any real context"));
386 			}
387 			return list;
388 		}
389 
390 		#endregion
391 	}
392 }