From aae16e8b62b915ce2d8fe06f3b8944f9090607f0 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 23 Nov 2018 12:55:28 +0100 Subject: [PATCH] General fixes for Tests --- Upsilon/Binder/Binder.cs | 5 ++- Upsilon/Binder/BoundScope.cs | 4 +- Upsilon/Evaluator/EvaluationScope.cs | 20 ++++----- Upsilon/Evaluator/Script.cs | 33 ++++----------- Upsilon/StandardLibraries/StandardLibrary.cs | 2 +- .../BasicMathExpressions.cs | 14 ++++--- .../{ => GeneralTests}/FunctionTests.cs | 30 ++++++++------ UpsilonTests/{ => GeneralTests}/IfTests.cs | 18 +++++--- .../{ => GeneralTests}/MathPrecedence.cs | 10 +++-- UpsilonTests/{ => GeneralTests}/ScopeTests.cs | 10 +++-- .../{ => GeneralTests}/StringTests.cs | 12 ++++-- UpsilonTests/{ => GeneralTests}/TableTests.cs | 24 ++++++----- .../UserDataDictionaryTests.cs | 7 +++- .../{ => GeneralTests}/UserDataListTests.cs | 9 ++-- .../UserDataOperatorTests.cs | 17 ++++---- .../{ => GeneralTests}/UserDataTests.cs | 19 +++++---- .../BasicFunctionsTests.cs | 9 +++- UpsilonTests/TestClass.cs | 41 +++++++++++++++++++ Ycicle/Program.cs | 7 +++- 19 files changed, 185 insertions(+), 106 deletions(-) rename UpsilonTests/{ => GeneralTests}/BasicMathExpressions.cs (75%) rename UpsilonTests/{ => GeneralTests}/FunctionTests.cs (84%) rename UpsilonTests/{ => GeneralTests}/IfTests.cs (77%) rename UpsilonTests/{ => GeneralTests}/MathPrecedence.cs (67%) rename UpsilonTests/{ => GeneralTests}/ScopeTests.cs (77%) rename UpsilonTests/{ => GeneralTests}/StringTests.cs (77%) rename UpsilonTests/{ => GeneralTests}/TableTests.cs (83%) rename UpsilonTests/{ => GeneralTests}/UserDataDictionaryTests.cs (75%) rename UpsilonTests/{ => GeneralTests}/UserDataListTests.cs (78%) rename UpsilonTests/{ => GeneralTests}/UserDataOperatorTests.cs (88%) rename UpsilonTests/{ => GeneralTests}/UserDataTests.cs (85%) create mode 100644 UpsilonTests/TestClass.cs diff --git a/Upsilon/Binder/Binder.cs b/Upsilon/Binder/Binder.cs index ea416b2..863f4a5 100644 --- a/Upsilon/Binder/Binder.cs +++ b/Upsilon/Binder/Binder.cs @@ -258,7 +258,10 @@ namespace Upsilon.Binder } else if (assignment.Type == Type.Unknown && assignment is BoundVariableExpression v) { - v.Variable.Type = v.Type; + v.Variable.Type = variable.Type; + } + else if (assignment.Type == Type.Unknown) + { } else { diff --git a/Upsilon/Binder/BoundScope.cs b/Upsilon/Binder/BoundScope.cs index ff2a081..4679356 100644 --- a/Upsilon/Binder/BoundScope.cs +++ b/Upsilon/Binder/BoundScope.cs @@ -2,11 +2,11 @@ using System.Collections.Generic; namespace Upsilon.Binder { - internal class BoundScope + public class BoundScope { public readonly BoundScope ParentScope; private BoundScope _readOnlyScope; - internal readonly Dictionary Variables; + public readonly Dictionary Variables; internal readonly Dictionary LocalVariables; diff --git a/Upsilon/Evaluator/EvaluationScope.cs b/Upsilon/Evaluator/EvaluationScope.cs index adec341..d9268f6 100644 --- a/Upsilon/Evaluator/EvaluationScope.cs +++ b/Upsilon/Evaluator/EvaluationScope.cs @@ -4,26 +4,26 @@ using Upsilon.Binder; namespace Upsilon.Evaluator { - internal class EvaluationScope + public class EvaluationScope { private readonly EvaluationScope _parentScope; private EvaluationScope _getOnlyParentScope; - internal readonly Dictionary Variables; - internal readonly Dictionary LocalVariables; + public readonly Dictionary Variables; + private readonly Dictionary _localVariables; internal EvaluationScope(EvaluationScope parentScope) { _parentScope = parentScope; Variables = new Dictionary(); - LocalVariables = new Dictionary(); + _localVariables = new Dictionary(); } internal EvaluationScope(Dictionary vars) { Variables = vars; - LocalVariables = new Dictionary(); + _localVariables = new Dictionary(); } internal static EvaluationScope CreateWithGetOnlyParent(EvaluationScope parent) @@ -36,13 +36,13 @@ namespace Upsilon.Evaluator { if (symbol.Local) { - if (LocalVariables.ContainsKey(symbol.Name)) + if (_localVariables.ContainsKey(symbol.Name)) { - LocalVariables[symbol.Name] = obj; + _localVariables[symbol.Name] = obj; } else { - LocalVariables.Add(symbol.Name, obj); + _localVariables.Add(symbol.Name, obj); } } else @@ -71,7 +71,7 @@ namespace Upsilon.Evaluator public bool TryGet(VariableSymbol symbol, out LuaType obj) { - if (LocalVariables.TryGetValue(symbol.Name, out obj)) + if (_localVariables.TryGetValue(symbol.Name, out obj)) return true; if (Variables.TryGetValue(symbol.Name, out obj)) return true; @@ -86,7 +86,7 @@ namespace Upsilon.Evaluator public bool TryGet(string variable, out LuaType obj) { - if (LocalVariables.TryGetValue(variable, out obj)) + if (_localVariables.TryGetValue(variable, out obj)) return true; if (Variables.TryGetValue(variable, out obj)) return true; diff --git a/Upsilon/Evaluator/Script.cs b/Upsilon/Evaluator/Script.cs index 1fa3cf4..5495bb6 100644 --- a/Upsilon/Evaluator/Script.cs +++ b/Upsilon/Evaluator/Script.cs @@ -13,43 +13,24 @@ namespace Upsilon.Evaluator public class Script { private SourceText ScriptString { get; } - private Evaluator Evaluator { get; set; } + private Evaluator Evaluator { get; } private readonly BlockStatementSyntax _parsed; private BoundScript _bound; - public Diagnostics Diagnostics { get; private set; } - private Binder.Binder Binder { get; set; } + public Diagnostics Diagnostics { get; } + private Binder.Binder Binder { get; } private EvaluationScope Scope { get; } - private static BoundScope _staticBoundScope; - private static EvaluationScope _staticScope; - - private static EvaluationScope StaticScope - { - get - { - var scope = _staticScope; - if (scope != null) - { - return scope; - } - - var (evaluationScope, boundScope) = StandardLibrary.Create(); - _staticBoundScope = boundScope; - return (_staticScope = evaluationScope); - } - } - - public Script(string scriptString) + public Script(string scriptString, Dictionary boundStaticScope, Dictionary staticScope ) { ScriptString = new SourceText(scriptString); Diagnostics = new Diagnostics(ScriptString); _parsed = Parser.Parser.Parse(scriptString, Diagnostics); - Scope = EvaluationScope.CreateWithGetOnlyParent(StaticScope); - Binder = new Binder.Binder(Diagnostics, _staticBoundScope.Variables); + var boundScope = new BoundScope(boundStaticScope, null); + Binder = new Binder.Binder(Diagnostics, boundScope.Variables); + Scope = EvaluationScope.CreateWithGetOnlyParent(new EvaluationScope(staticScope)); Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope); - Scope = Evaluator.Scope; } private Script(string scriptString, Binder.Binder binder, Evaluator evaluator) diff --git a/Upsilon/StandardLibraries/StandardLibrary.cs b/Upsilon/StandardLibraries/StandardLibrary.cs index 692a80a..d895703 100644 --- a/Upsilon/StandardLibraries/StandardLibrary.cs +++ b/Upsilon/StandardLibraries/StandardLibrary.cs @@ -6,7 +6,7 @@ using Upsilon.Evaluator; namespace Upsilon.StandardLibraries { - internal class StandardLibrary + public class StandardLibrary { public static (EvaluationScope, BoundScope) Create() { diff --git a/UpsilonTests/BasicMathExpressions.cs b/UpsilonTests/GeneralTests/BasicMathExpressions.cs similarity index 75% rename from UpsilonTests/BasicMathExpressions.cs rename to UpsilonTests/GeneralTests/BasicMathExpressions.cs index 9b46b76..98eb8a7 100644 --- a/UpsilonTests/BasicMathExpressions.cs +++ b/UpsilonTests/GeneralTests/BasicMathExpressions.cs @@ -5,8 +5,12 @@ using Xunit; namespace UpsilonTests { - public class BasicMathExpressions + public class BasicMathExpressions : TestClass { + public BasicMathExpressions(StaticScriptFixture fix) : base(fix) + { + } + [Theory] [InlineData("1+1", 2)] [InlineData("1000+1", 1001)] @@ -17,7 +21,7 @@ namespace UpsilonTests [InlineData("0.005 + 2.2", 2.205)] public void Addition(string input, double expectedOutput) { - var actual = new Script(input).Evaluate(); + var actual = new Script(input, BoundScope, StaticScope).Evaluate(); Assert.Equal(expectedOutput, actual, 8); } @@ -30,7 +34,7 @@ namespace UpsilonTests [InlineData("10.256-2.8546", 7.4014)] public void Subtraction(string input, double expectedOutput) { - var actual = new Script(input).Evaluate(); + var actual = new Script(input, BoundScope, StaticScope).Evaluate(); Assert.Equal(expectedOutput, actual, 8); } @@ -40,7 +44,7 @@ namespace UpsilonTests [InlineData("21312 * 41684", 888369408)] public void Multiplication(string input, double expectedOutput) { - var actual = new Script(input).Evaluate(); + var actual = new Script(input, BoundScope, StaticScope).Evaluate(); Assert.Equal(expectedOutput, actual, 8); } @@ -51,7 +55,7 @@ namespace UpsilonTests [InlineData("656486 / 5146.0", 127.57209483)] public void Divison(string input, double expectedOutput) { - var actual = new Script(input).Evaluate(); + var actual = new Script(input, BoundScope, StaticScope).Evaluate(); Assert.Equal(expectedOutput, actual, 8); } diff --git a/UpsilonTests/FunctionTests.cs b/UpsilonTests/GeneralTests/FunctionTests.cs similarity index 84% rename from UpsilonTests/FunctionTests.cs rename to UpsilonTests/GeneralTests/FunctionTests.cs index caf620b..f933a5e 100644 --- a/UpsilonTests/FunctionTests.cs +++ b/UpsilonTests/GeneralTests/FunctionTests.cs @@ -5,8 +5,13 @@ using Xunit; namespace UpsilonTests { - public class FunctionTests + public class FunctionTests : TestClass { + + public FunctionTests(StaticScriptFixture fix) : base(fix) + { + } + [Fact] public void BasicFunctionTest() { @@ -17,7 +22,7 @@ end a = 50 testFunc() "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -35,7 +40,7 @@ end a = 50 testFunc(100) "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -52,7 +57,7 @@ function testFunc (var1) end testFunc(100) "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.Evaluate(); Assert.Single(script.Diagnostics.Messages); @@ -67,7 +72,7 @@ function testFunc (var1) var1 == true end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var val = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -83,7 +88,7 @@ function testFunc () end a = testFunc() "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -101,7 +106,7 @@ function testFunc () end a = testFunc() "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -121,7 +126,7 @@ function testFunc () end a = testFunc() "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -137,7 +142,7 @@ a = 100 return 60 a = 87 "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -152,7 +157,7 @@ function testFunc () return 100 end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.EvaluateFunction("testFunc"); Assert.Empty(script.Diagnostics.Messages); @@ -171,7 +176,7 @@ function testFunc (b) end end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.EvaluateFunction("testFunc", new object[] {true}); Assert.Empty(script.Diagnostics.Messages); @@ -190,13 +195,12 @@ function add (a, b) return a + b end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.EvaluateFunction("add", new object[] {400, 128}); Assert.Empty(script.Diagnostics.Messages); Assert.Equal(528, result); } - } } \ No newline at end of file diff --git a/UpsilonTests/IfTests.cs b/UpsilonTests/GeneralTests/IfTests.cs similarity index 77% rename from UpsilonTests/IfTests.cs rename to UpsilonTests/GeneralTests/IfTests.cs index 5d76ebb..5fca7d6 100644 --- a/UpsilonTests/IfTests.cs +++ b/UpsilonTests/GeneralTests/IfTests.cs @@ -5,13 +5,21 @@ using Xunit; namespace UpsilonTests { - public class IfTests + public class IfTests : TestClass { + public IfTests(StaticScriptFixture fix) : base(fix) + { + } + [Fact] public void BasicIfTest() { - var input = "if true then val = true end"; - var script = new Script(input); + const string input = @" +if true then + return true +end +return false"; + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var actual = script.Evaluate(); Assert.True(actual); @@ -30,7 +38,7 @@ if {condition} then else val = {in2} end"; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var actual = script.Evaluate(); Assert.Equal(expected, actual); @@ -50,7 +58,7 @@ elseif {condition2} then else val = {in3} end"; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var actual = script.Evaluate(); Assert.Equal(expected, actual); diff --git a/UpsilonTests/MathPrecedence.cs b/UpsilonTests/GeneralTests/MathPrecedence.cs similarity index 67% rename from UpsilonTests/MathPrecedence.cs rename to UpsilonTests/GeneralTests/MathPrecedence.cs index b5c5a07..c0f82b9 100644 --- a/UpsilonTests/MathPrecedence.cs +++ b/UpsilonTests/GeneralTests/MathPrecedence.cs @@ -5,14 +5,18 @@ using Xunit; namespace UpsilonTests { - public class MathPrecedence + public class MathPrecedence : TestClass { + public MathPrecedence(StaticScriptFixture fix) : base(fix) + { + } + [Theory] [InlineData("5 * (10 + 5)", 75)] [InlineData("(10 + 5) * 5", 75)] public void Parenthesis(string input, double expectedOutput) { - var actual = new Script(input).Evaluate(); + var actual = new Script(input, BoundScope, StaticScope).Evaluate(); Assert.Equal(expectedOutput, actual, 8); } @@ -21,7 +25,7 @@ namespace UpsilonTests [InlineData("5 + 10 * 5", 55)] public void MultiplicationBeforeAddition(string input, double expectedOutput) { - var actual = new Script(input).Evaluate(); + var actual = new Script(input, BoundScope, StaticScope).Evaluate(); Assert.Equal(expectedOutput, actual, 8); } diff --git a/UpsilonTests/ScopeTests.cs b/UpsilonTests/GeneralTests/ScopeTests.cs similarity index 77% rename from UpsilonTests/ScopeTests.cs rename to UpsilonTests/GeneralTests/ScopeTests.cs index d4dafe9..4153897 100644 --- a/UpsilonTests/ScopeTests.cs +++ b/UpsilonTests/GeneralTests/ScopeTests.cs @@ -4,8 +4,12 @@ using Xunit; namespace UpsilonTests { - public class ScopeTests + public class ScopeTests : TestClass { + public ScopeTests(StaticScriptFixture fix) : base(fix) + { + } + [Fact] public void LocalInnerScopeDoesNotOverrideGlobal() { @@ -15,7 +19,7 @@ if true then local a = 100 end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -33,7 +37,7 @@ if true then end b = a "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); diff --git a/UpsilonTests/StringTests.cs b/UpsilonTests/GeneralTests/StringTests.cs similarity index 77% rename from UpsilonTests/StringTests.cs rename to UpsilonTests/GeneralTests/StringTests.cs index e2fe05a..4f94108 100644 --- a/UpsilonTests/StringTests.cs +++ b/UpsilonTests/GeneralTests/StringTests.cs @@ -3,8 +3,12 @@ using Xunit; namespace UpsilonTests { - public class StringTests + public class StringTests : TestClass { + public StringTests(StaticScriptFixture fix) : base(fix) + { + } + [Fact] public void BasicStringVariable() { @@ -12,7 +16,7 @@ namespace UpsilonTests string = ""test"" return string "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -26,7 +30,7 @@ return string string = ""test"" return string[3] "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -40,7 +44,7 @@ return string[3] string = ""test"" + ""123"" return string "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); diff --git a/UpsilonTests/TableTests.cs b/UpsilonTests/GeneralTests/TableTests.cs similarity index 83% rename from UpsilonTests/TableTests.cs rename to UpsilonTests/GeneralTests/TableTests.cs index 23cca6a..e4e4041 100644 --- a/UpsilonTests/TableTests.cs +++ b/UpsilonTests/GeneralTests/TableTests.cs @@ -3,8 +3,12 @@ using Xunit; namespace UpsilonTests { - public class TableTests + public class TableTests : TestClass { + public TableTests(StaticScriptFixture fix) : base(fix) + { + } + [Fact] public void BasicNumberTable() { @@ -14,7 +18,7 @@ table = { } return table[2] "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -32,7 +36,7 @@ table = { } return table[""another""] "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -50,7 +54,7 @@ table = { } return table[""test""] "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Single(script.Diagnostics.Messages); @@ -70,7 +74,7 @@ table = { } return table[1][1][2] "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -90,7 +94,7 @@ table = { } return table[""test""]() "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -114,7 +118,7 @@ table = { } return table[""func""]()()[1]() "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -134,7 +138,7 @@ table = { } return table[""test""]() "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -152,7 +156,7 @@ table = { } return table[1]() "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); @@ -167,7 +171,7 @@ table = {} table[1] = 400 return table[1] "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.Evaluate(); Assert.Empty(script.Diagnostics.Messages); diff --git a/UpsilonTests/UserDataDictionaryTests.cs b/UpsilonTests/GeneralTests/UserDataDictionaryTests.cs similarity index 75% rename from UpsilonTests/UserDataDictionaryTests.cs rename to UpsilonTests/GeneralTests/UserDataDictionaryTests.cs index 436357f..a635dc0 100644 --- a/UpsilonTests/UserDataDictionaryTests.cs +++ b/UpsilonTests/GeneralTests/UserDataDictionaryTests.cs @@ -4,7 +4,7 @@ using Xunit; namespace UpsilonTests { - public class UserDataDictionaryTests + public class UserDataDictionaryTests : TestClass { [Fact] public void BasicStringKeyed() @@ -20,12 +20,15 @@ function getValue(arr) return arr[""here""] end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.EvaluateFunction("getValue", new[] {arr}); Assert.Empty(script.Diagnostics.Messages); Assert.Equal(1683, evaluated); } + public UserDataDictionaryTests(StaticScriptFixture fix) : base(fix) + { + } } } \ No newline at end of file diff --git a/UpsilonTests/UserDataListTests.cs b/UpsilonTests/GeneralTests/UserDataListTests.cs similarity index 78% rename from UpsilonTests/UserDataListTests.cs rename to UpsilonTests/GeneralTests/UserDataListTests.cs index efd3297..f6e9d92 100644 --- a/UpsilonTests/UserDataListTests.cs +++ b/UpsilonTests/GeneralTests/UserDataListTests.cs @@ -4,7 +4,7 @@ using Xunit; namespace UpsilonTests { - public class UserDataListTests + public class UserDataListTests : TestClass { [Fact] public void BasicArrayTest() @@ -15,7 +15,7 @@ function getValue(arr) return arr[3] end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.EvaluateFunction("getValue", new[] {arr}); Assert.Empty(script.Diagnostics.Messages); @@ -31,12 +31,15 @@ function getValue(arr) return arr[2] end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var evaluated = script.EvaluateFunction("getValue", new[] {arr}); Assert.Empty(script.Diagnostics.Messages); Assert.Equal(30, evaluated); } + public UserDataListTests(StaticScriptFixture fix) : base(fix) + { + } } } \ No newline at end of file diff --git a/UpsilonTests/UserDataOperatorTests.cs b/UpsilonTests/GeneralTests/UserDataOperatorTests.cs similarity index 88% rename from UpsilonTests/UserDataOperatorTests.cs rename to UpsilonTests/GeneralTests/UserDataOperatorTests.cs index 17a8925..16fe481 100644 --- a/UpsilonTests/UserDataOperatorTests.cs +++ b/UpsilonTests/GeneralTests/UserDataOperatorTests.cs @@ -7,7 +7,7 @@ using Xunit; namespace UpsilonTests { - public class UserDataOperatorTests : IClassFixture + public class UserDataOperatorTests : TestClass, IClassFixture { public class UserDataOperatorTestsFixture : IDisposable { @@ -73,7 +73,7 @@ function add(o1, o2) return o1 + o2 end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var o1 = new UserDataHelper(100); var o2 = new UserDataHelper(215); @@ -90,7 +90,7 @@ function add(o1, o2) return o1 + o2 end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var o1 = new UserDataHelper(100); const double o2 = 1.5; @@ -107,7 +107,7 @@ function subtract(o1, o2) return o1 - o2 end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var o1 = new UserDataHelper(100); var o2 = new UserDataHelper(1.5); @@ -124,7 +124,7 @@ function multiply(o1, o2) return o1 * o2 end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var o1 = new UserDataHelper(100); var o2 = new UserDataHelper(4); @@ -141,7 +141,7 @@ function divide(o1, o2) return o1 / o2 end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var o1 = new UserDataHelper(100); var o2 = new UserDataHelper(10); @@ -158,7 +158,7 @@ function negate(o1) return -o1 end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var o1 = new UserDataHelper(100); var result = script.EvaluateFunction("negate", new object[] {o1}); @@ -166,5 +166,8 @@ end Assert.Equal(-100, result.Value); } + public UserDataOperatorTests(StaticScriptFixture fix) : base(fix) + { + } } } \ No newline at end of file diff --git a/UpsilonTests/UserDataTests.cs b/UpsilonTests/GeneralTests/UserDataTests.cs similarity index 85% rename from UpsilonTests/UserDataTests.cs rename to UpsilonTests/GeneralTests/UserDataTests.cs index 5a247e5..3418205 100644 --- a/UpsilonTests/UserDataTests.cs +++ b/UpsilonTests/GeneralTests/UserDataTests.cs @@ -6,7 +6,7 @@ using Xunit; namespace UpsilonTests { - public class UserDataTests : IClassFixture + public class UserDataTests : TestClass, IClassFixture { public class UserDataTestsFixture : IDisposable { @@ -53,7 +53,7 @@ function test(o) return o.fieldString end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.EvaluateFunction("test", new[] {obj}); Assert.Empty(script.Diagnostics.Messages); @@ -69,7 +69,7 @@ function test(o) o.FieldStringSet = ""Test"" end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.EvaluateFunction("test", new[] {obj}); Assert.Empty(script.Diagnostics.Messages); @@ -85,7 +85,7 @@ function test(o) o.testMethod() end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.EvaluateFunction("test", new[] {obj}); Assert.Empty(script.Diagnostics.Messages); @@ -101,7 +101,7 @@ function test(o) return o.add(100, 20) end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); var result = script.EvaluateFunction("test", new[] {obj}); Assert.Empty(script.Diagnostics.Messages); @@ -117,7 +117,7 @@ function test(o) return o._privateTestField end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.EvaluateFunction("test", new[] {obj}); Assert.Single(script.Diagnostics.Messages); @@ -132,7 +132,7 @@ function test(o) o.GetOnly = true end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.EvaluateFunction("test", new[] {obj}); Assert.Single(script.Diagnostics.Messages); @@ -148,12 +148,15 @@ function test(o) o.PrivateSet = true end "; - var script = new Script(input); + var script = new Script(input, BoundScope, StaticScope); Assert.Empty(script.Diagnostics.Messages); script.EvaluateFunction("test", new[] {obj}); Assert.Single(script.Diagnostics.Messages); Assert.False(obj.PrivateSet); } + public UserDataTests(StaticScriptFixture fix) : base(fix) + { + } } } \ No newline at end of file diff --git a/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs b/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs index c9b51b6..4cc9055 100644 --- a/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs +++ b/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs @@ -3,12 +3,17 @@ using Xunit; namespace UpsilonTests.StandardLibraryTests { - public class BasicFunctionsTests + public class BasicFunctionsTests : TestClass { + public BasicFunctionsTests(StaticScriptFixture fix) : base(fix) + { + } + [Fact] public void Assert() { - new Script("assert(true)").Evaluate(); + new Script("assert(true)", BoundScope, StaticScope).Evaluate(); } + } } \ No newline at end of file diff --git a/UpsilonTests/TestClass.cs b/UpsilonTests/TestClass.cs new file mode 100644 index 0000000..22b6e79 --- /dev/null +++ b/UpsilonTests/TestClass.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using Upsilon.BaseTypes; +using Upsilon.Binder; +using Upsilon.Evaluator; +using Upsilon.StandardLibraries; +using Xunit; + +namespace UpsilonTests +{ + [Collection("collection")] + public abstract class TestClass + { + public Dictionary StaticScope { get; } + public Dictionary BoundScope { get; } + + public TestClass(StaticScriptFixture fix) + { + StaticScope = fix.StaticScope.Variables; + BoundScope = fix.BoundScope.Variables; + } + } + + [CollectionDefinition("collection")] + public class StaticScriptContext : ICollectionFixture + { + + } + + public class StaticScriptFixture + { + public EvaluationScope StaticScope { get; } + public BoundScope BoundScope { get; } + + public StaticScriptFixture() + { + var (evaluationScope, boundScope) = StandardLibrary.Create(); + StaticScope = evaluationScope; + BoundScope = boundScope; + } + } +} \ No newline at end of file diff --git a/Ycicle/Program.cs b/Ycicle/Program.cs index bc72ed7..bae7676 100644 --- a/Ycicle/Program.cs +++ b/Ycicle/Program.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Text; using Upsilon; using Upsilon.Evaluator; +using Upsilon.StandardLibraries; namespace Ycicle { @@ -13,12 +14,16 @@ namespace Ycicle { Console.WriteLine("Upsilon REPL"); Script script = null; + var (evaluationScope, boundScope) = StandardLibrary.Create(); + while (true) { Console.Write("ยป "); var input = Console.ReadLine(); if (input == "exit") return; - script = script == null ? new Script(input) : Script.ContinueWith(script, input); + script = script == null + ? new Script(input, boundScope.Variables, evaluationScope.Variables) + : Script.ContinueWith(script, input); if (script.Diagnostics.Messages.Count > 0) {