# Code coverage report for ./../tap/lib/assert.js

## Statements: 28.81% (51 / 177) Branches: 15.11% (21 / 139) Functions: 40.91% (9 / 22) Lines: 28.98% (51 / 176) Ignored: none

All files » ./../tap/lib/ » assert.js

    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
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    3102

    2
    2

    2

    2

    2
    2
    7
    7
    7

    2

    2

    2
    16
    16
    16

    2
    1
    1
    1

    2
    3
    3
    3
    3

    2

    2

    2

    2

    2

    2
    2
    2

    2

    2

    2
    2

    2

    2

    2

    2

    2
    28
    28
    456

    2

    2

    2
    3

    3

    2

     var synonyms = require('./synonyms.js')
    // TODO: use 'deeper' module?
    var deepEqual = require('deep-equal')
    var bufferEqual = require('buffer-equal')

    // Load Buffer the old way for browserify's sake
    var Buffer = require('buffer').Buffer

    // this is actually the "working half" of the Test class.
    // each method figures out if it's a pass or fail, and decorates
    // the extra bit, and then calls either pass() or fail() or some
    // other assert method.
    //
    // typically, a plugin would do this on a specific instance, eg on
    // the root test harness instance.  but we do this here to add some
    // useful prototype methods.

    exports.decorate = decorate

    function decorate (t) {
      t.addAssert('ok', 1, function (obj, message, extra) {
        message = message || 'expect truthy value'
        Eif (obj)
          return this.pass(message, extra)

        return this.fail(message, extra)
      })

      t.addAssert('notOk', 1, function (obj, message, extra) {
        message = message || 'expect falsey value'
        return this.ok(!obj, message, extra)
      })

      t.addAssert('error', 1, function (er, message, extra) {
        if (!er)
          return this.pass(message || 'should not error', extra)

        if (!(er instanceof Error)) {
          extra.found = er
          return this.fail(message || 'non-Error error encountered', extra)
        }

        message = message || er.message
        extra.found = er
        return this.fail(message, extra)
      })

      t.addAssert('equal', 2, function (f, w, m, e) {
        m = m || 'should be equal'
        Eif (f === w)
          return this.pass(m, e)

        e.found = f
        e.wanted = w
        e.compare = '==='

        if (typeof f === 'object' &&
            typeof w === 'object' &&
            f &&
            w &&
            deepEqual(f, w))
          e.note = 'Objects never === one another'

        return this.fail(m, e)
      })

      t.addAssert('not', 2, function (f, w, m, e) {
        m = m || 'should not be equal'
        Eif (f !== w)
          return this.pass(m, e)

        e.found = f
        e.doNotWant = w
        e.compare = '!=='

        return this.fail(m, e)
      })

      t.addAssert('same', 2, function (f, w, m, e) {
        m = m || 'should be equivalent'
        e.found = f
        e.wanted = w
        return this.ok(equivalent(f, w, false), m, e)
      })

      t.addAssert('notSame', 2, function (f, w, m, e) {
        m = m || 'should not be equivalent'
        e.found = f
        e.doNotWant = w
        return this.notOk(equivalent(f, w, false), m, e)
      })

      t.addAssert('strictSame', 2, function (f, w, m, e) {
        m = m || 'should be equivalent strictly'
        e.found = f
        e.wanted = w
        return this.ok(equivalent(f, w, true), m, e)
      })

      t.addAssert('strictNotSame', 2, function (f, w, m, e) {
        m = m || 'should be equivalent strictly'
        e.found = f
        e.doNotWant = w
        return this.notOk(equivalent(f, w, true), m, e)
      })

      t.addAssert('match', 2, function (f, w, m, e) {
        m = m || 'should match pattern provided'
        e.found = f
        e.pattern = w
        return this.ok(match(f, w, e, false), m, e)
      })

      t.addAssert('notMatch', 2, function (f, w, m, e) {
        m = m || 'should not match pattern provided'
        e.found = f
        e.pattern = w
        return this.ok(match(f, w, e, true), m, e)
      })

      t.addAssert('type', 2, function (obj, klass, m, e) {
        var name = klass
        Iif (typeof name === 'function')
          name = name.name || '(anonymous constructor)'
        m = m || 'type is ' + name

        // simplest case, it literally is the same thing
        Iif (obj === klass)
          return this.pass(m, e)

        var type = typeof obj
        Iif (!obj && type === 'object')
          type = 'null'

        Iif (type === 'object' && klass !== 'object') {
          if (typeof klass === 'function') {
            e.found = Object.getPrototypeOf(obj).constructor.name
            e.wanted = name
            return this.ok(obj instanceof klass, m, e)
          }

          // check prototype chain for name
          // at this point, we already know klass is not a function
          // if the klass specified is an obj in the proto chain, pass
          // if the name specified is the name of a ctor in the chain, pass
          var p = obj
          do {
            var ctor = p.constructor && p.constructor.name
            if (p === klass || ctor === name)
              return this.pass(m, e)
          } while (p = Object.getPrototypeOf(p))
        }

        return this.equal(type, name, m, e)
      })

      t.addAssert('throws', 4, function (fn_, wanted_, m_, e_, m, e__) {
        var fn, wanted, e
        for (var i = 0; i < arguments.length - 1; i++) {
          var arg = arguments[i]
          if (typeof arg === 'function')
            fn = arg
          else if (typeof arg === 'string' && arg)
            m = arg
          else if (typeof arg === 'object') {
            if (!wanted)
              wanted = arg
            else
              e = arg
          }
        }

        for (var i in e__) {
          e[i] = e__[i]
        }

        m = m || 'expected to throw'
        if (wanted) {
          if (wanted instanceof Error) {
            var w = {
              message: wanted.message
            }
            if (wanted.name)
              w.name = wanted.name

            for (var i in wanted) {
              w[i] = wanted[i]
            }
            wanted = w

            m += ': ' + (wanted.name || 'Error') + ' ' + wanted.message
            e = e || {}
            if (e !== wanted)
              e.wanted = wanted
          } else if (typeof wanted === 'string') {
            wanted = {
              message: wanted
            }
          }
        }

        if (typeof fn !== 'function') {
          e = e || {}
          e.todo = true
          return this.pass(m, e)
        }

        try {
          fn()
          return this.fail(m, e)
        } catch (er) {
          // 'name' is a getter.
          if (er.name)
            er.name = er.name + ''
          if (wanted)
            return this.has(er, wanted, m, e)
          else
            return this.pass(m, e)
        }
      })

      t.addAssert('doesNotThrow', 1, function (fn, m, e) {
        if (typeof fn === 'string') {
          var x = fn
          fn = m
          m = x
        }
        m = m || 'expected to not throw'
        if (typeof fn !== 'function') {
          e.todo = true
          return this.pass(m, e)
        }

        try {
          fn()
          return this.pass(m, e)
        } catch (er) {
          return this.fail(m, this._extraFromError(er, e))
        }
      })

      // synonyms are helpful.
      Object.keys(synonyms).forEach(function (c) {
        Eif (t[c]) {
          synonyms[c].forEach(function (s) {
            Object.defineProperty(t, s, {
              value: t[c],
              enumerable: false,
              configurable: true,
              writable: true
            })
          })
        }
      })
    }

    // TODO: this "partial deep equal" logic should be a module.
    function selectFields (a, b) {
      // get the values in A of the fields in B
      var ret = Array.isArray(b) ? [] : {}
      Object.keys(b).forEach(function (k) {
        if (!hasOwn(a, k)) return
        var v = b[k]
          , av = a[k]
        if (v && av && typeof v === "object" && typeof av === "object"
            && !(Buffer.isBuffer(v))
            && !(v instanceof Date)
            && !(v instanceof RegExp)
            && !(v instanceof String)
            && !(v instanceof Boolean)
            && !(v instanceof Number)
            && !(Array.isArray(v))) {
          ret[k] = selectFields(av, v)
        } else
          ret[k] = av
      })
      return ret
    }

    function match (f, w, e, flip) {
      if (Object.prototype.toString.call(w) === '[object RegExp]' &&
          typeof f === 'string') {
        var match = f.match(w)
        e.match = match
        var ok = !!match
        return !flip === ok
      }

      var eq
      if (f && typeof f === 'object' && typeof w === 'object')
        eq = equivalent(selectFields(f, w), w, false)
      else
        eq = f === w

      return eq === !flip
    }

    function equivalent (f, w, strict) {
      Iif (Buffer.isBuffer(f) && Buffer.isBuffer(w))
        return bufferEqual(f, w)
      else
        return deepEqual(f, w, { strict: strict })
    }

    function hasOwn (obj, key) {
      return Object.hasOwnProperty.call(obj, key)
    }