Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions config_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,20 @@ bool NginxConfigParser::Parse(std::istream* config_file, NginxConfig* config) {
// Error.
break;
}
return true;
if( config_stack.size()==1){
return true;
}else
{
break;
}
} else {
// Error. Unknown token.
break;
}

last_token_type = token_type;
}

printf ("Bad transition from %s to %s\n",
TokenTypeAsString(last_token_type),
TokenTypeAsString(token_type));
Expand Down
48 changes: 44 additions & 4 deletions config_parser_test.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
#include <sstream>
#include <string>
#include "gtest/gtest.h"
#include "config_parser.h"

TEST(NginxConfigParserTest, SimpleConfig) {
TEST(NginxConfigTest, ToString)
{
NginxConfigStatement statement;
statement.tokens_.push_back("foo");
statement.tokens_.push_back("bar");
EXPECT_EQ(statement.ToString(0), "foo bar;\n");
}
//blah
TEST(NginxConfigParserTest, SimpleConfig)
{
std::string config_string = "foo bar;";
std::stringstream config_stream(config_string);
NginxConfigParser parser;
NginxConfig out_config;
NginxConfig config;
EXPECT_TRUE(parser.Parse(&config_stream, &config));
EXPECT_EQ(1, config.statements_.size())
<< "Config has one statements";
EXPECT_EQ("foo", config.statements_.at(0)->tokens_.at(0));
}

bool success = parser.Parse("example_config", &out_config);
class NginxStringConfigTest : public :: testing :: Test {
protected:
bool ParseString(const std::string config_string) {
std::stringstream config_stream(config_string);
return parser_.Parse(&config_stream, &out_config_);
}
NginxConfigParser parser_;
NginxConfig out_config_;
//NginxConfigStatement statements;
};

TEST_F(NginxStringConfigTest, AnotherSimpleConfig)
{
EXPECT_TRUE(ParseString("foo bar;"));
EXPECT_EQ(1, out_config_.statements_.size())
<<"Config has one statements";
EXPECT_EQ("foo", out_config_.statements_.at(0)->tokens_.at(0));
}

EXPECT_TRUE(success);
TEST_F(NginxStringConfigTest, InvalidConfig)
{
EXPECT_FALSE(ParseString("foo bar"));
}