Skip to content

Commit be30b57

Browse files
Update tests
1 parent 7176a41 commit be30b57

6 files changed

Lines changed: 93 additions & 15 deletions

File tree

Sharplasu.Tests/Model/PositionTest.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ second line
3434
Assert.AreEqual(5, new Point(1, 5).Offset(code));
3535
Assert.AreEqual(17, new Point(1, 17).Offset(code));
3636
Assert.AreEqual(18, new Point(2, 0).Offset(code));
37-
Assert.ThrowsException<InvalidOperationException>(() => new Point(1, 18).Offset(code));
38-
Assert.ThrowsException<InvalidOperationException>(() => new Point(4, 0).Offset(code));
37+
Assert.ThrowsExactly<InvalidOperationException>(() => new Point(1, 18).Offset(code));
38+
Assert.ThrowsExactly<InvalidOperationException>(() => new Point(4, 0).Offset(code));
3939
}
4040

4141
[TestMethod]
@@ -200,10 +200,13 @@ public void IllegalPositionAccepted()
200200
}
201201

202202
[TestMethod]
203-
[ExpectedException(typeof(InvalidOperationException))]
203+
204204
public void IllegalPositionNotAccepted()
205205
{
206-
var position = new Position(new Point(10, 1), new Point(5, 2), validate: true);
206+
Assert.ThrowsExactly<InvalidOperationException>(() =>
207+
{
208+
var position = new Position(new Point(10, 1), new Point(5, 2), validate: true);
209+
});
207210
}
208211

209212
[TestMethod]

Sharplasu.Tests/Models/SimpleLang/Models.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@ namespace Strumenta.Sharplasu.Tests.Models.SimpleLang;
33

44
public static class Models
55
{
6+
public class EFunc : Node { };
7+
8+
public class EncodingFunction : EFunc
9+
{
10+
public string Name { get; set; }
11+
public StringLiteral String { get; set; }
12+
}
13+
14+
public class CapitalizeFunction : EFunc
15+
{
16+
public string Name { get; set; }
17+
public StringLiteral String { get; set; }
18+
}
19+
20+
public class FileRoot : Node
21+
{
22+
public List<EFunc> eFuncs { get; set; }
23+
}
24+
625
public static CompilationUnit GetCompilationUnit()
726
{
827
var cu = new CompilationUnit
@@ -26,4 +45,32 @@ public static CompilationUnit GetCompilationUnit()
2645
cu.AssignParents();
2746
return cu;
2847
}
48+
49+
public static FileRoot GetExampleCompilationUnit()
50+
{
51+
var cu = new FileRoot()
52+
{
53+
eFuncs = new List<EFunc>
54+
{
55+
new EncodingFunction()
56+
{
57+
Name = "",
58+
String = new StringLiteral
59+
{
60+
Value = "bar"
61+
}
62+
},
63+
new CapitalizeFunction()
64+
{
65+
Name = "",
66+
String = new StringLiteral
67+
{
68+
Value = "bar"
69+
}
70+
}
71+
}
72+
};
73+
cu.AssignParents();
74+
return cu;
75+
}
2976
}

Sharplasu.Tests/SymbolResolution/AssertTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class AssertTest
1414
public void TestAssertion()
1515
{
1616
var cu = DeclarativeLocalSymbolResolverTest.GetCompilationUnit();
17-
Assert.ThrowsException<SymbolResolutionException>(() => cu.AssertAllReferencesResolved());
17+
Assert.ThrowsExactly<SymbolResolutionException>(() => cu.AssertAllReferencesResolved());
1818
DeclarativeLocalSymbolResolverTest.GetFullSymbolResolver().ResolveSymbols(cu);
19-
Assert.ThrowsException<SymbolResolutionException>(() => cu.AssertNotAllReferencesResolved());
19+
Assert.ThrowsExactly<SymbolResolutionException>(() => cu.AssertNotAllReferencesResolved());
2020
}
2121

2222
[TestMethod]

Sharplasu.Tests/Testing/Testing.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void TestDifferentParsingResults()
8686
}
8787
};
8888

89-
Assert.ThrowsException<ASTDifferenceException>(() => AssertParsingResultsAreEqual(result1, result2));
89+
Assert.ThrowsExactly<ASTDifferenceException>(() => AssertParsingResultsAreEqual(result1, result2));
9090
}
9191

9292
[TestMethod]
@@ -214,7 +214,7 @@ public void TestAssertASTsWithDifferentNodes()
214214
}
215215
};
216216

217-
Assert.ThrowsException<ASTDifferenceException>(() => AssertASTsAreEqual(ast1, ast2));
217+
Assert.ThrowsExactly<ASTDifferenceException>(() => AssertASTsAreEqual(ast1, ast2));
218218
}
219219

220220
[TestMethod]
@@ -268,7 +268,7 @@ public void TestAssertASTsWithSameNodeTypesButDifferentNodeValues()
268268
}
269269
};
270270

271-
Assert.ThrowsException<ASTDifferenceException>(() => AssertASTsAreEqual(ast1, ast2));
271+
Assert.ThrowsExactly<ASTDifferenceException>(() => AssertASTsAreEqual(ast1, ast2));
272272
}
273273

274274
[TestMethod]
@@ -335,13 +335,13 @@ public void TestTwoDifferentNodesWithOneNullProperty()
335335
}
336336
}
337337
};
338-
Assert.ThrowsException<ASTDifferenceException>(() => AssertASTsAreEqual(ast1, ast2));
338+
Assert.ThrowsExactly<ASTDifferenceException>(() => AssertASTsAreEqual(ast1, ast2));
339339
}
340340

341341
[TestMethod]
342342
public void CheckRequire()
343343
{
344-
Assert.ThrowsException<InvalidOperationException>(() => Require(false));
345-
Assert.ThrowsException<InvalidOperationException>(() => Require(false, () => "Hello. I'm an error"));
344+
Assert.ThrowsExactly<InvalidOperationException>(() => Require(false));
345+
Assert.ThrowsExactly<InvalidOperationException>(() => Require(false, () => "Hello. I'm an error"));
346346
}
347347
}

Sharplasu.Tests/Traversing/TraversingTest.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Strumenta.Sharplasu.Traversing;
44
using Type = System.Type;
55
using Expression = Strumenta.Sharplasu.Tests.Models.SimpleLang.Expression;
6+
using System;
7+
using static Strumenta.Sharplasu.Tests.Models.SimpleLang.Models;
68

79
namespace Strumenta.Sharplasu.Tests
810
{
@@ -29,7 +31,7 @@ public void TestWalk()
2931
[TestMethod]
3032
public void TestWalkLeavesFirst()
3133
{
32-
var cu = Tests.Models.SimpleLang.Models.GetCompilationUnit();
34+
var cu = Tests.Models.SimpleLang.Models.GetCompilationUnit();
3335
TestSequences(
3436
MapNodesToTypes(cu.WalkLeavesFirst()),
3537
new List<System.Type>
@@ -145,18 +147,45 @@ public void TestWalkDescendants()
145147
});
146148
}
147149

150+
public IEnumerable<Node> GetNotEncodingChildren(Node x)
151+
{
152+
if (x is FileRoot)
153+
{
154+
List<Node> children = new List<Node>();
155+
x.WalkDescendants().ToList().ForEach(t =>
156+
{
157+
children.AddRange(GetNotEncodingChildren(t));
158+
});
159+
return children;
160+
}
161+
else if (x is EncodingFunction)
162+
return x.WalkChildren();
163+
else
164+
return Enumerable.Empty<Node>();
165+
}
166+
167+
148168
[TestMethod]
149169
public void TestFindAncestorOfType()
150170
{
151171
var cu = Tests.Models.SimpleLang.Models.GetCompilationUnit();
152172
var identifier = cu.WalkDescendants().First(n => n.GetType() == typeof(Identifier));
173+
var ast = Tests.Models.SimpleLang.Models.GetExampleCompilationUnit();
174+
var nodes = ast.WalkDescendants(x => {
175+
return GetNotEncodingChildren(x);
176+
});
177+
foreach (var node in nodes)
178+
{
179+
Console.WriteLine(node.ToString());
180+
}
153181
Assert.IsInstanceOfType(identifier.FindAncestorOfType<CompilationUnit>(), typeof(CompilationUnit));
154182
Assert.IsInstanceOfType(identifier.FindAncestorOfType<SetStatement>(), typeof(SetStatement));
155183
Assert.IsNull(identifier.FindAncestorOfType<DisplayStatement>());
156184
}
157185

158186
private List<Type> MapNodesToTypes(IEnumerable<Node> nodeSequence)
159187
{
188+
160189
return nodeSequence.Select(node => node.GetType()).ToList();
161190
}
162191

Sharplasu/Traversing/Structurally.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public static IEnumerable<Node> WalkChildren(this Node node)
4949
if (item is Node)
5050
children.Add(item as Node);
5151
}
52-
}
53-
52+
}
5453
});
5554
return children.AsEnumerable<Node>();
5655
}

0 commit comments

Comments
 (0)