using Microsoft.VisualStudio.TestTools.UnitTesting; using ShintenScript; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShintenScriptTest { [TestClass] public class ParserTest { [TestMethod] public void TestBlock() { Lexer lexer = new ScaffoldLexer(new Token[] { new Token { type = Token.Type.LBRACE }, new Token { type = Token.Type.NUMBER, data = 1f }, new Token { type = Token.Type.NUMBER, data = 2f }, new Token { type = Token.Type.NUMBER, data = 3f }, new Token { type = Token.Type.RBRACE }, }); IASTNode ast = new Parser(lexer).ParseExpression(); if (ast is ASTNodeBlock block) { Assert.AreEqual(3, block.statements.Count); Assert.IsInstanceOfType(block.statements[0], typeof(ASTNodeLiteral)); Assert.IsInstanceOfType(block.statements[1], typeof(ASTNodeLiteral)); Assert.IsInstanceOfType(block.statements[2], typeof(ASTNodeLiteral)); } else Assert.Fail(); } [TestMethod] public void TestComparison() { Lexer lexer = new ScaffoldLexer(new Token[] { new Token { type = Token.Type.NUMBER, data = 1f }, new Token { type = Token.Type.LE }, new Token { type = Token.Type.NUMBER, data = 2f }, new Token { type = Token.Type.NE }, new Token { type = Token.Type.NUMBER, data = 3f }, }); IASTNode ast = new Parser(lexer).ParseExpression(); if (ast is ASTNodeComparison comp) { Assert.AreEqual(3, comp.nodes.Count); Assert.AreEqual(2, comp.comparisons.Count); Assert.IsInstanceOfType(comp.nodes[0], typeof(ASTNodeLiteral)); Assert.IsInstanceOfType(comp.nodes[1], typeof(ASTNodeLiteral)); Assert.IsInstanceOfType(comp.nodes[2], typeof(ASTNodeLiteral)); Assert.AreEqual(Token.Type.LE, comp.comparisons[0]); Assert.AreEqual(Token.Type.NE, comp.comparisons[1]); } else Assert.Fail(); } [TestMethod] public void TestDivision() { Lexer lexer = new ScaffoldLexer(new Token[] { new Token { type = Token.Type.NUMBER, data = 1f }, new Token { type = Token.Type.SLASH }, new Token { type = Token.Type.NUMBER, data = 2f }, }); IASTNode ast = new Parser(lexer).ParseExpression(); Assert.IsInstanceOfType(ast, typeof(ASTNodeDivide)); } [TestMethod] public void TestMinus() { Lexer lexer = new ScaffoldLexer(new Token[] { new Token { type = Token.Type.MINUS }, new Token { type = Token.Type.NUMBER, data = 2f }, new Token { type = Token.Type.MINUS }, new Token { type = Token.Type.NUMBER, data = 3f }, }); IASTNode ast = new Parser(lexer).ParseExpression(); if (ast is ASTNodeSubtract sub) { Assert.IsInstanceOfType(sub.lhs, typeof(ASTNodeNegative)); Assert.IsInstanceOfType(sub.rhs, typeof(ASTNodeLiteral)); } else Assert.Fail(); } [TestMethod] public void TestOrderOfOperations() { Lexer lexer = new ScaffoldLexer(new Token[] { new Token { type = Token.Type.NUMBER, data = 1f }, new Token { type = Token.Type.PLUS }, new Token { type = Token.Type.NUMBER, data = 2f }, new Token { type = Token.Type.ASTERISK }, new Token { type = Token.Type.NUMBER, data = 3f }, }); IASTNode ast = new Parser(lexer).ParseExpression(); if (ast is ASTNodeAdd add) { Assert.IsInstanceOfType(add.lhs, typeof(ASTNodeLiteral)); if (add.rhs is ASTNodeMultiply mul) { Assert.IsInstanceOfType(mul.lhs, typeof(ASTNodeLiteral)); Assert.IsInstanceOfType(mul.rhs, typeof(ASTNodeLiteral)); } else Assert.Fail(); } else Assert.Fail(); lexer = new ScaffoldLexer(new Token[] { new Token { type = Token.Type.NUMBER, data = 1f }, new Token { type = Token.Type.ASTERISK }, new Token { type = Token.Type.NUMBER, data = 2f }, new Token { type = Token.Type.PLUS }, new Token { type = Token.Type.NUMBER, data = 3f }, }); ast = new Parser(lexer).ParseExpression(); if (ast is ASTNodeAdd add2) { Assert.IsInstanceOfType(add2.rhs, typeof(ASTNodeLiteral)); if (add2.lhs is ASTNodeMultiply mul) { Assert.IsInstanceOfType(mul.lhs, typeof(ASTNodeLiteral)); Assert.IsInstanceOfType(mul.rhs, typeof(ASTNodeLiteral)); } else Assert.Fail(); } else Assert.Fail(); } } }