Home | History | Annotate | Download | only in preprocessor_tests
      1 //
      2 // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 #include "PreprocessorTest.h"
      8 #include "Token.h"
      9 
     10 #define CLOSED_RANGE(x, y) testing::Range(x, static_cast<char>((y) + 1))
     11 
     12 class IdentifierTest : public PreprocessorTest
     13 {
     14 protected:
     15     void expectIdentifier(const std::string& str)
     16     {
     17         const char* cstr = str.c_str();
     18         ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0));
     19 
     20         pp::Token token;
     21         mPreprocessor.lex(&token);
     22         EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
     23         EXPECT_EQ(str, token.text);
     24     }
     25 };
     26 
     27 class SingleLetterIdentifierTest : public IdentifierTest,
     28                                    public testing::WithParamInterface<char>
     29 {
     30 };
     31 
     32 // This test covers identifier names of form [_a-zA-Z].
     33 TEST_P(SingleLetterIdentifierTest, Identified)
     34 {
     35     std::string str(1, GetParam());
     36     expectIdentifier(str);
     37 }
     38 
     39 // Test string: '_'
     40 INSTANTIATE_TEST_CASE_P(Underscore,
     41                         SingleLetterIdentifierTest,
     42                         testing::Values('_'));
     43 
     44 // Test string: [a-z]
     45 INSTANTIATE_TEST_CASE_P(a_z,
     46                         SingleLetterIdentifierTest,
     47                         CLOSED_RANGE('a', 'z'));
     48 
     49 // Test string: [A-Z]
     50 INSTANTIATE_TEST_CASE_P(A_Z,
     51                         SingleLetterIdentifierTest,
     52                         CLOSED_RANGE('A', 'Z'));
     53 
     54 typedef std::tr1::tuple<char, char> IdentifierParams;
     55 class DoubleLetterIdentifierTest :
     56     public IdentifierTest,
     57     public testing::WithParamInterface<IdentifierParams>
     58 {
     59 };
     60 
     61 // This test covers identifier names of form [_a-zA-Z][_a-zA-Z0-9].
     62 TEST_P(DoubleLetterIdentifierTest, Identified)
     63 {
     64     std::string str;
     65     str.push_back(std::tr1::get<0>(GetParam()));
     66     str.push_back(std::tr1::get<1>(GetParam()));
     67 
     68     expectIdentifier(str);
     69 }
     70 
     71 // Test string: "__"
     72 INSTANTIATE_TEST_CASE_P(Underscore_Underscore,
     73                         DoubleLetterIdentifierTest,
     74                         testing::Combine(testing::Values('_'),
     75                                          testing::Values('_')));
     76 
     77 // Test string: "_"[a-z]
     78 INSTANTIATE_TEST_CASE_P(Underscore_a_z,
     79                         DoubleLetterIdentifierTest,
     80                         testing::Combine(testing::Values('_'),
     81                                          CLOSED_RANGE('a', 'z')));
     82 
     83 // Test string: "_"[A-Z]
     84 INSTANTIATE_TEST_CASE_P(Underscore_A_Z,
     85                         DoubleLetterIdentifierTest,
     86                         testing::Combine(testing::Values('_'),
     87                                          CLOSED_RANGE('A', 'Z')));
     88 
     89 // Test string: "_"[0-9]
     90 INSTANTIATE_TEST_CASE_P(Underscore_0_9,
     91                         DoubleLetterIdentifierTest,
     92                         testing::Combine(testing::Values('_'),
     93                                          CLOSED_RANGE('0', '9')));
     94 
     95 // Test string: [a-z]"_"
     96 INSTANTIATE_TEST_CASE_P(a_z_Underscore,
     97                         DoubleLetterIdentifierTest,
     98                         testing::Combine(CLOSED_RANGE('a', 'z'),
     99                                          testing::Values('_')));
    100 
    101 // Test string: [a-z][a-z]
    102 INSTANTIATE_TEST_CASE_P(a_z_a_z,
    103                         DoubleLetterIdentifierTest,
    104                         testing::Combine(CLOSED_RANGE('a', 'z'),
    105                                          CLOSED_RANGE('a', 'z')));
    106 
    107 // Test string: [a-z][A-Z]
    108 INSTANTIATE_TEST_CASE_P(a_z_A_Z,
    109                         DoubleLetterIdentifierTest,
    110                         testing::Combine(CLOSED_RANGE('a', 'z'),
    111                                          CLOSED_RANGE('A', 'Z')));
    112 
    113 // Test string: [a-z][0-9]
    114 INSTANTIATE_TEST_CASE_P(a_z_0_9,
    115                         DoubleLetterIdentifierTest,
    116                         testing::Combine(CLOSED_RANGE('a', 'z'),
    117                                          CLOSED_RANGE('0', '9')));
    118 
    119 // Test string: [A-Z]"_"
    120 INSTANTIATE_TEST_CASE_P(A_Z_Underscore,
    121                         DoubleLetterIdentifierTest,
    122                         testing::Combine(CLOSED_RANGE('A', 'Z'),
    123                                          testing::Values('_')));
    124 
    125 // Test string: [A-Z][a-z]
    126 INSTANTIATE_TEST_CASE_P(A_Z_a_z,
    127                         DoubleLetterIdentifierTest,
    128                         testing::Combine(CLOSED_RANGE('A', 'Z'),
    129                                          CLOSED_RANGE('a', 'z')));
    130 
    131 // Test string: [A-Z][A-Z]
    132 INSTANTIATE_TEST_CASE_P(A_Z_A_Z,
    133                         DoubleLetterIdentifierTest,
    134                         testing::Combine(CLOSED_RANGE('A', 'Z'),
    135                                          CLOSED_RANGE('A', 'Z')));
    136 
    137 // Test string: [A-Z][0-9]
    138 INSTANTIATE_TEST_CASE_P(A_Z_0_9,
    139                         DoubleLetterIdentifierTest,
    140                         testing::Combine(CLOSED_RANGE('A', 'Z'),
    141                                          CLOSED_RANGE('0', '9')));
    142 
    143 // The tests above cover one-letter and various combinations of two-letter
    144 // identifier names. This test covers all characters in a single string.
    145 TEST_F(IdentifierTest, AllLetters)
    146 {
    147     std::string str;
    148     for (char c = 'a'; c <= 'z'; ++c)
    149         str.push_back(c);
    150 
    151     str.push_back('_');
    152 
    153     for (char c = 'A'; c <= 'Z'; ++c)
    154         str.push_back(c);
    155 
    156     str.push_back('_');
    157 
    158     for (char c = '0'; c <= '9'; ++c)
    159         str.push_back(c);
    160 
    161     expectIdentifier(str);
    162 }
    163