예제 #1
0
        /// <summary>
        /// Expects the action to throw an exception of type T
        /// </summary>
        /// <param name="src">Action to test</param>
        /// <typeparam name="T">Type of exception which is expected</typeparam>
        /// <returns>Continuaiotn which can be used to test exception messages</returns>
        public static IThrowContinuation Throw <T>(
            this ICanAddMatcher <Action> src
            ) where T : Exception
        {
            var continuation = new ThrowContinuation();
            var expectedType = typeof(T);

            src.AddMatcher(fn =>
            {
                MatcherResult result;
                try
                {
                    fn();
                    result = new MatcherResult(false,
                                               $"Expected to throw an exception of type {expectedType.Name} but none was thrown");
                }
                catch (Exception ex)
                {
                    var passed  = ex is T;
                    var message = passed
                        ? $"Expected not to throw an exception of type {expectedType.Name}"
                        : $"Expected to throw an exception of type {expectedType.Name} but {ex.GetType().Name} was thrown instead ({ex.Message})";
                    result = new MatcherResult(passed, message);
                    continuation.Exception = ex;
                }
                return(result);
            });
            return(continuation);
        }
예제 #2
0
        /// <summary>
        /// Expects the action to throw an exception of type T
        /// </summary>
        /// <param name="src">Action to test</param>
        /// <param name="customMessageGenerator">Custom message to add to failure messages</param>
        /// <typeparam name="T">Type of exception which is expected</typeparam>
        /// <returns>Continuation which can be used to test exception messages</returns>
        public static IThrowContinuation <T> Throw <T>(
            this ICanAddMatcher <Action> src,
            Func <string> customMessageGenerator
            ) where T : Exception
        {
            var continuation = new ThrowContinuation <T>();
            var expectedType = typeof(T);

            src.AddMatcher(
                fn =>
            {
                MatcherResult result;
                try
                {
                    fn();
                    result = new MatcherResult(
                        false,
                        FinalMessageFor(
                            () => $"Expected to throw an exception of type {expectedType.Name} but none was thrown",
                            customMessageGenerator
                            ));
                }
                catch (Exception actual)
                {
                    var passed = actual is T;
                    result     = new MatcherResult(
                        passed,
                        FinalMessageFor(
                            () =>
                    {
                        var actualType   = actual.GetType();
                        var haveSameName = expectedType.Name == actualType.Name;
                        var expectedName = haveSameName
                                        ? $"{expectedType.Namespace}.{expectedType.Name}"
                                        : expectedType.Name;
                        var actualName = haveSameName
                                        ? $"{actualType.Namespace}.{actualType.Name}"
                                        : actualType.Name;
                        return(passed
                                        ? $@"Expected not to throw an exception of type {expectedName}"
                                        : $@"Expected to throw an exception of type {
                                            expectedName
                                        } but {
                                            actualName
                                        } was thrown instead ({
                                            actual.Message
                                        })");
                    },
                            customMessageGenerator));
                    continuation.Exception = actual as T;
                }

                return(result);
            });
            return(continuation);
        }
예제 #3
0
        public static IThrowContinuation Throw(this IContinuation <Action> src)
        {
            var continuation = new ThrowContinuation();

            src.AddMatcher(fn =>
            {
                MatcherResult result;
                try
                {
                    fn();
                    result = new MatcherResult(false, "Expected to throw an exception but none was thrown");
                }
                catch (Exception ex)
                {
                    continuation.Exception = ex;
                    result = new MatcherResult(true, $"Exception thrown:\n${ex.Message}\n${ex.StackTrace}");
                }
                return(result);
            });
            return(continuation);
        }
예제 #4
0
        /// <summary>
        /// Expects the action to throw an exception of type T
        /// </summary>
        /// <param name="src">Action to test</param>
        /// <param name="customMessageGenerator">Custom message to add to failure messages</param>
        /// <typeparam name="T">Type of exception which is expected</typeparam>
        /// <returns>Continuation which can be used to test exception messages</returns>
        public static IThrowContinuation <T> Throw <T>(
            this ICanAddMatcher <Action> src,
            Func <string> customMessageGenerator
            ) where T : Exception
        {
            var continuation = new ThrowContinuation <T>();
            var expectedType = typeof(T);

            src.AddMatcher(
                fn =>
            {
                MatcherResult result;
                try
                {
                    fn();
                    result = new MatcherResult(
                        false,
                        FinalMessageFor(
                            () => $"Expected to throw an exception of type {expectedType.Name} but none was thrown",
                            customMessageGenerator
                            ));
                }
                catch (Exception ex)
                {
                    var passed = ex is T;
                    result     = new MatcherResult(
                        passed,
                        FinalMessageFor(
                            () => passed
                                    ? $"Expected not to throw an exception of type {expectedType.Name}"
                                    : $"Expected to throw an exception of type {expectedType.Name} but {ex.GetType().Name} was thrown instead ({ex.Message})",
                            customMessageGenerator));
                    continuation.Exception = ex as T;
                }

                return(result);
            });
            return(continuation);
        }
예제 #5
0
        /// <summary>
        /// Expects the Action to throw any kind of exception
        /// </summary>
        /// <param name="src">Action to run</param>
        /// <param name="customMessageGenerator">Custom message to add to failure messages</param>
        /// <returns>Continuation which can be used to test exception messages</returns>
        public static IThrowContinuation <Exception> Throw(
            this ICanAddMatcher <Action> src,
            Func <string> customMessageGenerator
            )
        {
            var continuation = new ThrowContinuation <Exception>();

            src.AddMatcher(
                fn =>
            {
                MatcherResult result;
                try
                {
                    fn();
                    result = new MatcherResult(
                        false,
                        FinalMessageFor(
                            () => "Expected to throw an exception but none was thrown",
                            customMessageGenerator
                            )
                        );
                }
                catch (Exception ex)
                {
                    continuation.Exception = ex;
                    result = new MatcherResult(
                        true,
                        FinalMessageFor(
                            () => $"Exception thrown:\n{ex.Message}\n{ex.StackTrace}",
                            customMessageGenerator
                            ),
                        ex);
                }

                return(result);
            });
            return(continuation);
        }
예제 #6
0
    /// <summary>
    /// Construct a continuation description for each block
    /// </summary>
    private void BuildContinuationMap (CfgBlock[] blocks) {
      Hashtable conts = new Hashtable();
			
      conts[this.entry_point] = new StraightContinuation( (CfgBlock) this.b2next[this.entry_point]);
      conts[this.normal_exit_point] = new StraightContinuation(this.exit_point);
      conts[this.excp_exit_point] = new UnwindContinuation();
      conts[this.exit_point] = new DefaultContinuation();

      foreach(CfgBlock block in blocks) {
        // the special (and empty ...) blocks have already been dealt with
        if (block is ISpecialBlock) {
          continue;
        }

        Statement stat = get_last_stat(block);
        if (stat == null) {
          conts[block] = new StraightContinuation((CfgBlock) this.b2next[block]);
          continue;
        }
        switch(stat.NodeType) {
          case NodeType.Return: 
            conts[block] = new ReturnContinuation();
            break;

          case NodeType.Throw:
          case NodeType.Rethrow:
            conts[block] = new ThrowContinuation();
            break;

          case NodeType.Branch:
            Branch branch = (Branch) stat;
            // consider the edge for jump taken
            if (branch.Condition == null) { 
              // uncond. jump
              conts[block] = new StraightContinuation((CfgBlock)branch.Target);
            }
            else {
              conts[block] = new IfContinuation((CfgBlock)branch.Target, (CfgBlock) this.b2next[block]);
            }
            break;

          case NodeType.SwitchInstruction:
            SwitchInstruction swi = (SwitchInstruction)stat;
            conts[block] = new SwitchContinuation(swi.Targets, (CfgBlock) this.b2next[block]);
            break;

          case NodeType.EndFinally:
            conts[block] = new EndFaultContinuation();
            break;

          default:
            conts[block] = new StraightContinuation((CfgBlock) this.b2next[block]);
            break;
        }
      }
      this.b2_cont = conts;
    }