Exemple #1
0
        public static void InspectBadValue <TException, TInspected>(Action <Inspected <TInspected> > assert, TInspected inspectedValue)
            where TException : ContractViolationException
        {
            Expression <Func <TInspected> > fetchInspected = () => inspectedValue;
            var inspectedName = ContractsExpression.ExtractName(fetchInspected);

            var inspected = Contract.Argument(() => inspectedValue);

            AssertThrows <TException, TInspected>(
                inspected: inspected,
                assert: assert,
                inspectionType: InspectionType.Argument,
                badValueName: inspectedName);

            inspected = Contract.Argument(() => inspectedValue, () => inspectedValue);
            AssertThrows <TException, TInspected>(
                inspected: inspected,
                assert: assert,
                inspectionType: InspectionType.Argument,
                badValueName: inspectedName);

            inspected = Contract.Invariant(() => inspectedValue);
            AssertThrows <TException, TInspected>(
                inspected: inspected,
                assert: assert,
                inspectionType: InspectionType.Invariant,
                badValueName: inspectedName);

            inspected = Contract.Invariant(() => inspectedValue, () => inspectedValue);
            AssertThrows <TException, TInspected>(
                inspected: inspected,
                assert: assert,
                inspectionType: InspectionType.Invariant,
                badValueName: inspectedName);

            const string returnvalueName = "ReturnValue";

            inspected = Contract.ReturnValue(inspectedValue);
            AssertThrows <TException, TInspected>(
                inspected: inspected,
                assert: assert,
                inspectionType: InspectionType.ReturnValue,
                badValueName: returnvalueName);

            var exception = Assert.Throws <TException>(() => Return(inspectedValue, assert));

            exception.BadValue.Type.Should().Be(InspectionType.ReturnValue);
            exception.BadValue.Name.Should().Be(returnvalueName);

            exception = Assert.Throws <TException>(() => ReturnOptimized(inspectedValue, assert));
            exception.BadValue.Type.Should().Be(InspectionType.ReturnValue);
            exception.BadValue.Name.Should().Be(returnvalueName);
        }
Exemple #2
0
        public static void InspectGoodValues <TInspected>(Action <Inspected <TInspected> > assert, TInspected goodValue)
        {
            Expression <Func <TInspected> > fetchInspected = () => goodValue;
            var inspectedName = ContractsExpression.ExtractName(fetchInspected);

            var inspected = Contract.Argument(() => goodValue);

            assert(inspected);

            inspected = Contract.Argument(() => goodValue);
            assert(inspected);

            inspected = Contract.Invariant(() => goodValue);
            assert(inspected);

            Return(goodValue, assert);
        }