Permalink
Browse files

testing structure of AST from DOT file

  • Loading branch information...
1 parent ccc14f7 commit 55415615cd6ab1ef9c86249bde6eff635d014e3f @aabs committed Mar 20, 2012
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
@@ -20,6 +21,13 @@ public void TestAttribute()
{
var x = parser.attribute();
WriteToDotFile(x.Tree, "AST-attr.dot");
+ TestForNodesAndArcs("AST-attr.dot",
+ new[]
+ {
+ Tuple.Create("XMLATTR", "a"),
+ Tuple.Create("XMLATTR", "VALSTR"),
+ Tuple.Create("VALSTR", "'b'")
+ });
}
catch (RecognitionException e)
{
@@ -35,6 +43,12 @@ public void TestSimpleElement()
{
var x = parser.element();
WriteToDotFile(x.Tree, "AST-simple-element.dot");
+ TestForNodesAndArcs("AST-simple-element.dot",
+ new[]
+ {
+ Tuple.Create("XMLOPENELEM", "a"),
+ Tuple.Create("XMLOPENELEM", "XMLATTRS"),
+ });
}
catch (RecognitionException e)
{
@@ -50,6 +64,15 @@ public void TestElementWithAttrs()
{
var x = parser.element();
WriteToDotFile(x.Tree, "AST-simple-element-with-one-attr.dot");
+ TestForNodesAndArcs("AST-simple-element-with-one-attr.dot",
+ new[]
+ {
+ Tuple.Create("XMLATTR", "x"),
+ Tuple.Create("XMLATTR", "c"),
+ Tuple.Create("XMLATTR", "VALSTR"),
+ Tuple.Create("VALSTR", "'y'"),
+ Tuple.Create("VALSTR", "'d'"),
+ });
}
catch (RecognitionException e)
{
@@ -65,6 +88,18 @@ public void TestProperElement()
{
var x = parser.content();
WriteToDotFile(x.Tree, "AST-proper-element.dot");
+ TestForNodesAndArcs("AST-proper-element.dot",
+ new[]
+ {
+ Tuple.Create("XMLATTR", "x"),
+ Tuple.Create("XMLATTR", "c"),
+ Tuple.Create("XMLATTR", "VALSTR"),
+ Tuple.Create("VALSTR", "'y'"),
+ Tuple.Create("VALSTR", "'d'"),
+ Tuple.Create("XML", "XMLOPENELEM"),
+ Tuple.Create("XML", "XMLCLOSEELEM"),
+ Tuple.Create("XMLCLOSEELEM", "a"),
+ });
}
catch (RecognitionException e)
{
@@ -80,6 +115,20 @@ public void TestMatchExpression()
{
var x = parser.matchOperation();
WriteToDotFile(x.Tree, "AST-match.dot");
+ TestForNodesAndArcs("AST-match.dot",
+ new[]
+ {
+ Tuple.Create("MATCH", "VARREF"),
+ Tuple.Create("VARREF", "x"),
+ Tuple.Create("XMLATTR", "x"),
+ Tuple.Create("XMLATTR", "c"),
+ Tuple.Create("XMLATTR", "VALSTR"),
+ Tuple.Create("VALSTR", "'y'"),
+ Tuple.Create("VALSTR", "'d'"),
+ Tuple.Create("XML", "XMLOPENELEM"),
+ Tuple.Create("XML", "XMLCLOSEELEM"),
+ Tuple.Create("XMLCLOSEELEM", "a"),
+ });
}
catch (RecognitionException e)
{
@@ -95,6 +144,20 @@ public void TestMatchStatement()
{
var x = parser.statement();
WriteToDotFile(x.Tree, "AST-match-statement.dot");
+ TestForNodesAndArcs("AST-match-statement.dot",
+ new[]
+{
+ Tuple.Create("MATCH", "VARREF"),
+ Tuple.Create("VARREF", "x"),
+ Tuple.Create("XMLATTR", "x"),
+ Tuple.Create("XMLATTR", "c"),
+ Tuple.Create("XMLATTR", "VALSTR"),
+ Tuple.Create("VALSTR", "'y'"),
+ Tuple.Create("VALSTR", "'d'"),
+ Tuple.Create("XML", "XMLOPENELEM"),
+ Tuple.Create("XML", "XMLCLOSEELEM"),
+ Tuple.Create("XMLCLOSEELEM", "a"),
+ });
}
catch (RecognitionException e)
{
@@ -124,6 +187,21 @@ public void TestModule()
{
var x = parser.module();
WriteToDotFile(x.Tree, "AST-module.dot");
+ TestForNodesAndArcs("AST-module.dot",
+ new[]
+ {
+ Tuple.Create("ROOT", "module"),
+ Tuple.Create("ROOT", "Imports"),
+ Tuple.Create("Imports", "import"),
+ Tuple.Create("import", "std"),
+ Tuple.Create("import", "xml"),
+ Tuple.Create("XMLATTR", "postcode"),
+ Tuple.Create("XMLATTR", "VALSTR"),
+ Tuple.Create("VALSTR", "'blah'"),
+ Tuple.Create("XML", "landlord"),
+ Tuple.Create("XMLCLOSEELEM", "address"),
+ Tuple.Create("FUNCDEC", "EntryPoint"),
+ });
}
catch (RecognitionException e)
{
@@ -144,5 +222,17 @@ internal void WriteToDotFile(CommonTree tree, string fileName)
string st = gen.ToDot(tree);
File.WriteAllText(@"C:\dat\repository\personal\dev\projects\loblang\test-documents\" + fileName, st);
}
+ internal static void TestForNodesAndArcs(string fileName, IEnumerable<Tuple<string, string>> pairs )
+ {
+ var content = File.ReadAllText(@"C:\dat\repository\personal\dev\projects\loblang\test-documents\" + fileName);
+ foreach (var pair in pairs)
+ {
+ var sourceNodeName = pair.Item1;
+ var destNodeName = pair.Item2;
+ Assert.IsTrue(content.Contains(string.Format("[label=\"{0}\"];", sourceNodeName)), string.Format("could not find '{0}' node name definition", sourceNodeName));
+ Assert.IsTrue(content.Contains(string.Format("[label=\"{0}\"];", destNodeName)), string.Format("could not find '{0}' node name definition", destNodeName));
+ Assert.IsTrue(content.Contains(string.Format("// \"{0}\" -> \"{1}\"", sourceNodeName, destNodeName)), string.Format("unable to find an arc between '{0}' and '{1}'", sourceNodeName, destNodeName));
+ }
+ }
}
}
View
Binary file not shown.
@@ -8,9 +8,12 @@ digraph {
n0 [label="XMLATTR"];
n1 [label="a"];
- n2 [label="'b'"];
+ n2 [label="VALSTR"];
+ n2 [label="VALSTR"];
+ n3 [label="'b'"];
n0 -> n1 // "XMLATTR" -> "a"
- n0 -> n2 // "XMLATTR" -> "'b'"
+ n0 -> n2 // "XMLATTR" -> "VALSTR"
+ n2 -> n3 // "VALSTR" -> "'b'"
}
@@ -6,7 +6,7 @@ digraph {
width=.25, height=.25, color="black", fillcolor="white", style="filled, solid, bold"];
edge [arrowsize=.5, color="black", style="bold"]
- n0 [label="XMLELEM"];
+ n0 [label="XMLOPENELEM"];
n1 [label="a"];
n2 [label="XMLATTRS"];
n2 [label="XMLATTRS"];
@@ -23,8 +23,8 @@ digraph {
n9 [label="VALSTR"];
n10 [label="'d'"];
- n0 -> n1 // "XMLELEM" -> "a"
- n0 -> n2 // "XMLELEM" -> "XMLATTRS"
+ n0 -> n1 // "XMLOPENELEM" -> "a"
+ n0 -> n2 // "XMLOPENELEM" -> "XMLATTRS"
n2 -> n3 // "XMLATTRS" -> "XMLATTR"
n3 -> n4 // "XMLATTR" -> "x"
n3 -> n5 // "XMLATTR" -> "VALSTR"
@@ -6,9 +6,11 @@ digraph {
width=.25, height=.25, color="black", fillcolor="white", style="filled, solid, bold"];
edge [arrowsize=.5, color="black", style="bold"]
- n0 [label="XMLELEM"];
+ n0 [label="XMLOPENELEM"];
n1 [label="a"];
+ n2 [label="XMLATTRS"];
- n0 -> n1 // "XMLELEM" -> "a"
+ n0 -> n1 // "XMLOPENELEM" -> "a"
+ n0 -> n2 // "XMLOPENELEM" -> "XMLATTRS"
}

0 comments on commit 5541561

Please sign in to comment.