Beispiel #1
0
        public void Cleanup()
        {
            try
            {
                foreach (var given in Givens.Select(x => new { Method = x.Key, Text = x.Value }))
                {
                    if (!Context.CleanUps.ContainsKey(given.Text))
                    {
                        continue;
                    }

                    var result = Context.TestRunContext[TestType.Name + given.Text];

                    //if it doesn't take arguments just execute it
                    if (!Context.CleanUps[given.Text].Method.GetParameters().Any())
                    {
                        Context.CleanUps[given.Text].DynamicInvoke();
                        continue;
                    }

                    //if it takes one argument pass in the result
                    if (Context.CleanUps[given.Text].Method.GetParameters().Count() == 1)
                    {
                        Context.CleanUps[given.Text].DynamicInvoke(result);
                        continue;
                    }

                    //if it takes multiple arguments parse the tuple and pass them in
                    var count = result.GetType().GetGenericArguments().Count();
                    if (count > 0)
                    {
                        var type    = result.GetType();
                        var argList = new List <object>();

                        for (int i = 1; i <= count; i++)
                        {
                            argList.Add(type.GetProperty("Item" + i).GetValue(result, null));
                        }

                        Context.CleanUps[given.Text].DynamicInvoke(argList.ToArray());
                        continue;
                    }

                    Console.WriteLine("Found cleanup for {0} but didn't know how to call it", given.Text);
                }

                foreach (var teardownMethod in _teardownMethods)
                {
                    teardownMethod.Invoke();
                }
            }
            catch
            {
            }
        }