Пример #1
0
        public void TestNumber_VB()
        {
            Parser parser; Token token;

            double eps = 0.0001;

            parser = TestHelper.CreateParser(TerminalFactory.CreateVbNumber("Number"));

            //Simple integer
            token = parser.ParseInput("123 ");
            CheckType(token, typeof(int));
            Assert.True(token.Details != null, "ScanDetails object not found in token.");
            Assert.True((int)token.Value == 123, "Failed to read int value");

            //Test all suffixes
            token = parser.ParseInput("123S ");
            CheckType(token, typeof(Int16));
            Assert.True((Int16)token.Value == 123, "Failed to read short value");

            token = parser.ParseInput("123I ");
            CheckType(token, typeof(Int32));
            Assert.True((Int32)token.Value == 123, "Failed to read int value");

            token = parser.ParseInput("123% ");
            CheckType(token, typeof(Int32));
            Assert.True((Int32)token.Value == 123, "Failed to read int value");

            token = parser.ParseInput("123L ");
            CheckType(token, typeof(long));
            Assert.True((long)token.Value == 123, "Failed to read long value");

            token = parser.ParseInput("123& ");
            CheckType(token, typeof(Int64));
            Assert.True((Int64)token.Value == 123, "Failed to read long value");

            token = parser.ParseInput("123us ");
            CheckType(token, typeof(UInt16));
            Assert.True((UInt16)token.Value == 123, "Failed to read ushort value");

            token = parser.ParseInput("123ui ");
            CheckType(token, typeof(UInt32));
            Assert.True((UInt32)token.Value == 123, "Failed to read uint value");

            token = parser.ParseInput("123ul ");
            CheckType(token, typeof(ulong));
            Assert.True((ulong)token.Value == 123, "Failed to read ulong value");

            //Hex and octal
            token = parser.ParseInput("&H012 ");
            CheckType(token, typeof(int));
            Assert.True((int)token.Value == 0x012, "Failed to read hex int value");

            token = parser.ParseInput("&H012L ");
            CheckType(token, typeof(long));
            Assert.True((long)token.Value == 0x012, "Failed to read hex long value");

            token = parser.ParseInput("&O012 ");
            CheckType(token, typeof(int));
            Assert.True((int)token.Value == 10, "Failed to read octal int value"); //12(oct) = 10(dec)

            token = parser.ParseInput("&o012L ");
            CheckType(token, typeof(long));
            Assert.True((long)token.Value == 10, "Failed to read octal long value");

            //Floating point types
            token = parser.ParseInput("123.4 ");
            CheckType(token, typeof(double));
            Assert.True(Math.Abs((double)token.Value - 123.4) < eps, "Failed to read double value #1");

            token = parser.ParseInput("1234e-1 ");
            CheckType(token, typeof(double));
            Assert.True(Math.Abs((double)token.Value - 1234e-1) < eps, "Failed to read double value #2");

            token = parser.ParseInput("12.34e+01 ");
            CheckType(token, typeof(double));
            Assert.True(Math.Abs((double)token.Value - 123.4) < eps, "Failed to read double value  #3");

            token = parser.ParseInput("0.1234E3 ");
            CheckType(token, typeof(double));
            Assert.True(Math.Abs((double)token.Value - 123.4) < eps, "Failed to read double value  #4");

            token = parser.ParseInput("123.4R ");
            CheckType(token, typeof(double));
            Assert.True(Math.Abs((double)token.Value - 123.4) < eps, "Failed to read double value #5");

            token = parser.ParseInput("123.4# ");
            CheckType(token, typeof(double));
            Assert.True(Math.Abs((double)token.Value - 123.4) < eps, "Failed to read double value #6");

            token = parser.ParseInput("123.4f ");
            CheckType(token, typeof(float));
            Assert.True(Math.Abs((Single)token.Value - 123.4) < eps, "Failed to read float(single) value");

            token = parser.ParseInput("123.4! ");
            CheckType(token, typeof(float));
            Assert.True(Math.Abs((Single)token.Value - 123.4) < eps, "Failed to read float(single) value");

            token = parser.ParseInput("123.4D ");
            CheckType(token, typeof(decimal));
            Assert.True(Math.Abs((decimal)token.Value - 123.4m) < Convert.ToDecimal(eps), "Failed to read decimal value");

            token = parser.ParseInput("123.4@ ");
            CheckType(token, typeof(decimal));
            Assert.True(Math.Abs((decimal)token.Value - 123.4m) < Convert.ToDecimal(eps), "Failed to read decimal value");

            //Quick parse
            token = parser.ParseInput("1 ");
            CheckType(token, typeof(int));
            //When going through quick parse path (for one-digit numbers), the NumberScanInfo record is not created and hence is absent in Attributes
            Assert.True(token.Details == null, "Quick parse test failed: ScanDetails object is found in token - quick parse path should not produce this object.");
            Assert.True((int)token.Value == 1, "Failed to read quick-parse value");
        }