|
| 1 | +import { expect } from 'chai' |
| 2 | + |
| 3 | +import { options } from '@tko/utils' |
| 4 | +import { Parser } from '../dist' |
| 5 | + |
| 6 | +function ctxStub(ctx?) { |
| 7 | + return { |
| 8 | + lookup(v) { |
| 9 | + return ctx?.[v] |
| 10 | + }, |
| 11 | + $data: ctx |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function evaluate(expr: string, vars: object = {}) { |
| 16 | + const bindings = new Parser().parse(`r: ${expr}`, ctxStub(vars)) |
| 17 | + return bindings.r() |
| 18 | +} |
| 19 | + |
| 20 | +describe('options.strictEquality', function () { |
| 21 | + afterEach(function () { |
| 22 | + options.strictEquality = false |
| 23 | + }) |
| 24 | + |
| 25 | + it('defaults to loose equality (==)', function () { |
| 26 | + expect(options.strictEquality).to.equal(false) |
| 27 | + // 0 == '' is true with loose equality |
| 28 | + expect(evaluate('x == y', { x: 0, y: '' })).to.equal(true) |
| 29 | + // null == undefined is true with loose equality |
| 30 | + expect(evaluate('x == y', { x: null, y: undefined })).to.equal(true) |
| 31 | + }) |
| 32 | + |
| 33 | + it('switches to strict equality when enabled', function () { |
| 34 | + options.strictEquality = true |
| 35 | + // 0 === '' is false with strict equality |
| 36 | + expect(evaluate('x == y', { x: 0, y: '' })).to.equal(false) |
| 37 | + // 0 === 0 is true |
| 38 | + expect(evaluate('x == y', { x: 0, y: 0 })).to.equal(true) |
| 39 | + // null === undefined is false |
| 40 | + expect(evaluate('x == y', { x: null, y: undefined })).to.equal(false) |
| 41 | + }) |
| 42 | + |
| 43 | + it('defaults to loose inequality (!=)', function () { |
| 44 | + // 0 != '' is false with loose equality (both are falsy) |
| 45 | + expect(evaluate('x != y', { x: 0, y: '' })).to.equal(false) |
| 46 | + }) |
| 47 | + |
| 48 | + it('switches to strict inequality when enabled', function () { |
| 49 | + options.strictEquality = true |
| 50 | + // 0 !== '' is true with strict equality |
| 51 | + expect(evaluate('x != y', { x: 0, y: '' })).to.equal(true) |
| 52 | + }) |
| 53 | + |
| 54 | + it('can be toggled back to loose', function () { |
| 55 | + options.strictEquality = true |
| 56 | + expect(evaluate('x == y', { x: 0, y: '' })).to.equal(false) // strict |
| 57 | + |
| 58 | + options.strictEquality = false |
| 59 | + expect(evaluate('x == y', { x: 0, y: '' })).to.equal(true) // loose again |
| 60 | + }) |
| 61 | + |
| 62 | + it('does not affect === and !== operators', function () { |
| 63 | + expect(evaluate('x === y', { x: 0, y: 0 })).to.equal(true) |
| 64 | + expect(evaluate('x === y', { x: 0, y: '' })).to.equal(false) |
| 65 | + expect(evaluate('x !== y', { x: 0, y: '' })).to.equal(true) |
| 66 | + |
| 67 | + options.strictEquality = true |
| 68 | + // === and !== should behave the same regardless of the setting |
| 69 | + expect(evaluate('x === y', { x: 0, y: 0 })).to.equal(true) |
| 70 | + expect(evaluate('x === y', { x: 0, y: '' })).to.equal(false) |
| 71 | + expect(evaluate('x !== y', { x: 0, y: '' })).to.equal(true) |
| 72 | + }) |
| 73 | +}) |
0 commit comments