1unit 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
12interface
13
14uses
15  SysUtils,
16  TestFramework,
17  Generics.Defaults,
18  Generics.Collections,
19  Antlr.Runtime.Tools;
20
21type
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
107implementation
108
109type
110  IFoo = interface(IANTLRInterface)
111  ['{48E3FC72-4E63-46D8-8450-A561ECF76995}']
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;
119    function GetValue: String;
120    procedure SetValue(const V: String);
121    function Clone: IANTLRInterface;
122  end;
123
124function TFoo.GetValue: String;
125begin
126  Result := FValue;
127end;
128
129procedure TFoo.SetValue(const V: String);
130begin
131  FValue := V;
132end;
133
134function TFoo.Clone: IANTLRInterface;
135var
136  Foo: IFoo;
137begin
138  Foo := TFoo.Create;
139  Foo.Value := FValue;
140  Result := Foo;
141end;
142
143procedure TestIANTLRString.SetUp;
144begin
145  FIANTLRString := TANTLRString.Create('foo');
146end;
147
148procedure TestIANTLRString.TearDown;
149begin
150  FIANTLRString := nil;
151end;
152
153procedure TestIANTLRString.TestGetValue;
154var
155  ReturnValue: string;
156begin
157  ReturnValue := FIANTLRString.GetValue;
158  CheckEquals(ReturnValue,'foo');
159end;
160
161procedure TestIANTLRString.TestSetValue;
162var
163  Value: string;
164begin
165  Value := 'bar';
166  FIANTLRString.SetValue(Value);
167  CheckEquals(FIANTLRString.Value,'bar');
168end;
169
170procedure TestTANTLRString.SetUp;
171begin
172  FANTLRString := TANTLRString.Create('foo');
173end;
174
175procedure TestTANTLRString.TearDown;
176begin
177  FANTLRString.Free;
178  FANTLRString := nil;
179end;
180
181procedure TestTANTLRString.TestToString;
182var
183  ReturnValue: string;
184begin
185  ReturnValue := FANTLRString.ToString;
186  CheckEquals(ReturnValue,'foo');
187end;
188
189procedure TestICloneable.SetUp;
190var
191  Foo: IFoo;
192begin
193  Foo := TFoo.Create;
194  Foo.Value := 'original';
195  FICloneable := Foo as ICloneable;
196end;
197
198procedure TestICloneable.TearDown;
199begin
200  FICloneable := nil;
201end;
202
203procedure TestICloneable.TestClone;
204var
205  ReturnValue: IANTLRInterface;
206begin
207  ReturnValue := FICloneable.Clone;
208  Check(Supports(ReturnValue, IFoo));
209  CheckEquals((ReturnValue as IFoo).Value,(FICloneable as IFoo).Value);
210end;
211
212procedure TestIList.SetUp;
213begin
214  FIList := TList<Integer>.Create;
215end;
216
217procedure TestIList.TearDown;
218begin
219  FIList := nil;
220end;
221
222procedure TestIList.TestGetCapacity;
223var
224  ReturnValue: Integer;
225begin
226  FIList.Capacity := 100;
227  ReturnValue := FIList.GetCapacity;
228  CheckEquals(ReturnValue,100);
229end;
230
231procedure TestIList.TestSetCapacity;
232var
233  Value: Integer;
234begin
235  Value := 100;
236  FIList.SetCapacity(Value);
237  CheckEquals(FIList.Capacity,100);
238end;
239
240procedure TestIList.TestGetCount;
241var
242  ReturnValue: Integer;
243begin
244  FIList.Clear;
245  FIList.Add(123);
246  ReturnValue := FIList.GetCount;
247  CheckEquals(ReturnValue,1);
248end;
249
250procedure TestIList.TestSetCount;
251var
252  Value: Integer;
253begin
254  Value := 4;
255  FIList.SetCount(Value);
256  CheckEquals(FIList.Count,4);
257end;
258
259procedure TestIList.TestGetItem;
260var
261  ReturnValue: Integer;
262  Index: Integer;
263begin
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);
272end;
273
274procedure TestIList.TestSetItem;
275var
276  Value: Integer;
277  Index: Integer;
278begin
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);
288end;
289
290procedure TestIList.TestAdd;
291var
292  ReturnValue: Integer;
293  Value: Integer;
294begin
295  FIList.Clear;
296  Value := 3;
297  ReturnValue := FIList.Add(Value);
298  CheckEquals(ReturnValue,0);
299end;
300
301procedure TestIList.TestAddRange;
302var
303  Values: array [0..3] of Integer;
304begin
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);
315end;
316
317procedure TestIList.TestInsert;
318var
319  Value: Integer;
320  Index: Integer;
321begin
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);
333end;
334
335procedure TestIList.TestRemove;
336var
337  ReturnValue: Integer;
338  Value: Integer;
339begin
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);
348end;
349
350procedure TestIList.TestDelete;
351var
352  Index: Integer;
353begin
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);
362end;
363
364procedure TestIList.TestDeleteRange;
365var
366  ACount: Integer;
367  AIndex: Integer;
368begin
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);
379end;
380
381procedure TestIList.TestClear;
382begin
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);
390end;
391
392procedure TestIList.TestContains;
393var
394  ReturnValue: Boolean;
395  Value: Integer;
396begin
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);
408end;
409
410procedure TestIList.TestIndexOf;
411var
412  ReturnValue: Integer;
413  Value: Integer;
414begin
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);
426end;
427
428procedure TestIDictionary.SetUp;
429begin
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);
435end;
436
437procedure TestIDictionary.TearDown;
438begin
439  FIDictionary := nil;
440end;
441
442procedure TestIDictionary.TestGetItem;
443var
444  ReturnValue: Integer;
445  Key: String;
446begin
447  Key := 'Baz';
448  ReturnValue := FIDictionary.GetItem(Key);
449  CheckEquals(ReturnValue,7);
450end;
451
452procedure TestIDictionary.TestSetItem;
453var
454  Value: Integer;
455  Key: String;
456begin
457  Key := 'Bar';
458  Value := 20;
459  FIDictionary.SetItem(Key, Value);
460  CheckEquals(FIDictionary['Bar'],20);
461end;
462
463procedure TestIDictionary.TestGetCount;
464var
465  ReturnValue: Integer;
466begin
467  ReturnValue := FIDictionary.GetCount;
468  CheckEquals(ReturnValue,4);
469end;
470
471procedure TestIDictionary.TestAdd;
472var
473  Value: Integer;
474  Key: String;
475begin
476  Key := 'Key';
477  Value := -1;
478  FIDictionary.Add(Key, Value);
479  CheckEquals(FIDictionary['Key'],-1);
480end;
481
482procedure TestIDictionary.TestRemove;
483var
484  Key: String;
485begin
486  Key := 'Bar';
487  FIDictionary.Remove(Key);
488  CheckEquals(FIDictionary.Count,3);
489end;
490
491procedure TestIDictionary.TestTryGetValue;
492var
493  ReturnValue: Boolean;
494  Value: Integer;
495  Key: String;
496begin
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);
505end;
506
507procedure TestIDictionary.TestContainsKey;
508var
509  ReturnValue: Boolean;
510  Key: String;
511begin
512  Key := 'Foo';
513  ReturnValue := FIDictionary.ContainsKey(Key);
514  CheckTrue(ReturnValue);
515
516  Key := 'foo';
517  ReturnValue := FIDictionary.ContainsKey(Key);
518  CheckFalse(ReturnValue);
519end;
520
521procedure TestIDictionary.TestContainsValue;
522var
523  ReturnValue: Boolean;
524  Value: Integer;
525begin
526  Value := 3;
527  ReturnValue := FIDictionary.ContainsValue(Value);
528  CheckTrue(ReturnValue);
529
530  Value := 2;
531  ReturnValue := FIDictionary.ContainsValue(Value);
532  CheckFalse(ReturnValue);
533end;
534
535procedure TestIDictionary.TestEnumeration;
536var
537  Pair: TPair<String, Integer>;
538  Foo, Bar, Baz, Zip: Boolean;
539begin
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);
577end;
578
579{ TestTLocalStorage }
580
581procedure TestTLocalStorage.TestLocalIntegerStorage;
582var
583  Locals: TLocalStorage;
584begin
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;
598end;
599
600procedure TestTLocalStorage.TestLocalInterfaceStorage;
601var
602  Locals: TLocalStorage;
603begin
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;
623end;
624
625initialization
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);
633end.
634