示例#1
0
        public void ThrowIfInvalidEnum_Test()
        {
            // arrange
            var values = new List <(Expression <Func <PortState> > expression, bool shouldRaceNull, bool shouldRaceArgument)>
            {
                (null, true, false),
                (() => (PortState)10, false, true),
                (() => PortState.Open, false, false),
                (() => (PortState)1, false, false)
            };

            // act & assert
            foreach (var value in values)
            {
                if (value.shouldRaceNull)
                {
                    NAssert.Throws <ArgumentNullException>(() => CheckUtil.ThrowIfInvalidEnum(value.expression), "ArgumentNullException expected but not thrown.");
                }
                if (value.shouldRaceArgument)
                {
                    NAssert.Throws <ArgumentException>(() => CheckUtil.ThrowIfInvalidEnum(value.expression), "ArgumentException expected but not thrown.");
                }
                if (!value.shouldRaceNull && !value.shouldRaceArgument)
                {
                    NAssert.DoesNotThrow(() => CheckUtil.ThrowIfInvalidEnum(value.expression), "Unexpected exception.");
                }
            }
        }
示例#2
0
        public void ThrowIfInvalidEnum_Callback_Test()
        {
            // arrange
            var values = new List <(Expression <Func <PortState> > expression, bool shouldRaceNull, bool shouldRaceArgument)>
            {
                (null, true, false),
                (() => (PortState)10, false, true),
                (() => PortState.Open, false, false),
                (() => (PortState)1, false, false)
            };
            var expectedCallbacks = values.Count(v => !v.shouldRaceNull && v.shouldRaceArgument);
            var actualCallbacks   = 0;

            // act && assert for exceptions
            foreach (var value in values)
            {
                if (value.shouldRaceNull)
                {
                    // should not race the callback
                    NAssert.Throws <ArgumentNullException>(() => CheckUtil.ThrowIfInvalidEnum(value.expression, ex => actualCallbacks++), "ArgumentNullException expected but not thrown.");
                }
                if (value.shouldRaceArgument)
                {
                    // should race the callback
                    NAssert.Throws <ArgumentException>(() => CheckUtil.ThrowIfInvalidEnum(value.expression, ex => actualCallbacks++), "ArgumentException expected but not thrown.");
                }
                if (!value.shouldRaceNull && !value.shouldRaceArgument)
                {
                    // should not race the callback
                    NAssert.DoesNotThrow(() => CheckUtil.ThrowIfInvalidEnum(value.expression, ex => actualCallbacks++), "Unexpected exception.");
                }
            }
            // assert for correct amount of callbacks called
            NAssert.AreEqual(expectedCallbacks, actualCallbacks, "Incorrect amount of callbacks was called.");
        }