Source: josm/unittest.mjs

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
/**
* @module josm/unittest
*/

/* global Java */

import * as util from 'josm/util'
const System = Java.type('java.lang.System')

const out = System.out

export function TestCase(name, test) {
  this._name = name
  this._test = test
}

export function test() {
  switch (arguments.length) {
    case 1:
      return new TestCase('No Name', arguments[0])
    case 2:
      return new TestCase(arguments[0], arguments[1])
    default:
      util.assert(false, 'Unsupported arguments')
  }
}

TestCase.prototype.run = function () {
  try {
    this._test()
    out.println('PASS - ' + this._name)
    return true
  } catch (e) {
    out.println(`FAIL - ${this._name}`)
    const desc = e.message || e.description || e
    out.println(`    description: ${desc}`)
    let context
    if (e.lineNumber || e.fileName) {
      context = `   filename: ${e.fileName}, line: ${e.lineNumber}`
    }
    if (context) {
      out.println(`    context: ${context}`)
    }
    return false
  }
}

export function Suite(name) {
  this._name = name
  this._tests = []
}

export function suite() {
  if (arguments.length === 0) return new Suite()
  let idx = 0
  const name = arguments[0]
  let suite
  if (util.isString(name)) {
    suite = new Suite(name)
    idx = 1
  } else {
    suite = new Suite()
  }
  for (let i = idx; i < arguments.length; i++) {
    const test = arguments[i]
    if (test instanceof TestCase) {
      suite.add(test)
    } else if (util.isFunction(test)) {
      suite.add(test(test))
    } else {
      util.assert(false, 'Unsupported arguments')
    }
  }
  return suite
}

Suite.prototype.add = function (test) {
  this._tests.push(test)
}

Suite.prototype.run = function () {
  out.println('----------------------------------------------------------------------')
  if (this._name) {
    out.println('suite: ' + this._name)
    out.println('----------------------------------------------------------------------')
  }
  let numtests = 0
  let numfail = 0
  let numok = 0
  for (let i = 0; i < this._tests.length; i++) {
    const ret = this._tests[i].run()
    numtests++
    if (ret) {
      numok++
    } else {
      numfail++
    }
  }
  out.println('----------------------------------------------------------------------')
  out.println(' # tests: ' + numtests + ' # PASS : ' + numok + '  # FAIL : ' + numfail)
  out.println('----------------------------------------------------------------------')
  return numfail
}

export function expectError() {
  let name = 'no name'
  let f
  switch (arguments.length) {
    case 0: return
    case 1: f = arguments[0]; break
    case 2:
      name = arguments[0]
      f = arguments[1]
      break
    default:
      util.assert(false, 'Unexpected number of arguments')
  }
  try {
    f()
    util.assert(false, "''{0}'': should have failed. Didn''t catch an error.", name)
  } catch (e) {
    // OK
  }
}

export function expectAssertionError() {
  let name = 'no name'
  let f
  switch (arguments.length) {
    case 0: return
    case 1: f = arguments[0]; break
    case 2:
      name = arguments[0]
      f = arguments[1]
      break
    default:
      util.assert(false, 'Unexpected number of arguments')
  }
  try {
    f()
    util.assert(false, "''{0}'': should have failed. Didn''t catch an error.", name)
  } catch (e) {
    if (e.name !== 'AssertionError') {
      util.assert(false, "''{0}'': expected an AssertionError, caught {1}.", name, e.toSource())
    }
  }
}