1 //
2 // Copyright 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 <tuple>
8
9 #include "PreprocessorTest.h"
10 #include "compiler/preprocessor/Token.h"
11
12 namespace angle
13 {
14
15 class SpaceTest : public PreprocessorTest
16 {
17 protected:
SpaceTest()18 SpaceTest() : PreprocessorTest(SH_GLES2_SPEC) {}
19
expectSpace(const std::string & str)20 void expectSpace(const std::string &str)
21 {
22 const char *cstr = str.c_str();
23 ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0));
24
25 pp::Token token;
26 // "foo" is returned after ignoring the whitespace characters.
27 mPreprocessor.lex(&token);
28 EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
29 EXPECT_EQ("foo", token.text);
30 // The whitespace character is however recorded with the next token.
31 EXPECT_TRUE(token.hasLeadingSpace());
32 }
33 };
34
35 // Whitespace characters allowed in GLSL.
36 // Note that newline characters (\n) will be tested separately.
37 static const char kSpaceChars[] = {' ', '\t', '\v', '\f'};
38
39 // This test fixture tests the processing of a single whitespace character.
40 // All tests in this fixture are ran with all possible whitespace character
41 // allowed in GLSL.
42 class SpaceCharTest : public SpaceTest, public testing::WithParamInterface<char>
43 {};
44
TEST_P(SpaceCharTest,SpaceIgnored)45 TEST_P(SpaceCharTest, SpaceIgnored)
46 {
47 // Construct test string with the whitespace char before "foo".
48 std::string str(1, GetParam());
49 str.append("foo");
50
51 expectSpace(str);
52 }
53
54 INSTANTIATE_TEST_SUITE_P(SingleSpaceChar, SpaceCharTest, testing::ValuesIn(kSpaceChars));
55
56 // This test fixture tests the processing of a string containing consecutive
57 // whitespace characters. All tests in this fixture are ran with all possible
58 // combinations of whitespace characters allowed in GLSL.
59 typedef std::tuple<char, char, char> SpaceStringParams;
60 class SpaceStringTest : public SpaceTest, public testing::WithParamInterface<SpaceStringParams>
61 {};
62
TEST_P(SpaceStringTest,SpaceIgnored)63 TEST_P(SpaceStringTest, SpaceIgnored)
64 {
65 // Construct test string with the whitespace char before "foo".
66 std::string str;
67 str.push_back(std::get<0>(GetParam()));
68 str.push_back(std::get<1>(GetParam()));
69 str.push_back(std::get<2>(GetParam()));
70 str.append("foo");
71
72 expectSpace(str);
73 }
74
75 INSTANTIATE_TEST_SUITE_P(SpaceCharCombination,
76 SpaceStringTest,
77 testing::Combine(testing::ValuesIn(kSpaceChars),
78 testing::ValuesIn(kSpaceChars),
79 testing::ValuesIn(kSpaceChars)));
80
81 // The tests above make sure that the space char is recorded in the
82 // next token. This test makes sure that a token is not incorrectly marked
83 // to have leading space.
TEST_F(SpaceTest,LeadingSpace)84 TEST_F(SpaceTest, LeadingSpace)
85 {
86 const char *str = " foo+ -bar";
87 ASSERT_TRUE(mPreprocessor.init(1, &str, 0));
88
89 pp::Token token;
90 mPreprocessor.lex(&token);
91 EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
92 EXPECT_EQ("foo", token.text);
93 EXPECT_TRUE(token.hasLeadingSpace());
94
95 mPreprocessor.lex(&token);
96 EXPECT_EQ('+', token.type);
97 EXPECT_FALSE(token.hasLeadingSpace());
98
99 mPreprocessor.lex(&token);
100 EXPECT_EQ('-', token.type);
101 EXPECT_TRUE(token.hasLeadingSpace());
102
103 mPreprocessor.lex(&token);
104 EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
105 EXPECT_EQ("bar", token.text);
106 EXPECT_FALSE(token.hasLeadingSpace());
107 }
108
109 } // namespace angle
110