Пример #1
0
        private static void ValidateParser <T>(ParserTestData <T> testData)
        {
            ValidateParserHelper(testData);

            if (testData.ExpectedSuccess)
            {
                // Add several bytes of junk after the real text to ensure the parser successfully ignores it. By adding lots of junk, this also
                // exercises the "string is too long to rule out overflow based on length" paths of the integer parsers.
                ParserTestData <T> testDataWithExtraCharacter = new ParserTestData <T>(testData.Text + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", testData.ExpectedValue, testData.FormatSymbol, expectedSuccess: true)
                {
                    ExpectedBytesConsumed = testData.ExpectedBytesConsumed
                };
                ValidateParserHelper(testDataWithExtraCharacter);
            }
        }
Пример #2
0
        private static void ValidateParserHelper <T>(ParserTestData <T> testData)
        {
            ReadOnlySpan <byte> utf8Text = testData.Text.ToUtf8Span();
            bool success = TryParseUtf8 <T>(utf8Text, out T value, out int bytesConsumed, testData.FormatSymbol);

            if (testData.ExpectedSuccess)
            {
                if (!success)
                {
                    throw new TestException($"This parse attempt {testData} was expected to succeed: instead, it failed.");
                }

                T expected = testData.ExpectedValue;
                T actual   = value;
                if (!IsParsedValueEqual <T>(expected: expected, actual: actual))
                {
                    string expectedString = expected.DisplayString();
                    string actualString   = actual.DisplayString();
                    throw new TestException($"This parse attempt {testData} succeeded as expected but parsed to the wrong value:\n  Expected: {expectedString}\n  Actual:   {actualString}\n");
                }

                int expectedBytesConsumed = testData.ExpectedBytesConsumed;
                if (expectedBytesConsumed != bytesConsumed)
                {
                    throw new TestException($"This parse attempt {testData} returned the correct value but the wrong `bytesConsumed` value:\n  Expected: {expectedBytesConsumed}\n  Actual:   {bytesConsumed}\n");
                }
            }
            else
            {
                if (success)
                {
                    throw new TestException($"This parse attempt {testData} was expected to fail: instead, it succeeded and returned {value}.");
                }

                if (bytesConsumed != 0)
                {
                    throw new TestException($"This parse attempt {testData} failed as expected but did not set `bytesConsumed` to 0");
                }

                if (!(value.Equals(default(T))))
                {
                    throw new TestException($"This parse attempt {testData} failed as expected but did not set `value` to default(T)");
                }
            }
        }
Пример #3
0
 public static void TestParserSingle(ParserTestData <float> testData)
 {
     ValidateParser(testData);
 }
Пример #4
0
 public static void TestParserGuid(ParserTestData <Guid> testData)
 {
     ValidateParser(testData);
 }
Пример #5
0
 public static void TestParserDecimal(ParserTestData <decimal> testData)
 {
     ValidateParser(testData);
 }
Пример #6
0
 public static void TestParserDouble(ParserTestData <double> testData)
 {
     ValidateParser(testData);
 }
Пример #7
0
 public static void TestParserUInt32(ParserTestData <uint> testData)
 {
     ValidateParser(testData);
 }
Пример #8
0
 public static void TestParserUInt64(ParserTestData <ulong> testData)
 {
     ValidateParser(testData);
 }
Пример #9
0
 public static void TestParserUInt16(ParserTestData <ushort> testData)
 {
     ValidateParser(testData);
 }
Пример #10
0
 public static void TestParserByte(ParserTestData <byte> testData)
 {
     ValidateParser(testData);
 }
Пример #11
0
 public static void TestParserBoolean(ParserTestData <bool> testData)
 {
     ValidateParser(testData);
 }
Пример #12
0
 public static void TestParserTimeSpan(ParserTestData <TimeSpan> testData)
 {
     ValidateParser(testData);
 }
Пример #13
0
 public static void TestParserDateTimeOffset(ParserTestData <DateTimeOffset> testData)
 {
     ValidateParser(testData);
 }