Skip to content

Commit

Permalink
Move all C# tests into a single project directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Sep 3, 2023
1 parent b8934f8 commit 134e50e
Show file tree
Hide file tree
Showing 20 changed files with 1,705 additions and 1,794 deletions.
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ test-java: $(test_grammars:%.peg=%/Grammar.java)

DOTNET_SDK?=netcoreapp3.1
test-cs: $(test_grammars:%.peg=%/Grammar.cs)
cd test/cs/choices && dotnet test --framework ${DOTNET_SDK}
cd test/cs/node_actions && dotnet test --framework ${DOTNET_SDK}
cd test/cs/predicates && dotnet test --framework ${DOTNET_SDK}
cd test/cs/quantifiers && dotnet test --framework ${DOTNET_SDK}
cd test/cs/sequences && dotnet test --framework ${DOTNET_SDK}
cd test/cs/terminals && dotnet test --framework ${DOTNET_SDK}
cd test/cs && dotnet test --framework ${DOTNET_SDK}

test-js: test/javascript/node_modules $(test_grammars:%.peg=%.js)
cd test/javascript && npm test
Expand Down
136 changes: 136 additions & 0 deletions test/cs/ChoicesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
namespace canopy.choices {
using System.Collections.Generic;
using System.Collections;
using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using canopy.test.grammars.choices;

[TestClass]
public class ChoiceStringsTest : ParseHelper {
[TestMethod]
public void parsesAnyOfTheChoiceOptions(){
expect(Choices.parse("choice-abc: a")).toMatch(node("a", 12));
expect(Choices.parse("choice-abc: b")).toMatch(node("b", 12));
expect(Choices.parse("choice-abc: c")).toMatch(node("c", 12));
}

[TestMethod]
[ExpectedException(typeof(ParseError),
"Expected a ParseError")]
public void rejectsInputMatchingNoneOfTheOptions() {
Choices.parse("choice-abc: d");
}

[TestMethod]
[ExpectedException(typeof(ParseError),
"Expected a ParseError")]
public void rejectsSuperstringsOfTheOptions() {
Choices.parse("choice-abc: ab");
}

[TestMethod]
public void parsesAChoiceAsPartOfASequence(){
expect(Choices.parse("choice-seq: repeat")).toMatch(
node("repeat", 12)
.elem(node("re", 12).noElems())
.elem(node("peat", 14).noElems())
);
}

[TestMethod]
[ExpectedException(typeof(ParseError),
"Expected a ParseError")]
public void doesNotBacktrackIfLaterRulesFail() {
Choices.parse("choice-seq: reppeat");
}
}

[TestClass]
public class ChoiceRepetitionTest : ParseHelper {
[TestMethod]
public void parsesADifferentOptionOnEachIteration(){
expect(Choices.parse("choice-rep: abcabba")).toMatch(
node("abcabba", 12)
.elem(node("a", 12).noElems())
.elem(node("b", 13).noElems())
.elem(node("c", 14).noElems())
.elem(node("a", 15).noElems())
.elem(node("b", 16).noElems())
.elem(node("b", 17).noElems())
.elem(node("a", 18).noElems())
);
}

[TestMethod]
[ExpectedException(typeof(ParseError),
"Expected a ParseError")]
public void rejectsIfAnyIterationDoesNotMatchTheOptions() {
Choices.parse("choice-rep: abcadba");
}
}

[TestClass]
public class ChoiceSequenceTest : ParseHelper {
[TestMethod]
public void parsesOneBranchOfTheChoice(){
expect(Choices.parse("choice-bind: ab")).toMatch(
node("ab", 13)
.elem(node("a", 13).noElems())
.elem(node("b", 14).noElems())
);
}

[TestMethod]
[ExpectedException(typeof(ParseError),
"Expected a ParseError")]
public void testBindsSequencesTighterThanChoices() {
Choices.parse("choice-bind: abef");
}
}

public class ParseHelper {
public Node<Label> expect(TreeNode node) {
return new NodeWrapper(node.elements[1]);
}

public NodeSpec<Label> node(String text, int offset) {
return new NodeSpec<Label>(text, offset);
}
}

#pragma warning disable CS8602
public class NodeWrapper : Node<Label> {
private TreeNode? node;

Check warning on line 105 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 7.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 105 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 3.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 105 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 3.1.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 105 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 2.1.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 105 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 5.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 105 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 2.2.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 105 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 6.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

public NodeWrapper(TreeNode? node) {

Check warning on line 107 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 7.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 107 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 3.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 107 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 3.1.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 107 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 2.1.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 107 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 5.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 107 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 2.2.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 107 in test/cs/ChoicesTest.cs

View workflow job for this annotation

GitHub Actions / 6.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
this.node = node;
}

public String text() {
return node.text;
}

public int offset() {
return node.offset;
}

public List<Node<Label>> elements() {
List<Node<Label>> ret = new List<Node<Label>>();
foreach (var item in node.elements) {
ret.Add(new NodeWrapper(item));
}
return ret;
}

public Node<Label> get(Label key) {
return new NodeWrapper(node.get(key));
}

public void toMatch(NodeSpec<Label> spec) {
spec.assertMatches(this);
}
}
#pragma warning restore CS8602
}
Loading

0 comments on commit 134e50e

Please sign in to comment.