コード例 #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();
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #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)));
        }
コード例 #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)));
        }
コード例 #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)));
        }
コード例 #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)));
        }
コード例 #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)));
        }
コード例 #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)));
        }
コード例 #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)));
        }
コード例 #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)));
        }
コード例 #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();
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #14
0
 public void TestCreatingTokenizerWithNullScanner()
 {
     Assert.That(() => new Tokenizer(null), Throws.TypeOf(typeof(ArgumentNullException)));
 }
コード例 #15
0
 public void NoneExistentFileThrowsFileNotFoundException()
 {
     Assert.That(() => TestContext.AddTestAttachment("NotAFile.txt"), Throws.InstanceOf <FileNotFoundException>());
 }
コード例 #16
0
 public void TestParserConstructorThrowsIfNullParameter()
 {
     Assert.That(() => new CParser(null), Throws.TypeOf(typeof(ArgumentNullException)));
 }
コード例 #17
0
 public void TestFailingToTokenizeInvalidFloatingPointNumber()
 {
     Assert.That(() => TestTokenizingNumericLiteral("0.0.0"), Throws.TypeOf(typeof(FormatException)));
 }
コード例 #18
0
 public void TestFailingToTokenizeInvalidScientificNotationNumber2()
 {
     Assert.That(() => TestTokenizingNumericLiteral("5ee"), Throws.TypeOf(typeof(FormatException)));
 }
コード例 #19
0
 public void TestFailingToTokenizeInvalidNumericLiteral()
 {
     Assert.That(() => TestTokenizingNumericLiteral("5z"), Throws.TypeOf(typeof(UnexpectedCharEncounteredException)));
 }
コード例 #20
0
 public void InvalidFilePathsThrowsArgumentException(string filePath)
 {
     Assert.That(() => TestContext.AddTestAttachment(filePath), Throws.InstanceOf <ArgumentException>());
 }
コード例 #21
0
 public void TestScannerShouldNotBeConstructedWithNullStreamReader()
 {
     Assert.That(() => new Scanner(null), Throws.TypeOf(typeof(ArgumentNullException)));
 }
コード例 #22
0
 public void TestConstructorWithNullTokenizerThrowsException()
 {
     Assert.That(() => new CLexer(null), Throws.TypeOf(typeof(ArgumentNullException)));
 }