public void IfElseExpressionTest1() { var tests = new StatementEvalTestCase[] { new() { Input = "if (true) { 10 }", Expected = 10 }, new() { Input = "if (false) { 10 }", Expected = null }, new() { Input = "if (1) { 10 }", Expected = 10 }, new() { Input = "if (1 < 2) { 10 }", Expected = 10 }, new() { Input = "if (1 > 2) { 10 }", Expected = null }, new() { Input = "if (1 > 2) { 10 } else { 20 }", Expected = 20 }, new() { Input = "if (1 < 2) { 10 } else { 20 }", Expected = 10 } }; foreach (var boolEvalTestCase in tests) { var evaluated = StaticTests.TestEval(boolEvalTestCase.Input); if (boolEvalTestCase.Expected is null) { Assert.True(StaticTests.TestNullObject(evaluated), $"evaluated object is not Null, got {evaluated}"); } else { StaticTests.TestIntegerObject(evaluated, boolEvalTestCase.Expected); } } }
public void HashIndexExpressionTest() { var passingTests = new IntegerTests[] { new() { Input = "{'a': 5}['a']", Expected = 5 }, new() { Input = "let key = 'k'; {'k': 5}[key]", Expected = 5 }, new() { Input = "{5: 5}[5]", Expected = 5 }, new() { Input = "{true: 5}[true]", Expected = 5 }, new() { Input = "{false: 5}[false]", Expected = 5 } }; var failingTests = new OperatorTest[] { new() { Input = "{}[5]", Expected = "ERROR: [SC16] `{}` does not contain key `5`" }, new() { Input = "{'a': 5}[5]", Expected = "ERROR: [SC16] `{'a': 5}` does not contain key `5`" } }; foreach (var passingTest in passingTests) { var evaluated = StaticTests.TestEval(passingTest.Input); StaticTests.TestIntegerObject(evaluated, passingTest.Expected); } foreach (var failingTest in failingTests) { var evaluated = StaticTests.TestEval(failingTest.Input); Assert.AreEqual(failingTest.Expected, evaluated.Inspect()); } } } }
public void HashLiteralTest() { const string input = @"let two = 'two'; { 'one': 10 -9, two: 2, 'thr' + 'ee': 6/2, 4: 4, true: 5, false: 6 };"; var evaluated = StaticTests.TestEval(input); Assert.AreEqual(nameof(ScriptyHash), evaluated.GetType().Name); var result = (ScriptyHash)evaluated; var expected = new Dictionary <HashKey, int> { { ((ScriptyString)"one").HashKey(), 1 }, { ((ScriptyString)"two").HashKey(), 2 }, { ((ScriptyString)"three").HashKey(), 3 }, { ((ScriptyInteger)4).HashKey(), 4 }, { Evaluator.True.HashKey(), 5 }, { Evaluator.False.HashKey(), 6 } }; Assert.AreEqual(expected.Count, result.Pairs.Count); foreach (var(hkey, hval) in expected) { var pairExists = result.Pairs.TryGetValue(hkey, out var pair); Assert.True(pairExists); StaticTests.TestIntegerObject(pair.Value, hval); } }
public void ClosureTest() { const string input = @" let newAdder = fun(x) { fun(y) {x + y; }; }; let addTwo = newAdder(2); addTwo(2);"; var evaluated = StaticTests.TestEval(input); StaticTests.TestIntegerObject(evaluated, 4); }
public void ArrayObjectTest() { const string input = "[1, 2 * 2, 3 + 3]"; var evaluated = StaticTests.TestEval(input); Assert.AreEqual(nameof(ScriptyArray), evaluated.GetType().Name); var array = (ScriptyArray)evaluated; Assert.AreEqual(3, array.Elements.Count); StaticTests.TestIntegerObject(array.Elements.First(), 1); StaticTests.TestIntegerObject(array.Elements[1], 4); StaticTests.TestIntegerObject(array.Elements[2], 6); }
public void LetStatementsTest2() { var tests = new LetStatementWithIntegerValueEvalTestCase[] { new() { Input = "let a = 5; a;", Expected = 5 }, new() { Input = "let a = 5 * 5; a;", Expected = 25 }, new() { Input = "let a = 5; let b = a; b;", Expected = 5 }, new() { Input = "let a = 5; let b = a; let c = a + b + 5; c;", Expected = 15 } }; foreach (var letStatementWithIntegerValueEvalTestCase in tests) { var evaledValue = StaticTests.TestEval(letStatementWithIntegerValueEvalTestCase.Input); StaticTests.TestIntegerObject(evaledValue, letStatementWithIntegerValueEvalTestCase.Expected); } } }
public void FunctionApplicationTest() { var tests = new LetStatementWithIntegerValueEvalTestCase[] { new() { Input = "let id = fun(x) { x; }; id(5);", Expected = 5 }, new() { Input = "let id = fun(x) { return x; }; id(5);", Expected = 5 }, new() { Input = "let double = fun(x) { x * 2; }; double(5);", Expected = 10 }, new() { Input = "let add = fun(x, y) { x + y; }; add(228, 322)", Expected = 550 }, new() { Input = "let add = fun(x, y) { x + y; }; add(5 + 5, add(5, 5));", Expected = 20 }, new() { Input = "fun(x) { x; }(5);", Expected = 5 } }; foreach (var functionTests in tests) { var evaluated = StaticTests.TestEval(functionTests.Input); StaticTests.TestIntegerObject(evaluated, functionTests.Expected); } }
public void ReturnStatementTest() { var tests = new StatementEvalTestCase[] { new() { Input = "return 10;", Expected = 10 }, new() { Input = "return 10; 9;", Expected = 10 }, new() { Input = "return 2 * 5; 9;", Expected = 10 }, new() { Input = "9; return 2 * 5; 9;", Expected = 10 }, new() { Input = "if (10 > 1) { if (15 > 1) { return 10; } return 1; }", Expected = 10 } }; foreach (var statementEvalTestCase in tests) { var evaluated = StaticTests.TestEval(statementEvalTestCase.Input); StaticTests.TestIntegerObject(evaluated, statementEvalTestCase.Expected); } }
private void ArrayIndexExpressionIterationTest(string input, object expected) { var evaluated = StaticTests.TestEval(input); switch (expected.GetType().Name) { case "Int64": var integer = (long)expected; StaticTests.TestIntegerObject(evaluated, integer); break; case "String": var str = (string)expected; var err = (ScriptyError)evaluated; Assert.AreEqual(str, err.Message); break; default: Console.WriteLine(expected.GetType().Name); break; } }
public void IntegerExpressionEvalTest() { var tests = new IntegerTests[] { new() { Input = "5", Expected = 5 }, new() { Input = "228322", Expected = 228322 }, new() { Input = "-322", Expected = -322 }, new() { Input = "-228", Expected = -228 }, new() { Input = "5 + 5 + 5 + 5 - 10", Expected = 10 }, new() { Input = "2 * 2 * 2 * 2 * 2", Expected = 32 }, new() { Input = "100 + -50", Expected = 50 }, new() { Input = "-50 + 100 + -50", Expected = 0 }, new() { Input = "50 / 2 * 2 + 10", Expected = 60 }, new() { Input = "20 + 2 * -10", Expected = 0 }, new() { Input = "2 * (5 + 10)", Expected = 30 }, new() { Input = "(5 + 10 * 2 + 15 / 3) * 2 + -10", Expected = 50 } }; foreach (var integerTest in tests) { var evaluated = StaticTests.TestEval(integerTest.Input); StaticTests.TestIntegerObject(evaluated, integerTest.Expected); } }