Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / templates / GenericCollectionTester.cs @ 3

1
/*
2
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
3
 Permission is hereby granted, free of charge, to any person obtaining a copy
4
 of this software and associated documentation files (the "Software"), to deal
5
 in the Software without restriction, including without limitation the rights
6
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
 copies of the Software, and to permit persons to whom the Software is
8
 furnished to do so, subject to the following conditions:
9
 
10
 The above copyright notice and this permission notice shall be included in
11
 all copies or substantial portions of the Software.
12
 
13
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
 SOFTWARE.
20
*/
21

    
22
using System;
23
using C5;
24
using NUnit.Framework;
25
using SCG = System.Collections.Generic;
26
using System.Reflection;
27

    
28
namespace C5UnitTests.Templates
29
{
30
  public abstract class GenericCollectionTester<U, W>
31
  {
32
    protected CircularQueue<MethodInfo> testMethods;
33
    public GenericCollectionTester()
34
    {
35
      testMethods = new CircularQueue<MethodInfo>();
36
      foreach (MethodInfo minfo in this.GetType().GetMethods())
37
      {
38
        if (minfo.GetParameters().Length == 0 &&
39
             minfo.GetCustomAttributes(typeof(TestAttribute), false).Length > 0)
40
          testMethods.Enqueue(minfo);
41
      }
42
    }
43

    
44
    public virtual void Test(Fun<U> factory)
45
    {
46
      foreach (MethodInfo minfo in testMethods)
47
      {
48
        foreach (W testSpec in GetSpecs())
49
        {
50
          SetUp(factory(), testSpec);
51
          //Console.WriteLine("Testing {0}, with method {1} and testSpec {{{2}}}", typeof(U), minfo.Name, testSpec);
52
          try
53
          {
54
            minfo.Invoke(this, null);
55
          }
56
          catch (TargetInvocationException)
57
          {
58
            //if (e.InnerException is ExpectedExceptionAttribute)
59
            //{
60
            //}
61
            //else
62
            throw;
63
          }
64
          //tearDown
65
        }
66
      }
67
    }
68

    
69
    public abstract void SetUp(U collection, W testSpec);
70
    public abstract SCG.IEnumerable<W> GetSpecs();
71
  }
72

    
73
  public abstract class GenericCollectionTester<U> : GenericCollectionTester<U, int>
74
  {
75
    public override System.Collections.Generic.IEnumerable<int> GetSpecs()
76
    {
77
      return new int[] { 0 };
78
    }
79

    
80
    public override void SetUp(U collection, int testSpec)
81
    {
82
      SetUp(collection);
83
    }
84

    
85
    public abstract void SetUp(object collection);
86
  }
87
}