mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 14:58:10 -05:00
Import code from previous AssetBuilder version
This commit is contained in:
@ -0,0 +1,190 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using ZoneCodeGenerator.Parsing;
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
using ZoneCodeGenerator.Parsing.Matching.Matchers;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
[TestClass]
|
||||
public class MatcherArrayTest
|
||||
{
|
||||
private Mock<ILexer> lexerMock;
|
||||
private int tokenOffset;
|
||||
private List<string> tokens;
|
||||
private MatchingContext matchingContext;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
tokenOffset = 0;
|
||||
tokens = new List<string>();
|
||||
|
||||
lexerMock = new Mock<ILexer>();
|
||||
|
||||
lexerMock.Setup(lexer => lexer.PeekToken(It.IsAny<int>()))
|
||||
.Returns((int index) => tokens.ElementAtOrDefault(index + tokenOffset));
|
||||
lexerMock.Setup(lexer => lexer.NextToken())
|
||||
.Returns(() => tokens.ElementAtOrDefault(tokenOffset++));
|
||||
lexerMock.Setup(lexer => lexer.SkipTokens(It.IsAny<int>()))
|
||||
.Callback((int count) => tokenOffset += count);
|
||||
|
||||
matchingContext = new MatchingContext(lexerMock.Object, new Dictionary<string, TokenMatcher>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDecimalArraysAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"[", "15", "]"
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureHexArraysAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"[", "0x20", "]"
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureEnumValueArraysAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"[", "ENUM_VALUE", "]"
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureFirstSquareBracketIsRequiredToBeRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"13", "]"
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureSecondSquareBracketIsRequiredToBeRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"[", "13"
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureSquareBracketsAreRequiredToBeRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"13"
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureSquareBracketsNeedToBeInTheCorrectOrder()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"]", "13", "["
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNamedOutputIsDecimalNumberForDecimalInput()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"[", "13", "]"
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray().WithName("array_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("13", result["array_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNamedOutputIsDecimalNumberForHexInput()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"[", "0x133", "]"
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray().WithName("array_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("307", result["array_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNamedOutputIsStringForEnumValueInput()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"[", "HELLO_WORLD", "]"
|
||||
});
|
||||
|
||||
var matcher = new MatcherArray().WithName("array_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("HELLO_WORLD", result["array_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using ZoneCodeGenerator.Parsing;
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
using ZoneCodeGenerator.Parsing.Matching.Matchers;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
[TestClass]
|
||||
public class MatcherGroupAndTest
|
||||
{
|
||||
private Mock<ILexer> lexerMock;
|
||||
private MatchingContext matchingContext;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
lexerMock = new Mock<ILexer>();
|
||||
matchingContext = new MatchingContext(lexerMock.Object, new Dictionary<string, TokenMatcher>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureAllTestsSuccessfulMakesGroupSuccessful()
|
||||
{
|
||||
var groupAnd = new MatcherGroupAnd
|
||||
(
|
||||
new TestMatcher(true, 1),
|
||||
new TestMatcher(true, 1),
|
||||
new TestMatcher(true, 1),
|
||||
new TestMatcher(true, 1)
|
||||
);
|
||||
|
||||
var result = groupAnd.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(4, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureLastTestFailingLetsGroupFail()
|
||||
{
|
||||
var groupAnd = new MatcherGroupAnd
|
||||
(
|
||||
new TestMatcher(true, 4),
|
||||
new TestMatcher(true, 2),
|
||||
new TestMatcher(true, 1),
|
||||
new TestMatcher(false, 0)
|
||||
);
|
||||
|
||||
var result = groupAnd.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureFirstTestFailingLetsGroupFail()
|
||||
{
|
||||
var groupAnd = new MatcherGroupAnd
|
||||
(
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(true, 1),
|
||||
new TestMatcher(true, 1),
|
||||
new TestMatcher(true, 1)
|
||||
);
|
||||
|
||||
var result = groupAnd.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureTestsAreCalledWithCorrectTokenOffsets()
|
||||
{
|
||||
var testMatchers = new[]
|
||||
{
|
||||
new TestMatcher(true, 4),
|
||||
new TestMatcher(true, 2),
|
||||
new TestMatcher(true, 1),
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(true, 1)
|
||||
};
|
||||
var iTokenMatcherArray = (TokenMatcher[])testMatchers.Clone();
|
||||
var groupAnd = new MatcherGroupAnd(iTokenMatcherArray);
|
||||
|
||||
var result = groupAnd.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
|
||||
Assert.IsTrue(testMatchers[0].WasTested);
|
||||
Assert.AreEqual(0, testMatchers[0].TestTokenOffset);
|
||||
|
||||
Assert.IsTrue(testMatchers[1].WasTested);
|
||||
Assert.AreEqual(4, testMatchers[1].TestTokenOffset);
|
||||
|
||||
Assert.IsTrue(testMatchers[2].WasTested);
|
||||
Assert.AreEqual(6, testMatchers[2].TestTokenOffset);
|
||||
|
||||
Assert.IsTrue(testMatchers[3].WasTested);
|
||||
Assert.AreEqual(7, testMatchers[3].TestTokenOffset);
|
||||
|
||||
Assert.IsFalse(testMatchers[4].WasTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureGroupWithNoTestsThrowsException()
|
||||
{
|
||||
MatcherGroupAnd matcherGroup = null;
|
||||
|
||||
Assert.ThrowsException<ArgumentException>(
|
||||
() => matcherGroup = new MatcherGroupAnd()
|
||||
);
|
||||
|
||||
Assert.IsNull(matcherGroup);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,186 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using ZoneCodeGenerator.Parsing;
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
using ZoneCodeGenerator.Parsing.Matching.Matchers;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
[TestClass]
|
||||
public class MatcherGroupLoopTest
|
||||
{
|
||||
private const string LoopToken = "loop_token";
|
||||
|
||||
private int testsUntilUnsuccessful;
|
||||
private int timesTested;
|
||||
private Mock<ILexer> lexerMock;
|
||||
private Mock<TokenMatcher> tokenMatcherMock;
|
||||
private MatchingContext matchingContext;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
timesTested = 0;
|
||||
testsUntilUnsuccessful = 0;
|
||||
lexerMock = new Mock<ILexer>();
|
||||
tokenMatcherMock = new Mock<TokenMatcher>();
|
||||
|
||||
tokenMatcherMock.Setup(matcher => matcher.Test(It.IsAny<MatchingContext>(), It.IsAny<int>()))
|
||||
.Returns(() =>
|
||||
{
|
||||
timesTested++;
|
||||
|
||||
if(testsUntilUnsuccessful == 0)
|
||||
return new TokenMatchingResult(false, 0);
|
||||
|
||||
testsUntilUnsuccessful--;
|
||||
|
||||
var matchResult = new TokenMatchingResult(true, 1);
|
||||
|
||||
matchResult.AddNamedMatch(LoopToken, "test");
|
||||
|
||||
return matchResult;
|
||||
});
|
||||
|
||||
matchingContext = new MatchingContext(lexerMock.Object, new Dictionary<string, TokenMatcher>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureZeroOneMultipleAcceptsNoSuccessfulTests()
|
||||
{
|
||||
testsUntilUnsuccessful = 0;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(1, timesTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureZeroOneMultipleAcceptsOneSuccessfulTest()
|
||||
{
|
||||
testsUntilUnsuccessful = 1;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(2, timesTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureZeroOneMultipleAcceptsMultipleSuccessfulTest()
|
||||
{
|
||||
testsUntilUnsuccessful = 5;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(5, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(6, timesTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureOneMultipleRejectsNoSuccessfulTests()
|
||||
{
|
||||
testsUntilUnsuccessful = 0;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(1, timesTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureOneMultipleAcceptsOneSuccessfulTest()
|
||||
{
|
||||
testsUntilUnsuccessful = 1;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(2, timesTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureOneMultipleAcceptsMultipleSuccessfulTests()
|
||||
{
|
||||
testsUntilUnsuccessful = 3;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(4, timesTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureMultipleRejectsNoSuccessfulTests()
|
||||
{
|
||||
testsUntilUnsuccessful = 0;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.Multiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(1, timesTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureMultipleRejectsOneSuccessfulTest()
|
||||
{
|
||||
testsUntilUnsuccessful = 1;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.Multiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(2, timesTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureMultipleAcceptsMultipleSuccessfulTests()
|
||||
{
|
||||
testsUntilUnsuccessful = 2;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.Multiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(2, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(3, timesTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureAllNamedMatchesAreIncluded()
|
||||
{
|
||||
testsUntilUnsuccessful = 10;
|
||||
var matcherGroup = new MatcherGroupLoop(MatcherGroupLoop.LoopMode.Multiple, tokenMatcherMock.Object);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(10, result.ConsumedTokenCount);
|
||||
Assert.AreEqual(11, timesTested);
|
||||
|
||||
var namedMatches = result[LoopToken];
|
||||
|
||||
Assert.AreEqual(10, namedMatches.Count);
|
||||
|
||||
foreach(var namedMatch in namedMatches)
|
||||
Assert.AreEqual("test", namedMatch);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using ZoneCodeGenerator.Parsing;
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
using ZoneCodeGenerator.Parsing.Matching.Matchers;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
[TestClass]
|
||||
public class MatcherGroupOptionalTest
|
||||
{
|
||||
private Mock<ILexer> lexerMock;
|
||||
private MatchingContext matchingContext;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
lexerMock = new Mock<ILexer>();
|
||||
matchingContext = new MatchingContext(lexerMock.Object, new Dictionary<string, TokenMatcher>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureIsSuccessfulWhenMatcherIsSuccessful()
|
||||
{
|
||||
var matcherGroup = new MatcherGroupOptional(new TestMatcher(true, 5));
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(5, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureIsSuccessfulWhenMatcherIsUnsuccessful()
|
||||
{
|
||||
var matcherGroup = new MatcherGroupOptional(new TestMatcher(false, 0));
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using ZoneCodeGenerator.Parsing;
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
using ZoneCodeGenerator.Parsing.Matching.Matchers;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
[TestClass]
|
||||
public class MatcherGroupOrTest
|
||||
{
|
||||
private Mock<ILexer> lexerMock;
|
||||
private MatchingContext matchingContext;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
lexerMock = new Mock<ILexer>();
|
||||
matchingContext = new MatchingContext(lexerMock.Object, new Dictionary<string, TokenMatcher>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureAnyTestSuccessfulMakesGroupSuccessful()
|
||||
{
|
||||
var matcherGroup = new MatcherGroupOr
|
||||
(
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(true, 3),
|
||||
new TestMatcher(false, 0)
|
||||
);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(3, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureLastTestSuccessfulMakesGroupSuccessful()
|
||||
{
|
||||
var matcherGroup = new MatcherGroupOr
|
||||
(
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(true, 4)
|
||||
);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(4, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureAllTestsSuccessfulMakesGroupSuccessful()
|
||||
{
|
||||
var matcherGroup = new MatcherGroupOr
|
||||
(
|
||||
new TestMatcher(true, 1),
|
||||
new TestMatcher(true, 2),
|
||||
new TestMatcher(true, 3),
|
||||
new TestMatcher(true, 4)
|
||||
);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNoTestsSuccessfulMakesGroupUnsuccessful()
|
||||
{
|
||||
var matcherGroup = new MatcherGroupOr
|
||||
(
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(false, 0)
|
||||
);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureTestsAreCalledWithCorrectTokenOffsets()
|
||||
{
|
||||
var testMatchers = new[]
|
||||
{
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(false, 0),
|
||||
new TestMatcher(true, 4),
|
||||
new TestMatcher(false, 0),
|
||||
};
|
||||
var iTokenMatcherArray = (TokenMatcher[])testMatchers.Clone();
|
||||
var matcherGroup = new MatcherGroupOr(iTokenMatcherArray);
|
||||
|
||||
var result = matcherGroup.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(4, result.ConsumedTokenCount);
|
||||
|
||||
Assert.IsTrue(testMatchers[0].WasTested);
|
||||
Assert.AreEqual(0, testMatchers[0].TestTokenOffset);
|
||||
|
||||
Assert.IsTrue(testMatchers[1].WasTested);
|
||||
Assert.AreEqual(0, testMatchers[1].TestTokenOffset);
|
||||
|
||||
Assert.IsTrue(testMatchers[2].WasTested);
|
||||
Assert.AreEqual(0, testMatchers[2].TestTokenOffset);
|
||||
|
||||
Assert.IsTrue(testMatchers[3].WasTested);
|
||||
Assert.AreEqual(0, testMatchers[3].TestTokenOffset);
|
||||
|
||||
Assert.IsFalse(testMatchers[4].WasTested);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureGroupWithNoTestsThrowsException()
|
||||
{
|
||||
MatcherGroupOr matcherGroup = null;
|
||||
|
||||
Assert.ThrowsException<ArgumentException>(
|
||||
() => matcherGroup = new MatcherGroupOr()
|
||||
);
|
||||
|
||||
Assert.IsNull(matcherGroup);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using ZoneCodeGenerator.Parsing;
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
using ZoneCodeGenerator.Parsing.Matching.Matchers;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
[TestClass]
|
||||
public class MatcherLiteralTest
|
||||
{
|
||||
private Mock<ILexer> lexerMock;
|
||||
private int tokenOffset;
|
||||
private List<string> tokens;
|
||||
|
||||
private MatchingContext matchingContext;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
tokenOffset = 0;
|
||||
tokens = new List<string>();
|
||||
|
||||
lexerMock = new Mock<ILexer>();
|
||||
|
||||
lexerMock.Setup(lexer => lexer.PeekToken(It.IsAny<int>()))
|
||||
.Returns((int index) => tokens.ElementAtOrDefault(index + tokenOffset));
|
||||
lexerMock.Setup(lexer => lexer.NextToken())
|
||||
.Returns(() => tokens.ElementAtOrDefault(tokenOffset++));
|
||||
lexerMock.Setup(lexer => lexer.SkipTokens(It.IsAny<int>()))
|
||||
.Callback((int count) => tokenOffset += count);
|
||||
|
||||
matchingContext = new MatchingContext(lexerMock.Object, new Dictionary<string, TokenMatcher>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureMatchingSameStringReturnsSuccessful()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"const"
|
||||
});
|
||||
|
||||
var matcher = new MatcherLiteral("const");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureMatchingDifferentStringReturnsUnsuccessful()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"long"
|
||||
});
|
||||
|
||||
var matcher = new MatcherLiteral("const");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureMatchingIsCaseSensitive()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"CONST"
|
||||
});
|
||||
|
||||
var matcher = new MatcherLiteral("const");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNamedMatchIsLiteralItself()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"const"
|
||||
});
|
||||
|
||||
var matcher = new MatcherLiteral("const").WithName("test_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("const", result["test_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,159 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using ZoneCodeGenerator.Parsing;
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
using ZoneCodeGenerator.Parsing.Matching.Matchers;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
[TestClass]
|
||||
public class MatcherNameTest
|
||||
{
|
||||
private Mock<ILexer> lexerMock;
|
||||
private int tokenOffset;
|
||||
private List<string> tokens;
|
||||
|
||||
private MatchingContext matchingContext;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
tokenOffset = 0;
|
||||
tokens = new List<string>();
|
||||
|
||||
lexerMock = new Mock<ILexer>();
|
||||
|
||||
lexerMock.Setup(lexer => lexer.PeekToken(It.IsAny<int>()))
|
||||
.Returns((int index) => tokens.ElementAtOrDefault(index + tokenOffset));
|
||||
lexerMock.Setup(lexer => lexer.NextToken())
|
||||
.Returns(() => tokens.ElementAtOrDefault(tokenOffset++));
|
||||
lexerMock.Setup(lexer => lexer.SkipTokens(It.IsAny<int>()))
|
||||
.Callback((int count) => tokenOffset += count);
|
||||
|
||||
matchingContext = new MatchingContext(lexerMock.Object, new Dictionary<string, TokenMatcher>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureRecognizesNormalName()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"variable_name"
|
||||
});
|
||||
|
||||
var matcher = new MatcherName();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureRecognizesUnderscoreAtTheBeginning()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"_variable_name"
|
||||
});
|
||||
|
||||
var matcher = new MatcherName();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureAcceptsDigit()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"v4ri4bl3_n4m3"
|
||||
});
|
||||
|
||||
var matcher = new MatcherName();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDoesNotAcceptDigitAtTheBeginning()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"5variable_name"
|
||||
});
|
||||
|
||||
var matcher = new MatcherName();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDoesNotAcceptCKeyword()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"float"
|
||||
});
|
||||
|
||||
var matcher = new MatcherName();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDoesNotAcceptSymbols()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"%$§"
|
||||
});
|
||||
|
||||
var matcher = new MatcherName();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDoesNotAcceptEmptyString()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
""
|
||||
});
|
||||
|
||||
var matcher = new MatcherName();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNamedOutputIsMatchedName()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"variable_n4me"
|
||||
});
|
||||
|
||||
var matcher = new MatcherName().WithName("name_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("variable_n4me", result["name_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,160 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using ZoneCodeGenerator.Parsing;
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
using ZoneCodeGenerator.Parsing.Matching.Matchers;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
[TestClass]
|
||||
public class MatcherNumberTest
|
||||
{
|
||||
private Mock<ILexer> lexerMock;
|
||||
private int tokenOffset;
|
||||
private List<string> tokens;
|
||||
|
||||
private MatchingContext matchingContext;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
tokenOffset = 0;
|
||||
tokens = new List<string>();
|
||||
|
||||
lexerMock = new Mock<ILexer>();
|
||||
|
||||
lexerMock.Setup(lexer => lexer.PeekToken(It.IsAny<int>()))
|
||||
.Returns((int index) => tokens.ElementAtOrDefault(index + tokenOffset));
|
||||
lexerMock.Setup(lexer => lexer.NextToken())
|
||||
.Returns(() => tokens.ElementAtOrDefault(tokenOffset++));
|
||||
lexerMock.Setup(lexer => lexer.SkipTokens(It.IsAny<int>()))
|
||||
.Callback((int count) => tokenOffset += count);
|
||||
|
||||
matchingContext = new MatchingContext(lexerMock.Object, new Dictionary<string, TokenMatcher>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDecimalNumbersAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"1337"
|
||||
});
|
||||
|
||||
var matcher = new MatcherNumber();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureHexNumbersAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"0x1A4"
|
||||
});
|
||||
|
||||
var matcher = new MatcherNumber();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureHexNumbersWithInvalidLettersAreInvalid()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"0xAFZF"
|
||||
});
|
||||
|
||||
var matcher = new MatcherNumber();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureWordsAreInvalid()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"Hello"
|
||||
});
|
||||
|
||||
var matcher = new MatcherNumber();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureSymbolsAreInvalid()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"%"
|
||||
});
|
||||
|
||||
var matcher = new MatcherNumber();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureJustHexPrefixIsInvalid()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"0x"
|
||||
});
|
||||
|
||||
var matcher = new MatcherNumber();
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNamedOutputIsDecimalNumberForDecimalInput()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"420"
|
||||
});
|
||||
|
||||
var matcher = new MatcherNumber().WithName("number_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("420", result["number_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNamedOutputIsDecimalNumberForHexInput()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"0x1a5"
|
||||
});
|
||||
|
||||
var matcher = new MatcherNumber().WithName("number_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("421", result["number_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,278 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using ZoneCodeGenerator.Parsing;
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
using ZoneCodeGenerator.Parsing.Matching.Matchers;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
[TestClass]
|
||||
public class MatcherTypenameTest
|
||||
{
|
||||
private Mock<ILexer> lexerMock;
|
||||
private int tokenOffset;
|
||||
private List<string> tokens;
|
||||
|
||||
private MatchingContext matchingContext;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
tokenOffset = 0;
|
||||
tokens = new List<string>();
|
||||
|
||||
lexerMock = new Mock<ILexer>();
|
||||
|
||||
lexerMock.Setup(lexer => lexer.PeekToken(It.IsAny<int>()))
|
||||
.Returns((int index) => tokens.ElementAtOrDefault(index + tokenOffset));
|
||||
lexerMock.Setup(lexer => lexer.NextToken())
|
||||
.Returns(() => tokens.ElementAtOrDefault(tokenOffset++));
|
||||
lexerMock.Setup(lexer => lexer.SkipTokens(It.IsAny<int>()))
|
||||
.Callback((int count) => tokenOffset += count);
|
||||
|
||||
matchingContext = new MatchingContext(lexerMock.Object, new Dictionary<string, TokenMatcher>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureBaseTypeNamesAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"int"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("int", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNormalTypeNamesAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"GfxWorld"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("GfxWorld", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureUnsignedTypeNamesAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"unsigned", "int"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(2, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("unsigned int", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureSignedTypeNamesAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"signed", "int"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(2, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("signed int", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureConstTypeNamesAreRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"const", "int"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(2, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("const int", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDoubleLongIsRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"long", "long"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(2, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("long long", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDoubleLongWithPrefixesIsRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"const", "unsigned", "long", "long"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(4, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("const unsigned long long", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDoubleIntIsNotRecognized()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"int", "int"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("int", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureNamespacesAreAllowed()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"std", ":", ":", "string"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(4, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("std::string", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureAcceptsTypeNamesWithNumbersAndUnderscores()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"std", ":", ":", "int32_t"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(4, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("std::int32_t", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureShortNamespacesAreAllowed()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"a", ":", ":", "b"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(4, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("a::b", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureDoubleColonPrefixIsNotAllowed()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
":", ":", "string"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
Assert.That.IsZero(result["type_token"].Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureSingleDoubleColonNamespacesDoNotMatch()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"std", ":", "string"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsTrue(result.Successful);
|
||||
Assert.AreEqual(1, result.ConsumedTokenCount);
|
||||
Assert.AreEqual("std", result["type_token"].ElementAtOrDefault(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureSymbolsAreNotAllowed()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"%"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
Assert.That.IsZero(result["type_token"].Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnsureSymbolsWithUnsignedAreNotAllowed()
|
||||
{
|
||||
tokens.AddRange(new List<string>
|
||||
{
|
||||
"unsigned", "%"
|
||||
});
|
||||
|
||||
var matcher = new MatcherTypename().WithName("type_token");
|
||||
var result = matcher.Test(matchingContext, 0);
|
||||
|
||||
Assert.IsFalse(result.Successful);
|
||||
Assert.AreEqual(0, result.ConsumedTokenCount);
|
||||
Assert.That.IsZero(result["type_token"].Count);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
using ZoneCodeGenerator.Parsing.Matching;
|
||||
|
||||
namespace ZoneCodeGeneratorTests.Parsing.Matching.Matchers
|
||||
{
|
||||
class TestMatcher : TokenMatcher
|
||||
{
|
||||
private readonly int tokenCount;
|
||||
private readonly string tokenName;
|
||||
|
||||
public bool Successful { get; set; }
|
||||
public int TestTokenOffset { get; private set; }
|
||||
public bool WasTested { get; private set; }
|
||||
|
||||
public TestMatcher(bool successful, int tokenCount, string tokenName = "")
|
||||
{
|
||||
Successful = successful;
|
||||
this.tokenCount = tokenCount;
|
||||
this.tokenName = tokenName;
|
||||
TestTokenOffset = 0;
|
||||
WasTested = false;
|
||||
}
|
||||
|
||||
protected override string GetIdentifier()
|
||||
{
|
||||
return "TestMatcher";
|
||||
}
|
||||
|
||||
protected override TokenMatchingResult PerformTest(MatchingContext context, int tokenOffset)
|
||||
{
|
||||
var result = new TokenMatchingResult(Successful, Successful ? tokenCount : 0);
|
||||
|
||||
if(!string.IsNullOrEmpty(tokenName))
|
||||
result.AddNamedMatch(tokenName, "test");
|
||||
|
||||
TestTokenOffset = tokenOffset;
|
||||
WasTested = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user