public void TestNestedImplicitFunctionDeclarations() { var source = CreateTestNode(@" {func_bool_bool(bool(func_int_bool(1)))} "); dialogue.Library.RegisterFunction("func_int_bool", (int i) => i == 1); dialogue.Library.RegisterFunction("func_bool_bool", (bool b) => b); testPlan = new TestPlanBuilder() .AddLine("True") .GetPlan(); // the library is NOT attached to this compilation job; all // functions will be implicitly declared var compilationJob = CompilationJob.CreateFromString("input", source); var result = Compiler.Compile(compilationJob); Assert.Empty(result.Diagnostics); Assert.Equal(2, result.Declarations.Count()); // Both declarations that resulted from the compile should be functions found on line 1 foreach (var decl in result.Declarations) { Assert.Equal(3, decl.Range.Start.Line); Assert.IsType <FunctionType>(decl.Type); } dialogue.SetProgram(result.Program); stringTable = result.StringTable; RunStandardTestcase(); }
public void TestTypeConversion() { var source = CreateTestNode(@" string + string(number): {""1"" + string(1)} string + string(bool): {""1"" + string(true)} number + number(string): {1 + number(""1"")} number + number(bool): {1 + number(true)} bool and bool(string): {true and bool(""true"")} bool and bool(number): {true and bool(1)} "); testPlan = new TestPlanBuilder() .AddLine("string + string(number): 11") .AddLine("string + string(bool): 1True") .AddLine("number + number(string): 2") .AddLine("number + number(bool): 2") .AddLine("bool and bool(string): True") .AddLine("bool and bool(number): True") .GetPlan(); var result = Compiler.Compile(CompilationJob.CreateFromString("input", source, dialogue.Library)); Assert.Empty(result.Diagnostics); dialogue.SetProgram(result.Program); stringTable = result.StringTable; RunStandardTestcase(); }
public void TestInitialValues() { var source = CreateTestNode(@" <<declare $int = 42>> <<declare $str = ""Hello"">> <<declare $bool = true>> // internal decls {$int} {$str} {$bool} // external decls {$external_int} {$external_str} {$external_bool} "); testPlan = new TestPlanBuilder() // internal decls .AddLine("42") .AddLine("Hello") .AddLine("True") // external decls .AddLine("42") .AddLine("Hello") .AddLine("True") .GetPlan(); CompilationJob compilationJob = CompilationJob.CreateFromString("input", source, dialogue.Library); compilationJob.VariableDeclarations = new[] { new Declaration { Name = "$external_str", Type = BuiltinTypes.String, DefaultValue = "Hello", }, new Declaration { Name = "$external_int", Type = BuiltinTypes.Boolean, DefaultValue = true, }, new Declaration { Name = "$external_bool", Type = BuiltinTypes.Number, DefaultValue = 42, }, }; var result = Compiler.Compile(compilationJob); Assert.Empty(result.Diagnostics); this.storage.SetValue("$external_str", "Hello"); this.storage.SetValue("$external_int", 42); this.storage.SetValue("$external_bool", true); dialogue.SetProgram(result.Program); stringTable = result.StringTable; RunStandardTestcase(); }
public void TestImplicitFunctionDeclarations() { var source = CreateTestNode(@" {func_void_bool()} {func_void_bool() and bool(func_void_bool())} { 1 + func_void_int() } { ""he"" + func_void_str() } {func_int_bool(1)} {true and func_int_bool(1)} {func_bool_bool(false)} {true and func_bool_bool(false)} {func_str_bool(""hello"")} {true and func_str_bool(""hello"")} "); dialogue.Library.RegisterFunction("func_void_bool", () => true); dialogue.Library.RegisterFunction("func_void_int", () => 1); dialogue.Library.RegisterFunction("func_void_str", () => "llo"); dialogue.Library.RegisterFunction("func_int_bool", (int i) => true); dialogue.Library.RegisterFunction("func_bool_bool", (bool b) => true); dialogue.Library.RegisterFunction("func_str_bool", (string s) => true); testPlan = new TestPlanBuilder() .AddLine("True") .AddLine("True") .AddLine("2") .AddLine("hello") .AddLine("True") .AddLine("True") .AddLine("True") .AddLine("True") .AddLine("True") .AddLine("True") .GetPlan(); // the library is NOT attached to this compilation job; all // functions will be implicitly declared var compilationJob = CompilationJob.CreateFromString("input", source); var result = Compiler.Compile(compilationJob); Assert.Empty(result.Diagnostics); dialogue.SetProgram(result.Program); stringTable = result.StringTable; RunStandardTestcase(); }
public void TestTypeConversionFailure(string test) { var source = CreateTestNode(test); testPlan = new TestPlanBuilder() .AddLine("test failure if seen") .GetPlan(); Assert.Throws <FormatException>(() => { var compilationJob = CompilationJob.CreateFromString("input", source, dialogue.Library); var result = Compiler.Compile(compilationJob); dialogue.SetProgram(result.Program); stringTable = result.StringTable; RunStandardTestcase(); }); }
public void TestSelectingOptionFromInsideOptionCallback() { var testCase = new TestPlanBuilder() .AddOption("option 1") .AddOption("option 2") .AddSelect(0) .AddLine("final line") .GetPlan(); dialogue.LineHandler = (line) => { var lineText = stringTable[line.ID]; var parsedText = dialogue.ParseMarkup(lineText.text).Text; testCase.Next(); Assert.Equal(TestPlan.Step.Type.Line, testCase.nextExpectedType); Assert.Equal(testCase.nextExpectedValue, parsedText); dialogue.Continue(); }; dialogue.OptionsHandler = (optionSet) => { testCase.Next(); int optionCount = optionSet.Options.Count(); Assert.Equal(TestPlan.Step.Type.Select, testCase.nextExpectedType); // Assert that the list of options we were given is // identical to the list of options we expect var actualOptionList = optionSet.Options .Select(o => (GetComposedTextForLine(o.Line), o.IsAvailable)) .ToList(); Assert.Equal(testCase.nextExpectedOptions, actualOptionList); var expectedOptionCount = testCase.nextExpectedOptions.Count(); Assert.Equal(expectedOptionCount, optionCount); dialogue.SetSelectedOption(0); }; dialogue.CommandHandler = (command) => { testCase.Next(); Assert.Equal(TestPlan.Step.Type.Command, testCase.nextExpectedType); dialogue.Continue(); }; dialogue.DialogueCompleteHandler = () => { testCase.Next(); Assert.Equal(TestPlan.Step.Type.Stop, testCase.nextExpectedType); dialogue.Continue(); }; var code = CreateTestNode("-> option 1\n->option 2\nfinal line\n"); var job = CompilationJob.CreateFromString("input", code); var result = Compiler.Compile(job); Assert.Empty(result.Diagnostics); this.stringTable = result.StringTable; dialogue.SetProgram(result.Program); dialogue.SetNode("Start"); dialogue.Continue(); }