PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/antlr-3.4/runtime/ObjC/Framework/test/runtime/tree/ANTLRCommonTreeTest.m

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Objective C | 555 lines | 415 code | 83 blank | 57 comment | 40 complexity | af537a503a11cd558894280798820d18 MD5 | raw file
  1. //
  2. // ANTLRCommonTreeTest.m
  3. // ANTLR
  4. //
  5. // Created by Ian Michell on 26/05/2010.
  6. // Copyright 2010 Ian Michell. All rights reserved.
  7. //
  8. #import "ANTLRBaseTree.h"
  9. #import "ANTLRCommonTreeTest.h"
  10. #import "ANTLRStringStream.h"
  11. #import "ANTLRCommonTree.h"
  12. #import "ANTLRCommonToken.h"
  13. #import "ANTLRError.h"
  14. #import "ANTLRRuntimeException.h"
  15. @implementation ANTLRCommonTreeTest
  16. -(void) test01InitAndRelease
  17. {
  18. ANTLRCommonTree *tree = [ANTLRCommonTree newTree];
  19. STAssertNotNil(tree, @"Tree was nil");
  20. // FIXME: It doesn't do anything else, perhaps initWithTree should set something somewhere, java says no though...
  21. return;
  22. }
  23. -(void) test02InitWithTree
  24. {
  25. ANTLRCommonTree *tree = [ANTLRCommonTree newTree];
  26. STAssertNotNil(tree, @"Tree was nil");
  27. if (tree != nil)
  28. STAssertEquals([tree getType], (NSInteger)ANTLRTokenTypeInvalid, @"Tree should have an invalid token type, because it has no token");
  29. // [tree release];
  30. return;
  31. }
  32. -(void) test03WithToken
  33. {
  34. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  35. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  36. token.line = 1;
  37. token.charPositionInLine = 4;
  38. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  39. STAssertNotNil(tree, @"Tree was nil");
  40. if (tree != nil)
  41. STAssertNotNil(tree.token, @"Tree with token was nil");
  42. if (tree != nil && tree.token != nil) {
  43. STAssertEquals((NSUInteger) tree.token.line, (NSUInteger)1, [NSString stringWithFormat:@"Tree should be at line 1, but was at %d", tree.token.line] );
  44. STAssertEquals((NSUInteger) tree.token.charPositionInLine, (NSUInteger)4, [NSString stringWithFormat:@"Char position should be 1, but was at %d", tree.token.charPositionInLine]);
  45. STAssertNotNil(((ANTLRCommonToken *)tree.token).text, @"Tree with token with text was nil");
  46. }
  47. if (tree != nil && tree.token != nil && tree.token.text != nil)
  48. STAssertTrue([tree.token.text isEqualToString:@"||"], @"Text was not ||");
  49. //[tree release];
  50. return;
  51. }
  52. -(void) test04InvalidTreeNode
  53. {
  54. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:[ANTLRCommonToken invalidToken]];
  55. STAssertNotNil(tree, @"Tree was nil");
  56. STAssertEquals(tree.token.type, (NSInteger)ANTLRTokenTypeInvalid, @"Tree Token type was not ANTLRTokenTypeInvalid");
  57. //[tree release];
  58. return;
  59. }
  60. -(void) test05InitWithCommonTreeNode
  61. {
  62. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  63. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  64. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  65. STAssertNotNil(tree, @"Tree was nil");
  66. STAssertNotNil(tree.token, @"Tree token was nil");
  67. ANTLRCommonTree *newTree = [ANTLRCommonTree newTreeWithTree:tree];
  68. STAssertNotNil(newTree, @"New tree was nil");
  69. STAssertNotNil(newTree.token, @"New tree token was nil");
  70. STAssertEquals(newTree.token, tree.token, @"Tokens did not match");
  71. STAssertEquals(newTree.startIndex, tree.startIndex, @"Token start index did not match %d:%d", newTree.startIndex, tree.startIndex);
  72. STAssertEquals(newTree.stopIndex, tree.stopIndex, @"Token stop index did not match %d:%d", newTree.stopIndex, tree.stopIndex);
  73. //[stream release];
  74. //[tree release];
  75. //[newTree release];
  76. //[token release];
  77. return;
  78. }
  79. -(void) test06CopyTree
  80. {
  81. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  82. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  83. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  84. STAssertNotNil(tree, @"Tree was nil");
  85. ANTLRCommonTree *newTree = (ANTLRCommonTree *)[tree copyWithZone:nil];
  86. STAssertTrue([newTree isKindOfClass:[ANTLRCommonTree class]], @"Copied tree was not an ANTLRCommonTree");
  87. STAssertNotNil(newTree, @"New tree was nil");
  88. // STAssertEquals(newTree.token, tree.token, @"Tokens did not match");
  89. STAssertEquals(newTree.stopIndex, tree.stopIndex, @"Token stop index did not match");
  90. STAssertEquals(newTree.startIndex, tree.startIndex, @"Token start index did not match");
  91. //[stream release];
  92. //[tree release];
  93. //[newTree release];
  94. // [token release];
  95. return;
  96. }
  97. -(void) test07Description
  98. {
  99. NSString *aString;
  100. ANTLRCommonTree *errorTree = [ANTLRCommonTree invalidNode];
  101. STAssertNotNil(errorTree, @"Error tree node is nil");
  102. if (errorTree != nil) {
  103. aString = [errorTree description];
  104. STAssertNotNil( aString, @"errorTree description returned nil");
  105. if (aString != nil)
  106. STAssertTrue([aString isEqualToString:@"<errornode>"], @"Not a valid error node description %@", aString);
  107. }
  108. //[errorTree release];
  109. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeUP];
  110. STAssertNotNil(tree, @"Tree is nil");
  111. if (tree != nil)
  112. STAssertNil([tree description], @"Tree description was not nil, was: %@", [tree description]);
  113. //[tree release];
  114. tree = [ANTLRCommonTree newTree];
  115. STAssertNotNil(tree, @"Tree is nil");
  116. if (tree != nil) {
  117. aString = [tree description];
  118. STAssertNotNil(aString, @"tree description returned nil");
  119. if (aString != nil)
  120. STAssertTrue([aString isEqualToString:@"nil"], @"Tree description was not empty", [tree description]);
  121. }
  122. //[tree release];
  123. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  124. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  125. tree = [ANTLRCommonTree newTreeWithToken:token];
  126. STAssertNotNil(tree, @"Tree node is nil");
  127. aString = [tree description];
  128. STAssertNotNil(aString, @"tree description returned nil");
  129. if (aString != nil)
  130. STAssertTrue([aString isEqualToString:@"||"], @"description was not || was instead %@", [tree description]);
  131. //[tree release];
  132. return;
  133. }
  134. -(void) test08Text
  135. {
  136. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  137. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  138. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  139. STAssertNotNil(tree, @"Tree was nil");
  140. STAssertTrue([tree.token.text isEqualToString:@"||"], @"Tree text was not valid, should have been || was %@", tree.token.text);
  141. //[tree release];
  142. // test nil (for line coverage)
  143. tree = [ANTLRCommonTree newTree];
  144. STAssertNotNil(tree, @"Tree was nil");
  145. STAssertNil(tree.token.text, @"Tree text was not nil: %@", tree.token.text);
  146. return;
  147. }
  148. -(void) test09AddChild
  149. {
  150. // Create a new tree
  151. ANTLRCommonTree *parent = [ANTLRCommonTree newTreeWithTokenType:555];
  152. parent.token.line = 1;
  153. parent.token.charPositionInLine = 1;
  154. // Child tree
  155. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  156. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  157. token.line = 1;
  158. token.charPositionInLine = 4;
  159. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  160. // Add a child to the parent tree
  161. [parent addChild:tree];
  162. STAssertNotNil(parent, @"parent was nil");
  163. if (parent != nil)
  164. STAssertNotNil(parent.token, @"parent was nil");
  165. STAssertEquals((NSInteger)parent.token.line, (NSInteger)1, @"Tree should be at line 1 but is %d", parent.token.line);
  166. STAssertEquals((NSInteger)parent.token.charPositionInLine, (NSInteger)1, @"Char position should be 1 but is %d", parent.token.charPositionInLine);
  167. STAssertEquals((NSInteger)[parent getChildCount], (NSInteger)1, @"There should be 1 child but there were %d", [parent getChildCount]);
  168. STAssertEquals((NSInteger)[[parent getChild:0] getChildIndex], (NSInteger)0, @"Child index should be 0 was : %d", [[parent getChild:0] getChildIndex]);
  169. STAssertEquals([[parent getChild:0] getParent], parent, @"Parent not set for child");
  170. //[parent release];
  171. return;
  172. }
  173. -(void) test10AddChildren
  174. {
  175. // Create a new tree
  176. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  177. // Child tree
  178. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  179. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  180. token.line = 1;
  181. token.charPositionInLine = 4;
  182. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  183. // Add a child to the parent tree
  184. [parent addChild: tree];
  185. ANTLRCommonTree *newParent = [ANTLRCommonTree newTree];
  186. [newParent addChildren:parent.children];
  187. STAssertEquals([newParent getChild:0], [parent getChild:0], @"Children did not match");
  188. return;
  189. }
  190. -(void) test11AddSelfAsChild
  191. {
  192. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  193. @try
  194. {
  195. [parent addChild:parent];
  196. }
  197. @catch (NSException *e)
  198. {
  199. STAssertTrue([[e name] isEqualToString:@"ANTLRIllegalArgumentException"], @"Got wrong kind of exception! %@", [e name]);
  200. //[parent release];
  201. return;
  202. }
  203. STFail(@"Did not get an exception when adding an empty child!");
  204. return;
  205. }
  206. -(void) test12AddEmptyChildWithNoChildren
  207. {
  208. ANTLRCommonTree *emptyChild = [ANTLRCommonTree newTree];
  209. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  210. [parent addChild:emptyChild];
  211. STAssertEquals((NSInteger)[parent getChildCount], (NSInteger)0, @"There were supposed to be no children!");
  212. //[parent release];
  213. //[emptyChild release];
  214. return;
  215. }
  216. -(void) test13AddEmptyChildWithChildren
  217. {
  218. // Create a new tree
  219. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  220. // Child tree
  221. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  222. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  223. token.line = 1;
  224. token.charPositionInLine = 4;
  225. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  226. // Add a child to the parent tree
  227. [parent addChild: tree];
  228. ANTLRCommonTree *newParent = [ANTLRCommonTree newTree];
  229. [newParent addChild:parent];
  230. STAssertEquals((NSInteger)[newParent getChildCount], (NSInteger)1, @"Parent should only have 1 child: %d", [newParent getChildCount]);
  231. STAssertEquals([newParent getChild:0], tree, @"Child was not the correct object.");
  232. //[parent release];
  233. //[newParent release];
  234. //[tree release];
  235. return;
  236. }
  237. -(void) test14ChildAtIndex
  238. {
  239. // Create a new tree
  240. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  241. // Child tree
  242. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  243. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  244. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  245. // Add a child to the parent tree
  246. [parent addChild: tree];
  247. STAssertEquals((NSInteger)[parent getChildCount], (NSInteger)1, @"There were either no children or more than 1: %d", [parent getChildCount]);
  248. ANTLRCommonTree *child = [parent getChild:0];
  249. STAssertNotNil(child, @"Child at index 0 should not be nil");
  250. STAssertEquals(child, tree, @"Child and Original tree were not the same");
  251. //[parent release];
  252. return;
  253. }
  254. -(void) test15SetChildAtIndex
  255. {
  256. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  257. // Child tree
  258. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  259. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  260. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  261. tree = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeUP];
  262. tree.token.text = @"<UP>";
  263. [parent addChild:tree];
  264. STAssertTrue([parent getChild:0] == tree, @"Trees don't match");
  265. [parent setChild:0 With:tree];
  266. ANTLRCommonTree *child = [parent getChild:0];
  267. STAssertTrue([parent getChildCount] == 1, @"There were either no children or more than 1: %d", [parent getChildCount]);
  268. STAssertNotNil(child, @"Child at index 0 should not be nil");
  269. STAssertEquals(child, tree, @"Child and Original tree were not the same");
  270. //[parent release];
  271. return;
  272. }
  273. -(void) test16GetAncestor
  274. {
  275. ANTLRCommonTree *parent = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeUP];
  276. parent.token.text = @"<UP>";
  277. ANTLRCommonTree *down = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeDOWN];
  278. down.token.text = @"<DOWN>";
  279. [parent addChild:down];
  280. // Child tree
  281. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  282. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  283. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  284. [down addChild:tree];
  285. STAssertTrue([tree hasAncestor:ANTLRTokenTypeUP], @"Should have an ancestor of type ANTLRTokenTypeUP");
  286. ANTLRCommonTree *ancestor = [tree getAncestor:ANTLRTokenTypeUP];
  287. STAssertNotNil(ancestor, @"Ancestor should not be nil");
  288. STAssertEquals(ancestor, parent, @"Acenstors do not match");
  289. //[parent release];
  290. return;
  291. }
  292. -(void) test17FirstChildWithType
  293. {
  294. // Create a new tree
  295. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  296. ANTLRCommonTree *up = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeUP];
  297. ANTLRCommonTree *down = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeDOWN];
  298. [parent addChild:up];
  299. [parent addChild:down];
  300. ANTLRCommonTree *found = (ANTLRCommonTree *)[parent getFirstChildWithType:ANTLRTokenTypeDOWN];
  301. STAssertNotNil(found, @"Child with type DOWN should not be nil");
  302. if (found != nil) {
  303. STAssertNotNil(found.token, @"Child token with type DOWN should not be nil");
  304. if (found.token != nil)
  305. STAssertEquals((NSInteger)found.token.type, (NSInteger)ANTLRTokenTypeDOWN, @"Token type was not correct, should be down!");
  306. }
  307. found = (ANTLRCommonTree *)[parent getFirstChildWithType:ANTLRTokenTypeUP];
  308. STAssertNotNil(found, @"Child with type UP should not be nil");
  309. if (found != nil) {
  310. STAssertNotNil(found.token, @"Child token with type UP should not be nil");
  311. if (found.token != nil)
  312. STAssertEquals((NSInteger)found.token.type, (NSInteger)ANTLRTokenTypeUP, @"Token type was not correct, should be up!");
  313. }
  314. //[parent release];
  315. return;
  316. }
  317. -(void) test18SanityCheckParentAndChildIndexesForParentTree
  318. {
  319. // Child tree
  320. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  321. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  322. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  323. ANTLRCommonTree *parent = [ANTLRCommonTree newTreeWithTokenType:555];
  324. STAssertNotNil(tree, @"tree should not be nil");
  325. @try
  326. {
  327. [tree sanityCheckParentAndChildIndexes];
  328. }
  329. @catch (NSException * e)
  330. {
  331. STFail(@"Exception was thrown and this is not what's right...");
  332. }
  333. BOOL passed = NO;
  334. @try
  335. {
  336. [tree sanityCheckParentAndChildIndexes:parent At:0];
  337. }
  338. @catch (NSException * e)
  339. {
  340. STAssertTrue([[e name] isEqualToString:@"ANTLRIllegalStateException"], @"Exception was not an ANTLRIllegalStateException but was %@", [e name]);
  341. passed = YES;
  342. }
  343. if (!passed)
  344. {
  345. STFail(@"An exception should have been thrown");
  346. }
  347. STAssertNotNil(parent, @"parent should not be nil");
  348. [parent addChild:tree];
  349. @try
  350. {
  351. [tree sanityCheckParentAndChildIndexes:parent At:0];
  352. }
  353. @catch (NSException * e)
  354. {
  355. STFail(@"No exception should have been thrown!");
  356. }
  357. return;
  358. }
  359. -(void) test19DeleteChild
  360. {
  361. // Child tree
  362. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  363. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  364. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  365. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  366. [parent addChild:tree];
  367. ANTLRCommonTree *deletedChild = [parent deleteChild:0];
  368. STAssertEquals(deletedChild, tree, @"Children do not match!");
  369. STAssertEquals((NSInteger)[parent getChildCount], (NSInteger)0, @"Child count should be zero!");
  370. return;
  371. }
  372. -(void) test20TreeDescriptions
  373. {
  374. // Child tree
  375. ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:@"this||is||a||double||piped||separated||csv"];
  376. ANTLRCommonToken *token = [ANTLRCommonToken newToken:stream Type:555 Channel:ANTLRTokenChannelDefault Start:4 Stop:5];
  377. ANTLRCommonTree *tree = [ANTLRCommonTree newTreeWithToken:token];
  378. // Description for tree
  379. NSString *treeDesc = [tree treeDescription];
  380. STAssertNotNil(treeDesc, @"Tree description should not be nil");
  381. STAssertTrue([treeDesc isEqualToString:@"||"], @"Tree description was not || but rather %@", treeDesc);
  382. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  383. STAssertTrue([[parent treeDescription] isEqualToString:@"nil"], @"Tree description was not nil was %@", [parent treeDescription]);
  384. [parent addChild:tree];
  385. treeDesc = [parent treeDescription];
  386. STAssertTrue([treeDesc isEqualToString:@"||"], @"Tree description was not || but was: %@", treeDesc);
  387. // Test non empty parent
  388. ANTLRCommonTree *down = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeDOWN];
  389. down.token.text = @"<DOWN>";
  390. [tree addChild:down];
  391. treeDesc = [parent treeDescription];
  392. STAssertTrue([treeDesc isEqualToString:@"(|| <DOWN>)"], @"Tree description was wrong expected (|| <DOWN>) but got: %@", treeDesc);
  393. return;
  394. }
  395. -(void) test21ReplaceChildrenAtIndexWithNoChildren
  396. {
  397. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  398. ANTLRCommonTree *parent2 = [ANTLRCommonTree newTree];
  399. ANTLRCommonTree *child = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeDOWN];
  400. child.token.text = @"<DOWN>";
  401. [parent2 addChild:child];
  402. @try
  403. {
  404. [parent replaceChildrenFrom:1 To:2 With:parent2];
  405. }
  406. @catch (NSException *ex)
  407. {
  408. STAssertTrue([[ex name] isEqualToString:@"ANTLRIllegalArgumentException"], @"Expected an illegal argument exception... Got instead: %@", [ex name]);
  409. return;
  410. }
  411. STFail(@"Exception was not thrown when I tried to replace a child on a parent with no children");
  412. return;
  413. }
  414. -(void) test22ReplaceChildrenAtIndex
  415. {
  416. ANTLRCommonTree *parent1 = [ANTLRCommonTree newTree];
  417. ANTLRCommonTree *child1 = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeUP];
  418. [parent1 addChild:child1];
  419. ANTLRCommonTree *parent2 = [ANTLRCommonTree newTree];
  420. ANTLRCommonTree *child2 = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeDOWN];
  421. child2.token.text = @"<DOWN>";
  422. [parent2 addChild:child2];
  423. [parent2 replaceChildrenFrom:0 To:0 With:parent1];
  424. STAssertEquals([parent2 getChild:0], child1, @"Child for parent 2 should have been from parent 1");
  425. return;
  426. }
  427. -(void) test23ReplaceChildrenAtIndexWithChild
  428. {
  429. ANTLRCommonTree *replacement = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeUP];
  430. replacement.token.text = @"<UP>";
  431. ANTLRCommonTree *parent = [ANTLRCommonTree newTree];
  432. ANTLRCommonTree *child = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeDOWN];
  433. child.token.text = @"<DOWN>";
  434. [parent addChild:child];
  435. [parent replaceChildrenFrom:0 To:0 With:replacement];
  436. STAssertTrue([parent getChild:0] == replacement, @"Children do not match");
  437. return;
  438. }
  439. -(void) test24ReplacechildrenAtIndexWithLessChildren
  440. {
  441. ANTLRCommonTree *parent1 = [ANTLRCommonTree newTree];
  442. ANTLRCommonTree *child1 = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeUP];
  443. [parent1 addChild:child1];
  444. ANTLRCommonTree *parent2 = [ANTLRCommonTree newTree];
  445. ANTLRCommonTree *child2 = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeEOF];
  446. [parent2 addChild:child2];
  447. ANTLRCommonTree *child3 = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeDOWN];
  448. child2.token.text = @"<DOWN>";
  449. [parent2 addChild:child3];
  450. [parent2 replaceChildrenFrom:0 To:1 With:parent1];
  451. STAssertEquals((NSInteger)[parent2 getChildCount], (NSInteger)1, @"Should have one child but has %d", [parent2 getChildCount]);
  452. STAssertEquals([parent2 getChild:0], child1, @"Child for parent 2 should have been from parent 1");
  453. return;
  454. }
  455. -(void) test25ReplacechildrenAtIndexWithMoreChildren
  456. {
  457. ANTLRCommonTree *parent1 = [ANTLRCommonTree newTree];
  458. ANTLRCommonTree *child1 = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeUP];
  459. [parent1 addChild:child1];
  460. ANTLRCommonTree *child2 = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeEOF];
  461. [parent1 addChild:child2];
  462. ANTLRCommonTree *parent2 = [ANTLRCommonTree newTree];
  463. ANTLRCommonTree *child3 = [ANTLRCommonTree newTreeWithTokenType:ANTLRTokenTypeDOWN];
  464. child2.token.text = @"<DOWN>";
  465. [parent2 addChild:child3];
  466. [parent2 replaceChildrenFrom:0 To:0 With:parent1];
  467. STAssertEquals((NSInteger)[parent2 getChildCount], (NSInteger)2, @"Should have one child but has %d", [parent2 getChildCount]);
  468. STAssertEquals([parent2 getChild:0], child1, @"Child for parent 2 should have been from parent 1");
  469. STAssertEquals([parent2 getChild:1], child2, @"An extra child (child2) should be in the children collection");
  470. return;
  471. }
  472. @end