50 lines
1.8 KiB
C#
50 lines
1.8 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 TypeParseTest(string input, string value)
|
|
{
|
|
TypeNode node = RunParser<TypeNode>(GrammarParser.TypeParser(), input);
|
|
Assert.Equal(value, node.TypeToken.LiteralValue);
|
|
}
|
|
|
|
[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);
|
|
}
|
|
|
|
[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<BlockNode>(GrammarParser.VariableDeclarationsParser(), input);
|
|
Assert.Equal(count, node.Statements.Count);
|
|
}
|
|
}
|