fan-1.0 /src/sys/dotnet/fan/sys/Test.cs

Language C# Lines 223
MD5 Hash 574fdb23885926fcd91a3c78900b0f9c Estimated Cost $4,048 (why?)
Repository https://bitbucket.org/bedlaczech/fan-1.0 View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// Copyright (c) 2006, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
//   20 Dec 06  Andy Frank  Creation
//

using System;
using System.Collections;
using Fanx.Util;

namespace Fan.Sys
{
  /// <summary>
  /// Test is the base class of unit tests.
  /// </summary>
  public class Test : FanObj
  {

  //////////////////////////////////////////////////////////////////////////
  // Constructors
  //////////////////////////////////////////////////////////////////////////

    public static void make_(Test t) {}

  //////////////////////////////////////////////////////////////////////////
  // Obj Overrides
  //////////////////////////////////////////////////////////////////////////

    public override Type @typeof()
    {
      return Sys.TestType;
    }

  //////////////////////////////////////////////////////////////////////////
  // Lifecycle
  //////////////////////////////////////////////////////////////////////////

    public Method curTestMethod()
    {
      if (m_curTestMethod == null)
        throw Err.make("No test currently executing for " + @typeof()).val;
      return m_curTestMethod;
    }

    public virtual void setup() {}

    public virtual void teardown() {}

  //////////////////////////////////////////////////////////////////////////
  // verify
  //////////////////////////////////////////////////////////////////////////

    public void verify(bool cond) { verify(cond, null); }
    public void verify(bool cond, string msg)
    {
      if (!cond) fail(msg);
      verifyCount++;
    }

    public void verifyFalse(bool cond) { verifyFalse(cond, null); }
    public void verifyFalse(bool cond, string msg)
    {
      if (cond) fail(msg);
      verifyCount++;
    }

    public void verifyNull(object a) { verifyNull(a, null); }
    public void verifyNull(object a, string msg)
    {
      if (a != null)
      {
        if (msg == null) msg = s(a) + " is not null";
        fail(msg);
      }
      verifyCount++;
    }

    public void verifyNotNull(object a) { verifyNotNull(a, null); }
    public void verifyNotNull(object a, string msg)
    {
      if (a == null)
      {
        if (msg == null) msg = "is null";
        fail(msg);
      }
      verifyCount++;
    }

    public void verifyEq(object expected, object actual) { verifyEq(expected, actual, null); }
    public void verifyEq(object expected, object actual, string msg)
    {
      if (!OpUtil.compareEQ(expected, actual))
      {
        //if (msg == null) msg = s(expected) + " != " + s(actual);
        if (msg == null) msg = s(expected) +
          " [" + (expected != null ? expected.GetType().ToString() : "null") + "] != "
          + s(actual) + " [" + (actual != null ? actual.GetType().ToString() : "null") + "]";
        fail(msg);
      }
      if (expected != null && actual != null)
      {
        if (hash(expected) != hash(actual))
        {
          fail("Equal but different hash codes: " +
            expected + " (0x" + FanInt.toHex(hash(expected)) + ") ?= " +
            actual   + " (0x" + FanInt.toHex(hash(actual)) + ")");
        }
      }
      verifyCount++;
    }

    public void verifyNotEq(object expected, object actual) { verifyNotEq(expected, actual, null); }
    public void verifyNotEq(object expected, object actual, string msg)
    {
      if (!OpUtil.compareNE(expected, actual))
      {
        if (msg == null) msg = s(expected) + " == " + s(actual);
        fail(msg);
      }
      verifyCount++;
    }

    public void verifySame(object expected, object actual) { verifySame(expected, actual, null); }
    public void verifySame(object expected, object actual, string msg)
    {
      if (expected != actual)
      {
        if (msg == null) msg = s(expected) + " !== " + s(actual);
        fail(msg);
      }
      verifyCount++;
    }

    public void verifyNotSame(object expected, object actual) { verifyNotSame(expected, actual, null); }
    public void verifyNotSame(object expected, object actual, string msg)
    {
      if (expected == actual)
      {
        if (msg == null) msg = s(expected) + " === " + s(actual);
        fail(msg);
      }
      verifyCount++;
    }

    public void verifyType(object obj, Type t)
    {
      verifyEq(Type.of(obj), t);
    }

    public void verifyErr(Type errType, Func f)
    {
      try
      {
        f.call(this);
      }
      catch (Err.Val e)
      {
        if (verbose) System.Console.WriteLine("  verifyErr: " + e);
        if (e.err().@typeof() == errType) { verifyCount++; return; }
        fail(e.err().@typeof() + " thrown, expected " + errType);
      }
      catch (System.Exception e)
      {
        if (verbose) System.Console.WriteLine("  verifyErr: " + e);
        Err err = Fan.Sys.Err.make(e);
        if (err.@typeof() == errType) { verifyCount++; return; }
        fail(e.GetType() + " thrown, expected " + errType);
      }
      fail("No err thrown, expected " + errType);
    }

    public void fail() { fail(null); }
    public void fail(string msg)
    {
      throw err(msg);
    }

    private Exception err(string msg)
    {
      if (msg == null)
        return Fan.Sys.TestErr.make("Test failed").val;
      else
        return Fan.Sys.TestErr.make("Test failed: " + msg).val;
    }

    private static string s(object obj)
    {
      if (obj == null) return "null";
      if (obj is string) return FanStr.toCode((string)obj);
      if (obj is List) return ((List)obj).of().ToString() + obj;
      return toStr(obj);
    }

  //////////////////////////////////////////////////////////////////////////
  // Utils
  //////////////////////////////////////////////////////////////////////////

    public File tempDir()
    {
      if (m_tempDir == null)
      {
        m_tempDir = Env.cur().tempDir().plus(Uri.fromStr("test/"), false);
        m_tempDir.delete();
        m_tempDir.create();
      }
      return m_tempDir;
    }

  //////////////////////////////////////////////////////////////////////////
  // Fields
  //////////////////////////////////////////////////////////////////////////

    internal static readonly Hashtable idMap = new Hashtable(); // qname -> Integer

    public int verifyCount;
    public static bool verbose;
    public Method m_curTestMethod;
    File m_tempDir = null;

  }
}
Back to Top