CanonSharp/CanonSharp.Tests/ParserTests/StatementParserTests.cs

83 lines
2.5 KiB
C#
Raw Normal View History

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<AssignNode>(GrammarParser.StatementParser(), "temp := 1");
Assert.Equal("temp", node.Variable.IdentifierName);
Assert.Equal(1, node.Expression.Convert<IntegerValueNode>().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<BlockNode>(GrammarParser.CompoundStatementParser(), input);
Assert.Equal(2, node.Statements.Count);
AssignNode assignNode = node.Statements[0].Convert<AssignNode>();
Assert.Equal("temp", assignNode.Variable.IdentifierName);
assignNode = node.Statements[1].Convert<AssignNode>();
Assert.Equal("flag", assignNode.Variable.IdentifierName);
}
[Fact]
public void CompoundStatementTest2()
{
BlockNode node = RunParser<BlockNode>(GrammarParser.CompoundStatementParser(), "begin end");
Assert.Empty(node.Statements);
}
[Fact]
public void ProgramHeadTest1()
{
ProgramHead head = RunParser<ProgramHead>(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<ProgramBody>(GrammarParser.ProgramBodyParser(), input).MainBlock;
Assert.Equal(2, node.Statements.Count);
AssignNode assignNode = node.Statements[0].Convert<AssignNode>();
Assert.Equal("temp", assignNode.Variable.IdentifierName);
assignNode = node.Statements[1].Convert<AssignNode>();
Assert.Equal("flag", assignNode.Variable.IdentifierName);
}
}