Home | History | Annotate | Download | only in Antlr3.Runtime.Tests
      1 unit Antlr.Runtime.Tests;
      2 
      3 interface
      4 
      5 uses
      6   Classes,
      7   SysUtils,
      8   TestFramework,
      9   Antlr.Runtime;
     10 
     11 type
     12   // Test methods for class IANTLRStringStream
     13   TestANTLRStringStream = class(TTestCase)
     14   strict private
     15     const
     16       NL = #13#10;
     17       GRAMMARSTR = ''
     18         + 'parser grammar p;' + NL
     19         + 'prog : WHILE ID LCURLY (assign)* RCURLY EOF;' + NL
     20         + 'assign : ID ASSIGN expr SEMI ;' + NL
     21         + 'expr : INT | FLOAT | ID ;' + NL;
     22   public
     23     procedure SetUp; override;
     24     procedure TearDown; override;
     25   published
     26     procedure TestSizeOnEmptyANTLRStringStream;
     27     procedure TestSizeOnANTLRStringStream;
     28     procedure TestConsumeOnANTLRStringStream;
     29     procedure TestResetOnANTLRStringStream;
     30     procedure TestSubstringOnANTLRStringStream;
     31   end;
     32 
     33 implementation
     34 
     35 { TestANTLRStringStream }
     36 
     37 procedure TestANTLRStringStream.SetUp;
     38 begin
     39 end;
     40 
     41 procedure TestANTLRStringStream.TearDown;
     42 begin
     43 end;
     44 
     45 procedure TestANTLRStringStream.TestConsumeOnANTLRStringStream;
     46 var
     47   Stream: IANTLRStringStream;
     48 begin
     49   Stream := TANTLRStringStream.Create('One'#13#10'Two');
     50   CheckEquals(0, Stream.Index);
     51   CheckEquals(0, Stream.CharPositionInLine);
     52   CheckEquals(1, Stream.Line);
     53 
     54   Stream.Consume; // O
     55   CheckEquals(1, Stream.Index);
     56   CheckEquals(1, Stream.CharPositionInLine);
     57   CheckEquals(1, Stream.Line);
     58 
     59   Stream.Consume; // n
     60   CheckEquals(2, Stream.Index);
     61   CheckEquals(2, Stream.CharPositionInLine);
     62   CheckEquals(1, Stream.Line);
     63 
     64   Stream.Consume; // e
     65   CheckEquals(3, Stream.Index);
     66   CheckEquals(3, Stream.CharPositionInLine);
     67   CheckEquals(1, Stream.Line);
     68 
     69   Stream.Consume; // #13
     70   CheckEquals(4, Stream.Index);
     71   CheckEquals(4, Stream.CharPositionInLine);
     72   CheckEquals(1, Stream.Line);
     73 
     74   Stream.Consume; // #10
     75   CheckEquals(5, Stream.Index);
     76   CheckEquals(0, Stream.CharPositionInLine);
     77   CheckEquals(2, Stream.Line);
     78 
     79   Stream.Consume; // T
     80   CheckEquals(6, Stream.Index);
     81   CheckEquals(1, Stream.CharPositionInLine);
     82   CheckEquals(2, Stream.Line);
     83 
     84   Stream.Consume; // w
     85   CheckEquals(7, Stream.Index);
     86   CheckEquals(2, Stream.CharPositionInLine);
     87   CheckEquals(2, Stream.Line);
     88 
     89   Stream.Consume; // o
     90   CheckEquals(8, Stream.Index);
     91   CheckEquals(3, Stream.CharPositionInLine);
     92   CheckEquals(2, Stream.Line);
     93 
     94   Stream.Consume; // EOF
     95   CheckEquals(8, Stream.Index);
     96   CheckEquals(3, Stream.CharPositionInLine);
     97   CheckEquals(2, Stream.Line);
     98 
     99   Stream.Consume; // EOF
    100   CheckEquals(8, Stream.Index);
    101   CheckEquals(3, Stream.CharPositionInLine);
    102   CheckEquals(2, Stream.Line);
    103 end;
    104 
    105 procedure TestANTLRStringStream.TestResetOnANTLRStringStream;
    106 var
    107   Stream: IANTLRStringStream;
    108 begin
    109   Stream := TANTLRStringStream.Create('One'#13#10'Two');
    110   CheckEquals(0, Stream.Index);
    111   CheckEquals(0, Stream.CharPositionInLine);
    112   CheckEquals(1, Stream.Line);
    113 
    114   Stream.Consume; // O
    115   Stream.Consume; // n
    116 
    117   CheckEquals(Ord('e'), Stream.LA(1));
    118   CheckEquals(2, Stream.Index);
    119 
    120   Stream.Reset;
    121   CheckEquals(Ord('O'), Stream.LA(1));
    122   CheckEquals(0, Stream.Index);
    123   CheckEquals(0, Stream.CharPositionInLine);
    124   CheckEquals(1, Stream.Line);
    125   Stream.Consume; // O
    126 
    127   CheckEquals(Ord('n'), Stream.LA(1));
    128   CheckEquals(1, Stream.Index);
    129   CheckEquals(1, Stream.CharPositionInLine);
    130   CheckEquals(1, Stream.Line);
    131   Stream.Consume; // n
    132 
    133   CheckEquals(Ord('e'), Stream.LA(1));
    134   CheckEquals(2, Stream.Index);
    135   CheckEquals(2, Stream.CharPositionInLine);
    136   CheckEquals(1, Stream.Line);
    137   Stream.Consume; // n
    138 end;
    139 
    140 procedure TestANTLRStringStream.TestSizeOnANTLRStringStream;
    141 var
    142   S1, S2, S3: IANTLRStringStream;
    143 begin
    144   S1 := TANTLRStringStream.Create('lexer'#13#10);
    145   CheckEquals(7, S1.Size);
    146 
    147   S2 := TANTLRStringStream.Create(GRAMMARSTR);
    148   CheckEquals(Length(GRAMMARSTR), S2.Size);
    149 
    150   S3 := TANTLRStringStream.Create('grammar P;');
    151   CheckEquals(10, S3.Size);
    152 end;
    153 
    154 procedure TestANTLRStringStream.TestSizeOnEmptyANTLRStringStream;
    155 var
    156   S1: IANTLRStringStream;
    157 begin
    158   S1 := TANTLRStringStream.Create('');
    159   CheckEquals(0, S1.Size);
    160   CheckEquals(0, S1.Index);
    161 end;
    162 
    163 procedure TestANTLRStringStream.TestSubstringOnANTLRStringStream;
    164 var
    165   Stream: IANTLRStringStream;
    166 begin
    167   Stream := TANTLRStringStream.Create('One'#13#10'Two'#13#10'Three');
    168 
    169   CheckEquals('Two', Stream.Substring(5, 7));
    170   CheckEquals('One', Stream.Substring(0, 2));
    171   CheckEquals('Three', Stream.Substring(10, 14));
    172 
    173   Stream.Consume;
    174 
    175   CheckEquals('Two', Stream.Substring(5, 7));
    176   CheckEquals('One', Stream.Substring(0, 2));
    177   CheckEquals('Three', Stream.Substring(10, 14));
    178 end;
    179 
    180 initialization
    181   // Register any test cases with the test runner
    182   RegisterTest(TestANTLRStringStream.Suite);
    183 end.
    184