using CanonSharp.Pascal.Parser; using CanonSharp.Pascal.SyntaxTree; using CanonSharp.Tests.Utils; namespace CanonSharp.Tests.ParserTests; public class StatementParserTests : GrammarParserTestBase { [Fact] public void StatementTest1() { AssignNode node = RunParser(GrammarParser.StatementParser(), "temp := 1"); Assert.Equal("temp", node.Variable.IdentifierName); Assert.Equal(1, node.Expression.Convert().Value); } [Theory] [InlineData(""" begin temp := 1 + 1; flag := true and false; end """)] [InlineData(""" begin temp := 1 + 1; flag := true and false end """)] public void CompoundStatementTest1(string input) { BlockNode node = RunParser(GrammarParser.CompoundStatementParser(), input); Assert.Equal(2, node.Statements.Count); AssignNode assignNode = node.Statements[0].Convert(); Assert.Equal("temp", assignNode.Variable.IdentifierName); assignNode = node.Statements[1].Convert(); Assert.Equal("flag", assignNode.Variable.IdentifierName); } [Fact] public void CompoundStatementTest2() { BlockNode node = RunParser(GrammarParser.CompoundStatementParser(), "begin end"); Assert.Empty(node.Statements); } [Fact] public void ProgramHeadTest1() { ProgramHead head = RunParser(GrammarParser.ProgramHeadParser(), "program main"); Assert.Equal("main", head.ProgramName.LiteralValue); } [Theory] [InlineData(""" begin temp := 1 + 1; flag := true and false; end """)] [InlineData(""" begin temp := 1 + 1; flag := true and false end """)] public void ProgramBodyTest1(string input) { BlockNode node = RunParser(GrammarParser.ProgramBodyParser(), input).MainBlock; Assert.Equal(2, node.Statements.Count); AssignNode assignNode = node.Statements[0].Convert(); Assert.Equal("temp", assignNode.Variable.IdentifierName); assignNode = node.Statements[1].Convert(); Assert.Equal("flag", assignNode.Variable.IdentifierName); } }