From a0dc0b69f578900313b36826d272e99934f0184d Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 24 Aug 2015 09:54:19 +0200 Subject: [PATCH 1/2] Add `Disposable::isDisposable` So that we can check whether an object correctly implements the `Disposable` contract. --- spec/disposable-spec.coffee | 10 ++++++++++ src/disposable.coffee | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/spec/disposable-spec.coffee b/spec/disposable-spec.coffee index 1f367ad..001c1ca 100644 --- a/spec/disposable-spec.coffee +++ b/spec/disposable-spec.coffee @@ -20,3 +20,13 @@ describe "Disposable", -> disposable.dispose() expect(disposalAction.callCount).toBe 1 + + describe "::isDisposable(object)", -> + it "returns true if the object implements the ::dispose function", -> + expect(Disposable.isDisposable(new Disposable(->))).toBe(true) + expect(Disposable.isDisposable({dispose: ->})).toBe(true) + + expect(Disposable.isDisposable(null)).toBe(false) + expect(Disposable.isDisposable(undefined)).toBe(false) + expect(Disposable.isDisposable({foo: ->})).toBe(false) + expect(Disposable.isDisposable({dispose: 1})).toBe(false) diff --git a/src/disposable.coffee b/src/disposable.coffee index 71d5724..fbffe82 100644 --- a/src/disposable.coffee +++ b/src/disposable.coffee @@ -6,6 +6,15 @@ module.exports = class Disposable disposed: false + # Public: Ensure that an `object` correctly implements the `Disposable` + # contract. + # + # * `object` Any object you want to perform the check against. + # + # Returns a {Boolean} indicating whether `object` is a valid `Disposable`. + @isDisposable: (object) -> + typeof object?.dispose is "function" + ### Section: Construction and Destruction ### From 69e9d608ef636ad852e0ba5ac3adf269bf164614 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 24 Aug 2015 18:28:52 +0200 Subject: [PATCH 2/2] :memo: Use dot notation to refer to class methods in tests --- spec/disposable-spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/disposable-spec.coffee b/spec/disposable-spec.coffee index 001c1ca..ec24879 100644 --- a/spec/disposable-spec.coffee +++ b/spec/disposable-spec.coffee @@ -21,7 +21,7 @@ describe "Disposable", -> disposable.dispose() expect(disposalAction.callCount).toBe 1 - describe "::isDisposable(object)", -> + describe ".isDisposable(object)", -> it "returns true if the object implements the ::dispose function", -> expect(Disposable.isDisposable(new Disposable(->))).toBe(true) expect(Disposable.isDisposable({dispose: ->})).toBe(true)