public void Constructor() { void ConstructorTest(int startIndex, string text, PkgdefTokenType tokenType, Exception expectedException = null) { if (expectedException != null) { AssertEx.Throws(() => new PkgdefToken(startIndex, text, tokenType), expectedException); } else { PkgdefToken token = new PkgdefToken(startIndex, text, tokenType); Assert.AreEqual(startIndex, token.GetStartIndex()); Assert.AreEqual(text, token.GetText()); Assert.AreEqual(tokenType, token.GetTokenType()); Assert.AreEqual(text.Length, token.GetLength()); Assert.AreEqual(startIndex + text.Length, token.GetAfterEndIndex()); Assert.AreEqual(startIndex + text.Length - 1, token.GetEndIndex()); } } ConstructorTest(-1, "hello", PkgdefTokenType.Unrecognized, expectedException: new PreConditionException("startIndex (-1) must be greater than or equal to 0.")); ConstructorTest(0, null, PkgdefTokenType.Unrecognized, expectedException: new PreConditionException("text cannot be null.")); ConstructorTest(0, "", PkgdefTokenType.Unrecognized, expectedException: new PreConditionException("text cannot be empty.")); ConstructorTest(0, "abc", PkgdefTokenType.Unrecognized); }
public void Unrecognized() { PkgdefToken token = PkgdefToken.Unrecognized(1, '&'); Assert.AreEqual(1, token.GetStartIndex()); Assert.AreEqual("&", token.GetText()); Assert.AreEqual(PkgdefTokenType.Unrecognized, token.GetTokenType()); }
public void Whitespace() { PkgdefToken token = PkgdefToken.Whitespace(1, " \t"); Assert.AreEqual(1, token.GetStartIndex()); Assert.AreEqual(" \t", token.GetText()); Assert.AreEqual(PkgdefTokenType.Whitespace, token.GetTokenType()); }
public void Create_WithString() { void CreateTest(string text, PkgdefToken[] expectedTokens = null, PkgdefIssue[] expectedIssues = null, Exception expectedException = null) { List <PkgdefIssue> issues = new List <PkgdefIssue>(); if (expectedException != null) { AssertEx.Throws(() => PkgdefTokenizer.Create(text, issues.Add), expectedException); } else { PkgdefTokenizer tokenizer = PkgdefTokenizer.Create(text, issues.Add); Assert.IsNotNull(tokenizer); Assert.IsFalse(tokenizer.HasStarted()); Assert.IsFalse(tokenizer.HasCurrent()); if (expectedTokens == null) { Assert.AreEqual(0, tokenizer.Count()); } else { CollectionAssert.AreEqual(expectedTokens, tokenizer.ToArray()); } if (expectedIssues == null) { Assert.AreEqual(0, issues.Count()); } else { CollectionAssert.AreEqual(expectedIssues, issues); } Assert.IsTrue(tokenizer.HasStarted()); Assert.IsFalse(tokenizer.HasCurrent()); } } CreateTest( null, expectedException: new PreConditionException("text cannot be null.")); CreateTest( "", new PkgdefToken[0]); CreateTest( "/", new[] { PkgdefToken.ForwardSlash(0) }); CreateTest( "\\", new[] { PkgdefToken.Backslash(0) }); CreateTest( " ", new[] { PkgdefToken.Whitespace(0, " ") }); CreateTest( " ", new[] { PkgdefToken.Whitespace(0, " ") }); CreateTest( "\t", new[] { PkgdefToken.Whitespace(0, "\t") }); CreateTest( "\t\t\t", new[] { PkgdefToken.Whitespace(0, "\t\t\t") }); CreateTest( " \t \t \t ", new[] { PkgdefToken.Whitespace(0, " \t \t \t ") }); CreateTest( "\r", new[] { PkgdefToken.Whitespace(0, "\r") }); CreateTest( " \r ", new[] { PkgdefToken.Whitespace(0, " \r ") }); CreateTest( "\n", new[] { PkgdefToken.NewLine(0, "\n") }); CreateTest( "\r\n", new[] { PkgdefToken.NewLine(0, "\r\n") }); CreateTest( "@", new[] { PkgdefToken.AtSign(0) }); CreateTest( "=", new[] { PkgdefToken.EqualsSign(0) }); CreateTest( "$", new[] { PkgdefToken.DollarSign(0) }); CreateTest( "[", new[] { PkgdefToken.LeftSquareBracket(0) }); CreateTest( "]", new[] { PkgdefToken.RightSquareBracket(0) }); CreateTest( "\"", new[] { PkgdefToken.DoubleQuote(0) }); CreateTest( ":", new[] { PkgdefToken.Colon(0) }); CreateTest( "{", new[] { PkgdefToken.LeftCurlyBracket(0) }); CreateTest( "}", new[] { PkgdefToken.RightCurlyBracket(0) }); CreateTest( "-", new[] { PkgdefToken.Dash(0) }); CreateTest( "a", new[] { PkgdefToken.Letters(0, "a") }); CreateTest( "abc", new[] { PkgdefToken.Letters(0, "abc") }); CreateTest( "1", new[] { PkgdefToken.Digits(0, "1") }); CreateTest( "123", new[] { PkgdefToken.Digits(0, "123") }); // Unrecognized tokens CreateTest( "&", new[] { PkgdefToken.Unrecognized(0, '&') }); CreateTest( "&^%", new[] { PkgdefToken.Unrecognized(0, '&'), PkgdefToken.Unrecognized(1, '^'), PkgdefToken.Unrecognized(2, '%'), }); }