Ejemplo n.º 1
0
        public void GetNameWithDigits()
        {
            Lexer lexer = new Lexer("name123");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("name123", result.Value);
            Assert.AreEqual(TokenType.Name, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 2
0
        public void GetKey()
        {
            Lexer lexer = new Lexer("name:");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("name", result.Value);
            Assert.AreEqual(TokenType.Key, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 3
0
        public void GetNameWithInitialUnderscore()
        {
            Lexer lexer = new Lexer("_foo");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("_foo", result.Value);
            Assert.AreEqual(TokenType.Name, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 4
0
        public void RaiseIfUnexpectedCharacter()
        {
            Lexer lexer = new Lexer("\\");

            try
            {
                lexer.NextToken();
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SyntaxError));
                Assert.AreEqual("unexpected '\\'", ex.Message);
            }
        }
Ejemplo n.º 5
0
        public void RaiseWhenClassVarNameStartsWithADigit()
        {
            Lexer lexer = new Lexer("@@123");

            try
            {
                lexer.NextToken();
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SyntaxError));
                Assert.AreEqual("invalid class variable name", ex.Message);
            }
        }
Ejemplo n.º 6
0
        public void GetSymbolInDoubleQuotes()
        {
            Lexer lexer = new Lexer(" :\"foo bar\"");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("foo bar", result.Value);
            Assert.AreEqual(TokenType.Symbol, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 7
0
        public void GetRealNumberWithSpacesUsingReader()
        {
            Lexer lexer = new Lexer(new StringReader("  123.45   "));
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("123.45", result.Value);
            Assert.AreEqual(TokenType.Real, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 8
0
        public void GetComparisonOperators()
        {
            Lexer lexer = new Lexer("== != < > <= >=");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("==", result.Value);
            Assert.AreEqual(TokenType.Operator, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("!=", result.Value);
            Assert.AreEqual(TokenType.Operator, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("<", result.Value);
            Assert.AreEqual(TokenType.Operator, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual(">", result.Value);
            Assert.AreEqual(TokenType.Operator, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("<=", result.Value);
            Assert.AreEqual(TokenType.Operator, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual(">=", result.Value);
            Assert.AreEqual(TokenType.Operator, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 9
0
        public void GetDoubleColonAsSeparator()
        {
            Lexer lexer = new Lexer("::");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("::", result.Value);
            Assert.AreEqual(TokenType.Separator, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 10
0
        public void GetStringWithEscapedChars()
        {
            Lexer lexer = new Lexer("\"foo\\t\\n\\r\"");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("foo\t\n\r", result.Value);
            Assert.AreEqual(TokenType.String, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 11
0
 public void RaiseIfDoubleQuoteStringIsNotClosed()
 {
     Lexer lexer = new Lexer("\"foo");
     lexer.NextToken();
 }
Ejemplo n.º 12
0
        public void GetCharacter()
        {
            Lexer lexer = new Lexer("'f'");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("f", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 13
0
        public void GetEscapedCharactersWithOctal()
        {
            Lexer lexer = new Lexer("'\\101' '\\123' '\\12' '\\1'");

            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("A", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("S", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("\n", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Value.Length);
            Assert.AreEqual(1, (int)result.Value[0]);
            Assert.AreEqual(TokenType.Character, result.Type);

            result = lexer.NextToken();

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 14
0
        public void RaiseWhenInvalidInstanceVarName()
        {
            Lexer lexer = new Lexer("@");

            try
            {
                lexer.NextToken();
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SyntaxError));
                Assert.AreEqual("invalid instance variable name", ex.Message);
            }
        }
Ejemplo n.º 15
0
        public void GetClassVarNameWithDigitsAndUnderscore()
        {
            Lexer lexer = new Lexer("@@a_123");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("a_123", result.Value);
            Assert.AreEqual(TokenType.ClassVarName, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 16
0
        public void GetOperatorAsSymbol()
        {
            Lexer lexer = new Lexer(":+");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("+", result.Value);
            Assert.AreEqual(TokenType.Symbol, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 17
0
        public void GetClassVarName()
        {
            Lexer lexer = new Lexer("@@a");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("a", result.Value);
            Assert.AreEqual(TokenType.ClassVarName, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 18
0
        public void RaiseIfSymbolStartsWithADigit()
        {
            Lexer lexer = new Lexer(":123");

            try
            {
                lexer.NextToken();
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SyntaxError));
                Assert.AreEqual("unexpected integer", ex.Message);
            }
        }
Ejemplo n.º 19
0
        public void GetEscapedCharacters()
        {
            Lexer lexer = new Lexer("'\\t' '\\n' '\\r' '\\v' '\\f' '\\\\'");

            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("\t", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("\n", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("\r", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("\v", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("\f", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("\\", result.Value);
            Assert.AreEqual(TokenType.Character, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 20
0
        public void GetIntegerWithUnderscore()
        {
            Lexer lexer = new Lexer("123_000_000");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("123000000", result.Value);
            Assert.AreEqual(TokenType.Integer, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 21
0
        public void GetStringWithCommentChar()
        {
            Lexer lexer = new Lexer("\"#foo\"");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("#foo", result.Value);
            Assert.AreEqual(TokenType.String, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 22
0
        public void GetIntegerWithDotName()
        {
            Lexer lexer = new Lexer("123.foo");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("123", result.Value);
            Assert.AreEqual(TokenType.Integer, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual(".", result.Value);
            Assert.AreEqual(TokenType.Separator, result.Type);

            result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("foo", result.Value);
            Assert.AreEqual(TokenType.Name, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 23
0
        public void GetOctalInteger()
        {
            Lexer lexer = new Lexer("0o123");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("0o123", result.Value);
            Assert.AreEqual(TokenType.Integer, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 24
0
        public void GetBinaryInteger()
        {
            Lexer lexer = new Lexer("0b11011");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("0b11011", result.Value);
            Assert.AreEqual(TokenType.Integer, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 25
0
        public void RaiseIfCharacterIsNotClosed()
        {
            Lexer lexer = new Lexer("'f");

            try
            {
                lexer.NextToken();
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SyntaxError));
                Assert.AreEqual("unclosed character", ex.Message);
            }
        }
Ejemplo n.º 26
0
        public void GetHexadecimalInteger()
        {
            Lexer lexer = new Lexer("0x0123456789abcdef");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("0x0123456789abcdef", result.Value);
            Assert.AreEqual(TokenType.Integer, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 27
0
        public void GetComparisonAsOperator()
        {
            Lexer lexer = new Lexer("<=>");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("<=>", result.Value);
            Assert.AreEqual(TokenType.Operator, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 28
0
        public void GetHexadecimalIntegerWithUpperCaseLetters()
        {
            Lexer lexer = new Lexer("0x0123456789ABCDEF");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("0x0123456789ABCDEF", result.Value);
            Assert.AreEqual(TokenType.Integer, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 29
0
        public void GetBinaryOrAsOperator()
        {
            Lexer lexer = new Lexer("|");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("|", result.Value);
            Assert.AreEqual(TokenType.Operator, result.Type);

            Assert.IsNull(lexer.NextToken());
        }
Ejemplo n.º 30
0
        public void GetSymbolEndingWithExclamationMark()
        {
            Lexer lexer = new Lexer(" :foo!");
            var result = lexer.NextToken();

            Assert.IsNotNull(result);
            Assert.AreEqual("foo!", result.Value);
            Assert.AreEqual(TokenType.Symbol, result.Type);

            Assert.IsNull(lexer.NextToken());
        }