public void TestFailingTokenizingEndComment() { const string data = "test */"; MemoryStream stream = null; try { stream = new MemoryStream(Encoding.ASCII.GetBytes(data)); Scanner scanner = new Scanner(stream); stream = null; Tokenizer tokenizer = new Tokenizer(scanner) { CommentDelimiters = new[] { new CommentDelimiters("/*", "*/") } }; Token <TokenType> nextToken = tokenizer.GetNextToken(); Assert.That(nextToken.Value, Is.EqualTo("test")); Assert.That(nextToken.Type, Is.EqualTo(TokenType.SYMBOL)); Assert.That(nextToken.LineNumber, Is.EqualTo(1)); Assert.That(nextToken.PositionInLine, Is.EqualTo(1)); Assert.That(() => nextToken = tokenizer.GetNextToken(), Throws.TypeOf(typeof(UnexpectedCharEncounteredException))); } finally { if (stream != null) { stream.Dispose(); } } }
public void TestFailingTokenizingUnterminatedComment() { const string data = "/* test"; MemoryStream stream = null; try { stream = new MemoryStream(Encoding.ASCII.GetBytes(data)); Scanner scanner = new Scanner(stream); stream = null; Tokenizer tokenizer = new Tokenizer(scanner) { CommentDelimiters = new[] { new CommentDelimiters("/*", "*/") } }; Assert.That(() => { Token <TokenType> nextToken = tokenizer.GetNextToken(); }, Throws.TypeOf(typeof(UnexpectedCharEncounteredException))); } finally { if (stream != null) { stream.Dispose(); } } }
public void TestTokenizingUnterminatedStringLiteral() { const string data = @"""str1"; const char stringLiteralPunctuator = '"'; MemoryStream stream = null; try { stream = new MemoryStream(Encoding.ASCII.GetBytes(data)); Scanner scanner = new Scanner(stream); stream = null; Tokenizer tokenizer = new Tokenizer(scanner) { StringLiteralPunctuator = stringLiteralPunctuator }; Assert.That(() => { Token <TokenType> nextToken = tokenizer.GetNextToken(); }, Throws.TypeOf(typeof(UnexpectedCharEncounteredException))); } finally { if (stream != null) { stream.Dispose(); } } }
public void TestParserThrowsExceptionIfLexerThrowsException() { var m = new Mock <ICLexer>(); m.SetupGet(l => l.HasMoreTokens).Returns(true); m.Setup(l => l.GetNextToken(It.IsAny <bool>())).Throws(new Exception()); var p = new CParser(m.Object); Assert.That(() => p.PerformParse(), Throws.TypeOf(typeof(ParserException))); }
public void TestParsingInvalidFirstToken() { Stack <Token <CTokenType> > tokens = new Stack <Token <CTokenType> >(); var m = SetupMock(tokens); tokens.Push(GenerateToken(CTokenType.TERMINATOR)); var p = new CParser(m.Object); Assert.That(() => p.PerformParse(), Throws.TypeOf(typeof(ParserException))); }
public void TestParsingElseWithNoStartingIf() { Stack <Token <CTokenType> > tokens = new Stack <Token <CTokenType> >(); var m = SetupMock(tokens); tokens.Push(GenerateToken(CTokenType.PP_DIRECTIVE, "else")); tokens.Push(GenerateToken(CTokenType.PP_SYMBOL)); var p = new CParser(m.Object); Assert.That(() => p.PerformParse(), Throws.TypeOf(typeof(ParserException))); }
public void TestParsingUnterminatedPPDefinition() { Stack <Token <CTokenType> > tokens = new Stack <Token <CTokenType> >(); var m = SetupMock(tokens); tokens.Push(GenerateToken(CTokenType.PP_DIRECTIVE, "define")); tokens.Push(GenerateToken(CTokenType.PP_SYMBOL)); var p = new CParser(m.Object); Assert.That(() => p.PerformParse(), Throws.TypeOf(typeof(ParserException))); }
public void TestParsingInvalidPPDirectiveVerb() { Stack <Token <CTokenType> > tokens = new Stack <Token <CTokenType> >(); var m = SetupMock(tokens); tokens.Push(GenerateToken(CTokenType.KEYWORD)); tokens.Push(GenerateToken(CTokenType.PP_SYMBOL)); var p = new CParser(m.Object); Assert.That(() => p.PerformParse(), Throws.TypeOf(typeof(ParserException))); }
public void TestParsingInvalidInclude2() { Stack <Token <CTokenType> > tokens = new Stack <Token <CTokenType> >(); var m = SetupMock(tokens); tokens.Push(GenerateToken(CTokenType.PUNCTUATOR, ".")); tokens.Push(GenerateToken(CTokenType.PP_DIRECTIVE, "include")); tokens.Push(GenerateToken(CTokenType.PP_SYMBOL)); var p = new CParser(m.Object); Assert.That(() => p.PerformParse(), Throws.TypeOf(typeof(ParserException))); }
public void TestParsingInvalidPPFunctionDefinitionWithParameters5() { Stack <Token <CTokenType> > tokens = new Stack <Token <CTokenType> >(); var m = SetupMock(tokens); tokens.Push(GenerateToken(CTokenType.PUNCTUATOR, "(")); tokens.Push(GenerateToken(CTokenType.SYMBOL, "CTokenType")); tokens.Push(GenerateToken(CTokenType.PP_DIRECTIVE, "define")); tokens.Push(GenerateToken(CTokenType.PP_SYMBOL)); var p = new CParser(m.Object); Assert.That(() => p.PerformParse(), Throws.TypeOf(typeof(ParserException))); }
public void TestParsingValidEndif() { Stack <Token <CTokenType> > tokens = new Stack <Token <CTokenType> >(); var m = SetupMock(tokens); tokens.Push(GenerateToken(CTokenType.PP_DIRECTIVE, "endif")); tokens.Push(GenerateToken(CTokenType.PP_SYMBOL, "", 1)); tokens.Push(GenerateToken(CTokenType.NUMERIC_LITERAL, "1")); tokens.Push(GenerateToken(CTokenType.PP_DIRECTIVE, "if")); tokens.Push(GenerateToken(CTokenType.PP_SYMBOL)); var p = new CParser(m.Object); var sf = p.PerformParse(); Assert.That(() => sf.PopIfCond(), Throws.TypeOf(typeof(InvalidOperationException))); }
public void TestTokenizingStringWithUnexpectedCharacters() { const string data = "@"; MemoryStream stream = null; try { stream = new MemoryStream(Encoding.ASCII.GetBytes(data)); Scanner scanner = new Scanner(stream); stream = null; Tokenizer tokenizer = new Tokenizer(scanner); Assert.That(() => { Token <TokenType> nextToken = tokenizer.GetNextToken(); }, Throws.TypeOf(typeof(UnexpectedCharEncounteredException))); } finally { if (stream != null) { stream.Dispose(); } } }
public void TestFailingAtTokenizingSymbolsTerminatedByNonPunctuatorOrWhitespace() { const string data = "symbol'"; MemoryStream stream = null; try { stream = new MemoryStream(Encoding.ASCII.GetBytes(data)); Scanner scanner = new Scanner(stream); stream = null; Tokenizer tokenizer = new Tokenizer(scanner); Assert.That(() => { Token <TokenType> nextToken = tokenizer.GetNextToken(); }, Throws.TypeOf(typeof(UnexpectedCharEncounteredException))); } finally { if (stream != null) { stream.Dispose(); } } }
public void TestCreatingTokenizerWithNullScanner() { Assert.That(() => new Tokenizer(null), Throws.TypeOf(typeof(ArgumentNullException))); }
public void NoneExistentFileThrowsFileNotFoundException() { Assert.That(() => TestContext.AddTestAttachment("NotAFile.txt"), Throws.InstanceOf <FileNotFoundException>()); }
public void TestParserConstructorThrowsIfNullParameter() { Assert.That(() => new CParser(null), Throws.TypeOf(typeof(ArgumentNullException))); }
public void TestFailingToTokenizeInvalidFloatingPointNumber() { Assert.That(() => TestTokenizingNumericLiteral("0.0.0"), Throws.TypeOf(typeof(FormatException))); }
public void TestFailingToTokenizeInvalidScientificNotationNumber2() { Assert.That(() => TestTokenizingNumericLiteral("5ee"), Throws.TypeOf(typeof(FormatException))); }
public void TestFailingToTokenizeInvalidNumericLiteral() { Assert.That(() => TestTokenizingNumericLiteral("5z"), Throws.TypeOf(typeof(UnexpectedCharEncounteredException))); }
public void InvalidFilePathsThrowsArgumentException(string filePath) { Assert.That(() => TestContext.AddTestAttachment(filePath), Throws.InstanceOf <ArgumentException>()); }
public void TestScannerShouldNotBeConstructedWithNullStreamReader() { Assert.That(() => new Scanner(null), Throws.TypeOf(typeof(ArgumentNullException))); }
public void TestConstructorWithNullTokenizerThrowsException() { Assert.That(() => new CLexer(null), Throws.TypeOf(typeof(ArgumentNullException))); }