diff options
| Commit | 5521d5857b122699120fbd749776dea44ffaa35b (patch) | |
|---|---|---|
| Author | 2020-11-02 09:06:17 +0200 | |
| Committer | 2020-11-09 22:08:19 +0200 | |
| Files | 6355ced879e4b0bd425d0a557911cee0706582bf /JSImpl/test | |
| IPL-5521d5857b122699120fbd749776dea44ffaa35b.tar IPL-5521d5857b122699120fbd749776dea44ffaa35b.tar.gz IPL-5521d5857b122699120fbd749776dea44ffaa35b.zip | ||
| Parent | 407a57944600619a6693dfc4d8c525163bea0e13 (diff) | |
Add tests for the AST Interpreter
4 files changed, 67 insertions, 0 deletions
| -rw-r--r-- | JSImpl/test/ASTInterpreterTests.cpp | 58 | ||||
| -rw-r--r-- | JSImpl/test/Test.make | 6 | ||||
| -rw-r--r-- | JSImpl/test/Test.vcxproj | 2 | ||||
| -rw-r--r-- | JSImpl/test/Test.vcxproj.filters | 1 |
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 <gtest/gtest.h> + +#include <src/CommonTypes.h> +#include <src/Lexer.h> +#include <src/Parser.h> +#include <src/ASTPrinter.h> +#include <src/ASTInterpreter.h> + +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<double>(*Interpreter.ModifyVariable(name)); + } + + IPLString ValueAsString(const char* name) + { + return std::get<IPLString>(*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); +} diff --git a/JSImpl/test/Test.make b/JSImpl/test/Test.make index 38d963e..0510fa2 100644 --- a/JSImpl/test/Test.make +++ b/JSImpl/test/Test.make @@ -65,6 +65,7 @@ ifeq ($(config),debug) LINKCMD = $(CXX) -o $(TARGET) $(LINKOBJS) $(RESOURCES) $(ARCH) $(ALL_LDFLAGS) $(LIBS) OBJRESP = OBJECTS := \ + $(OBJDIR)/ASTInterpreterTests.o \ $(OBJDIR)/CodeGenerationTests.o \ $(OBJDIR)/LexerTests.o \ $(OBJDIR)/ParserTests.o \ @@ -102,6 +103,7 @@ ifeq ($(config),release) LINKCMD = $(CXX) -o $(TARGET) $(LINKOBJS) $(RESOURCES) $(ARCH) $(ALL_LDFLAGS) $(LIBS) OBJRESP = OBJECTS := \ + $(OBJDIR)/ASTInterpreterTests.o \ $(OBJDIR)/CodeGenerationTests.o \ $(OBJDIR)/LexerTests.o \ $(OBJDIR)/ParserTests.o \ @@ -178,6 +180,10 @@ $(LDRESP): $(LDDEPS) | $(TARGETDIR) $(OBJDIRS) $(SILENT) echo $^ > $@ endif +$(OBJDIR)/ASTInterpreterTests.o: ASTInterpreterTests.cpp $(GCH) $(MAKEFILE) | $(OBJDIR)/. + @echo $(notdir $<) + $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -c "$<" + $(OBJDIR)/CodeGenerationTests.o: CodeGenerationTests.cpp $(GCH) $(MAKEFILE) | $(OBJDIR)/. @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -c "$<" diff --git a/JSImpl/test/Test.vcxproj b/JSImpl/test/Test.vcxproj index b2e4e83..c33f333 100644 --- a/JSImpl/test/Test.vcxproj +++ b/JSImpl/test/Test.vcxproj @@ -123,6 +123,8 @@ </Link>
</ItemDefinitionGroup>
<ItemGroup>
+ <ClCompile Include="ASTInterpreterTests.cpp">
+ </ClCompile>
<ClCompile Include="LexerTests.cpp">
</ClCompile>
<ClCompile Include="CodeGenerationTests.cpp">
diff --git a/JSImpl/test/Test.vcxproj.filters b/JSImpl/test/Test.vcxproj.filters index 412f2b7..50fcfb0 100644 --- a/JSImpl/test/Test.vcxproj.filters +++ b/JSImpl/test/Test.vcxproj.filters @@ -1,6 +1,7 @@ <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="16.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
+ <ClCompile Include="ASTInterpreterTests.cpp" />
<ClCompile Include="LexerTests.cpp" />
<ClCompile Include="CodeGenerationTests.cpp" />
<ClCompile Include="ParserTests.cpp" />
|
