Path: blob/main_old/src/tests/preprocessor_tests/space_test.cpp
1693 views
//1// Copyright 2012 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#include <tuple>78#include "PreprocessorTest.h"9#include "compiler/preprocessor/Token.h"1011namespace angle12{1314class SpaceTest : public PreprocessorTest15{16protected:17SpaceTest() : PreprocessorTest(SH_GLES2_SPEC) {}1819void expectSpace(const std::string &str)20{21const char *cstr = str.c_str();22ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0));2324pp::Token token;25// "foo" is returned after ignoring the whitespace characters.26mPreprocessor.lex(&token);27EXPECT_EQ(pp::Token::IDENTIFIER, token.type);28EXPECT_EQ("foo", token.text);29// The whitespace character is however recorded with the next token.30EXPECT_TRUE(token.hasLeadingSpace());31}32};3334// Whitespace characters allowed in GLSL.35// Note that newline characters (\n) will be tested separately.36static const char kSpaceChars[] = {' ', '\t', '\v', '\f'};3738// This test fixture tests the processing of a single whitespace character.39// All tests in this fixture are ran with all possible whitespace character40// allowed in GLSL.41class SpaceCharTest : public SpaceTest, public testing::WithParamInterface<char>42{};4344TEST_P(SpaceCharTest, SpaceIgnored)45{46// Construct test string with the whitespace char before "foo".47std::string str(1, GetParam());48str.append("foo");4950expectSpace(str);51}5253INSTANTIATE_TEST_SUITE_P(SingleSpaceChar, SpaceCharTest, testing::ValuesIn(kSpaceChars));5455// This test fixture tests the processing of a string containing consecutive56// whitespace characters. All tests in this fixture are ran with all possible57// combinations of whitespace characters allowed in GLSL.58typedef std::tuple<char, char, char> SpaceStringParams;59class SpaceStringTest : public SpaceTest, public testing::WithParamInterface<SpaceStringParams>60{};6162TEST_P(SpaceStringTest, SpaceIgnored)63{64// Construct test string with the whitespace char before "foo".65std::string str;66str.push_back(std::get<0>(GetParam()));67str.push_back(std::get<1>(GetParam()));68str.push_back(std::get<2>(GetParam()));69str.append("foo");7071expectSpace(str);72}7374INSTANTIATE_TEST_SUITE_P(SpaceCharCombination,75SpaceStringTest,76testing::Combine(testing::ValuesIn(kSpaceChars),77testing::ValuesIn(kSpaceChars),78testing::ValuesIn(kSpaceChars)));7980// The tests above make sure that the space char is recorded in the81// next token. This test makes sure that a token is not incorrectly marked82// to have leading space.83TEST_F(SpaceTest, LeadingSpace)84{85const char *str = " foo+ -bar";86ASSERT_TRUE(mPreprocessor.init(1, &str, 0));8788pp::Token token;89mPreprocessor.lex(&token);90EXPECT_EQ(pp::Token::IDENTIFIER, token.type);91EXPECT_EQ("foo", token.text);92EXPECT_TRUE(token.hasLeadingSpace());9394mPreprocessor.lex(&token);95EXPECT_EQ('+', token.type);96EXPECT_FALSE(token.hasLeadingSpace());9798mPreprocessor.lex(&token);99EXPECT_EQ('-', token.type);100EXPECT_TRUE(token.hasLeadingSpace());101102mPreprocessor.lex(&token);103EXPECT_EQ(pp::Token::IDENTIFIER, token.type);104EXPECT_EQ("bar", token.text);105EXPECT_FALSE(token.hasLeadingSpace());106}107108} // namespace angle109110111