public void ParseResult_CreateException_OverflowException(int error)
        {
            var pr = new ParseResult((ParseResultType)error);

            var ex = pr.CreateException();

            Assert.IsInstanceOfType(ex, typeof(OverflowException));
        }
Esempio n. 2
0
        public void ParseResult_General()
        {
            // Make sure that passing in the [Success] status always fails.
            var ex = new Lazy <Exception>(() => new Exception());

            new Action(() => new ParseResult(ParseResultType.Success))
            .AssertThrowsExact <ArgumentException>(
                "Did not throw when passed [Success] code (0).");
            new Action(() => new ParseResult(ParseResultType.Success, ex))
            .AssertThrowsExact <ArgumentException>(
                "Did not throw when passed [Success] code (1).");

            // Make sure that an invalid status fails.
            new Action(() => new ParseResult((ParseResultType)(-1)))
            .AssertThrowsExact <ArgumentException>(
                "Did not throw on invalid status code (0).");
            new Action(() => new ParseResult((ParseResultType)(-1), ex))
            .AssertThrowsExact <ArgumentException>(
                "Did not throw on invalid status code (1).");

            // Make sure that a null exception provider fails.
            new Action(
                () => new ParseResult(ParseResultType.InvalidVersion, null))
            .AssertThrows <ArgumentNullException>(
                "Did not throw on null exception provider.");

            // Tests that accessing the properties of a default-constructed
            // [ParseResult] fails with an exception.
            var defPr = new ParseResult();

            new Action(() => defPr.Type.GetHashCode())
            .AssertThrows <InvalidOperationException>(
                "Did not throw on invalid access (0).");
            new Action(() => defPr.ComparatorSets.GetHashCode())
            .AssertThrows <InvalidOperationException>(
                "Did not throw on invalid access (1).");
            new Action(() => defPr.GetErrorMessage())
            .AssertThrows <InvalidOperationException>(
                "Did not throw on invalid access (2).");
            new Action(() => defPr.CreateException())
            .AssertThrows <InvalidOperationException>(
                "Did not throw on invalid access (3).");

            // Makes sure that attempting to create an exception/retrieve an
            // error message with a success-state [ParseResult] fails.
            var sucPr = new ParseResult(new IComparator[0][]);

            new Action(() => sucPr.GetErrorMessage())
            .AssertThrows <InvalidOperationException>(
                "Did not throw on invalid [GetErrorMessage] call.");
            new Action(() => sucPr.CreateException())
            .AssertThrows <InvalidOperationException>(
                "Did not throw on invalid [CreateException] call.");
        }
        public void ParseResult_DefaultConstructed()
        {
            // Default-constructed parse results don't contain a valid version or
            // an error code, so the only thing they can reasonably do is throw
            // an exception when you try to use them.
            var pr = new ParseResult();

            new Action(() => { var x = pr.Type == ParseResultType.Success; })
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on use of default-constructed result (0).");

            new Action(() => { var x = pr.Version == null; })
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on use of default-constructed result (1).");

            new Action(() => pr.GetErrorMessage())
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on use of default-constructed result (2).");

            new Action(() => pr.CreateException())
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on use of default-constructed result (3).");
        }
        public void ParseResult_General()
        {
            // Make sure that passing in the [Success] status always fails.
            var ex = new Lazy<Exception>(() => new Exception());
            new Action(() => new ParseResult(ParseResultType.Success))
                .AssertThrowsExact<ArgumentException>(
                    "Did not throw when passed [Success] code (0).");
            new Action(() => new ParseResult(ParseResultType.Success, ex))
                .AssertThrowsExact<ArgumentException>(
                    "Did not throw when passed [Success] code (1).");

            // Make sure that an invalid status fails.
            new Action(() => new ParseResult((ParseResultType)(-1)))
                .AssertThrowsExact<ArgumentException>(
                    "Did not throw on invalid status code (0).");
            new Action(() => new ParseResult((ParseResultType)(-1), ex))
                    .AssertThrowsExact<ArgumentException>(
                        "Did not throw on invalid status code (1).");

            // Make sure that a null exception provider fails.
            new Action(
                () => new ParseResult(ParseResultType.InvalidVersion, null))
                .AssertThrows<ArgumentNullException>(
                    "Did not throw on null exception provider.");

            // Tests that accessing the properties of a default-constructed
            // [ParseResult] fails with an exception.
            var defPr = new ParseResult();
            new Action(() => defPr.Type.GetHashCode())
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on invalid access (0).");
            new Action(() => defPr.ComparatorSets.GetHashCode())
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on invalid access (1).");
            new Action(() => defPr.GetErrorMessage())
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on invalid access (2).");
            new Action(() => defPr.CreateException())
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on invalid access (3).");

            // Makes sure that attempting to create an exception/retrieve an
            // error message with a success-state [ParseResult] fails.
            var sucPr = new ParseResult(new ComparatorToken[0][]);
            new Action(() => sucPr.GetErrorMessage())
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on invalid [GetErrorMessage] call.");
            new Action(() => sucPr.CreateException())
                .AssertThrows<InvalidOperationException>(
                    "Did not throw on invalid [CreateException] call.");
        }
        public void ParseResult_ErrorCodes()
        {
            ParseResult
                pr0 = new ParseResult(ParseResultType.NullString),
                pr1 = new ParseResult(ParseResultType.PreTrioInvalidChar),
                pr2 = new ParseResult(ParseResultType.TrioInvalidChar),
                pr3 = new ParseResult(ParseResultType.TrioItemLeadingZero),
                pr4 = new ParseResult(ParseResultType.TrioItemMissing),
                pr5 = new ParseResult(ParseResultType.TrioItemOverflow),
                pr6 = new ParseResult(ParseResultType.IdentifierMissing),
                pr7 = new ParseResult(ParseResultType.IdentifierInvalid),
                pr8 = new ParseResult(ParseResultType.MetadataMissing),
                pr9 = new ParseResult(ParseResultType.MetadataInvalid);

            // The [ParseResult] struct has a method, [CreateException],
            // that will handily create the appropriate exception to throw
            // for us. We want to make sure that it's returning the correct
            // type of exception.
            Assert.IsTrue(pr0.CreateException() is ArgumentNullException,
                          "Incorrect exception type (0).");

            Assert.IsTrue(pr1.CreateException() is FormatException,
                          "Incorrect exception type (1).");
            Assert.IsTrue(pr2.CreateException() is FormatException,
                          "Incorrect exception type (2).");
            Assert.IsTrue(pr3.CreateException() is FormatException,
                          "Incorrect exception type (3).");

            Assert.IsTrue(pr4.CreateException() is ArgumentException,
                          "Incorrect exception type (4).");

            Assert.IsTrue(pr5.CreateException() is OverflowException,
                          "Incorrect exception type (5).");

            Assert.IsTrue(pr6.CreateException() is ArgumentException,
                          "Incorrect exception type (6).");

            Assert.IsTrue(pr7.CreateException() is FormatException,
                          "Incorrect exception type (7).");

            Assert.IsTrue(pr8.CreateException() is ArgumentException,
                          "Incorrect exception type (8).");

            Assert.IsTrue(pr9.CreateException() is FormatException,
                          "Incorrect exception type (9).");

            // Next, we want to make sure that an attempt to create a 
            // fail-state [ParseResult] with the [Success] status fails.
            new Action(() => new ParseResult(ParseResultType.Success))
                .AssertThrowsExact<ArgumentException>(
                    "Attempt to create result with status [Success] did " +
                    "not throw.");

            // Then we want to test that calling [CreateException] on a
            // [ParseResult] without an error code throws an exception.
            new Action(() => new ParseResult(new SemanticVersion(1, 0))
                                .CreateException())
                .AssertThrows<InvalidOperationException>(
                    "Call to [CreateException] with status [Success] did " +
                    "not throw.");

            // And, last but not least, we want to make sure that an
            // invalid error code throws an exception.
            new Action(() => new ParseResult((ParseResultType)(-4025820)))
                .AssertThrowsExact<ArgumentException>(
                    "Did not throw on invalid error code.");
        }