1// 2// CommonTokenTest.m 3// ANTLR 4// 5// Created by Ian Michell on 25/05/2010. 6// Copyright 2010 Ian Michell and Alan Condit. All rights reserved. 7// 8 9#import "CommonTokenTest.h" 10#import "CommonToken.h" 11#import "ANTLRStringStream.h" 12 13@implementation CommonTokenTest 14 15-(void) test01InitAndRelease 16{ 17 CommonToken *token = [[CommonToken newToken] retain]; 18 STAssertNotNil(token, @"Token was nil"); 19 [token release]; 20} 21 22-(void) test02GetEOFToken 23{ 24 CommonToken *token = [[CommonToken eofToken] retain]; 25 STAssertNotNil(token, @"Token was nil"); 26 STAssertEquals(token.type, (NSInteger)TokenTypeEOF, @"Token was not of type TokenTypeEOF"); 27 [token release]; 28} 29 30-(void) test03InitWithTokenType 31{ 32 CommonToken *token = [[CommonToken newToken:TokenTypeUP] retain]; 33 token.text = @"<UP>"; 34 STAssertNotNil(token, @"Token was nil"); 35 STAssertEquals(token.type, (NSInteger)TokenTypeUP, @"Token was not of type TokenTypeUP"); 36 STAssertNotNil(token.text, @"Token text was nil, was expecting <UP>"); 37 STAssertTrue([token.text isEqualToString:@"<UP>"], @"Token text was not <UP> was instead: %@", token.text); 38 [token release]; 39} 40 41-(void) test04InitWithTokenTypeAndText 42{ 43 CommonToken *token = [[CommonToken newToken:TokenTypeUP Text:@"<UP>"] retain]; 44 STAssertNotNil(token, @"Token was nil"); 45 STAssertEquals(token.type, (NSInteger)TokenTypeUP, @"Token was not of type TokenTypeUP"); 46 STAssertNotNil(token.text, @"Token text was nil, was expecting <UP>"); 47 STAssertTrue([token.text isEqualToString:@"<UP>"], @"Token text was not <UP> was instead: %@", token.text); 48 [token release]; 49} 50 51-(void) test05InitWithCharStream 52{ 53 ANTLRStringStream *stream = [[ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"] retain]; 54 CommonToken *token = [[CommonToken newToken:stream Type:555 Channel:TokenChannelDefault Start:4 Stop:5] retain]; 55 STAssertNotNil(token, @"Token was nil"); 56 STAssertEquals(token.type, (NSInteger)555, @"Token was not of type 555"); // Nice random type number 57 STAssertNotNil(token.text, @"Token text was nil, was expecting ||"); 58 STAssertTrue([token.text isEqualToString:@"||"], @"Token text was not || was instead: %@", token.text); 59 [token release]; 60 [stream release]; 61} 62 63-(void) test06InitWithToken 64{ 65 ANTLRStringStream *stream = [[ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"] retain]; 66 CommonToken *token = [[CommonToken newToken:stream Type:555 Channel:TokenChannelDefault Start:4 Stop:5] retain]; 67 STAssertNotNil(token, @"Token was nil"); 68 STAssertEquals(token.type, (NSInteger)555, @"Token was not of type 555"); // Nice random type number 69 STAssertNotNil(token.text, @"Token text was nil, was expecting ||"); 70 STAssertTrue([token.text isEqualToString:@"||"], @"Token text was not || was instead: %@", token.text); 71 72 CommonToken *newToken = [[CommonToken newTokenWithToken:token] retain]; 73 STAssertNotNil(newToken, @"New token is nil!"); 74 STAssertEquals(newToken.type, token.type, @"Tokens types do not match %d:%d!", newToken.type, token.type); 75 STAssertEquals(newToken.line, token.line, @"Token lines do not match!"); 76 STAssertEquals(newToken.index, token.index, @"Token indexes do not match"); 77 STAssertEquals(newToken.channel, token.channel, @"Token channels are not the same"); 78 STAssertEquals(newToken.charPositionInLine, token.charPositionInLine, @"Token char positions in lines do not match"); 79 STAssertEquals(newToken.startIndex, token.startIndex, @"Token start positions do not match"); 80 STAssertEquals(newToken.stopIndex, token.stopIndex, @"Token stop positions do not match"); 81 STAssertTrue([newToken.text isEqualToString:token.text], @"Token text does not match!"); 82 [token release]; 83 [newToken release]; 84 [stream release]; 85} 86 87-(void) test07TokenDescription 88{ 89 NSString *aDescription; 90 ANTLRStringStream *stream = [[ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"] retain]; 91 CommonToken *token = [[CommonToken newToken:stream Type:555 Channel:TokenChannelDefault Start:4 Stop:5] retain]; 92 aDescription = [token description]; 93 STAssertTrue([aDescription isEqualToString:@"[@0, 4:5='||',<555>,0:0]"], @"String description for token is not correct! got %@", aDescription); 94 [token release]; 95 [stream release]; 96} 97 98@end 99