jackfiled
cf19f8197e
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>
83 lines
3.3 KiB
C#
83 lines
3.3 KiB
C#
using CanonSharp.Pascal.Parser;
|
|
using CanonSharp.Pascal.SyntaxTree;
|
|
using CanonSharp.Tests.Utils;
|
|
|
|
namespace CanonSharp.Tests.ParserTests;
|
|
|
|
public class VariableDeclarationTests : GrammarParserTestBase
|
|
{
|
|
[Theory]
|
|
[InlineData("boolean", "boolean")]
|
|
[InlineData("integer", "integer")]
|
|
[InlineData("char", "char")]
|
|
[InlineData("real", "real")]
|
|
public void BasicTypeParseTest(string input, string value)
|
|
{
|
|
TypeNode node = RunParser<TypeNode>(GrammarParser.TypeParser(), input);
|
|
Assert.Equal(value, node.TypeToken.LiteralValue);
|
|
}
|
|
|
|
[Fact]
|
|
public void ArrayTypeParseTest()
|
|
{
|
|
TypeNode node = RunParser<TypeNode>(GrammarParser.ArrayTypeParser(), "array [0..9] of integer");
|
|
|
|
Assert.Equal("integer", node.TypeToken.LiteralValue);
|
|
Assert.Single(node.ArrayRanges);
|
|
Assert.Equal(new ArrayRange(0, 9), node.ArrayRanges.First());
|
|
|
|
node = RunParser<TypeNode>(GrammarParser.ArrayTypeParser(), "array [0..10,0..10] of char");
|
|
Assert.Equal("char", node.TypeToken.LiteralValue);
|
|
Assert.Equal(2, node.ArrayRanges.Count);
|
|
Assert.Equal(new ArrayRange(0, 10), node.ArrayRanges[0]);
|
|
Assert.Equal(new ArrayRange(0, 10), node.ArrayRanges[1]);
|
|
}
|
|
|
|
[Fact]
|
|
public void VariableDeclarationTest()
|
|
{
|
|
VariableDeclarationNode node = RunParser<VariableDeclarationNode>(GrammarParser.VariableDeclarationParser(),
|
|
"a : integer");
|
|
|
|
Assert.Contains(node.Identifiers, token => token.LiteralValue == "a");
|
|
Assert.Equal("integer", node.TypeNode.TypeToken.LiteralValue);
|
|
|
|
node = RunParser<VariableDeclarationNode>(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);
|
|
}
|
|
|
|
[Fact]
|
|
public void ArrayVariableDeclarationTest()
|
|
{
|
|
VariableDeclarationNode node = RunParser<VariableDeclarationNode>(GrammarParser.VariableDeclarationParser(),
|
|
"test_array: array [0..9,10..20] of real");
|
|
|
|
Assert.Single(node.Identifiers);
|
|
Assert.Contains(node.Identifiers, token => token.LiteralValue == "test_array");
|
|
Assert.Equal("real", node.TypeNode.TypeToken.LiteralValue);
|
|
|
|
Assert.Equal(2, node.TypeNode.ArrayRanges.Count);
|
|
Assert.Equal(new ArrayRange(0, 9), node.TypeNode.ArrayRanges[0]);
|
|
Assert.Equal(new ArrayRange(10, 20), node.TypeNode.ArrayRanges[1]);
|
|
}
|
|
|
|
[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)]
|
|
[InlineData("var a : array [0..5] of integer", 1)]
|
|
[InlineData("var a,b,c: array [0..9,10..20] of integer; c,d : boolean", 2)]
|
|
public void VariableDeclarationsTest(string input, int count)
|
|
{
|
|
BlockNode node = RunParser<BlockNode>(GrammarParser.VariableDeclarationsParser(), input);
|
|
Assert.Equal(count, node.Statements.Count);
|
|
}
|
|
}
|