From 5521d5857b122699120fbd749776dea44ffaa35b Mon Sep 17 00:00:00 2001 From: Dimitar Trendafilov Date: Mon, 2 Nov 2020 09:06:17 +0200 Subject: Add tests for the AST Interpreter --- JSImpl/test/ASTInterpreterTests.cpp | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 JSImpl/test/ASTInterpreterTests.cpp (limited to 'JSImpl/test/ASTInterpreterTests.cpp') diff --git a/JSImpl/test/ASTInterpreterTests.cpp b/JSImpl/test/ASTInterpreterTests.cpp new file mode 100644 index 0000000..389f53c --- /dev/null +++ b/JSImpl/test/ASTInterpreterTests.cpp @@ -0,0 +1,58 @@ +#include + +#include +#include +#include +#include +#include + +struct ASTInterpret : ::testing::Test +{ + void Run(const char* program) + { + auto tokens = Tokenize(program).tokens; + auto expr = Parse(tokens); + Interpreter.Run(expr.get()); + } + + bool HasVariable(const char* name) + { + return Interpreter.HasVariable(name); + } + + double ValueAsNumber(const char* name) + { + return std::get(*Interpreter.ModifyVariable(name)); + } + + IPLString ValueAsString(const char* name) + { + return std::get(*Interpreter.ModifyVariable(name)); + } + + ASTInterpreter Interpreter; +}; + +TEST_F(ASTInterpret, VarNumber) +{ + Run("var answer = 6 * 7"); + ASSERT_TRUE(HasVariable("answer")); + ASSERT_DOUBLE_EQ(ValueAsNumber("answer"), 42.0); +} + +TEST_F(ASTInterpret, VarString) +{ + Run("var name = 'John' + ' ' + 'Doe'"); + ASSERT_TRUE(HasVariable("name")); + ASSERT_EQ(ValueAsString("name"), "John Doe"); +} + +TEST_F(ASTInterpret, StringPlusNumberNotSupported) +{ + ASSERT_THROW(Run("var data = 'John Doe' + 42"), std::runtime_error); +} + +TEST_F(ASTInterpret, StringMinusString) +{ + ASSERT_THROW(Run("var data = 'John' - 'Doe'"), std::runtime_error); +} -- cgit v1.3.1-23-gd82d From fe67e5fb5ce2217a1219f0420a011487b4475761 Mon Sep 17 00:00:00 2001 From: Dimitar Trendafilov Date: Mon, 2 Nov 2020 23:44:28 +0200 Subject: Add tests for comparison variant visitor --- JSImpl/test/ASTInterpreterTests.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'JSImpl/test/ASTInterpreterTests.cpp') diff --git a/JSImpl/test/ASTInterpreterTests.cpp b/JSImpl/test/ASTInterpreterTests.cpp index 389f53c..e8cf282 100644 --- a/JSImpl/test/ASTInterpreterTests.cpp +++ b/JSImpl/test/ASTInterpreterTests.cpp @@ -56,3 +56,22 @@ TEST_F(ASTInterpret, StringMinusString) { ASSERT_THROW(Run("var data = 'John' - 'Doe'"), std::runtime_error); } + +TEST_F(ASTInterpret, LessNumber) +{ + Run("var answer = 6 < 7"); + ASSERT_TRUE(HasVariable("answer")); + ASSERT_DOUBLE_EQ(ValueAsNumber("answer"), 1.0); +} + +TEST_F(ASTInterpret, LessString) +{ + Run("var name = 'John' < 'Doe'"); + ASSERT_TRUE(HasVariable("name")); + ASSERT_DOUBLE_EQ(ValueAsNumber("name"), 0.0); +} + +TEST_F(ASTInterpret, StringLessNumber) +{ + ASSERT_THROW(Run("var data = 'John' < 42"), std::runtime_error); +} -- cgit v1.3.1-23-gd82d