Commit d5bb405f by alokp@chromium.org

Added tests for pp::Token interface and white-space handling by the…

Added tests for pp::Token interface and white-space handling by the preprocessor. By the magic of test-case generators, we have 74 tests! Review URL: https://codereview.appspot.com/6009054 git-svn-id: https://angleproject.googlecode.com/svn/trunk@1036 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 5e75f344
......@@ -16,14 +16,20 @@
],
},
{
'target_name': 'pp_tests',
'target_name': 'preprocessor_tests',
'type': 'executable',
'dependencies': ['gtest'],
'dependencies': [
'../src/build_angle.gyp:preprocessor',
'gtest',
],
'include_dirs': [
'../src/compiler/preprocessor/new',
'../third_party/googletest/include',
],
'sources': [
'../third_party/googletest/src/gtest_main.cc',
'preprocessor_tests/token_test.cpp',
'preprocessor_tests/space_test.cpp',
],
},
],
......
//
// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "gtest/gtest.h"
#include "Preprocessor.h"
// Whitespace characters allowed in GLSL.
// Note that newline characters (\n) will be tested separately.
static const char kSpaceChars[] = {' ', '\t', '\v', '\f'};
#if GTEST_HAS_PARAM_TEST
// This test fixture tests the processing of a single whitespace character.
// All tests in this fixture are ran with all possible whitespace character
// allowed in GLSL.
class SpaceCharTest : public testing::TestWithParam<char>
{
};
TEST_P(SpaceCharTest, SpaceIgnored)
{
// Construct test string with the whitespace char before "foo".
const char* identifier = "foo";
std::string str(1, GetParam());
str.append(identifier);
const char* cstr = str.c_str();
pp::Token token;
pp::Preprocessor preprocessor;
ASSERT_TRUE(preprocessor.init(1, &cstr, 0));
// Identifier "foo" is returned after ignoring the whitespace characters.
EXPECT_EQ(pp::Token::IDENTIFIER, preprocessor.lex(&token));
EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
EXPECT_STREQ(identifier, token.value.c_str());
// The whitespace character is however recorded with the next token.
EXPECT_TRUE(token.hasLeadingSpace());
}
INSTANTIATE_TEST_CASE_P(SingleSpaceChar,
SpaceCharTest,
testing::ValuesIn(kSpaceChars));
#endif // GTEST_HAS_PARAM_TEST
#if GTEST_HAS_COMBINE
// This test fixture tests the processing of a string containing consecutive
// whitespace characters. All tests in this fixture are ran with all possible
// combinations of whitespace characters allowed in GLSL.
class SpaceStringTest : public testing::TestWithParam< std::tr1::tuple<char, char, char> >
{
};
TEST_P(SpaceStringTest, SpaceIgnored)
{
// Construct test string with the whitespace char before "foo".
const char* identifier = "foo";
std::string str(1, std::tr1::get<0>(GetParam()));
str.push_back(std::tr1::get<1>(GetParam()));
str.push_back(std::tr1::get<2>(GetParam()));
str.append(identifier);
const char* cstr = str.c_str();
pp::Token token;
pp::Preprocessor preprocessor;
ASSERT_TRUE(preprocessor.init(1, &cstr, 0));
// Identifier "foo" is returned after ignoring the whitespace characters.
EXPECT_EQ(pp::Token::IDENTIFIER, preprocessor.lex(&token));
EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
EXPECT_STREQ(identifier, token.value.c_str());
// The whitespace character is however recorded with the next token.
EXPECT_TRUE(token.hasLeadingSpace());
}
INSTANTIATE_TEST_CASE_P(SpaceCharCombination,
SpaceStringTest,
testing::Combine(testing::ValuesIn(kSpaceChars),
testing::ValuesIn(kSpaceChars),
testing::ValuesIn(kSpaceChars)));
#endif // GTEST_HAS_COMBINE
// The tests above make sure that the space char is recorded in the
// next token. This test makes sure that a token is not incorrectly marked
// to have leading space.
TEST(SpaceTest, LeadingSpace)
{
const char* str = " foo+ -bar";
pp::Token token;
pp::Preprocessor preprocessor;
ASSERT_TRUE(preprocessor.init(1, &str, 0));
EXPECT_EQ(pp::Token::IDENTIFIER, preprocessor.lex(&token));
EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
EXPECT_STREQ("foo", token.value.c_str());
EXPECT_TRUE(token.hasLeadingSpace());
EXPECT_EQ('+', preprocessor.lex(&token));
EXPECT_EQ('+', token.type);
EXPECT_FALSE(token.hasLeadingSpace());
EXPECT_EQ('-', preprocessor.lex(&token));
EXPECT_EQ('-', token.type);
EXPECT_TRUE(token.hasLeadingSpace());
EXPECT_EQ(pp::Token::IDENTIFIER, preprocessor.lex(&token));
EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
EXPECT_STREQ("bar", token.value.c_str());
EXPECT_FALSE(token.hasLeadingSpace());
}
//
// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "gtest/gtest.h"
#include "Token.h"
TEST(TokenTest, DefaultConstructor)
{
pp::Token token;
EXPECT_EQ(0, token.type);
EXPECT_EQ(0, token.flags);
EXPECT_EQ(0, token.location.line);
EXPECT_EQ(0, token.location.string);
EXPECT_STREQ("", token.value.c_str());
}
TEST(TokenTest, Assignment)
{
pp::Token token;
token.type = 1;
token.flags = 1;
token.location.line = 1;
token.location.string = 1;
token.value.assign("foo");
token = pp::Token();
EXPECT_EQ(0, token.type);
EXPECT_EQ(0, token.flags);
EXPECT_EQ(0, token.location.line);
EXPECT_EQ(0, token.location.string);
EXPECT_STREQ("", token.value.c_str());
}
TEST(TokenTest, Equals)
{
pp::Token token;
EXPECT_TRUE(token.equals(pp::Token()));
token.type = 1;
EXPECT_FALSE(token.equals(pp::Token()));
token.type = 0;
token.flags = 1;
EXPECT_FALSE(token.equals(pp::Token()));
token.flags = 0;
token.location.line = 1;
EXPECT_FALSE(token.equals(pp::Token()));
token.location.line = 0;
token.location.string = 1;
EXPECT_FALSE(token.equals(pp::Token()));
token.location.string = 0;
token.value.assign("foo");
EXPECT_FALSE(token.equals(pp::Token()));
token.value.clear();
EXPECT_TRUE(token.equals(pp::Token()));
}
TEST(TokenTest, HasLeadingSpace)
{
pp::Token token;
EXPECT_FALSE(token.hasLeadingSpace());
token.setHasLeadingSpace(true);
EXPECT_TRUE(token.hasLeadingSpace());
token.setHasLeadingSpace(false);
EXPECT_FALSE(token.hasLeadingSpace());
}
TEST(TokenTest, Write)
{
pp::Token token;
token.value.assign("foo");
std::stringstream out1;
out1 << token;
EXPECT_TRUE(out1.good());
EXPECT_STREQ("foo", out1.str().c_str());
token.setHasLeadingSpace(true);
std::stringstream out2;
out2 << token;
EXPECT_TRUE(out2.good());
EXPECT_STREQ(" foo", out2.str().c_str());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment