Skip to content

Commit

Permalink
Improve test coverage;
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbytes committed Jun 24, 2017
1 parent 611166c commit f128b48
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 19 deletions.
3 changes: 2 additions & 1 deletion tests/average.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
describe('average', function()
it('errors when its parent errors', function()
expect(Rx.Observable.throw():average().subscribe).to.fail()
local _, onError = observableSpy(Rx.Observable.throw():average())
expect(#onError).to.equal(1)
end)

it('produces a single value representing the average of the values produced by the source', function()
Expand Down
17 changes: 14 additions & 3 deletions tests/buffer.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
describe('buffer', function()
it('produces an error if its parent errors', function()
local observable = Rx.Observable.of(''):map(function(x) return x() end)
expect(observable.subscribe).to.fail()
expect(observable:buffer().subscribe).to.fail()
local _, onError = observableSpy(Rx.Observable.throw():buffer())
expect(#onError).to.equal(1)
end)

it('fails if size is not specified', function()
Expand All @@ -24,4 +23,16 @@ describe('buffer', function()
local observable = Rx.Observable.fromRange(5)
expect(observable:buffer(2)).to.produce({{1, 2}, {3, 4}, {5}})
end)

it('produces a partial buffer if the observable errors', function()
local observable = Rx.Observable.create(function(observer)
observer:onNext(1)
observer:onNext(2)
observer:onNext(3)
observer:onError('oops')
end)
local onNext, onError = observableSpy(observable:buffer(2))
expect(onNext).to.equal({{1, 2}, {3}})
expect(#onError).to.equal(1)
end)
end)
6 changes: 6 additions & 0 deletions tests/catch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ describe('catch', function()
Rx.Observable.throw():catch(handler):subscribe(nil, onError, nil)
expect(#onError).to.equal(1)
end)

it('calls onComplete when the parent completes', function()
local onComplete = spy()
Rx.Observable.throw():catch():subscribe(nil, nil, onComplete)
expect(#onComplete).to.equal(1)
end)
end)
5 changes: 5 additions & 0 deletions tests/concat.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
describe('concat', function()
it('produces an error if its parent errors', function()
local _, onError = observableSpy(Rx.Observable.throw():concat())
expect(#onError).to.equal(1)
end)

it('returns the first argument if it is the only argument', function()
local observable = Rx.Observable.fromRange(1, 3):concat()
expect(observable).to.produce(1, 2, 3)
Expand Down
3 changes: 2 additions & 1 deletion tests/contains.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
describe('contains', function()
it('errors when its parent errors', function()
expect(Rx.Observable.throw():contains(1).subscribe).to.fail()
local _, onError = observableSpy(Rx.Observable.throw():contains(1))
expect(#onError).to.equal(1)
end)

it('returns false if the source Observable produces no values', function()
Expand Down
5 changes: 2 additions & 3 deletions tests/count.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
describe('count', function()
it('passes through errors', function()
local observable = Rx.Observable.create(function(observer) observer:onError() end)
expect(observable.subscribe).to.fail()
expect(observable:count().subscribe).to.fail()
local _, onError = observableSpy(Rx.Observable.throw():count())
expect(#onError).to.equal(1)
end)

it('produces a single value representing the number of elements produced by the source', function()
Expand Down
3 changes: 2 additions & 1 deletion tests/defaultIfEmpty.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
describe('defaultIfEmpty', function()
it('errors if the source errors', function()
expect(Rx.Observable.throw():defaultIfEmpty(1).subscribe).to.fail()
local _, onError = observableSpy(Rx.Observable.throw():defaultIfEmpty(1))
expect(#onError).to.equal(1)
end)

it('produces the values from the source unchanged if at least one value is produced', function()
Expand Down
5 changes: 2 additions & 3 deletions tests/distinct.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ describe('distinct', function()
end)

it('produces an error if its parent errors', function()
local observable = Rx.Observable.of(''):map(function(x) return x() end)
expect(observable.subscribe).to.fail()
expect(observable:distinct().subscribe).to.fail()
local _, onError = observableSpy(Rx.Observable.throw():distinct())
expect(#onError).to.equal(1)
end)

it('completes when its parent completes', function()
Expand Down
5 changes: 2 additions & 3 deletions tests/distinctUntilChanged.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
describe('distinctUntilChanged', function()
it('produces an error if its parent errors', function()
local observable = Rx.Observable.of(''):map(function(x) return x() end)
expect(observable.subscribe).to.fail()
expect(observable:distinctUntilChanged().subscribe).to.fail()
local _, onError = observableSpy(Rx.Observable.throw():distinctUntilChanged())
expect(#onError).to.equal(1)
end)

describe('with the default comparator', function()
Expand Down
13 changes: 12 additions & 1 deletion tests/elementAt.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
describe('elementAt', function()
it('errors when its parent errors', function()
expect(Rx.Observable.throw():elementAt(1).subscribe).to.fail()
local _, onError = observableSpy(Rx.Observable:throw():elementAt(0))
expect(#onError).to.equal(1)
end)

it('chains subscriptions', function()
Expand All @@ -10,6 +11,16 @@ describe('elementAt', function()
expect(observable:elementAt(1):subscribe()).to.equal(subscription)
end)

it('unsubscribes the subscription if the element is produced', function()
local subject = Rx.Subject.create()
local subscription = subject:elementAt(2):subscribe()
subscription.unsubscribe = spy()
subject:onNext('a')
expect(#subscription.unsubscribe).to.equal(0)
subject:onNext('b')
expect(#subscription.unsubscribe).to.equal(1)
end)

it('errors if no index is specified', function()
expect(Rx.Observable.of(1):elementAt().subscribe).to.fail()
end)
Expand Down
5 changes: 2 additions & 3 deletions tests/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ describe('filter', function()
end)

it('errors when its parent errors', function()
local observable = Rx.Observable.of(''):map(function(x) return x() end)
expect(observable.subscribe).to.fail()
expect(observable:filter().subscribe).to.fail()
local _, onError = observableSpy(Rx.Observable.throw():filter())
expect(#onError).to.equal(1)
end)

it('calls onError if the predicate errors', function()
Expand Down

0 comments on commit f128b48

Please sign in to comment.