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