2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-30 13:37:55 +00:00

[4088] strings, options and equal tokens are now instantiated

This commit is contained in:
Tomek Mrugalski
2015-10-29 01:02:57 +01:00
parent 38ea55139b
commit d6b6b10784
5 changed files with 72 additions and 9 deletions

View File

@@ -6,8 +6,6 @@
EvalContext::EvalContext()
: trace_scanning (false), trace_parsing (false)
{
variables["one"] = 1;
variables["two"] = 2;
}
EvalContext::~EvalContext()

View File

@@ -18,7 +18,7 @@ public:
EvalContext ();
virtual ~EvalContext ();
std::map<std::string, int> variables;
isc::dhcp::Expression expression;
int result;

View File

@@ -35,7 +35,7 @@ blank [ \t]
{blank}+ loc.step();
[\n]+ loc.lines(yyleng); loc.step();
\"[a-zA-Z_0-9]*\" {
\'[a-zA-Z_0-9]*\' {
// This is a string, no need to do any conversions here.
return isc::eval::EvalParser::make_STRING(yytext, loc);
}

View File

@@ -11,6 +11,8 @@
#include <string>
#include <eval/token.h>
class EvalContext;
using namespace isc::dhcp;
}
// The parsing context.
%param { EvalContext& ctx }
@@ -45,12 +47,21 @@ class EvalContext;
// Expression can either be a single token or a (something == something) expression
expression:
token EQUAL token
token EQUAL token {
TokenPtr eq(new TokenEqual());
ctx.expression.push_back(eq);
}
| token;
token:
STRING { /* push back TokenString */ }
| OPTION { /* push back TokenOption */ }
STRING {
TokenPtr str(new TokenString($1));
ctx.expression.push_back(str);
}
| OPTION {
TokenPtr opt(new TokenOption($1));
ctx.expression.push_back(opt);
}
| SUBSTRING "(" token "," token "," token ")" {
/* push back TokenSubstring */
}

View File

@@ -15,6 +15,8 @@
#include <config.h>
#include <eval/token.h>
#include <eval/eval_context.h>
#include <eval/token.h>
#include <dhcp/pkt4.h>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
@@ -25,12 +27,64 @@ using namespace isc::dhcp;
namespace {
TEST(EvalContextTest, basic) {
class EvalContextTest : public ::testing::Test {
public:
void checkStringToken(const TokenPtr& token, const std::string& expected) {
ASSERT_TRUE(token);
boost::shared_ptr<TokenString> str = boost::dynamic_pointer_cast<TokenString>(token);
ASSERT_TRUE(str);
Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345));
ValueStack values;
EXPECT_NO_THROW(token->evaluate(*pkt4, values));
ASSERT_EQ(1, values.size());
EXPECT_EQ(expected, values.top());
}
void checkEqToken(const TokenPtr& token) {
ASSERT_TRUE(token);
boost::shared_ptr<TokenEqual> eq = boost::dynamic_pointer_cast<TokenEqual>(token);
EXPECT_TRUE(eq);
}
};
TEST_F(EvalContextTest, basic) {
EvalContext tmp;
EXPECT_NO_THROW(tmp.parseString("option[123] == \"MSFT\""));
EXPECT_NO_THROW(tmp.parseString("option[123] == 'MSFT'"));
}
TEST_F(EvalContextTest, string) {
EvalContext eval;
EXPECT_NO_THROW(eval.parseString("'foo'"));
ASSERT_EQ(1, eval.expression.size());
TokenPtr tmp = eval.expression.at(0);
checkStringToken(tmp, "foo");
}
TEST_F(EvalContextTest, equal) {
EvalContext eval;
EXPECT_NO_THROW(eval.parseString("'foo' == 'bar'"));
ASSERT_EQ(3, eval.expression.size());
TokenPtr tmp1 = eval.expression.at(0);
TokenPtr tmp2 = eval.expression.at(1);
TokenPtr tmp3 = eval.expression.at(2);
checkStringToken(tmp1, "foo");
checkStringToken(tmp2, "bar");
checkEqToken(tmp3);
}
};