Skip to content

Commit

Permalink
ParseTreeSimplicityTests.TestIfElse: testing simplicity of parse tree
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnemeti authored and yallie committed Jan 27, 2023
1 parent 7320861 commit cc99134
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions Irony.Tests/040.Irony.Tests.VsTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="CommentTerminalTests.cs" />
<Compile Include="ErrorRecoveryTests.cs" />
<Compile Include="OperatorTests.cs" />
<Compile Include="ParseTreeSimplicityTests.cs" />
<Compile Include="TestHelper.cs" />
<Compile Include="TokenPreviewResolution\ConflictResolutionTests.cs" />
<Compile Include="DataLiteralsTests.cs" />
Expand Down
1 change: 1 addition & 0 deletions Irony.Tests/050.Irony.Tests.NUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<Compile Include="IdentifierTerminalTests.cs" />
<Compile Include="IntegrationTests.cs" />
<Compile Include="NumberLiteralTests.cs" />
<Compile Include="ParseTreeSimplicityTests.cs" />
<Compile Include="OperatorTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="NewLineTests.cs" />
Expand Down
59 changes: 59 additions & 0 deletions Irony.Tests/ParseTreeSimplicityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using Irony.Parsing;

namespace Irony.Tests {
#if USE_NUNIT
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

[TestClass]
public class ParseTreeSimplicityTests {

#region Grammars
public class IfElseGrammar : Grammar {
public IfElseGrammar() {
var id = new IdentifierTerminal("id");

Root = new NonTerminal("root");
Root.Rule = ToTerm("if") + id + id + (ToTerm("else") + id).Q();
}
}//if else grammar class

#endregion

[TestMethod]
public void TestIfElse() {

var grammar = new IfElseGrammar();
var parser = new Parser(grammar);
TestHelper.CheckGrammarErrors(parser);

var parseTree = parser.Parse("if cond a");
TestHelper.CheckParseErrors(parseTree);
Assert.IsNotNull(parseTree.Root, "Root not found.");
Assert.AreEqual("root", parseTree.Root.Term.Name);
Assert.AreEqual("if", parseTree.Root.ChildNodes[0].Term.Name);
Assert.AreEqual("id", parseTree.Root.ChildNodes[1].Term.Name);
Assert.AreEqual("id", parseTree.Root.ChildNodes[2].Term.Name);

parseTree = parser.Parse("if cond a else b");
var errorMessage = $"Actual parse tree:{Environment.NewLine}{parseTree.ToXml()}";

TestHelper.CheckParseErrors(parseTree);
Assert.IsNotNull(parseTree.Root, "Root not found.");
Assert.AreEqual("root", parseTree.Root.Term.Name, errorMessage);
Assert.AreEqual("if", parseTree.Root.ChildNodes[0].Term.Name, errorMessage);
Assert.AreEqual("id", parseTree.Root.ChildNodes[1].Term.Name, errorMessage);
Assert.AreEqual("id", parseTree.Root.ChildNodes[2].Term.Name, errorMessage);
Assert.AreEqual("else", parseTree.Root.ChildNodes[3].ChildNodes[0].Term.Name, errorMessage);
Assert.AreEqual("id", parseTree.Root.ChildNodes[3].ChildNodes[1].Term.Name, errorMessage);

}

}//class
}//namespace

0 comments on commit cc99134

Please sign in to comment.