1// [The "BSD licence"] 2// Copyright (c) 2007 Kay Roepke 3// All rights reserved. 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions 7// are met: 8// 1. Redistributions of source code must retain the above copyright 9// notice, this list of conditions and the following disclaimer. 10// 2. Redistributions in binary form must reproduce the above copyright 11// notice, this list of conditions and the following disclaimer in the 12// documentation and/or other materials provided with the distribution. 13// 3. The name of the author may not be used to endorse or promote products 14// derived from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27#import "TestRewriteRuleTokenStream.h" 28#import "ANTLRRewriteRuleTokenStream.h" 29#import "ANTLRCommonTreeAdaptor.h" 30#import "ANTLRCommonToken.h" 31 32@implementation TestRewriteRuleTokenStream 33 34- (void) setUp 35{ 36 treeAdaptor = [ANTLRCommonTreeAdaptor newTreeAdaptor]; 37 stream = [ANTLRRewriteRuleTokenStream newANTLRRewriteRuleTokenStream:treeAdaptor 38 description:@"rewrite rule token stream"]; 39 token1 = [ANTLRCommonToken newToken:5]; 40 token2 = [ANTLRCommonToken newToken:6]; 41 token3 = [ANTLRCommonToken newToken:7]; 42 token4 = [ANTLRCommonToken newToken:8]; 43 [token1 setText:@"token 1"]; 44 [token2 setText:@"token 2"]; 45 [token3 setText:@"token 3"]; 46 [token4 setText:@"token 4"]; 47} 48 49- (void) tearDown 50{ 51 [token1 release]; token1 = nil; 52 [token2 release]; token2 = nil; 53 [token3 release]; token3 = nil; 54 [token4 release]; token4 = nil; 55 56 [treeAdaptor release]; treeAdaptor = nil; 57 [stream release]; stream = nil; 58} 59 60- (void) test01EmptyRewriteStream 61{ 62 treeAdaptor = [ANTLRCommonTreeAdaptor newTreeAdaptor]; 63 stream = [ANTLRRewriteRuleTokenStream newANTLRRewriteRuleTokenStream:treeAdaptor 64 description:@"rewrite rule token stream"]; 65 STAssertFalse([stream hasNext], @"-(BOOL)hasNext should be NO, but isn't"); 66 STAssertThrows([stream nextToken], @"-next on empty stream should throw exception, but doesn't"); 67} 68 69- (void) test02RewriteStreamCount 70{ 71 treeAdaptor = [ANTLRCommonTreeAdaptor newTreeAdaptor]; 72 stream = [ANTLRRewriteRuleTokenStream newANTLRRewriteRuleTokenStream:treeAdaptor 73 description:@"rewrite rule token stream"]; 74 token1 = [ANTLRCommonToken newToken:5]; 75 token2 = [ANTLRCommonToken newToken:6]; 76 [token1 setText:@"token 1"]; 77 [token2 setText:@"token 2"]; 78 STAssertTrue([stream size] == 0, 79 @"empty stream should have count==0"); 80 [stream addElement:token1]; 81 STAssertTrue([stream size] == 1, 82 @"single element stream should have count==1"); 83 [stream addElement:token2]; 84 STAssertTrue([stream size] == 2, 85 @"multiple stream should have count==2"); 86 87} 88 89- (void) test03SingleElement 90{ 91 treeAdaptor = [ANTLRCommonTreeAdaptor newTreeAdaptor]; 92 stream = [ANTLRRewriteRuleTokenStream newANTLRRewriteRuleTokenStream:treeAdaptor 93 description:@"rewrite rule token stream"]; 94 token1 = [ANTLRCommonToken newToken:5]; 95 token2 = [ANTLRCommonToken newToken:6]; 96 token3 = [ANTLRCommonToken newToken:7]; 97 token4 = [ANTLRCommonToken newToken:8]; 98 [token1 setText:@"token 1"]; 99 [token2 setText:@"token 2"]; 100 [token3 setText:@"token 3"]; 101 [token4 setText:@"token 4"]; 102 [stream addElement:token1]; 103 STAssertTrue([stream hasNext], @"-hasNext should be YES, but isn't"); 104 ANTLRCommonTree *tree = [stream nextNode]; 105 STAssertEqualObjects([tree getToken], token1, @"return token from stream should be token1, but isn't"); 106} 107 108- (void) test04SingleElementDup 109{ 110 [stream addElement:token1]; 111 ANTLRCommonTree *tree1, *tree2; 112 STAssertNoThrow(tree1 = [stream nextNode], 113 @"stream iteration should not throw exception" 114 ); 115 STAssertNoThrow(tree2 = [stream nextNode], 116 @"stream iteration past element count (single element) should not throw exception" 117 ); 118 STAssertEqualObjects([tree1 getToken], [tree2 getToken], 119 @"tokens should be the same"); 120 STAssertFalse(tree1 == tree2, 121 @"trees should be different, but aren't"); 122} 123 124- (void) test05MultipleElements 125{ 126 [stream addElement:token1]; 127 [stream addElement:token2]; 128 [stream addElement:token3]; 129 ANTLRCommonTree *tree1, *tree2, *tree3, *tree4; 130 STAssertNoThrow(tree1 = [stream nextNode], 131 @"stream iteration should not throw exception" 132 ); 133 STAssertEqualObjects([tree1 getToken], token1, 134 @"[tree1 token] should be equal to token1" 135 ); 136 STAssertNoThrow(tree2 = [stream nextNode], 137 @"stream iteration should not throw exception" 138 ); 139 STAssertEqualObjects([tree2 getToken], token2, 140 @"[tree2 token] should be equal to token2" 141 ); 142 STAssertNoThrow(tree3 = [stream nextNode], 143 @"stream iteration should not throw exception" 144 ); 145 STAssertEqualObjects([tree3 getToken], token3, 146 @"[tree3 token] should be equal to token3" 147 ); 148 STAssertThrows(tree4 = [stream nextNode], 149 @"iterating beyond end of stream should throw an exception" 150 ); 151} 152 153- (void) test06MultipleElementsAfterReset 154{ 155 [stream addElement:token1]; 156 [stream addElement:token2]; 157 [stream addElement:token3]; 158 ANTLRCommonTree *tree1, *tree2, *tree3; 159 160 // consume the stream completely 161 STAssertNoThrow(tree1 = [stream nextNode], 162 @"stream iteration should not throw exception" 163 ); 164 STAssertEqualObjects([tree1 getToken], token1, 165 @"[tree1 token] should be equal to token1" 166 ); 167 STAssertNoThrow(tree2 = [stream nextNode], 168 @"stream iteration should not throw exception" 169 ); 170 STAssertEqualObjects([tree2 getToken], token2, 171 @"[tree2 token] should be equal to token2" 172 ); 173 STAssertNoThrow(tree3 = [stream nextNode], 174 @"stream iteration should not throw exception" 175 ); 176 177 [stream reset]; // after resetting the stream it should dup 178 179 ANTLRCommonTree *tree1Dup, *tree2Dup, *tree3Dup; 180 181 STAssertNoThrow(tree1Dup = [stream nextNode], 182 @"stream iteration should not throw exception" 183 ); 184 STAssertTrue(tree1 != tree1Dup, 185 @"[tree1 token] should be equal to token1" 186 ); 187 STAssertNoThrow(tree2Dup = [stream nextNode], 188 @"stream iteration should not throw exception" 189 ); 190 STAssertTrue(tree2 != tree2Dup, 191 @"[tree2 token] should be equal to token2" 192 ); 193 STAssertNoThrow(tree3Dup = [stream nextNode], 194 @"stream iteration should not throw exception" 195 ); 196 STAssertTrue(tree3 != tree3Dup, 197 @"[tree3 token] should be equal to token3" 198 ); 199} 200 201@end 202