    1   package org.codehaus.groovy.syntax;
    2
    3
    4   import groovy.util.GroovyTestCase;
    5
    6   public class TokenTest
    7       extends GroovyTestCase
    8   {
    9
    10      public void testNothing()
    11      {
    12      }
    13
    14  /*
    15      private static final int LINE = 11;
    16      private static final int COLUMN = 33;
    17
    18      public void testConstruct()
    19      {
    20          Token token = new Token( 42,
    21                                   "forty-two",
    22                                   11,
    23                                   22 );
    24
    25          assertEquals( 42,
    26                        token.getType() );
    27
    28          assertEquals( "forty-two",
    29                        token.getText() );
    30
    31          assertEquals( 11,
    32                        token.getStartLine() );
    33
    34          assertEquals( 22,
    35                        token.getStartColumn() );
    36      }
    37
    38      public void testLeftCurlyBrace()
    39      {
    40          Token token = Token.leftCurlyBrace( LINE,
    41                                              COLUMN );
    42
    43          assertToken( token,
    44                       Token.LEFT_CURLY_BRACE,
    45                       "{" );
    46      }
    47
    48      public void testRightCurlyBrace()
    49      {
    50          Token token = Token.rightCurlyBrace( LINE,
    51                                               COLUMN );
    52
    53          assertToken( token,
    54                       Token.RIGHT_CURLY_BRACE,
    55                       "}" );
    56      }
    57
    58      public void testLeftSquareBracket()
    59      {
    60          Token token = Token.leftSquareBracket( LINE,
    61                                                 COLUMN );
    62
    63          assertToken( token,
    64                       Token.LEFT_SQUARE_BRACKET,
    65                       "[" );
    66      }
    67
    68      public void testRightSquareBracket()
    69      {
    70          Token token = Token.rightSquareBracket( LINE,
    71                                                  COLUMN );
    72
    73          assertToken( token,
    74                       Token.RIGHT_SQUARE_BRACKET,
    75                       "]" );
    76      }
    77
    78      public void testLeftParenthesis()
    79      {
    80          Token token = Token.leftParenthesis( LINE,
    81                                               COLUMN );
    82
    83          assertToken( token,
    84                       Token.LEFT_PARENTHESIS,
    85                       "(" );
    86      }
    87
    88      public void testRightParenthesis()
    89      {
    90          Token token = Token.rightParenthesis( LINE,
    91                                                COLUMN );
    92
    93          assertToken( token,
    94                       Token.RIGHT_PARENTHESIS,
    95                       ")" );
    96      }
    97
    98      public void testDot()
    99      {
    100         Token token = Token.dot( LINE,
    101                                  COLUMN );
    102
    103         assertToken( token,
    104                      Token.DOT,
    105                      "." );
    106     }
    107
    108     public void testDotDot()
    109     {
    110         Token token = Token.dotDot( LINE,
    111                                     COLUMN );
    112
    113         assertToken( token,
    114                      Token.DOT_DOT,
    115                      ".." );
    116     }
    117
    118     public void testNot()
    119     {
    120         Token token = Token.not( LINE,
    121                                  COLUMN );
    122
    123         assertToken( token,
    124                      Token.NOT,
    125                      "!" );
    126     }
    127
    128     public void testCompareNotEqual()
    129     {
    130         Token token = Token.compareNotEqual( LINE,
    131                                              COLUMN );
    132
    133         assertToken( token,
    134                      Token.COMPARE_NOT_EQUAL,
    135                      "!=" );
    136     }
    137
    138     public void testEqual()
    139     {
    140         Token token = Token.equal( LINE,
    141                                    COLUMN );
    142
    143         assertToken( token,
    144                      Token.EQUAL,
    145                      "=" );
    146     }
    147
    148     public void testCompareIdentical()
    149     {
    150         Token token = Token.compareIdentical( LINE,
    151                                               COLUMN );
    152
    153         assertToken( token,
    154                      Token.COMPARE_IDENTICAL,
    155                      "===" );
    156     }
    157
    158     public void testCompareEqual()
    159     {
    160         Token token = Token.compareEqual( LINE,
    161                                           COLUMN );
    162
    163         assertToken( token,
    164                      Token.COMPARE_EQUAL,
    165                      "==" );
    166     }
    167
    168     public void testCompareLessThan()
    169     {
    170         Token token = Token.compareLessThan( LINE,
    171                                              COLUMN );
    172
    173         assertToken( token,
    174                      Token.COMPARE_LESS_THAN,
    175                      "<" );
    176     }
    177
    178     public void testCompareLessThanEqual()
    179     {
    180         Token token = Token.compareLessThanEqual( LINE,
    181                                                   COLUMN );
    182
    183         assertToken( token,
    184                      Token.COMPARE_LESS_THAN_EQUAL,
    185                      "<=" );
    186     }
    187
    188     public void testCompareGreaterThan()
    189     {
    190         Token token = Token.compareGreaterThan( LINE,
    191                                                 COLUMN );
    192
    193         assertToken( token,
    194                      Token.COMPARE_GREATER_THAN,
    195                      ">" );
    196     }
    197
    198     public void testCompareGreaterThanEqual()
    199     {
    200         Token token = Token.compareGreaterThanEqual( LINE,
    201                                                      COLUMN );
    202
    203         assertToken( token,
    204                      Token.COMPARE_GREATER_THAN_EQUAL,
    205                      ">=" );
    206     }
    207
    208     public void testLogicalOr()
    209     {
    210         Token token = Token.logicalOr( LINE,
    211                                        COLUMN );
    212
    213         assertToken( token,
    214                      Token.LOGICAL_OR,
    215                      "||" );
    216     }
    217
    218     public void testLogicalAnd()
    219     {
    220         Token token = Token.logicalAnd( LINE,
    221                                         COLUMN );
    222
    223         assertToken( token,
    224                      Token.LOGICAL_AND,
    225                      "&&" );
    226     }
    227
    228     public void testPlus()
    229     {
    230         Token token = Token.plus( LINE,
    231                                   COLUMN );
    232
    233         assertToken( token,
    234                      Token.PLUS,
    235                      "+" );
    236     }
    237
    238     public void testPlusPlus()
    239     {
    240         Token token = Token.plusPlus( LINE,
    241                                       COLUMN );
    242
    243         assertToken( token,
    244                      Token.PLUS_PLUS,
    245                      "++" );
    246     }
    247
    248     public void testPlusEqual()
    249     {
    250         Token token = Token.plusEqual( LINE,
    251                                        COLUMN );
    252
    253         assertToken( token,
    254                      Token.PLUS_EQUAL,
    255                      "+=" );
    256     }
    257
    258     public void testMinus()
    259     {
    260         Token token = Token.minus( LINE,
    261                                    COLUMN );
    262
    263         assertToken( token,
    264                      Token.MINUS,
    265                      "-" );
    266     }
    267
    268     public void testMinusMinus()
    269     {
    270         Token token = Token.minusMinus( LINE,
    271                                         COLUMN );
    272
    273         assertToken( token,
    274                      Token.MINUS_MINUS,
    275                      "--" );
    276     }
    277
    278     public void testMinusEqual()
    279     {
    280         Token token = Token.minusEqual( LINE,
    281                                         COLUMN );
    282
    283         assertToken( token,
    284                      Token.MINUS_EQUAL,
    285                      "-=" );
    286     }
    287
    288     public void testDivide()
    289     {
    290         Token token = Token.divide( LINE,
    291                                     COLUMN );
    292
    293         assertToken( token,
    294                      Token.DIVIDE,
    295                      "/" );
    296     }
    297
    298     public void testDivideEqual()
    299     {
    300         Token token = Token.divideEqual( LINE,
    301                                          COLUMN );
    302
    303         assertToken( token,
    304                      Token.DIVIDE_EQUAL,
    305                      "/=" );
    306     }
    307
    308     public void testMod()
    309     {
    310         Token token = Token.mod( LINE,
    311                                  COLUMN );
    312
    313         assertToken( token,
    314                      Token.MOD,
    315                      "%" );
    316     }
    317
    318     public void testModEqual()
    319     {
    320         Token token = Token.modEqual( LINE,
    321                                       COLUMN );
    322
    323         assertToken( token,
    324                      Token.MOD_EQUAL,
    325                      "%=" );
    326     }
    327
    328     public void testMultiply()
    329     {
    330         Token token = Token.multiply( LINE,
    331                                       COLUMN );
    332
    333         assertToken( token,
    334                      Token.MULTIPLY,
    335                      "*" );
    336     }
    337
    338     public void testMultiplyEqual()
    339     {
    340         Token token = Token.multiplyEqual( LINE,
    341                                            COLUMN );
    342
    343         assertToken( token,
    344                      Token.MULTIPLY_EQUAL,
    345                      "*=" );
    346     }
    347
    348     public void testComma()
    349     {
    350         Token token = Token.comma( LINE,
    351                                    COLUMN );
    352
    353         assertToken( token,
    354                      Token.COMMA,
    355                      "," );
    356     }
    357
    358     public void testColon()
    359     {
    360         Token token = Token.colon( LINE,
    361                                    COLUMN );
    362
    363         assertToken( token,
    364                      Token.COLON,
    365                      ":" );
    366     }
    367
    368     public void testSemicolon()
    369     {
    370         Token token = Token.semicolon( LINE,
    371                                        COLUMN );
    372
    373         assertToken( token,
    374                      Token.SEMICOLON,
    375                      ";" );
    376     }
    377
    378     public void testQuestion()
    379     {
    380         Token token = Token.question( LINE,
    381                                       COLUMN );
    382
    383         assertToken( token,
    384                      Token.QUESTION,
    385                      "?" );
    386     }
    387
    388     public void testPipe()
    389     {
    390         Token token = Token.pipe( LINE,
    391                                   COLUMN );
    392
    393         assertToken( token,
    394                      Token.PIPE,
    395                      "|" );
    396     }
    397
    398     public void testDoubleQuoteString()
    399     {
    400         Token token = Token.doubleQuoteString( LINE,
    401                                                COLUMN,
    402                                                "cheese" );
    403
    404         assertToken( token,
    405                      Token.DOUBLE_QUOTE_STRING,
    406                      "cheese",
    407                      "<string literal>");
    408     }
    409
    410     public void testSingleQuoteString()
    411     {
    412         Token token = Token.singleQuoteString( LINE,
    413                                                COLUMN,
    414                                                "cheese" );
    415
    416         assertToken( token,
    417                      Token.SINGLE_QUOTE_STRING,
    418                      "cheese",
    419                      "<string literal>" );
    420     }
    421
    422     public void testIdentifier()
    423     {
    424         Token token = Token.identifier( LINE,
    425                                         COLUMN,
    426                                         "cheese" );
    427
    428         assertToken( token,
    429                      Token.IDENTIFIER,
    430                      "cheese",
    431                      "<identifier>" );
    432     }
    433
    434     public void testIntegerNumber()
    435     {
    436         Token token = Token.integerNumber( LINE,
    437                                            COLUMN,
    438                                            "42" );
    439
    440         assertToken( token,
    441                      Token.INTEGER_NUMBER,
    442                      "42",
    443                      "<number>" );
    444     }
    445
    446     public void testFloatNumber()
    447     {
    448         Token token = Token.floatNumber( LINE,
    449                                          COLUMN,
    450                                          "42.84" );
    451
    452         assertToken( token,
    453                      Token.FLOAT_NUMBER,
    454                      "42.84",
    455                      "<number>" );
    456     }
    457
    458     // ----------------------------------------------------------------------
    459     // ----------------------------------------------------------------------
    460
    461     public void testKeyword_As()
    462     {
    463         assertKeywordToken( "as",
    464                             Token.KEYWORD_AS );
    465     }
    466
    467     public void testKeyword_Abstract()
    468     {
    469         assertKeywordToken( "abstract",
    470                             Token.KEYWORD_ABSTRACT );
    471     }
    472
    473     public void testKeyword_Break()
    474     {
    475         assertKeywordToken( "break",
    476                             Token.KEYWORD_BREAK );
    477     }
    478
    479     public void testKeyword_Case()
    480     {
    481         assertKeywordToken( "case",
    482                             Token.KEYWORD_CASE );
    483     }
    484
    485     public void testKeyword_Catch()
    486     {
    487         assertKeywordToken( "catch",
    488                             Token.KEYWORD_CATCH );
    489     }
    490
    491     public void testKeyword_Class()
    492     {
    493         assertKeywordToken( "class",
    494                             Token.KEYWORD_CLASS );
    495     }
    496
    497     public void testKeyword_Const()
    498     {
    499         assertKeywordToken( "const",
    500                             Token.KEYWORD_CONST );
    501     }
    502
    503     public void testKeyword_Continue()
    504     {
    505         assertKeywordToken( "continue",
    506                             Token.KEYWORD_CONTINUE );
    507     }
    508
    509     public void testKeyword_Default()
    510     {
    511         assertKeywordToken( "default",
    512                             Token.KEYWORD_DEFAULT );
    513     }
    514
    515     public void testKeyword_Do()
    516     {
    517         assertKeywordToken( "do",
    518                             Token.KEYWORD_DO );
    519     }
    520
    521     public void testKeyword_Else()
    522     {
    523         assertKeywordToken( "else",
    524                             Token.KEYWORD_ELSE );
    525     }
    526
    527     public void testKeyword_Extends()
    528     {
    529         assertKeywordToken( "extends",
    530                             Token.KEYWORD_EXTENDS );
    531     }
    532
    533     public void testKeyword_Final()
    534     {
    535         assertKeywordToken( "final",
    536                             Token.KEYWORD_FINAL );
    537     }
    538
    539     public void testKeyword_Finally()
    540     {
    541         assertKeywordToken( "finally",
    542                             Token.KEYWORD_FINALLY );
    543     }
    544
    545     public void testKeyword_For()
    546     {
    547         assertKeywordToken( "for",
    548                             Token.KEYWORD_FOR );
    549     }
    550
    551     public void testKeyword_Goto()
    552     {
    553         assertKeywordToken( "goto",
    554                             Token.KEYWORD_GOTO );
    555     }
    556
    557     public void testKeyword_If()
    558     {
    559         assertKeywordToken( "if",
    560                             Token.KEYWORD_IF );
    561     }
    562
    563     public void testKeyword_Implements()
    564     {
    565         assertKeywordToken( "implements",
    566                             Token.KEYWORD_IMPLEMENTS );
    567     }
    568
    569     public void testKeyword_Import()
    570     {
    571         assertKeywordToken( "import",
    572                             Token.KEYWORD_IMPORT );
    573     }
    574
    575     public void testKeyword_Instanceof()
    576     {
    577         assertKeywordToken( "instanceof",
    578                             Token.KEYWORD_INSTANCEOF );
    579     }
    580
    581     public void testKeyword_Interface()
    582     {
    583         assertKeywordToken( "interface",
    584                             Token.KEYWORD_INTERFACE );
    585     }
    586
    587     public void testKeyword_Native()
    588     {
    589         assertKeywordToken( "native",
    590                             Token.KEYWORD_NATIVE );
    591     }
    592
    593     public void testKeyword_New()
    594     {
    595         assertKeywordToken( "new",
    596                             Token.KEYWORD_NEW );
    597     }
    598
    599     public void testKeyword_Package()
    600     {
    601         assertKeywordToken( "package",
    602                             Token.KEYWORD_PACKAGE );
    603     }
    604
    605     public void testKeyword_Private()
    606     {
    607         assertKeywordToken( "private",
    608                             Token.KEYWORD_PRIVATE );
    609     }
    610
    611     public void testKeyword_Property()
    612     {
    613         assertKeywordToken( "property",
    614                             Token.KEYWORD_PROPERTY );
    615     }
    616
    617     public void testKeyword_Protected()
    618     {
    619         assertKeywordToken( "protected",
    620                             Token.KEYWORD_PROTECTED );
    621     }
    622
    623     public void testKeyword_Public()
    624     {
    625         assertKeywordToken( "public",
    626                             Token.KEYWORD_PUBLIC );
    627     }
    628
    629     public void testKeyword_Return()
    630     {
    631         assertKeywordToken( "return",
    632                             Token.KEYWORD_RETURN );
    633     }
    634
    635     public void testKeyword_Static()
    636     {
    637         assertKeywordToken( "static",
    638                             Token.KEYWORD_STATIC );
    639     }
    640
    641     public void testKeyword_Super()
    642     {
    643         assertKeywordToken( "super",
    644                             Token.KEYWORD_SUPER );
    645     }
    646
    647     public void testKeyword_Switch()
    648     {
    649         assertKeywordToken( "switch",
    650                             Token.KEYWORD_SWITCH );
    651     }
    652
    653     public void testKeyword_Synchronized()
    654     {
    655         assertKeywordToken( "synchronized",
    656                             Token.KEYWORD_SYNCHRONIZED );
    657     }
    658
    659     public void testKeyword_This()
    660     {
    661         assertKeywordToken( "this",
    662                             Token.KEYWORD_THIS );
    663     }
    664
    665     public void testKeyword_Throw()
    666     {
    667         assertKeywordToken( "throw",
    668                             Token.KEYWORD_THROW );
    669     }
    670
    671     public void testKeyword_Throws()
    672     {
    673         assertKeywordToken( "throws",
    674                             Token.KEYWORD_THROWS );
    675     }
    676
    677     public void testKeyword_Try()
    678     {
    679         assertKeywordToken( "try",
    680                             Token.KEYWORD_TRY );
    681     }
    682
    683     public void testKeyword_While()
    684     {
    685         assertKeywordToken( "while",
    686                             Token.KEYWORD_WHILE );
    687     }
    688
    689     public void testUniqueKeywordTypes()
    690     {
    691         Map keywords = Token.getKeywordMap();
    692
    693         Set types = new HashSet();
    694
    695         types.addAll( keywords.values() );
    696
    697         assertEquals( types.size(),
    698                       keywords.size() );
    699     }
    700
    701     public void testUnknownTokenType()
    702     {
    703         assertEquals( "<unknown>",
    704                       Token.getTokenDescription( 6666 ) );
    705     }
    706
    707     // ----------------------------------------------------------------------
    708     // ----------------------------------------------------------------------
    709
    710     protected void assertKeywordToken(String text,
    711                                       int expectedType)
    712     {
    713         Token token = Token.keyword( LINE,
    714                                      COLUMN,
    715                                      text );
    716
    717         assertToken( token,
    718                      expectedType,
    719                      text );
    720     }
    721
    722     protected void assertToken(Token token,
    723                                int type,
    724                                String text)
    725     {
    726         assertToken( token,
    727                      type,
    728                      text,
    729                      '"' + text + '"' );
    730     }
    731
    732     protected void assertToken(Token token,
    733                                int type,
    734                                String text,
    735                                String description)
    736     {
    737         assertEquals( type,
    738                       token.getType() );
    739
    740         assertEquals( text,
    741                       token.getText() );
    742
    743         assertEquals( description,
    744                       token.getDescription() );
    745
    746         assertEquals( LINE,
    747                       token.getStartLine() );
    748
    749         assertEquals( COLUMN,
    750                       token.getStartColumn() );
    751     }
    752
    753 */
    754 }