using CanonSharp.Pascal.Parser; using CanonSharp.Pascal.SyntaxTree; using CanonSharp.Tests.Utils; namespace CanonSharp.Tests.ParserTests; public class ConstDeclarationTests : GrammarParserTestBase { [Fact] public void BooleanConstValueTest() { BooleanValueNode node = RunParser(GrammarParser.ConstValueParser(), "true"); Assert.True(node.Value); node = RunParser(GrammarParser.ConstValueParser(), "false"); Assert.False(node.Value); } [Fact] public void CharConstValueTest() { CharValueNode node = RunParser(GrammarParser.ConstValueParser(), "'a'"); Assert.Equal('a', node.Value); } [Fact] public void NumberConstValueTest() { IntegerValueNode integerValueNode = RunParser(GrammarParser.ConstValueParser(), "250"); Assert.Equal(250, integerValueNode.Value); FloatValueNode floatValueNode = RunParser(GrammarParser.ConstValueParser(), "100.5"); Assert.Equal(100.5, floatValueNode.Value); UnaryOperatorNode node = RunParser(GrammarParser.ConstValueParser(), "+250"); Assert.Equal(UnaryOperatorType.Plus, node.OperatorType); node = RunParser(GrammarParser.ConstValueParser(), "-100.5"); Assert.Equal(UnaryOperatorType.Minus, node.OperatorType); } [Fact] public void ConstDeclarationTest() { ConstantNode node = RunParser(GrammarParser.ConstDeclarationParser(), "a = true"); Assert.Equal("a", node.Identifier.LiteralValue); Assert.True(node.Value.Convert().Value); } [Theory] [InlineData("", 0)] [InlineData("const INFINITE = 100;", 1)] [InlineData("const a = true; b = false", 2)] [InlineData("const code1 = 100.4;code2 = 'a'", 2)] public void ConstDeclarationsCountTest(string input, int count) { BlockNode blockNode = RunParser(GrammarParser.ConstDeclarationsParser(), input); List constantNodes = blockNode.Statements.Select(node => node.Convert()).ToList(); Assert.Equal(count, constantNodes.Count); } }