|
| 1 | +if (typeof window === 'undefined') { |
| 2 | + var expect = require('expect.js'); |
| 3 | + var assert = require('../..'); |
| 4 | +} |
| 5 | + |
| 6 | +describe('strict mode support', function () { |
| 7 | + |
| 8 | + var legacyModeAssert = assert; |
| 9 | + |
| 10 | + beforeEach(function () { |
| 11 | + assert = assert.strict; |
| 12 | + }); |
| 13 | + afterEach(function () { |
| 14 | + assert = legacyModeAssert; |
| 15 | + }); |
| 16 | + |
| 17 | + function expectPowerAssertMessage (body, expectedLines) { |
| 18 | + try { |
| 19 | + body(); |
| 20 | + expect().fail("AssertionError should be thrown"); |
| 21 | + } catch (e) { |
| 22 | + if (e.message === 'AssertionError should be thrown') { |
| 23 | + throw e; |
| 24 | + } |
| 25 | + expect(e.message.split('\n').slice(2, -1)).to.eql(expectedLines); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + it('`strict` mode assert should also have `strict` property', function () { |
| 30 | + expect(typeof assert.strict).to.equal('function'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('`strict` property of strict mode assert should also be an empowered one', function () { |
| 34 | + // `_empowered` is a hidden property to determine whether target function is already empowered or not. |
| 35 | + expect(assert.strict._empowered).to.equal(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it('`strict` mode assert should also be a function', function () { |
| 39 | + var foo = 'foo', bar = 8; |
| 40 | + expectPowerAssertMessage(function () { |
| 41 | + assert(foo === bar); |
| 42 | + }, [ |
| 43 | + ' assert(foo === bar)', |
| 44 | + ' | | | ', |
| 45 | + ' | | 8 ', |
| 46 | + ' | false ', |
| 47 | + ' "foo" ', |
| 48 | + ' ', |
| 49 | + ' [number] bar', |
| 50 | + ' => 8', |
| 51 | + ' [string] foo', |
| 52 | + ' => "foo"' |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + it('equal becomes strictEqual', function () { |
| 57 | + var three = 3, threeInStr = '3'; |
| 58 | + expectPowerAssertMessage(function () { |
| 59 | + assert.equal(three, threeInStr); |
| 60 | + },[ |
| 61 | + ' assert.equal(three, threeInStr)', |
| 62 | + ' | | ', |
| 63 | + ' 3 "3" ' |
| 64 | + ]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('deepEqual becomes deepStrictEqual', function () { |
| 68 | + var three = 3, threeInStr = '3'; |
| 69 | + expectPowerAssertMessage(function () { |
| 70 | + assert.deepEqual({a: three}, {a: threeInStr}); |
| 71 | + },[ |
| 72 | + ' assert.deepEqual({ a: three }, { a: threeInStr })', |
| 73 | + ' | | | | ', |
| 74 | + ' | | | "3" ', |
| 75 | + ' | 3 Object{a:"3"} ', |
| 76 | + ' Object{a:3} ' |
| 77 | + ]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('structural compatibility between strict mode and legacy mode', function () { |
| 81 | + var legacyModeProps = Object.keys(legacyModeAssert); |
| 82 | + expect(legacyModeProps.length).to.be.above(0); |
| 83 | + legacyModeProps.every(function (name) { |
| 84 | + expect(assert).to.have.property(name); |
| 85 | + expect(typeof assert[name]).to.be.equal(typeof legacyModeAssert[name]); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments