utilSpec.js 2.66 KB
Newer Older
YazhouChen's avatar
YazhouChen committed
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
'use strict'

const expect = require('chai').expect
const Util = require('../').Util

describe('Util', function () {
  beforeEach(function () {
    this.minimal = require('./samples/01_minimal')()
  })

  describe('convertSetToArray', function () {
    it('should return elements in order', function () {
      const set = new Set()
      set.add('a')
      set.add(9)
      set.add('boo')
      set.add(Util)

      const arr = Util.convertSetToArray(set)

      expect(arr).to.deep.equal(['a', 9, 'boo', Util])
    })
  })

  describe('printPrettyKey', function () {
    it('should print classes by their name', function () {
      class A {}

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('A')
    })

    it('should print functions by their name', function () {
      function A () {}

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('A')
    })

    it('should print anonymous classes as [anonymous class]', function () {
      const A = class {}

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('[anonymous class]')
    })

    it('should print anonymous functions as [anonymous fn]', function () {
      const A = function () {}

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('[anonymous fn]')
    })

    it('should print numbers as the number itself', function () {
      const A = 1337

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('1337')
    })

    it('should print 0 as 0', function () {
      const A = 0

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('0')
    })

    it('should print null as [null]', function () {
      const A = null

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('[null]')
    })

    it('should print true as [true]', function () {
      const A = true

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('[true]')
    })

    it('should print false as [false]', function () {
      const A = false

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('[false]')
    })

    it('should print objects as {}', function () {
      const A = { foo: 'bar' }

      const pretty = Util.printPrettyKey(A)

      expect(pretty).to.be.a('string')
      expect(pretty).to.equal('{}')
    })
  })
})