-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(predicates): Check has been deprecated in favour of Ensure. New …
…"deprecated" decorator can be u
- Loading branch information
Showing
38 changed files
with
305 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import 'mocha'; | ||
import { given } from 'mocha-testdata'; | ||
|
||
import { check, hasLengthOf, isArray, isDefined, isGreaterThan, isInteger, TinyType } from '../src'; | ||
import { ensure, hasLengthOf, isArray, isDefined, isGreaterThan, isInteger, TinyType } from '../src'; | ||
import { expect } from './expect'; | ||
|
||
/** @test {check} */ | ||
describe('::check', () => { | ||
/** @test {ensure} */ | ||
describe('::ensure', () => { | ||
|
||
it(`advises the developer if they've forgotten to specify the checks`, () => { | ||
const value = 2; | ||
expect(() => check('SomeProperty', value)) | ||
expect(() => ensure('SomeProperty', value)) | ||
.to.throw(`Looks like you haven't specified any predicates to check the value against?`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import * as chai from 'chai'; | ||
import * as sinonChai from 'sinon-chai'; | ||
|
||
chai.use(sinonChai); | ||
|
||
export const expect = chai.expect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import 'mocha'; | ||
import * as sinon from 'sinon'; | ||
import { deprecated } from '../../src/objects'; | ||
import { expect } from '../expect'; | ||
|
||
/** @test {deprecated} */ | ||
describe('deprecated', () => { | ||
|
||
describe('when used to annotate a class', () => { | ||
it('logs a warning when the class is constructed', () => { | ||
const consoleWarn = sinon.spy(); | ||
|
||
@deprecated(undefined, consoleWarn) | ||
class Foo { | ||
} | ||
|
||
const foo = new Foo(); | ||
|
||
expect(consoleWarn) | ||
.to.have.been.calledWith('Foo has been deprecated.'); | ||
}); | ||
|
||
it('can provide additional suggestion on what other class should be used instead', () => { | ||
const consoleWarn = sinon.spy(); | ||
|
||
@deprecated('Please use Bar instead.', consoleWarn) | ||
class Foo { | ||
} | ||
|
||
const foo = new Foo(); | ||
|
||
expect(consoleWarn) | ||
.to.have.been.calledWith('Foo has been deprecated. Please use Bar instead.'); | ||
}); | ||
|
||
it('maintains the type and behaviour of the annotated class', () => { | ||
const consoleWarn = sinon.spy(); | ||
|
||
@deprecated('Please use Client instead.', consoleWarn) | ||
class Person { | ||
constructor(public readonly name: string) { | ||
} | ||
} | ||
|
||
const p = new Person('Alice'); | ||
|
||
expect(consoleWarn) | ||
.to.have.been.calledWith('Person has been deprecated. Please use Client instead.'); | ||
|
||
expect(p).to.be.instanceOf(Person); | ||
}); | ||
}); | ||
|
||
describe('when used to annotate a method', () => { | ||
|
||
it('logs a warning when the method is used', () => { | ||
const consoleWarn = sinon.spy(); | ||
|
||
class Person { | ||
@deprecated('', consoleWarn) | ||
greet() { return null; } | ||
welcome() { return null; } | ||
} | ||
|
||
const p = new Person(); | ||
p.greet(); | ||
|
||
expect(consoleWarn) | ||
.to.have.been.calledWith('Person#greet has been deprecated.'); | ||
}); | ||
|
||
it('can provide additional suggestion on what other method should be used instead', () => { | ||
const consoleWarn = sinon.spy(); | ||
|
||
class Person { | ||
|
||
@deprecated('Please use Person#welcome instead.', consoleWarn) | ||
greet() { return null; } | ||
welcome() { return null; } | ||
} | ||
|
||
const p = new Person(); | ||
p.greet(); | ||
|
||
expect(consoleWarn) | ||
.to.have.been.calledWith('Person#greet has been deprecated. Please use Person#welcome instead.'); | ||
}); | ||
|
||
it('maintains the behaviour of the annotated method', () => { | ||
const consoleWarn = sinon.spy(); | ||
|
||
class Person { | ||
constructor(public readonly name: string) { | ||
} | ||
|
||
@deprecated('Please use Person#welcome instead.', consoleWarn) | ||
greet(greeting: string) { | ||
return `${ greeting }, my name is ${this.name}`; | ||
} | ||
} | ||
|
||
const p = new Person('Alice'); | ||
|
||
expect(p.greet('Hi')).to.equal('Hi, my name is Alice'); | ||
|
||
expect(consoleWarn) | ||
.to.have.been.calledWith('Person#greet has been deprecated. Please use Person#welcome instead.'); | ||
}); | ||
}); | ||
|
||
describe('when used to deprecate a function', () => { | ||
it('logs a warning when the function is used', () => { | ||
const consoleWarn = sinon.spy(); | ||
|
||
function foo() { | ||
return null; | ||
} | ||
|
||
const deprecatedFoo = deprecated('Please use bar instead.', consoleWarn)(foo); | ||
|
||
deprecatedFoo(); | ||
|
||
expect(consoleWarn) | ||
.to.have.been.calledWith('foo has been deprecated. Please use bar instead.'); | ||
|
||
}); | ||
|
||
it('logs a warning when an arrow function is used', () => { | ||
const consoleWarn = sinon.spy(); | ||
|
||
const foo = () => null; | ||
|
||
const deprecatedFoo = deprecated('Please use bar instead.', consoleWarn)(foo); | ||
|
||
deprecatedFoo(); | ||
|
||
expect(consoleWarn) | ||
.to.have.been.calledWith('foo has been deprecated. Please use bar instead.'); | ||
}); | ||
}); | ||
|
||
describe('when used incorrectly', () => { | ||
|
||
it('complains', () => { | ||
const consoleWarn = sinon.spy(); | ||
|
||
expect(() => deprecated('something that does not make sense')(42)) | ||
.to.throw(`Only a class, method or function can be marked as deprecated. number given.`); | ||
}); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.