• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 unit Antlr.Runtime.Tools.Tests;
2 {
3 
4   Delphi DUnit Test Case
5   ----------------------
6   This unit contains a skeleton test case class generated by the Test Case Wizard.
7   Modify the generated code to correctly setup and call the methods from the unit
8   being tested.
9 
10 }
11 
12 interface
13 
14 uses
15   SysUtils,
16   TestFramework,
17   Generics.Defaults,
18   Generics.Collections,
19   Antlr.Runtime.Tools;
20 
21 type
22   // Test methods for class IANTLRString
23   TestIANTLRString = class(TTestCase)
24   strict private
25     FIANTLRString: IANTLRString;
26   public
27     procedure SetUp; override;
28     procedure TearDown; override;
29   published
30     procedure TestGetValue;
31     procedure TestSetValue;
32   end;
33 
34   // Test methods for class TANTLRString
35   TestTANTLRString = class(TTestCase)
36   strict private
37     FANTLRString: TANTLRString;
38   public
39     procedure SetUp; override;
40     procedure TearDown; override;
41   published
42     procedure TestToString;
43   end;
44 
45   // Test methods for class ICloneable
46   TestICloneable = class(TTestCase)
47   strict private
48     FICloneable: ICloneable;
49   public
50     procedure SetUp; override;
51     procedure TearDown; override;
52   published
53     procedure TestClone;
54   end;
55 
56   // Test methods for class IList
57   TestIList = class(TTestCase)
58   strict private
59     FIList: IList<Integer>;
60   public
61     procedure SetUp; override;
62     procedure TearDown; override;
63   published
64     procedure TestGetCapacity;
65     procedure TestSetCapacity;
66     procedure TestGetCount;
67     procedure TestSetCount;
68     procedure TestGetItem;
69     procedure TestSetItem;
70     procedure TestAdd;
71     procedure TestAddRange;
72     procedure TestInsert;
73     procedure TestRemove;
74     procedure TestDelete;
75     procedure TestDeleteRange;
76     procedure TestClear;
77     procedure TestContains;
78     procedure TestIndexOf;
79   end;
80 
81   // Test methods for class IDictionary
82   TestIDictionary = class(TTestCase)
83   strict private
84     FIDictionary: IDictionary<String, Integer>;
85   public
86     procedure SetUp; override;
87     procedure TearDown; override;
88   published
89     procedure TestGetItem;
90     procedure TestSetItem;
91     procedure TestGetCount;
92     procedure TestAdd;
93     procedure TestRemove;
94     procedure TestTryGetValue;
95     procedure TestContainsKey;
96     procedure TestContainsValue;
97     procedure TestEnumeration;
98   end;
99 
100   // Test methods for record TLocalStorage
101   TestTLocalStorage = class(TTestCase)
102   published
103     procedure TestLocalIntegerStorage;
104     procedure TestLocalInterfaceStorage;
105   end;
106 
107 implementation
108 
109 type
110   IFoo = interface(IANTLRInterface)
111   ['{48E3FC72-4E63-46D8-8450-A561ECF76995}']
GetValue()112     function GetValue: String;
113     procedure SetValue(const V: String);
114     property Value: String read GetValue write SetValue;
115   end;
116 
117   TFoo = class(TANTLRObject, ICloneable, IFoo)
118     FValue: String;
GetValue()119     function GetValue: String;
120     procedure SetValue(const V: String);
Clone()121     function Clone: IANTLRInterface;
122   end;
123 
TFoo.GetValue()124 function TFoo.GetValue: String;
125 begin
126   Result := FValue;
127 end;
128 
129 procedure TFoo.SetValue(const V: String);
130 begin
131   FValue := V;
132 end;
133 
TFoo.Clone()134 function TFoo.Clone: IANTLRInterface;
135 var
136   Foo: IFoo;
137 begin
138   Foo := TFoo.Create;
139   Foo.Value := FValue;
140   Result := Foo;
141 end;
142 
143 procedure TestIANTLRString.SetUp;
144 begin
145   FIANTLRString := TANTLRString.Create('foo');
146 end;
147 
148 procedure TestIANTLRString.TearDown;
149 begin
150   FIANTLRString := nil;
151 end;
152 
153 procedure TestIANTLRString.TestGetValue;
154 var
155   ReturnValue: string;
156 begin
157   ReturnValue := FIANTLRString.GetValue;
158   CheckEquals(ReturnValue,'foo');
159 end;
160 
161 procedure TestIANTLRString.TestSetValue;
162 var
163   Value: string;
164 begin
165   Value := 'bar';
166   FIANTLRString.SetValue(Value);
167   CheckEquals(FIANTLRString.Value,'bar');
168 end;
169 
170 procedure TestTANTLRString.SetUp;
171 begin
172   FANTLRString := TANTLRString.Create('foo');
173 end;
174 
175 procedure TestTANTLRString.TearDown;
176 begin
177   FANTLRString.Free;
178   FANTLRString := nil;
179 end;
180 
181 procedure TestTANTLRString.TestToString;
182 var
183   ReturnValue: string;
184 begin
185   ReturnValue := FANTLRString.ToString;
186   CheckEquals(ReturnValue,'foo');
187 end;
188 
189 procedure TestICloneable.SetUp;
190 var
191   Foo: IFoo;
192 begin
193   Foo := TFoo.Create;
194   Foo.Value := 'original';
195   FICloneable := Foo as ICloneable;
196 end;
197 
198 procedure TestICloneable.TearDown;
199 begin
200   FICloneable := nil;
201 end;
202 
203 procedure TestICloneable.TestClone;
204 var
205   ReturnValue: IANTLRInterface;
206 begin
207   ReturnValue := FICloneable.Clone;
208   Check(Supports(ReturnValue, IFoo));
209   CheckEquals((ReturnValue as IFoo).Value,(FICloneable as IFoo).Value);
210 end;
211 
212 procedure TestIList.SetUp;
213 begin
214   FIList := TList<Integer>.Create;
215 end;
216 
217 procedure TestIList.TearDown;
218 begin
219   FIList := nil;
220 end;
221 
222 procedure TestIList.TestGetCapacity;
223 var
224   ReturnValue: Integer;
225 begin
226   FIList.Capacity := 100;
227   ReturnValue := FIList.GetCapacity;
228   CheckEquals(ReturnValue,100);
229 end;
230 
231 procedure TestIList.TestSetCapacity;
232 var
233   Value: Integer;
234 begin
235   Value := 100;
236   FIList.SetCapacity(Value);
237   CheckEquals(FIList.Capacity,100);
238 end;
239 
240 procedure TestIList.TestGetCount;
241 var
242   ReturnValue: Integer;
243 begin
244   FIList.Clear;
245   FIList.Add(123);
246   ReturnValue := FIList.GetCount;
247   CheckEquals(ReturnValue,1);
248 end;
249 
250 procedure TestIList.TestSetCount;
251 var
252   Value: Integer;
253 begin
254   Value := 4;
255   FIList.SetCount(Value);
256   CheckEquals(FIList.Count,4);
257 end;
258 
259 procedure TestIList.TestGetItem;
260 var
261   ReturnValue: Integer;
262   Index: Integer;
263 begin
264   FIList.Clear;
265   FIList.Add(100);
266   FIList.Add(200);
267   FIList.Add(300);
268   FIList.Add(400);
269   Index := 2;
270   ReturnValue := FIList.GetItem(Index);
271   CheckEquals(ReturnValue,300);
272 end;
273 
274 procedure TestIList.TestSetItem;
275 var
276   Value: Integer;
277   Index: Integer;
278 begin
279   FIList.Clear;
280   FIList.Add(100);
281   FIList.Add(200);
282   FIList.Add(300);
283   FIList.Add(400);
284   Index := 3;
285   Value := 333;
286   FIList.SetItem(Index, Value);
287   CheckEquals(FIList.Items[3],333);
288 end;
289 
290 procedure TestIList.TestAdd;
291 var
292   ReturnValue: Integer;
293   Value: Integer;
294 begin
295   FIList.Clear;
296   Value := 3;
297   ReturnValue := FIList.Add(Value);
298   CheckEquals(ReturnValue,0);
299 end;
300 
301 procedure TestIList.TestAddRange;
302 var
303   Values: array [0..3] of Integer;
304 begin
305   FIList.Clear;
306   Values[0] := 111;
307   Values[1] := 222;
308   Values[2] := 333;
309   Values[3] := 444;
310   FIList.AddRange(Values);
311   CheckEquals(FIList[0],111);
312   CheckEquals(FIList[1],222);
313   CheckEquals(FIList[2],333);
314   CheckEquals(FIList[3],444);
315 end;
316 
317 procedure TestIList.TestInsert;
318 var
319   Value: Integer;
320   Index: Integer;
321 begin
322   FIList.Clear;
323   FIList.Add(100);
324   FIList.Add(200);
325   FIList.Add(300);
326   FIList.Add(400);
327   Index := 2;
328   Value := 250;
329   FIList.Insert(Index, Value);
330   CheckEquals(FIList[1],200);
331   CheckEquals(FIList[2],250);
332   CheckEquals(FIList[3],300);
333 end;
334 
335 procedure TestIList.TestRemove;
336 var
337   ReturnValue: Integer;
338   Value: Integer;
339 begin
340   FIList.Clear;
341   FIList.Add(100);
342   FIList.Add(200);
343   FIList.Add(300);
344   FIList.Add(400);
345   Value := 300;
346   ReturnValue := FIList.Remove(Value);
347   CheckEquals(ReturnValue,2);
348 end;
349 
350 procedure TestIList.TestDelete;
351 var
352   Index: Integer;
353 begin
354   FIList.Clear;
355   FIList.Add(100);
356   FIList.Add(200);
357   FIList.Add(300);
358   FIList.Add(400);
359   Index := 2;
360   FIList.Delete(Index);
361   CheckEquals(FIList[2],400);
362 end;
363 
364 procedure TestIList.TestDeleteRange;
365 var
366   ACount: Integer;
367   AIndex: Integer;
368 begin
369   FIList.Clear;
370   FIList.Add(100);
371   FIList.Add(200);
372   FIList.Add(300);
373   FIList.Add(400);
374   AIndex := 1;
375   ACount := 2;
376   FIList.DeleteRange(AIndex, ACount);
377   CheckEquals(FIlist[0],100);
378   CheckEquals(FIlist[1],400);
379 end;
380 
381 procedure TestIList.TestClear;
382 begin
383   FIList.Clear;
384   FIList.Add(100);
385   FIList.Add(200);
386   FIList.Add(300);
387   FIList.Add(400);
388   FIList.Clear;
389   CheckEquals(FIList.Count,0);
390 end;
391 
392 procedure TestIList.TestContains;
393 var
394   ReturnValue: Boolean;
395   Value: Integer;
396 begin
397   FIList.Clear;
398   FIList.Add(100);
399   FIList.Add(200);
400   FIList.Add(300);
401   FIList.Add(400);
402   Value := 200;
403   ReturnValue := FIList.Contains(Value);
404   CheckTrue(ReturnValue);
405   Value := 250;
406   ReturnValue := FIList.Contains(Value);
407   CheckFalse(ReturnValue);
408 end;
409 
410 procedure TestIList.TestIndexOf;
411 var
412   ReturnValue: Integer;
413   Value: Integer;
414 begin
415   FIList.Clear;
416   FIList.Add(100);
417   FIList.Add(200);
418   FIList.Add(300);
419   FIList.Add(400);
420   Value := 300;
421   ReturnValue := FIList.IndexOf(Value);
422   CheckEquals(ReturnValue,2);
423   Value := 301;
424   ReturnValue := FIList.IndexOf(Value);
425   CheckEquals(ReturnValue,-1);
426 end;
427 
428 procedure TestIDictionary.SetUp;
429 begin
430   FIDictionary := TDictionary<String, Integer>.Create;
431   FIDictionary.Add('Foo',1);
432   FIDictionary.Add('Bar',3);
433   FIDictionary.Add('Baz',7);
434   FIDictionary.Add('Zip',7);
435 end;
436 
437 procedure TestIDictionary.TearDown;
438 begin
439   FIDictionary := nil;
440 end;
441 
442 procedure TestIDictionary.TestGetItem;
443 var
444   ReturnValue: Integer;
445   Key: String;
446 begin
447   Key := 'Baz';
448   ReturnValue := FIDictionary.GetItem(Key);
449   CheckEquals(ReturnValue,7);
450 end;
451 
452 procedure TestIDictionary.TestSetItem;
453 var
454   Value: Integer;
455   Key: String;
456 begin
457   Key := 'Bar';
458   Value := 20;
459   FIDictionary.SetItem(Key, Value);
460   CheckEquals(FIDictionary['Bar'],20);
461 end;
462 
463 procedure TestIDictionary.TestGetCount;
464 var
465   ReturnValue: Integer;
466 begin
467   ReturnValue := FIDictionary.GetCount;
468   CheckEquals(ReturnValue,4);
469 end;
470 
471 procedure TestIDictionary.TestAdd;
472 var
473   Value: Integer;
474   Key: String;
475 begin
476   Key := 'Key';
477   Value := -1;
478   FIDictionary.Add(Key, Value);
479   CheckEquals(FIDictionary['Key'],-1);
480 end;
481 
482 procedure TestIDictionary.TestRemove;
483 var
484   Key: String;
485 begin
486   Key := 'Bar';
487   FIDictionary.Remove(Key);
488   CheckEquals(FIDictionary.Count,3);
489 end;
490 
491 procedure TestIDictionary.TestTryGetValue;
492 var
493   ReturnValue: Boolean;
494   Value: Integer;
495   Key: String;
496 begin
497   Key := 'Zip';
498   ReturnValue := FIDictionary.TryGetValue(Key, Value);
499   CheckTrue(ReturnValue);
500   CheckEquals(Value,7);
501 
502   Key := 'Oops';
503   ReturnValue := FIDictionary.TryGetValue(Key, Value);
504   CheckFalse(ReturnValue);
505 end;
506 
507 procedure TestIDictionary.TestContainsKey;
508 var
509   ReturnValue: Boolean;
510   Key: String;
511 begin
512   Key := 'Foo';
513   ReturnValue := FIDictionary.ContainsKey(Key);
514   CheckTrue(ReturnValue);
515 
516   Key := 'foo';
517   ReturnValue := FIDictionary.ContainsKey(Key);
518   CheckFalse(ReturnValue);
519 end;
520 
521 procedure TestIDictionary.TestContainsValue;
522 var
523   ReturnValue: Boolean;
524   Value: Integer;
525 begin
526   Value := 3;
527   ReturnValue := FIDictionary.ContainsValue(Value);
528   CheckTrue(ReturnValue);
529 
530   Value := 2;
531   ReturnValue := FIDictionary.ContainsValue(Value);
532   CheckFalse(ReturnValue);
533 end;
534 
535 procedure TestIDictionary.TestEnumeration;
536 var
537   Pair: TPair<String, Integer>;
538   Foo, Bar, Baz, Zip: Boolean;
539 begin
540   Foo := False;
541   Bar := False;
542   Baz := False;
543   Zip := False;
544 
545   for Pair in FIDictionary do
546   begin
547     if (Pair.Key = 'Foo') then
548     begin
549       Foo := True;
550       CheckEquals(Pair.Value, 1);
551     end
552     else
553     if (Pair.Key = 'Bar') then
554     begin
555       Bar := True;
556       CheckEquals(Pair.Value, 3);
557     end
558     else
559     if (Pair.Key = 'Baz') then
560     begin
561       Baz := True;
562       CheckEquals(Pair.Value, 7);
563     end
564     else
565     if (Pair.Key = 'Zip') then
566     begin
567       Zip := True;
568       CheckEquals(Pair.Value, 7);
569     end
570     else
571       Check(False, 'Unknown key in dictionary');
572   end;
573   CheckTrue(Foo);
574   CheckTrue(Bar);
575   CheckTrue(Baz);
576   CheckTrue(Zip);
577 end;
578 
579 { TestTLocalStorage }
580 
581 procedure TestTLocalStorage.TestLocalIntegerStorage;
582 var
583   Locals: TLocalStorage;
584 begin
585   Locals.Initialize;
586   try
587     Locals.AsInteger['x'] := 2;
588     Locals.AsInteger['X'] := 3;
589     CheckEquals(2, Locals.AsInteger['x']);
590     CheckEquals(3, Locals.AsInteger['X']);
591     CheckEquals(0, Locals.AsInteger['y']);
592     Locals.AsInteger['X'] := Locals.AsInteger['x'] * 2;
593     CheckEquals(4, Locals.AsInteger['X']);
594     CheckEquals(2, Locals.Count);
595   finally
596     Locals.Finalize;
597   end;
598 end;
599 
600 procedure TestTLocalStorage.TestLocalInterfaceStorage;
601 var
602   Locals: TLocalStorage;
603 begin
604   Locals.Initialize;
605   try
606     { Local variable Z is never accessed again. We add it to check that there
607       will be no memory leak. }
608     Locals['Z'] := TANTLRString.Create('Value Z');
609 
610     Locals['x'] := TANTLRString.Create('Value x');
611     Locals['X'] := TANTLRString.Create('Value X');
612     CheckEquals('Value x', (Locals['x'] as IANTLRString).Value);
613     CheckEquals('Value X', (Locals['X'] as IANTLRString).Value);
614     Check(Locals['y'] = nil);
615 
616     Locals['X'] := TANTLRString.Create(
617       (Locals['X'] as IANTLRString).Value + ' Update');
618     CheckEquals('Value X Update', (Locals['X'] as IANTLRString).Value);
619     CheckEquals(3, Locals.Count);
620   finally
621     Locals.Finalize;
622   end;
623 end;
624 
625 initialization
626   // Register any test cases with the test runner
627   RegisterTest(TestIANTLRString.Suite);
628   RegisterTest(TestTANTLRString.Suite);
629   RegisterTest(TestICloneable.Suite);
630   RegisterTest(TestIList.Suite);
631   RegisterTest(TestIDictionary.Suite);
632   RegisterTest(TestTLocalStorage.Suite);
633 end.
634