Exemplo n.º 1
0
        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();
                }
            }
        }
Exemplo n.º 2
0
        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();
                }
            }
        }
Exemplo n.º 3
0
        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();
                }
            }
        }
Exemplo n.º 4
0
        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)));
        }
Exemplo n.º 5
0
        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)));
        }
Exemplo n.º 6
0
        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)));
        }
Exemplo n.º 7
0
        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)));
        }
Exemplo n.º 8
0
        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)));
        }
Exemplo n.º 9
0
        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)));
        }
Exemplo n.º 10
0
        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)));
        }
Exemplo n.º 11
0
        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)));
        }
Exemplo n.º 12
0
        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();
                }
            }
        }
Exemplo n.º 13
0
        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();
                }
            }
        }
Exemplo n.º 14
0
 public void TestCreatingTokenizerWithNullScanner()
 {
     Assert.That(() => new Tokenizer(null), Throws.TypeOf(typeof(ArgumentNullException)));
 }
Exemplo n.º 15
0
 public void NoneExistentFileThrowsFileNotFoundException()
 {
     Assert.That(() => TestContext.AddTestAttachment("NotAFile.txt"), Throws.InstanceOf <FileNotFoundException>());
 }
Exemplo n.º 16
0
 public void TestParserConstructorThrowsIfNullParameter()
 {
     Assert.That(() => new CParser(null), Throws.TypeOf(typeof(ArgumentNullException)));
 }
Exemplo n.º 17
0
 public void TestFailingToTokenizeInvalidFloatingPointNumber()
 {
     Assert.That(() => TestTokenizingNumericLiteral("0.0.0"), Throws.TypeOf(typeof(FormatException)));
 }
Exemplo n.º 18
0
 public void TestFailingToTokenizeInvalidScientificNotationNumber2()
 {
     Assert.That(() => TestTokenizingNumericLiteral("5ee"), Throws.TypeOf(typeof(FormatException)));
 }
Exemplo n.º 19
0
 public void TestFailingToTokenizeInvalidNumericLiteral()
 {
     Assert.That(() => TestTokenizingNumericLiteral("5z"), Throws.TypeOf(typeof(UnexpectedCharEncounteredException)));
 }
Exemplo n.º 20
0
 public void InvalidFilePathsThrowsArgumentException(string filePath)
 {
     Assert.That(() => TestContext.AddTestAttachment(filePath), Throws.InstanceOf <ArgumentException>());
 }
Exemplo n.º 21
0
 public void TestScannerShouldNotBeConstructedWithNullStreamReader()
 {
     Assert.That(() => new Scanner(null), Throws.TypeOf(typeof(ArgumentNullException)));
 }
Exemplo n.º 22
0
 public void TestConstructorWithNullTokenizerThrowsException()
 {
     Assert.That(() => new CLexer(null), Throws.TypeOf(typeof(ArgumentNullException)));
 }