CanonSharp/CanonSharp.Tests/ParserTests/ConstDeclarationTests.cs
jackfiled cf19f8197e feat: Grammar Parser (#3)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/3
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
2024-08-18 12:01:27 +08:00

63 lines
2.2 KiB
C#

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<BooleanValueNode>(GrammarParser.ConstValueParser(), "true");
Assert.True(node.Value);
node = RunParser<BooleanValueNode>(GrammarParser.ConstValueParser(), "false");
Assert.False(node.Value);
}
[Fact]
public void CharConstValueTest()
{
CharValueNode node = RunParser<CharValueNode>(GrammarParser.ConstValueParser(), "'a'");
Assert.Equal('a', node.Value);
}
[Fact]
public void NumberConstValueTest()
{
IntegerValueNode integerValueNode = RunParser<IntegerValueNode>(GrammarParser.ConstValueParser(), "250");
Assert.Equal(250, integerValueNode.Value);
FloatValueNode floatValueNode = RunParser<FloatValueNode>(GrammarParser.ConstValueParser(), "100.5");
Assert.Equal(100.5, floatValueNode.Value);
UnaryOperatorNode node = RunParser<UnaryOperatorNode>(GrammarParser.ConstValueParser(), "+250");
Assert.Equal(UnaryOperatorType.Plus, node.OperatorType);
node = RunParser<UnaryOperatorNode>(GrammarParser.ConstValueParser(), "-100.5");
Assert.Equal(UnaryOperatorType.Minus, node.OperatorType);
}
[Fact]
public void ConstDeclarationTest()
{
ConstantNode node = RunParser<ConstantNode>(GrammarParser.ConstDeclarationParser(), "a = true");
Assert.Equal("a", node.Identifier.LiteralValue);
Assert.True(node.Value.Convert<BooleanValueNode>().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<BlockNode>(GrammarParser.ConstDeclarationsParser(), input);
List<ConstantNode> constantNodes = blockNode.Statements.Select(node => node.Convert<ConstantNode>()).ToList();
Assert.Equal(count, constantNodes.Count);
}
}