Commit 41096840 authored by Alastair Robertson's avatar Alastair Robertson

Update semantic analyer tests to parse an input string into an AST first

parent 5f5d147a
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "bpftrace.h"
#include "parser.tab.hh"
#include "driver.h"
#include "semantic_analyser.h"
namespace bpftrace {
......@@ -11,61 +11,36 @@ public:
MOCK_METHOD1(add_probe, int(ast::Probe &p));
};
namespace ast {
using ::testing::_;
TEST(semantic_analyser, probe_count)
void test(BPFtrace &bpftrace, const std::string &input, int result=0)
{
MockBPFtrace bpftrace;
EXPECT_CALL(bpftrace, add_probe(_)).Times(2);
Driver driver;
ASSERT_EQ(driver.parse_str(input), 0);
// kprobe:kprobe { 123; }
// kprobe:kprobe { 123; }
Integer expr(123);
ExprStatement stmt(&expr);
StatementList stmts = {&stmt};
std::string str = "kprobe";
Probe p1(str, str, &stmts);
Probe p2(str, str, &stmts);
ProbeList pl = {&p1, &p2};
Program root(&pl);
bpftrace::ast::SemanticAnalyser semantics(&root, bpftrace);
semantics.analyse();
std::ostringstream out;
ast::SemanticAnalyser semantics(driver.root_, bpftrace, out);
EXPECT_EQ(semantics.analyse(), result);
}
TEST(semantic_analyser, undefined_map)
void test(const std::string &input, int result=0)
{
BPFtrace bpftrace;
test(bpftrace, input, result);
}
// kprobe:kprobe / @mymap1 == 123 / { 123; }
std::string str = "kprobe";
std::string mapstr1 = "mymap1";
Integer myint(123);
Map map1(mapstr1);
Binop binop(&map1, bpftrace::Parser::token::EQ, &myint);
Predicate pred(&binop);
ExprStatement stmt(&myint);
StatementList stmts = {&stmt};
Probe p(str, str, &pred, &stmts);
ProbeList pl = {&p};
Program root(&pl);
std::ostringstream out1;
bpftrace::ast::SemanticAnalyser semantics1(&root, bpftrace, out1);
EXPECT_EQ(semantics1.analyse(), 10);
TEST(semantic_analyser, probe_count)
{
MockBPFtrace bpftrace;
EXPECT_CALL(bpftrace, add_probe(_)).Times(2);
// kprobe:kprobe / @mymap1 == 123 / { 123; @mymap1 = @mymap2; }
std::string mapstr2 = "mymap2";
Map map2(mapstr2);
AssignMapStatement assign(&map1, &map2);
stmts.push_back(&assign);
test(bpftrace, "a:b { 1; } c:d { 1; }");
}
std::ostringstream out2;
bpftrace::ast::SemanticAnalyser semantics2(&root, bpftrace, out2);
EXPECT_EQ(semantics2.analyse(), 10);
TEST(semantic_analyser, undefined_map)
{
test("a:b / @mymap == 123 / { 456; }", 10);
test("a:b / @mymap1 == 1234 / { 1234; @mymap1 = @mymap2; }", 10);
}
} // namespace ast
} // namespace bpftrace
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