mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 00:02:55 +00:00
ZoneCodeGenerator: Add tests for Matchers to test TokenOffset and Tag behaviour
This commit is contained in:
parent
d5ecaa186d
commit
117ba118af
@ -186,5 +186,53 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
Assert.AreEqual(3, result.ConsumedTokenCount);
|
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||||
Assert.AreEqual("HELLO_WORLD", result.NamedMatches["array_token"].ElementAtOrDefault(0));
|
Assert.AreEqual("HELLO_WORLD", result.NamedMatches["array_token"].ElementAtOrDefault(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMakesCorrectUseOfTokenOffset()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"random", "string", "[", "5", "]", ";"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherArray();
|
||||||
|
var result = matcher.Test(matchingContext, 2);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureAddsTagWhenMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"[", "42", "]", "randomString"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherArray().WithTag("asdf");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(1, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("asdf", result.MatchedTags[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureDoesNotAddTagWhenNotMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"nope", "[", "42", "]", "randomString"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherArray().WithTag("asdf");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(0, result.MatchedTags.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,5 +117,61 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
|
|
||||||
Assert.IsNull(matcherGroup);
|
Assert.IsNull(matcherGroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMakesCorrectUseOfTokenOffset()
|
||||||
|
{
|
||||||
|
var testMatchers = new[]
|
||||||
|
{
|
||||||
|
new TestMatcher(true, 3),
|
||||||
|
new TestMatcher(true, 1)
|
||||||
|
};
|
||||||
|
var iTokenMatcherArray = (TokenMatcher[])testMatchers.Clone();
|
||||||
|
var groupAnd = new MatcherGroupAnd(iTokenMatcherArray);
|
||||||
|
|
||||||
|
var result = groupAnd.Test(matchingContext, 5);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
|
||||||
|
Assert.AreEqual(5, testMatchers[0].TestTokenOffset);
|
||||||
|
Assert.AreEqual(8, testMatchers[1].TestTokenOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureAddsTagWhenMatched()
|
||||||
|
{
|
||||||
|
var testMatchers = new[]
|
||||||
|
{
|
||||||
|
new TestMatcher(true, 3).WithTag("testTagTheSecond"),
|
||||||
|
new TestMatcher(true, 1).WithTag("testTagTheThird")
|
||||||
|
};
|
||||||
|
var iTokenMatcherArray = (TokenMatcher[])testMatchers.Clone();
|
||||||
|
var groupAnd = new MatcherGroupAnd(iTokenMatcherArray).WithTag("testTagTheFirst");
|
||||||
|
|
||||||
|
var result = groupAnd.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(3, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("testTagTheFirst", result.MatchedTags[0]);
|
||||||
|
Assert.AreEqual("testTagTheSecond", result.MatchedTags[1]);
|
||||||
|
Assert.AreEqual("testTagTheThird", result.MatchedTags[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureDoesNotAddTagWhenNotMatched()
|
||||||
|
{
|
||||||
|
var testMatchers = new[]
|
||||||
|
{
|
||||||
|
new TestMatcher(true, 3).WithTag("testTagTheSecond"),
|
||||||
|
new TestMatcher(false, 1).WithTag("testTagTheThird")
|
||||||
|
};
|
||||||
|
var iTokenMatcherArray = (TokenMatcher[])testMatchers.Clone();
|
||||||
|
var groupAnd = new MatcherGroupAnd(iTokenMatcherArray).WithTag("testTagTheFirst");
|
||||||
|
|
||||||
|
var result = groupAnd.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.MatchedTags.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,7 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
var matchResult = new TokenMatchingResult(true, 1);
|
var matchResult = new TokenMatchingResult(true, 1);
|
||||||
|
|
||||||
matchResult.AddNamedMatch(LoopToken, "test");
|
matchResult.AddNamedMatch(LoopToken, "test");
|
||||||
|
matchResult.AppendTag("testTag");
|
||||||
|
|
||||||
return matchResult;
|
return matchResult;
|
||||||
});
|
});
|
||||||
@ -182,5 +183,50 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
foreach(var namedMatch in namedMatches)
|
foreach(var namedMatch in namedMatches)
|
||||||
Assert.AreEqual("test", namedMatch);
|
Assert.AreEqual("test", namedMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMakesCorrectUseOfTokenOffset()
|
||||||
|
{
|
||||||
|
testsUntilUnsuccessful = 2;
|
||||||
|
var groupLoop = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple, tokenMatcherMock.Object);
|
||||||
|
|
||||||
|
var result = groupLoop.Test(matchingContext, 4);
|
||||||
|
|
||||||
|
tokenMatcherMock.Verify(matcher => matcher.Test(It.IsAny<MatchingContext>(), 4));
|
||||||
|
tokenMatcherMock.Verify(matcher => matcher.Test(It.IsAny<MatchingContext>(), 5));
|
||||||
|
tokenMatcherMock.Verify(matcher => matcher.Test(It.IsAny<MatchingContext>(), 6));
|
||||||
|
tokenMatcherMock.VerifyNoOtherCalls();
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureAddsTagWhenMatched()
|
||||||
|
{
|
||||||
|
testsUntilUnsuccessful = 4;
|
||||||
|
var groupLoop = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple, tokenMatcherMock.Object).WithTag("loopTag");
|
||||||
|
|
||||||
|
var result = groupLoop.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(5, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("loopTag", result.MatchedTags[0]);
|
||||||
|
Assert.AreEqual("testTag", result.MatchedTags[1]);
|
||||||
|
Assert.AreEqual("testTag", result.MatchedTags[2]);
|
||||||
|
Assert.AreEqual("testTag", result.MatchedTags[3]);
|
||||||
|
Assert.AreEqual("testTag", result.MatchedTags[4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureDoesNotAddTagWhenNotMatched()
|
||||||
|
{
|
||||||
|
testsUntilUnsuccessful = 1;
|
||||||
|
var groupLoop = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.Multiple, tokenMatcherMock.Object).WithTag("loopTag");
|
||||||
|
|
||||||
|
var result = groupLoop.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.MatchedTags.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,5 +41,45 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
Assert.IsTrue(result.Successful);
|
Assert.IsTrue(result.Successful);
|
||||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMakesCorrectUseOfTokenOffset()
|
||||||
|
{
|
||||||
|
var testMatcher = new TestMatcher(true, 1);
|
||||||
|
var groupOptional = new MatcherGroupOptional(testMatcher);
|
||||||
|
|
||||||
|
var result = groupOptional.Test(matchingContext, 7);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
|
||||||
|
Assert.AreEqual(7, testMatcher.TestTokenOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureAddsTagWhenMatched()
|
||||||
|
{
|
||||||
|
var testMatcher = new TestMatcher(true, 1).WithTag("testTag");
|
||||||
|
var groupOptional = new MatcherGroupOptional(testMatcher).WithTag("optionalTag");
|
||||||
|
|
||||||
|
var result = groupOptional.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(2, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("optionalTag", result.MatchedTags[0]);
|
||||||
|
Assert.AreEqual("testTag", result.MatchedTags[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureDoesStillAddItsOwnTagWhenMatcherDoesNotMatch()
|
||||||
|
{
|
||||||
|
var testMatcher = new TestMatcher(false, 0).WithTag("testTag");
|
||||||
|
var groupOptional = new MatcherGroupOptional(testMatcher).WithTag("optionalTag");
|
||||||
|
|
||||||
|
var result = groupOptional.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(1, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("optionalTag", result.MatchedTags[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,5 +133,60 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
|
|
||||||
Assert.IsNull(matcherGroup);
|
Assert.IsNull(matcherGroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMakesCorrectUseOfTokenOffset()
|
||||||
|
{
|
||||||
|
var testMatchers = new[]
|
||||||
|
{
|
||||||
|
new TestMatcher(true, 3),
|
||||||
|
new TestMatcher(false, 1)
|
||||||
|
};
|
||||||
|
var iTokenMatcherArray = (TokenMatcher[])testMatchers.Clone();
|
||||||
|
var groupOr = new MatcherGroupOr(iTokenMatcherArray);
|
||||||
|
|
||||||
|
var result = groupOr.Test(matchingContext, 5);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
|
||||||
|
Assert.AreEqual(5, testMatchers[0].TestTokenOffset);
|
||||||
|
Assert.IsFalse(testMatchers[1].WasTested);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureAddsTagWhenMatched()
|
||||||
|
{
|
||||||
|
var testMatchers = new[]
|
||||||
|
{
|
||||||
|
new TestMatcher(false, 3).WithTag("testTagTheSecond"),
|
||||||
|
new TestMatcher(true, 1).WithTag("testTagTheThird")
|
||||||
|
};
|
||||||
|
var iTokenMatcherArray = (TokenMatcher[])testMatchers.Clone();
|
||||||
|
var groupOr = new MatcherGroupOr(iTokenMatcherArray).WithTag("testTagTheFirst");
|
||||||
|
|
||||||
|
var result = groupOr.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(2, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("testTagTheFirst", result.MatchedTags[0]);
|
||||||
|
Assert.AreEqual("testTagTheThird", result.MatchedTags[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureDoesNotAddTagWhenNotMatched()
|
||||||
|
{
|
||||||
|
var testMatchers = new[]
|
||||||
|
{
|
||||||
|
new TestMatcher(false, 3).WithTag("testTagTheSecond"),
|
||||||
|
new TestMatcher(false, 1).WithTag("testTagTheThird")
|
||||||
|
};
|
||||||
|
var iTokenMatcherArray = (TokenMatcher[])testMatchers.Clone();
|
||||||
|
var groupOr = new MatcherGroupOr(iTokenMatcherArray).WithTag("testTagTheFirst");
|
||||||
|
|
||||||
|
var result = groupOr.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.MatchedTags.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,5 +95,98 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||||
Assert.AreEqual("const", result.NamedMatches["test_token"].ElementAtOrDefault(0));
|
Assert.AreEqual("const", result.NamedMatches["test_token"].ElementAtOrDefault(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMatcherWithMultipleLiteralTokensMatchesWhenAllAreFound()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"const", "range", "of", "turtles"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherLiteral("const", "range");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(2, result.ConsumedTokenCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMatcherWithMultipleLiteralTokensDoesNotMatchWhenFirstTokenIsDifferent()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"static", "range", "of", "turtles"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherLiteral("const", "range");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMatcherWithMultipleLiteralTokensDoesNotMatchWhenSecondTokenIsDifferent()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"const", "element", "of", "turtles"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherLiteral("const", "const");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMakesCorrectUseOfTokenOffset()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"strawberry", "cake", "is", "yummy"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherLiteral("cake");
|
||||||
|
var result = matcher.Test(matchingContext, 1);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureAddsTagWhenMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"strawberry"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherLiteral("strawberry").WithTag("strawberryMatcher");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(1, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("strawberryMatcher", result.MatchedTags[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureDoesNotAddTagWhenNotMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"apple"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherLiteral("strawberry").WithTag("strawberryMatcher");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(0, result.MatchedTags.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -155,5 +155,54 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||||
Assert.AreEqual("variable_n4me", result.NamedMatches["name_token"].ElementAtOrDefault(0));
|
Assert.AreEqual("variable_n4me", result.NamedMatches["name_token"].ElementAtOrDefault(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMakesCorrectUseOfTokenOffset()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"static", "int", "variable_n4me", "=", "5", ";"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherName().WithName("name_token");
|
||||||
|
var result = matcher.Test(matchingContext, 2);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual("variable_n4me", result.NamedMatches["name_token"].ElementAtOrDefault(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureAddsTagWhenMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"variable_n4me"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherName().WithTag("very_cool_matcher");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(1, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("very_cool_matcher", result.MatchedTags[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureDoesNotAddTagWhenNotMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"1337"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherName().WithTag("very_cool_matcher");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(0, result.MatchedTags.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,5 +156,53 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||||
Assert.AreEqual("421", result.NamedMatches["number_token"].ElementAtOrDefault(0));
|
Assert.AreEqual("421", result.NamedMatches["number_token"].ElementAtOrDefault(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMakesCorrectUseOfTokenOffset()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"int", "kek", "=", "1337", ";"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherNumber();
|
||||||
|
var result = matcher.Test(matchingContext, 3);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureAddsTagWhenMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"1337"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherNumber().WithTag("Taggerino");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(1, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("Taggerino", result.MatchedTags[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureDoesNotAddTagWhenNotMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"not_a_number_yo"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherNumber().WithTag("Taggerino");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(0, result.MatchedTags.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -274,5 +274,53 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||||
Assert.IsFalse(result.NamedMatches.ContainsKey("type_token"));
|
Assert.IsFalse(result.NamedMatches.ContainsKey("type_token"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureMakesCorrectUseOfTokenOffset()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"5", "4", "3", "2", "std", ":", ":", "string", "randomString", ";"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherTypename();
|
||||||
|
var result = matcher.Test(matchingContext, 4);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(4, result.ConsumedTokenCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureAddsTagWhenMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"std", ":", ":", "something", "test"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherTypename().WithTag("naisu");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsTrue(result.Successful);
|
||||||
|
Assert.AreEqual(4, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(1, result.MatchedTags.Count);
|
||||||
|
Assert.AreEqual("naisu", result.MatchedTags[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void EnsureDoesNotAddTagWhenNotMatched()
|
||||||
|
{
|
||||||
|
tokens.AddRange(new List<string>
|
||||||
|
{
|
||||||
|
"1337", "asdf", ":", ":", "something", "test"
|
||||||
|
});
|
||||||
|
|
||||||
|
var matcher = new MatcherTypename().WithTag("naisu");
|
||||||
|
var result = matcher.Test(matchingContext, 0);
|
||||||
|
|
||||||
|
Assert.IsFalse(result.Successful);
|
||||||
|
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||||
|
Assert.AreEqual(0, result.MatchedTags.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,9 @@ namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
|||||||
{
|
{
|
||||||
var result = new TokenMatchingResult(Successful, Successful ? tokenCount : 0);
|
var result = new TokenMatchingResult(Successful, Successful ? tokenCount : 0);
|
||||||
|
|
||||||
|
if(result.Successful)
|
||||||
|
result.AppendTag(Tag);
|
||||||
|
|
||||||
if(!string.IsNullOrEmpty(tokenName))
|
if(!string.IsNullOrEmpty(tokenName))
|
||||||
result.AddNamedMatch(tokenName, "test");
|
result.AddNamedMatch(tokenName, "test");
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user