General fixes for Tests

This commit is contained in:
Deukhoofd 2018-11-23 12:55:28 +01:00
parent 1e9b0e0166
commit aae16e8b62
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
19 changed files with 185 additions and 106 deletions

View File

@ -258,7 +258,10 @@ namespace Upsilon.Binder
} }
else if (assignment.Type == Type.Unknown && assignment is BoundVariableExpression v) 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 else
{ {

View File

@ -2,11 +2,11 @@ using System.Collections.Generic;
namespace Upsilon.Binder namespace Upsilon.Binder
{ {
internal class BoundScope public class BoundScope
{ {
public readonly BoundScope ParentScope; public readonly BoundScope ParentScope;
private BoundScope _readOnlyScope; private BoundScope _readOnlyScope;
internal readonly Dictionary<string, VariableSymbol> Variables; public readonly Dictionary<string, VariableSymbol> Variables;
internal readonly Dictionary<string, VariableSymbol> LocalVariables; internal readonly Dictionary<string, VariableSymbol> LocalVariables;

View File

@ -4,26 +4,26 @@ using Upsilon.Binder;
namespace Upsilon.Evaluator namespace Upsilon.Evaluator
{ {
internal class EvaluationScope public class EvaluationScope
{ {
private readonly EvaluationScope _parentScope; private readonly EvaluationScope _parentScope;
private EvaluationScope _getOnlyParentScope; private EvaluationScope _getOnlyParentScope;
internal readonly Dictionary<string, LuaType> Variables; public readonly Dictionary<string, LuaType> Variables;
internal readonly Dictionary<string, LuaType> LocalVariables; private readonly Dictionary<string, LuaType> _localVariables;
internal EvaluationScope(EvaluationScope parentScope) internal EvaluationScope(EvaluationScope parentScope)
{ {
_parentScope = parentScope; _parentScope = parentScope;
Variables = new Dictionary<string, LuaType>(); Variables = new Dictionary<string, LuaType>();
LocalVariables = new Dictionary<string, LuaType>(); _localVariables = new Dictionary<string, LuaType>();
} }
internal EvaluationScope(Dictionary<string, LuaType> vars) internal EvaluationScope(Dictionary<string, LuaType> vars)
{ {
Variables = vars; Variables = vars;
LocalVariables = new Dictionary<string, LuaType>(); _localVariables = new Dictionary<string, LuaType>();
} }
internal static EvaluationScope CreateWithGetOnlyParent(EvaluationScope parent) internal static EvaluationScope CreateWithGetOnlyParent(EvaluationScope parent)
@ -36,13 +36,13 @@ namespace Upsilon.Evaluator
{ {
if (symbol.Local) if (symbol.Local)
{ {
if (LocalVariables.ContainsKey(symbol.Name)) if (_localVariables.ContainsKey(symbol.Name))
{ {
LocalVariables[symbol.Name] = obj; _localVariables[symbol.Name] = obj;
} }
else else
{ {
LocalVariables.Add(symbol.Name, obj); _localVariables.Add(symbol.Name, obj);
} }
} }
else else
@ -71,7 +71,7 @@ namespace Upsilon.Evaluator
public bool TryGet(VariableSymbol symbol, out LuaType obj) public bool TryGet(VariableSymbol symbol, out LuaType obj)
{ {
if (LocalVariables.TryGetValue(symbol.Name, out obj)) if (_localVariables.TryGetValue(symbol.Name, out obj))
return true; return true;
if (Variables.TryGetValue(symbol.Name, out obj)) if (Variables.TryGetValue(symbol.Name, out obj))
return true; return true;
@ -86,7 +86,7 @@ namespace Upsilon.Evaluator
public bool TryGet(string variable, out LuaType obj) public bool TryGet(string variable, out LuaType obj)
{ {
if (LocalVariables.TryGetValue(variable, out obj)) if (_localVariables.TryGetValue(variable, out obj))
return true; return true;
if (Variables.TryGetValue(variable, out obj)) if (Variables.TryGetValue(variable, out obj))
return true; return true;

View File

@ -13,43 +13,24 @@ namespace Upsilon.Evaluator
public class Script public class Script
{ {
private SourceText ScriptString { get; } private SourceText ScriptString { get; }
private Evaluator Evaluator { get; set; } private Evaluator Evaluator { get; }
private readonly BlockStatementSyntax _parsed; private readonly BlockStatementSyntax _parsed;
private BoundScript _bound; private BoundScript _bound;
public Diagnostics Diagnostics { get; private set; } public Diagnostics Diagnostics { get; }
private Binder.Binder Binder { get; set; } private Binder.Binder Binder { get; }
private EvaluationScope Scope { get; } private EvaluationScope Scope { get; }
private static BoundScope _staticBoundScope; public Script(string scriptString, Dictionary<string, VariableSymbol> boundStaticScope, Dictionary<string, LuaType> staticScope )
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)
{ {
ScriptString = new SourceText(scriptString); ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(ScriptString); Diagnostics = new Diagnostics(ScriptString);
_parsed = Parser.Parser.Parse(scriptString, Diagnostics); _parsed = Parser.Parser.Parse(scriptString, Diagnostics);
Scope = EvaluationScope.CreateWithGetOnlyParent(StaticScope); var boundScope = new BoundScope(boundStaticScope, null);
Binder = new Binder.Binder(Diagnostics, _staticBoundScope.Variables); Binder = new Binder.Binder(Diagnostics, boundScope.Variables);
Scope = EvaluationScope.CreateWithGetOnlyParent(new EvaluationScope(staticScope));
Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope); Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope);
Scope = Evaluator.Scope;
} }
private Script(string scriptString, Binder.Binder binder, Evaluator evaluator) private Script(string scriptString, Binder.Binder binder, Evaluator evaluator)

View File

@ -6,7 +6,7 @@ using Upsilon.Evaluator;
namespace Upsilon.StandardLibraries namespace Upsilon.StandardLibraries
{ {
internal class StandardLibrary public class StandardLibrary
{ {
public static (EvaluationScope, BoundScope) Create() public static (EvaluationScope, BoundScope) Create()
{ {

View File

@ -5,8 +5,12 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class BasicMathExpressions public class BasicMathExpressions : TestClass
{ {
public BasicMathExpressions(StaticScriptFixture fix) : base(fix)
{
}
[Theory] [Theory]
[InlineData("1+1", 2)] [InlineData("1+1", 2)]
[InlineData("1000+1", 1001)] [InlineData("1000+1", 1001)]
@ -17,7 +21,7 @@ namespace UpsilonTests
[InlineData("0.005 + 2.2", 2.205)] [InlineData("0.005 + 2.2", 2.205)]
public void Addition(string input, double expectedOutput) public void Addition(string input, double expectedOutput)
{ {
var actual = new Script(input).Evaluate<double>(); var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
Assert.Equal(expectedOutput, actual, 8); Assert.Equal(expectedOutput, actual, 8);
} }
@ -30,7 +34,7 @@ namespace UpsilonTests
[InlineData("10.256-2.8546", 7.4014)] [InlineData("10.256-2.8546", 7.4014)]
public void Subtraction(string input, double expectedOutput) public void Subtraction(string input, double expectedOutput)
{ {
var actual = new Script(input).Evaluate<double>(); var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
Assert.Equal(expectedOutput, actual, 8); Assert.Equal(expectedOutput, actual, 8);
} }
@ -40,7 +44,7 @@ namespace UpsilonTests
[InlineData("21312 * 41684", 888369408)] [InlineData("21312 * 41684", 888369408)]
public void Multiplication(string input, double expectedOutput) public void Multiplication(string input, double expectedOutput)
{ {
var actual = new Script(input).Evaluate<double>(); var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
Assert.Equal(expectedOutput, actual, 8); Assert.Equal(expectedOutput, actual, 8);
} }
@ -51,7 +55,7 @@ namespace UpsilonTests
[InlineData("656486 / 5146.0", 127.57209483)] [InlineData("656486 / 5146.0", 127.57209483)]
public void Divison(string input, double expectedOutput) public void Divison(string input, double expectedOutput)
{ {
var actual = new Script(input).Evaluate<double>(); var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
Assert.Equal(expectedOutput, actual, 8); Assert.Equal(expectedOutput, actual, 8);
} }

View File

@ -5,8 +5,13 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class FunctionTests public class FunctionTests : TestClass
{ {
public FunctionTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact] [Fact]
public void BasicFunctionTest() public void BasicFunctionTest()
{ {
@ -17,7 +22,7 @@ end
a = 50 a = 50
testFunc() testFunc()
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.Evaluate(); script.Evaluate();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -35,7 +40,7 @@ end
a = 50 a = 50
testFunc(100) testFunc(100)
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.Evaluate(); script.Evaluate();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -52,7 +57,7 @@ function testFunc (var1)
end end
testFunc(100) testFunc(100)
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.Evaluate(); script.Evaluate();
Assert.Single(script.Diagnostics.Messages); Assert.Single(script.Diagnostics.Messages);
@ -67,7 +72,7 @@ function testFunc (var1)
var1 == true var1 == true
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var val = script.Evaluate(); var val = script.Evaluate();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -83,7 +88,7 @@ function testFunc ()
end end
a = testFunc() a = testFunc()
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.Evaluate(); script.Evaluate();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -101,7 +106,7 @@ function testFunc ()
end end
a = testFunc() a = testFunc()
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.Evaluate(); script.Evaluate();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -121,7 +126,7 @@ function testFunc ()
end end
a = testFunc() a = testFunc()
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.Evaluate(); script.Evaluate();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -137,7 +142,7 @@ a = 100
return 60 return 60
a = 87 a = 87
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>(); var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -152,7 +157,7 @@ function testFunc ()
return 100 return 100
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("testFunc"); var result = script.EvaluateFunction<long>("testFunc");
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -171,7 +176,7 @@ function testFunc (b)
end end
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("testFunc", new object[] {true}); var result = script.EvaluateFunction<long>("testFunc", new object[] {true});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -190,13 +195,12 @@ function add (a, b)
return a + b return a + b
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("add", new object[] {400, 128}); var result = script.EvaluateFunction<long>("add", new object[] {400, 128});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(528, result); Assert.Equal(528, result);
} }
} }
} }

View File

@ -5,13 +5,21 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class IfTests public class IfTests : TestClass
{ {
public IfTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact] [Fact]
public void BasicIfTest() public void BasicIfTest()
{ {
var input = "if true then val = true end"; const string input = @"
var script = new Script(input); if true then
return true
end
return false";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var actual = script.Evaluate<bool>(); var actual = script.Evaluate<bool>();
Assert.True(actual); Assert.True(actual);
@ -30,7 +38,7 @@ if {condition} then
else else
val = {in2} val = {in2}
end"; end";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var actual = script.Evaluate<long>(); var actual = script.Evaluate<long>();
Assert.Equal(expected, actual); Assert.Equal(expected, actual);
@ -50,7 +58,7 @@ elseif {condition2} then
else else
val = {in3} val = {in3}
end"; end";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var actual = script.Evaluate<long>(); var actual = script.Evaluate<long>();
Assert.Equal(expected, actual); Assert.Equal(expected, actual);

View File

@ -5,14 +5,18 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class MathPrecedence public class MathPrecedence : TestClass
{ {
public MathPrecedence(StaticScriptFixture fix) : base(fix)
{
}
[Theory] [Theory]
[InlineData("5 * (10 + 5)", 75)] [InlineData("5 * (10 + 5)", 75)]
[InlineData("(10 + 5) * 5", 75)] [InlineData("(10 + 5) * 5", 75)]
public void Parenthesis(string input, double expectedOutput) public void Parenthesis(string input, double expectedOutput)
{ {
var actual = new Script(input).Evaluate<long>(); var actual = new Script(input, BoundScope, StaticScope).Evaluate<long>();
Assert.Equal(expectedOutput, actual, 8); Assert.Equal(expectedOutput, actual, 8);
} }
@ -21,7 +25,7 @@ namespace UpsilonTests
[InlineData("5 + 10 * 5", 55)] [InlineData("5 + 10 * 5", 55)]
public void MultiplicationBeforeAddition(string input, double expectedOutput) public void MultiplicationBeforeAddition(string input, double expectedOutput)
{ {
var actual = new Script(input).Evaluate<long>(); var actual = new Script(input, BoundScope, StaticScope).Evaluate<long>();
Assert.Equal(expectedOutput, actual, 8); Assert.Equal(expectedOutput, actual, 8);
} }

View File

@ -4,8 +4,12 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class ScopeTests public class ScopeTests : TestClass
{ {
public ScopeTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact] [Fact]
public void LocalInnerScopeDoesNotOverrideGlobal() public void LocalInnerScopeDoesNotOverrideGlobal()
{ {
@ -15,7 +19,7 @@ if true then
local a = 100 local a = 100
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.Evaluate(); script.Evaluate();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -33,7 +37,7 @@ if true then
end end
b = a b = a
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.Evaluate(); script.Evaluate();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);

View File

@ -3,8 +3,12 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class StringTests public class StringTests : TestClass
{ {
public StringTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact] [Fact]
public void BasicStringVariable() public void BasicStringVariable()
{ {
@ -12,7 +16,7 @@ namespace UpsilonTests
string = ""test"" string = ""test""
return string return string
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<string>(); var evaluated = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -26,7 +30,7 @@ return string
string = ""test"" string = ""test""
return string[3] return string[3]
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<string>(); var evaluated = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -40,7 +44,7 @@ return string[3]
string = ""test"" + ""123"" string = ""test"" + ""123""
return string return string
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<string>(); var evaluated = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);

View File

@ -3,8 +3,12 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class TableTests public class TableTests : TestClass
{ {
public TableTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact] [Fact]
public void BasicNumberTable() public void BasicNumberTable()
{ {
@ -14,7 +18,7 @@ table = {
} }
return table[2] return table[2]
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>(); var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -32,7 +36,7 @@ table = {
} }
return table[""another""] return table[""another""]
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>(); var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -50,7 +54,7 @@ table = {
} }
return table[""test""] return table[""test""]
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate(); var evaluated = script.Evaluate();
Assert.Single(script.Diagnostics.Messages); Assert.Single(script.Diagnostics.Messages);
@ -70,7 +74,7 @@ table = {
} }
return table[1][1][2] return table[1][1][2]
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>(); var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -90,7 +94,7 @@ table = {
} }
return table[""test""]() return table[""test""]()
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>(); var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -114,7 +118,7 @@ table = {
} }
return table[""func""]()()[1]() return table[""func""]()()[1]()
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>(); var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -134,7 +138,7 @@ table = {
} }
return table[""test""]() return table[""test""]()
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>(); var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -152,7 +156,7 @@ table = {
} }
return table[1]() return table[1]()
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>(); var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -167,7 +171,7 @@ table = {}
table[1] = 400 table[1] = 400
return table[1] return table[1]
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>(); var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);

View File

@ -4,7 +4,7 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class UserDataDictionaryTests public class UserDataDictionaryTests : TestClass
{ {
[Fact] [Fact]
public void BasicStringKeyed() public void BasicStringKeyed()
@ -20,12 +20,15 @@ function getValue(arr)
return arr[""here""] return arr[""here""]
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr}); var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(1683, evaluated); Assert.Equal(1683, evaluated);
} }
public UserDataDictionaryTests(StaticScriptFixture fix) : base(fix)
{
}
} }
} }

View File

@ -4,7 +4,7 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class UserDataListTests public class UserDataListTests : TestClass
{ {
[Fact] [Fact]
public void BasicArrayTest() public void BasicArrayTest()
@ -15,7 +15,7 @@ function getValue(arr)
return arr[3] return arr[3]
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr}); var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -31,12 +31,15 @@ function getValue(arr)
return arr[2] return arr[2]
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr}); var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(30, evaluated); Assert.Equal(30, evaluated);
} }
public UserDataListTests(StaticScriptFixture fix) : base(fix)
{
}
} }
} }

View File

@ -7,7 +7,7 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class UserDataOperatorTests : IClassFixture<UserDataOperatorTests.UserDataOperatorTestsFixture> public class UserDataOperatorTests : TestClass, IClassFixture<UserDataOperatorTests.UserDataOperatorTestsFixture>
{ {
public class UserDataOperatorTestsFixture : IDisposable public class UserDataOperatorTestsFixture : IDisposable
{ {
@ -73,7 +73,7 @@ function add(o1, o2)
return o1 + o2 return o1 + o2
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100); var o1 = new UserDataHelper(100);
var o2 = new UserDataHelper(215); var o2 = new UserDataHelper(215);
@ -90,7 +90,7 @@ function add(o1, o2)
return o1 + o2 return o1 + o2
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100); var o1 = new UserDataHelper(100);
const double o2 = 1.5; const double o2 = 1.5;
@ -107,7 +107,7 @@ function subtract(o1, o2)
return o1 - o2 return o1 - o2
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100); var o1 = new UserDataHelper(100);
var o2 = new UserDataHelper(1.5); var o2 = new UserDataHelper(1.5);
@ -124,7 +124,7 @@ function multiply(o1, o2)
return o1 * o2 return o1 * o2
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100); var o1 = new UserDataHelper(100);
var o2 = new UserDataHelper(4); var o2 = new UserDataHelper(4);
@ -141,7 +141,7 @@ function divide(o1, o2)
return o1 / o2 return o1 / o2
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100); var o1 = new UserDataHelper(100);
var o2 = new UserDataHelper(10); var o2 = new UserDataHelper(10);
@ -158,7 +158,7 @@ function negate(o1)
return -o1 return -o1
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100); var o1 = new UserDataHelper(100);
var result = script.EvaluateFunction<UserDataHelper>("negate", new object[] {o1}); var result = script.EvaluateFunction<UserDataHelper>("negate", new object[] {o1});
@ -166,5 +166,8 @@ end
Assert.Equal(-100, result.Value); Assert.Equal(-100, result.Value);
} }
public UserDataOperatorTests(StaticScriptFixture fix) : base(fix)
{
}
} }
} }

View File

@ -6,7 +6,7 @@ using Xunit;
namespace UpsilonTests namespace UpsilonTests
{ {
public class UserDataTests : IClassFixture<UserDataTests.UserDataTestsFixture> public class UserDataTests : TestClass, IClassFixture<UserDataTests.UserDataTestsFixture>
{ {
public class UserDataTestsFixture : IDisposable public class UserDataTestsFixture : IDisposable
{ {
@ -53,7 +53,7 @@ function test(o)
return o.fieldString return o.fieldString
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<string>("test", new[] {obj}); var result = script.EvaluateFunction<string>("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -69,7 +69,7 @@ function test(o)
o.FieldStringSet = ""Test"" o.FieldStringSet = ""Test""
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj}); script.EvaluateFunction("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -85,7 +85,7 @@ function test(o)
o.testMethod() o.testMethod()
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj}); script.EvaluateFunction("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -101,7 +101,7 @@ function test(o)
return o.add(100, 20) return o.add(100, 20)
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("test", new[] {obj}); var result = script.EvaluateFunction<long>("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
@ -117,7 +117,7 @@ function test(o)
return o._privateTestField return o._privateTestField
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj}); script.EvaluateFunction("test", new[] {obj});
Assert.Single(script.Diagnostics.Messages); Assert.Single(script.Diagnostics.Messages);
@ -132,7 +132,7 @@ function test(o)
o.GetOnly = true o.GetOnly = true
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj}); script.EvaluateFunction("test", new[] {obj});
Assert.Single(script.Diagnostics.Messages); Assert.Single(script.Diagnostics.Messages);
@ -148,12 +148,15 @@ function test(o)
o.PrivateSet = true o.PrivateSet = true
end end
"; ";
var script = new Script(input); var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj}); script.EvaluateFunction("test", new[] {obj});
Assert.Single(script.Diagnostics.Messages); Assert.Single(script.Diagnostics.Messages);
Assert.False(obj.PrivateSet); Assert.False(obj.PrivateSet);
} }
public UserDataTests(StaticScriptFixture fix) : base(fix)
{
}
} }
} }

View File

@ -3,12 +3,17 @@ using Xunit;
namespace UpsilonTests.StandardLibraryTests namespace UpsilonTests.StandardLibraryTests
{ {
public class BasicFunctionsTests public class BasicFunctionsTests : TestClass
{ {
public BasicFunctionsTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact] [Fact]
public void Assert() public void Assert()
{ {
new Script("assert(true)").Evaluate(); new Script("assert(true)", BoundScope, StaticScope).Evaluate();
} }
} }
} }

41
UpsilonTests/TestClass.cs Normal file
View File

@ -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<string, LuaType> StaticScope { get; }
public Dictionary<string, VariableSymbol> BoundScope { get; }
public TestClass(StaticScriptFixture fix)
{
StaticScope = fix.StaticScope.Variables;
BoundScope = fix.BoundScope.Variables;
}
}
[CollectionDefinition("collection")]
public class StaticScriptContext : ICollectionFixture<StaticScriptFixture>
{
}
public class StaticScriptFixture
{
public EvaluationScope StaticScope { get; }
public BoundScope BoundScope { get; }
public StaticScriptFixture()
{
var (evaluationScope, boundScope) = StandardLibrary.Create();
StaticScope = evaluationScope;
BoundScope = boundScope;
}
}
}

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Text; using System.Text;
using Upsilon; using Upsilon;
using Upsilon.Evaluator; using Upsilon.Evaluator;
using Upsilon.StandardLibraries;
namespace Ycicle namespace Ycicle
{ {
@ -13,12 +14,16 @@ namespace Ycicle
{ {
Console.WriteLine("Upsilon REPL"); Console.WriteLine("Upsilon REPL");
Script script = null; Script script = null;
var (evaluationScope, boundScope) = StandardLibrary.Create();
while (true) while (true)
{ {
Console.Write("» "); Console.Write("» ");
var input = Console.ReadLine(); var input = Console.ReadLine();
if (input == "exit") return; 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) if (script.Diagnostics.Messages.Count > 0)
{ {