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