Commit c745adb0 by alokp@chromium.org

Added PreprocessorTest::preprocess that preprocesses the input string and…

Added PreprocessorTest::preprocess that preprocesses the input string and compares the output with that of the expected string. Renamed other *Test::preprocess methods to something different and clearer. git-svn-id: https://angleproject.googlecode.com/svn/trunk@1126 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 19d7aa60
......@@ -54,6 +54,7 @@
'preprocessor_tests/number_test.cpp',
'preprocessor_tests/operator_test.cpp',
'preprocessor_tests/pragma_test.cpp',
'preprocessor_tests/PreprocessorTest.cpp',
'preprocessor_tests/PreprocessorTest.h',
'preprocessor_tests/space_test.cpp',
'preprocessor_tests/token_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 "PreprocessorTest.h"
#include "Token.h"
void PreprocessorTest::preprocess(const char* input, const char* expected)
{
ASSERT_TRUE(mPreprocessor.init(1, &input, NULL));
int line = 1;
pp::Token token;
std::stringstream stream;
do
{
mPreprocessor.lex(&token);
for (; line < token.location.line; ++line)
{
stream << "\n";
}
stream << token;
} while (token.type != pp::Token::LAST);
std::string actual = stream.str();
EXPECT_STREQ(expected, actual.c_str());
}
......@@ -18,6 +18,10 @@ class PreprocessorTest : public testing::Test
protected:
PreprocessorTest() : mPreprocessor(&mDiagnostics, &mDirectiveHandler) { }
// Preprocesses the input string and verifies that it matches
// expected output.
void preprocess(const char* input, const char* expected);
MockDiagnostics mDiagnostics;
MockDirectiveHandler mDirectiveHandler;
pp::Preprocessor mPreprocessor;
......
......@@ -9,33 +9,25 @@
class ErrorTest : public PreprocessorTest
{
protected:
void preprocess(const char* str)
{
ASSERT_TRUE(mPreprocessor.init(1, &str, NULL));
pp::Token token;
mPreprocessor.lex(&token);
EXPECT_EQ(pp::Token::LAST, token.type);
EXPECT_EQ("", token.value);
}
};
TEST_F(ErrorTest, Empty)
{
const char* str = "#error\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler, handleError(pp::SourceLocation(0, 1), ""));
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(ErrorTest, OneTokenMessage)
{
const char* str = "#error foo\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -43,12 +35,13 @@ TEST_F(ErrorTest, OneTokenMessage)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(ErrorTest, TwoTokenMessage)
{
const char* str = "#error foo bar\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -56,7 +49,7 @@ TEST_F(ErrorTest, TwoTokenMessage)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(ErrorTest, Comments)
......@@ -72,6 +65,7 @@ TEST_F(ErrorTest, Comments)
"/*foo*/"
"//foo"
"\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -79,12 +73,13 @@ TEST_F(ErrorTest, Comments)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(ErrorTest, MissingNewline)
{
const char* str = "#error foo";
const char* expected = "";
using testing::_;
// Directive successfully parsed.
......@@ -93,5 +88,5 @@ TEST_F(ErrorTest, MissingNewline)
// Error reported about EOF.
EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_DIRECTIVE, _, _));
preprocess(str);
preprocess(str, expected);
}
......@@ -9,21 +9,12 @@
class ExtensionTest : public PreprocessorTest
{
protected:
void preprocess(const char* str)
{
ASSERT_TRUE(mPreprocessor.init(1, &str, NULL));
pp::Token token;
mPreprocessor.lex(&token);
EXPECT_EQ(pp::Token::LAST, token.type);
EXPECT_EQ("", token.value);
}
};
TEST_F(ExtensionTest, Valid)
{
const char* str = "#extension foo : bar\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -31,7 +22,7 @@ TEST_F(ExtensionTest, Valid)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(ExtensionTest, Comments)
......@@ -49,6 +40,7 @@ TEST_F(ExtensionTest, Comments)
"/*foo*/"
"//foo"
"\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -56,12 +48,13 @@ TEST_F(ExtensionTest, Comments)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(ExtensionTest, MissingNewline)
{
const char* str = "#extension foo : bar";
const char* expected = "";
using testing::_;
// Directive successfully parsed.
......@@ -70,7 +63,7 @@ TEST_F(ExtensionTest, MissingNewline)
// Error reported about EOF.
EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_DIRECTIVE, _, _));
preprocess(str);
preprocess(str, expected);
}
struct ExtensionTestParam
......@@ -88,6 +81,7 @@ class InvalidExtensionTest : public ExtensionTest,
TEST_P(InvalidExtensionTest, Identified)
{
ExtensionTestParam param = GetParam();
const char* expected = "\n";
using testing::_;
// No handleExtension call.
......@@ -95,7 +89,7 @@ TEST_P(InvalidExtensionTest, Identified)
// Invalid extension directive call.
EXPECT_CALL(mDiagnostics, print(param.id, pp::SourceLocation(0, 1), _));
preprocess(param.str);
preprocess(param.str, expected);
}
static const ExtensionTestParam kParams[] = {
......
......@@ -12,7 +12,7 @@
class IdentifierTest : public PreprocessorTest
{
protected:
void preprocess(const std::string& str)
void expectIdentifier(const std::string& str)
{
const char* cstr = str.c_str();
ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0));
......@@ -33,7 +33,7 @@ class SingleLetterIdentifierTest : public IdentifierTest,
TEST_P(SingleLetterIdentifierTest, Identified)
{
std::string str(1, GetParam());
preprocess(str);
expectIdentifier(str);
}
// Test string: '_'
......@@ -65,7 +65,7 @@ TEST_P(DoubleLetterIdentifierTest, Identified)
str.push_back(std::tr1::get<0>(GetParam()));
str.push_back(std::tr1::get<1>(GetParam()));
preprocess(str);
expectIdentifier(str);
}
// Test string: "__"
......@@ -158,5 +158,5 @@ TEST_F(IdentifierTest, AllLetters)
for (int c = '0'; c <= '9'; ++c)
str.push_back(c);
preprocess(str);
expectIdentifier(str);
}
......@@ -10,10 +10,10 @@
class LocationTest : public PreprocessorTest
{
protected:
void preprocess(int count,
const char* const string[],
const int length[],
const pp::SourceLocation& location)
void expectLocation(int count,
const char* const string[],
const int length[],
const pp::SourceLocation& location)
{
ASSERT_TRUE(mPreprocessor.init(count, string, length));
......@@ -35,7 +35,7 @@ TEST_F(LocationTest, String0_Line1)
loc.line = 1;
SCOPED_TRACE("String0_Line1");
preprocess(1, &str, NULL, loc);
expectLocation(1, &str, NULL, loc);
}
TEST_F(LocationTest, String0_Line2)
......@@ -46,7 +46,7 @@ TEST_F(LocationTest, String0_Line2)
loc.line = 2;
SCOPED_TRACE("String0_Line2");
preprocess(1, &str, NULL, loc);
expectLocation(1, &str, NULL, loc);
}
TEST_F(LocationTest, String1_Line1)
......@@ -57,7 +57,7 @@ TEST_F(LocationTest, String1_Line1)
loc.line = 1;
SCOPED_TRACE("String1_Line1");
preprocess(2, str, NULL, loc);
expectLocation(2, str, NULL, loc);
}
TEST_F(LocationTest, String1_Line2)
......@@ -68,7 +68,7 @@ TEST_F(LocationTest, String1_Line2)
loc.line = 2;
SCOPED_TRACE("String1_Line2");
preprocess(2, str, NULL, loc);
expectLocation(2, str, NULL, loc);
}
TEST_F(LocationTest, NewlineInsideCommentCounted)
......@@ -79,7 +79,7 @@ TEST_F(LocationTest, NewlineInsideCommentCounted)
loc.line = 3;
SCOPED_TRACE("NewlineInsideCommentCounted");
preprocess(1, &str, NULL, loc);
expectLocation(1, &str, NULL, loc);
}
TEST_F(LocationTest, ErrorLocationAfterComment)
......@@ -106,7 +106,7 @@ TEST_F(LocationTest, TokenStraddlingTwoStrings)
loc.line = 1;
SCOPED_TRACE("TokenStraddlingTwoStrings");
preprocess(2, str, NULL, loc);
expectLocation(2, str, NULL, loc);
}
TEST_F(LocationTest, TokenStraddlingThreeStrings)
......@@ -117,7 +117,7 @@ TEST_F(LocationTest, TokenStraddlingThreeStrings)
loc.line = 1;
SCOPED_TRACE("TokenStraddlingThreeStrings");
preprocess(3, str, NULL, loc);
expectLocation(3, str, NULL, loc);
}
TEST_F(LocationTest, EndOfFileWithoutNewline)
......
......@@ -81,7 +81,7 @@ INSTANTIATE_TEST_CASE_P(HexadecimalInteger_A_F,
class FloatTest : public PreprocessorTest
{
protected:
void preprocess(const std::string& str)
void expectFloat(const std::string& str)
{
const char* cstr = str.c_str();
ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0));
......@@ -110,7 +110,7 @@ TEST_P(FloatScientificTest, FloatIdentified)
str.push_back(std::tr1::get<3>(GetParam())); // exponent [0-9].
SCOPED_TRACE("FloatScientificTest");
preprocess(str);
expectFloat(str);
}
INSTANTIATE_TEST_CASE_P(FloatScientific,
......@@ -143,7 +143,7 @@ TEST_P(FloatFractionTest, FloatIdentified)
str.push_back(fraction);
SCOPED_TRACE("FloatFractionTest");
preprocess(str);
expectFloat(str);
}
INSTANTIATE_TEST_CASE_P(FloatFraction_X_X,
......@@ -166,5 +166,5 @@ INSTANTIATE_TEST_CASE_P(FloatFraction_X_0,
TEST_F(FloatTest, FractionScientific)
{
SCOPED_TRACE("FractionScientific");
preprocess("0.1e+2");
expectFloat("0.1e+2");
}
......@@ -9,21 +9,12 @@
class PragmaTest : public PreprocessorTest
{
protected:
void preprocess(const char* str)
{
ASSERT_TRUE(mPreprocessor.init(1, &str, NULL));
pp::Token token;
mPreprocessor.lex(&token);
EXPECT_EQ(pp::Token::LAST, token.type);
EXPECT_EQ("", token.value);
}
};
TEST_F(PragmaTest, EmptyName)
{
const char* str = "#pragma\n";
const char* expected = "\n";
using testing::_;
// No handlePragma calls.
......@@ -31,12 +22,13 @@ TEST_F(PragmaTest, EmptyName)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(PragmaTest, EmptyValue)
{
const char* str = "#pragma foo\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -44,12 +36,13 @@ TEST_F(PragmaTest, EmptyValue)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(PragmaTest, NameValue)
{
const char* str = "#pragma foo(bar)\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -57,7 +50,7 @@ TEST_F(PragmaTest, NameValue)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(PragmaTest, Comments)
......@@ -77,6 +70,7 @@ TEST_F(PragmaTest, Comments)
"/*foo*/"
"//foo"
"\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -84,12 +78,13 @@ TEST_F(PragmaTest, Comments)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(PragmaTest, MissingNewline)
{
const char* str = "#pragma foo(bar)";
const char* expected = "";
using testing::_;
// Pragma successfully parsed.
......@@ -98,7 +93,7 @@ TEST_F(PragmaTest, MissingNewline)
// Error reported about EOF.
EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_DIRECTIVE, _, _));
preprocess(str);
preprocess(str, expected);
}
class InvalidPragmaTest : public PragmaTest,
......@@ -109,6 +104,7 @@ class InvalidPragmaTest : public PragmaTest,
TEST_P(InvalidPragmaTest, Identified)
{
const char* str = GetParam();
const char* expected = "\n";
using testing::_;
// No handlePragma calls.
......@@ -118,7 +114,7 @@ TEST_P(InvalidPragmaTest, Identified)
print(pp::Diagnostics::UNRECOGNIZED_PRAGMA,
pp::SourceLocation(0, 1), _));
preprocess(str);
preprocess(str, expected);
}
INSTANTIATE_TEST_CASE_P(All, InvalidPragmaTest, testing::Values(
......
......@@ -10,7 +10,7 @@
class SpaceTest : public PreprocessorTest
{
protected:
void preprocess(const std::string& str)
void expectSpace(const std::string& str)
{
const char* cstr = str.c_str();
ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0));
......@@ -42,8 +42,8 @@ TEST_P(SpaceCharTest, SpaceIgnored)
// Construct test string with the whitespace char before "foo".
std::string str(1, GetParam());
str.append("foo");
preprocess(str);
expectSpace(str);
}
INSTANTIATE_TEST_CASE_P(SingleSpaceChar,
......@@ -68,7 +68,7 @@ TEST_P(SpaceStringTest, SpaceIgnored)
str.push_back(std::tr1::get<2>(GetParam()));
str.append("foo");
preprocess(str);
expectSpace(str);
}
INSTANTIATE_TEST_CASE_P(SpaceCharCombination,
......
......@@ -9,21 +9,12 @@
class VersionTest : public PreprocessorTest
{
protected:
void preprocess(const char* str)
{
ASSERT_TRUE(mPreprocessor.init(1, &str, NULL));
pp::Token token;
mPreprocessor.lex(&token);
EXPECT_EQ(pp::Token::LAST, token.type);
EXPECT_EQ("", token.value);
}
};
TEST_F(VersionTest, Valid)
{
const char* str = "#version 200\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -31,7 +22,7 @@ TEST_F(VersionTest, Valid)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(VersionTest, CommentsIgnored)
......@@ -45,6 +36,7 @@ TEST_F(VersionTest, CommentsIgnored)
"/*foo*/"
"//foo"
"\n";
const char* expected = "\n";
using testing::_;
EXPECT_CALL(mDirectiveHandler,
......@@ -52,12 +44,13 @@ TEST_F(VersionTest, CommentsIgnored)
// No error or warning.
EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
preprocess(str);
preprocess(str, expected);
}
TEST_F(VersionTest, MissingNewline)
{
const char* str = "#version 200";
const char* expected = "";
using testing::_;
// Directive successfully parsed.
......@@ -66,7 +59,7 @@ TEST_F(VersionTest, MissingNewline)
// Error reported about EOF.
EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_DIRECTIVE, _, _));
preprocess(str);
preprocess(str, expected);
}
struct VersionTestParam
......@@ -83,6 +76,7 @@ class InvalidVersionTest : public VersionTest,
TEST_P(InvalidVersionTest, Identified)
{
VersionTestParam param = GetParam();
const char* expected = "\n";
using testing::_;
// No handleVersion call.
......@@ -90,7 +84,7 @@ TEST_P(InvalidVersionTest, Identified)
// Invalid version directive call.
EXPECT_CALL(mDiagnostics, print(param.id, pp::SourceLocation(0, 1), _));
preprocess(param.str);
preprocess(param.str, expected);
}
static const VersionTestParam kParams[] = {
......
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