From c7077bd0fd310866ab5fc07ec87c476dca8240cc Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 17 Aug 2024 15:44:58 +0800 Subject: [PATCH] add: Variable declarations test. --- CanonSharp.Pascal/Parser/GrammarParserBase.cs | 4 +- .../ParserTests/ProgramParserTests.cs | 62 +++++++++++++++++++ .../ParserTests/VariableDeclarationTests.cs | 31 +++++++++- 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/CanonSharp.Pascal/Parser/GrammarParserBase.cs b/CanonSharp.Pascal/Parser/GrammarParserBase.cs index 28404fa..83e1338 100644 --- a/CanonSharp.Pascal/Parser/GrammarParserBase.cs +++ b/CanonSharp.Pascal/Parser/GrammarParserBase.cs @@ -9,7 +9,9 @@ namespace CanonSharp.Pascal.Parser; public abstract class GrammarParserBuilder { protected static IParser Keyword(string value) - => Satisfy(token => token.TokenType == LexicalTokenType.Keyword && token.LiteralValue == value); + => Satisfy(token => + token.TokenType == LexicalTokenType.Keyword && + token.LiteralValue.Equals(value, StringComparison.OrdinalIgnoreCase)); protected static IParser Operator(string value) => Satisfy(token => token.TokenType == LexicalTokenType.Operator && token.LiteralValue == value); diff --git a/CanonSharp.Tests/ParserTests/ProgramParserTests.cs b/CanonSharp.Tests/ParserTests/ProgramParserTests.cs index 818cb35..6431711 100644 --- a/CanonSharp.Tests/ParserTests/ProgramParserTests.cs +++ b/CanonSharp.Tests/ParserTests/ProgramParserTests.cs @@ -66,4 +66,66 @@ public class ProgramParserTests : GrammarParserTestBase Assert.Equal(1, binaryOperatorNode.Left.Convert().Value); Assert.Equal(1, binaryOperatorNode.Right.Convert().Value); } + + [Fact] + public void ProgramParseTest4() + { + Program program = ProgramParse(""" + program main; + var a : integer; + begin + a := 1 + 1; + end. + """); + + IList variableDeclarationNodes = program.Body.VariableDeclarations.Statements + .Select(x => x.Convert()).ToList(); + Assert.Single(variableDeclarationNodes); + Assert.Single(variableDeclarationNodes.First().Identifiers); + Assert.Equal("a", variableDeclarationNodes.First().Identifiers.First().LiteralValue); + } + + [Theory] + [InlineData(""" + program DoNothing; + begin + end. + """)] + [InlineData(""" + program Add; + var a : Integer; + begin + a := 1 + 1 + end. + """)] + [InlineData(""" + program varTest; + var a : integer; + begin + a := 9 div 1; + end. + """)] + [InlineData(""" + program main; + var a : integer; + begin + a := 1 + +1; + a := 1 - -1; + a := 1 + -1; + a := 1 - +1; + end. + """)] + [InlineData(""" + program main; + const a = true; b = false; + var c, d : boolean; + begin + c := true; + d := false; + end. + """)] + public void ProgramParseTest(string input) + { + ProgramParse(input); + } } diff --git a/CanonSharp.Tests/ParserTests/VariableDeclarationTests.cs b/CanonSharp.Tests/ParserTests/VariableDeclarationTests.cs index 2501c7f..6c5fe5d 100644 --- a/CanonSharp.Tests/ParserTests/VariableDeclarationTests.cs +++ b/CanonSharp.Tests/ParserTests/VariableDeclarationTests.cs @@ -16,5 +16,34 @@ public class VariableDeclarationTests : GrammarParserTestBase TypeNode node = RunParser(GrammarParser.TypeParser(), input); Assert.Equal(value, node.TypeToken.LiteralValue); } -} + [Fact] + public void VariableDeclarationTest() + { + VariableDeclarationNode node = RunParser(GrammarParser.VariableDeclarationParser(), + "a : integer"); + + Assert.Contains(node.Identifiers, token => token.LiteralValue == "a"); + Assert.Equal("integer", node.TypeNode.TypeToken.LiteralValue); + + node = RunParser(GrammarParser.VariableDeclarationParser(), + "a, b, c : boolean"); + + Assert.Contains(node.Identifiers, token => token.LiteralValue == "a"); + Assert.Contains(node.Identifiers, token => token.LiteralValue == "b"); + Assert.Contains(node.Identifiers, token => token.LiteralValue == "c"); + Assert.Equal("boolean", node.TypeNode.TypeToken.LiteralValue); + } + + [Theory] + [InlineData("var a : integer;", 1)] + [InlineData("var a : integer; b : boolean;", 2)] + [InlineData("var a : integer; b,c : boolean;" + + "d : char", 3)] + [InlineData("var a,d,e : integer; b : boolean; c: real;", 3)] + public void VariableDeclarationsTest(string input, int count) + { + BlockNode node = RunParser(GrammarParser.VariableDeclarationsParser(), input); + Assert.Equal(count, node.Statements.Count); + } +}