feat: negation support (#4)

* feat: negation support

* test: coverage for not, update old coverage
This commit is contained in:
Marc 2023-03-28 21:24:44 -04:00
parent c937fc3a60
commit b34766244f
Signed by: marc
GPG key ID: 048E042F22B5DC79
4 changed files with 48 additions and 42 deletions

View file

@ -6,7 +6,6 @@ function getContext(): IContext {
if (!runnerContext) {
runnerContext = {
collectedTests: new Map<TestFilePath, any>(),
collecting: true,
}
}

View file

@ -7,21 +7,36 @@ class TestAssertionFailed extends Error {
}
}
// TODO(mcat): This should just be using `assert`
class Expectation<ValueType> {
value: ValueType
negated: boolean
constructor(value: ValueType) {
this.value = value
this.negated = false
}
/*
* Negates the expectation.
*/
get not() {
this.negated = !this.negated
return this
}
toEqual(value: ValueType) {
assert.deepEqual(this.value, value, new TestAssertionFailed(`NotEqual! ${this.value} != ${value}`))
if (this.negated) {
assert.notDeepEqual(this.value, value, new TestAssertionFailed(`Equal! ${this.value} = ${value}`))
} else {
assert.deepEqual(this.value, value, new TestAssertionFailed(`NotEqual! ${this.value} != ${value}`))
}
}
toBe(value: ValueType) {
if (Object.is(this.value, value)) return
throw new TestAssertionFailed(`NotEqual! ${this.value} !== ${value}`)
const isSame = Object.is(this.value, value)
if ((isSame && !this.negated) || (!isSame && this.negated)) return
throw new TestAssertionFailed(`NotEqual! ${this.value} ${this.negated ? '===' : '!=='} ${value}`)
}
}

View file

@ -4,5 +4,4 @@ export type TestCaseFunction = () => void
export interface IContext {
collectedTests: Map<TestFilePath, any>
collecting: boolean
}

View file

@ -1,73 +1,66 @@
import assert from 'assert'
import { test, expect } from '../src/testCaseUtils'
test('Equality (number)', () => {
expect(1).toEqual(1)
assert.doesNotThrow(() => expect(1).toEqual(1))
})
test('Equality (string)', () => {
expect('expectations').toEqual('expectations')
assert.doesNotThrow(() => expect('expectations').toEqual('expectations'))
})
test('Equality (boolean)', () => {
expect(true).toEqual(true)
assert.doesNotThrow(() => expect(true).toEqual(true))
})
test('Equality (failed - number)', () => {
try {
expect(1).toEqual(2)
} catch (e) {
expect(1).toEqual(1)
}
assert.throws(() => expect(1).toEqual(2))
})
test('Equality (failed - string)', () => {
try {
expect('expectation').toEqual('something else')
} catch (e) {
expect(1).toEqual(1)
}
assert.throws(() => expect('expectation').toEqual('something else'))
})
test('Equality (failed - boolean)', () => {
try {
expect(true).toEqual(false)
} catch (e) {
expect(1).toEqual(1)
}
assert.throws(() => expect(true).toEqual(false))
})
test('Identity comparison (number)', () => {
expect(1).toBe(1)
assert.doesNotThrow(() => expect(1).toBe(1))
})
test('Identity comparison (boolean)', () => {
expect(true).toBe(true)
assert.doesNotThrow(() => expect(true).toBe(true))
})
test('Identity comparison (string)', () => {
expect('identity').toBe('identity')
assert.doesNotThrow(() => expect('identity').toBe('identity'))
})
test('Identity comparison (failed - number)', () => {
try {
expect(1).toEqual(2)
} catch (e) {
expect(1).toEqual(1)
}
assert.throws(() => expect(1).toEqual(2))
})
test('Identity comparison (failed - boolean)', () => {
try {
expect(false).toBe(true)
} catch (e) {
expect(1).toEqual(1)
}
assert.throws(() => expect(false).toBe(true))
})
test('Identity comparison (failed - string)', () => {
try {
expect('yes').toBe('no')
} catch (e) {
expect(1).toEqual(1)
}
assert.throws(() => expect('yes').toBe('no'))
})
test('Equality negation', () => {
assert.doesNotThrow(() => expect('yes').not.toEqual('no'))
})
test('Stacked equality negation', () => {
assert.doesNotThrow(() => expect('yes').not.not.toEqual('yes'))
})
test('Identity negation', () => {
assert.doesNotThrow(() => expect('yes').not.toBe('no'))
})
test('Identity negation (fail)', () => {
assert.throws(() => expect('yes').not.toBe('yes'))
})