Skip to content

Commit

Permalink
Add an assertion like 'expect(observable).to.produce.error()' to test…
Browse files Browse the repository at this point in the history
… 'onError'.

Fixed wrong test codes with the assertion.

Changed the behaviors of the following functions caused by argument types, to raise an error in the creation phase.

- Observable.defer
- Observable:buffer
- Observable:elementAt
- Observable:skipLast
- Observable:takeLast
- Observable:window
  • Loading branch information
naxxster committed Mar 27, 2019
1 parent c6e6c54 commit f9ff630
Show file tree
Hide file tree
Showing 55 changed files with 154 additions and 150 deletions.
1 change: 1 addition & 0 deletions doc/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Tests
- To run a specific test file, run `lua tests/runner.lua average` to just run the tests in `tests/average.lua`.
- In addition to lust's default operators, there are also a few additional utilities available via `runner.lua`:
- `expect(Observable).to.produce(...)` will assert that the Observable produces the specified values, in order. If you need to assert against multiple values emitted by a single `onNext`, you can pass in a table (i.e. `{{1, 2, 3}, {4, 5, 6}}` to check that an Observable calls `onNext` twice with 3 values each).
- `expect(Observable).to.produce.error()` will assert that the Observable produces an error.
- There is also `expect(Observable).to.produce.nothing()`.
- `local onNext, onError, onCompleted = observableSpy(observable)` will create three spies for each of the three events. You can read more about spies on lust's README.

Expand Down
24 changes: 24 additions & 0 deletions rx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ end
-- @arg {function} factory - A function that returns an Observable.
-- @returns {Observable}
function Observable.defer(fn)
if not fn or type(fn) ~= 'function' then
error('Expected a function')
end

return setmetatable({
subscribe = function(_, ...)
local observable = fn()
Expand Down Expand Up @@ -396,6 +400,10 @@ end
-- values.
-- @arg {number} size - The size of the buffer.
function Observable:buffer(size)
if not size or type(size) ~= 'number' then
error('Expected a number')
end

return Observable.create(function(observer)
local buffer = {}

Expand Down Expand Up @@ -802,6 +810,10 @@ end
-- @arg {number} index - The index of the item, with an index of 1 representing the first.
-- @returns {Observable}
function Observable:elementAt(index)
if not index or type(index) ~= 'number' then
error('Expected a number')
end

return Observable.create(function(observer)
local subscription
local i = 1
Expand Down Expand Up @@ -1354,6 +1366,10 @@ end
-- @arg {number} count - The number of items to omit from the end.
-- @returns {Observable}
function Observable:skipLast(count)
if not count or type(count) ~= 'number' then
error('Expected a number')
end

local buffer = {}
return Observable.create(function(observer)
local function emit()
Expand Down Expand Up @@ -1550,6 +1566,10 @@ end
-- @arg {number} count - The number of elements to produce.
-- @returns {Observable}
function Observable:takeLast(count)
if not count or type(count) ~= 'number' then
error('Expected a number')
end

return Observable.create(function(observer)
local buffer = {}

Expand Down Expand Up @@ -1708,6 +1728,10 @@ end
-- of the most recent values as multiple arguments to onNext.
-- @returns {Observable}
function Observable:window(size)
if not size or type(size) ~= 'number' then
error('Expected a number')
end

return Observable.create(function(observer)
local window = {}

Expand Down
4 changes: 4 additions & 0 deletions src/observable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ end
-- @arg {function} factory - A function that returns an Observable.
-- @returns {Observable}
function Observable.defer(fn)
if not fn or type(fn) ~= 'function' then
error('Expected a function')
end

return setmetatable({
subscribe = function(_, ...)
local observable = fn()
Expand Down
4 changes: 4 additions & 0 deletions src/operators/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ local util = require 'util'
-- values.
-- @arg {number} size - The size of the buffer.
function Observable:buffer(size)
if not size or type(size) ~= 'number' then
error('Expected a number')
end

return Observable.create(function(observer)
local buffer = {}

Expand Down
4 changes: 4 additions & 0 deletions src/operators/elementAt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ local Observable = require 'observable'
-- @arg {number} index - The index of the item, with an index of 1 representing the first.
-- @returns {Observable}
function Observable:elementAt(index)
if not index or type(index) ~= 'number' then
error('Expected a number')
end

return Observable.create(function(observer)
local subscription
local i = 1
Expand Down
4 changes: 4 additions & 0 deletions src/operators/skipLast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ local util = require 'util'
-- @arg {number} count - The number of items to omit from the end.
-- @returns {Observable}
function Observable:skipLast(count)
if not count or type(count) ~= 'number' then
error('Expected a number')
end

local buffer = {}
return Observable.create(function(observer)
local function emit()
Expand Down
4 changes: 4 additions & 0 deletions src/operators/takeLast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ local util = require 'util'
-- @arg {number} count - The number of elements to produce.
-- @returns {Observable}
function Observable:takeLast(count)
if not count or type(count) ~= 'number' then
error('Expected a number')
end

return Observable.create(function(observer)
local buffer = {}

Expand Down
4 changes: 4 additions & 0 deletions src/operators/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ local util = require 'util'
-- of the most recent values as multiple arguments to onNext.
-- @returns {Observable}
function Observable:window(size)
if not size or type(size) ~= 'number' then
error('Expected a number')
end

return Observable.create(function(observer)
local window = {}

Expand Down
10 changes: 3 additions & 7 deletions tests/all.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
describe('all', function()
it('passes through errors', function()
expect(Rx.Observable.throw():all().subscribe).to.fail()
expect(Rx.Observable.throw():all()).to.produce.error()
end)

it('calls onError if the predicate errors', function()
local observable = Rx.Observable.fromRange(3):all(error)
local onError = spy()
observable:subscribe(nil, onError, nil)
expect(#onError).to.equal(1)
expect(Rx.Observable.fromRange(3):all(error)).to.produce.error()
end)

it('produces an error if the parent errors', function()
local _, onError = observableSpy(Rx.Observable.throw():all(function(x) return x end))
expect(#onError).to.equal(1)
expect(Rx.Observable.throw():all(function(x) return x end)).to.produce.error()
end)

it('produces true if all elements satisfy the predicate', function()
Expand Down
3 changes: 1 addition & 2 deletions tests/average.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
describe('average', function()
it('errors when its parent errors', function()
local _, onError = observableSpy(Rx.Observable.throw():average())
expect(#onError).to.equal(1)
expect(Rx.Observable.throw():average()).to.produce.error()
end)

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

it('fails if size is not specified', function()
expect(Rx.Observable.fromRange(5):buffer().subscribe).to.fail()
expect(function () Rx.Observable.fromRange(5):buffer() end).to.fail()
end)

it('produces values wrapped to the specified width', function()
Expand Down
4 changes: 1 addition & 3 deletions tests/catch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ describe('catch', function()

it('calls onError if the supplied function errors', function()
local handler = error
local onError = spy()
Rx.Observable.throw():catch(handler):subscribe(nil, onError, nil)
expect(#onError).to.equal(1)
expect(Rx.Observable.throw():catch(handler)).to.produce.error()
end)

it('calls onComplete when the parent completes', function()
Expand Down
8 changes: 1 addition & 7 deletions tests/combineLatest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ describe('combineLatest', function()
expect(#errored).to.equal(1)
end)

it('calls onError if the combinator is absent', function()
expect(Rx.Observable.combineLatest(Rx.Observable.fromRange(3)).subscribe).to.fail()
end)

it('calls onError if the combinator errors', function()
local onError = spy()
Rx.Observable.combineLatest(Rx.Observable.fromRange(3), error):subscribe(nil, onError, nil)
expect(#onError).to.equal(1)
expect(Rx.Observable.combineLatest(Rx.Observable.fromRange(3), error)).to.produce.error()
end)
end)
4 changes: 2 additions & 2 deletions tests/compact.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
describe('compact', 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:compact().subscribe).to.fail()
expect(observable).to.produce.error()
expect(observable:compact()).to.produce.error()
end)

it('does not produce values that are false or nil', function()
Expand Down
5 changes: 2 additions & 3 deletions tests/concat.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
describe('concat', function()
it('produces an error if its parent errors', function()
local _, onError = observableSpy(Rx.Observable.throw():concat())
expect(#onError).to.equal(1)
expect(Rx.Observable.throw():concat()).to.produce.error()
end)

it('returns the first argument if it is the only argument', function()
Expand All @@ -28,7 +27,7 @@ describe('concat', function()
it('should error if any of the sources error', function()
local badObservable = Rx.Observable.create(function(observer) observer:onError('oh no') end)
local observable = Rx.Observable.of(1):concat(Rx.Observable.of(2), badObservable)
expect(observable.subscribe).to.fail()
expect(observable).to.produce.error()
end)

it('should complete once the rightmost observable completes', function()
Expand Down
3 changes: 1 addition & 2 deletions tests/contains.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
describe('contains', function()
it('errors when its parent errors', function()
local _, onError = observableSpy(Rx.Observable.throw():contains(1))
expect(#onError).to.equal(1)
expect(Rx.Observable.throw():contains(1)).to.produce.error()
end)

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

it('produces a single value representing the number of elements produced by the source', function()
Expand All @@ -15,8 +14,6 @@ describe('count', function()
end)

it('calls onError if the predicate errors', function()
local onError = spy()
Rx.Observable.fromRange(3):count(error):subscribe(nil, onError, nil)
expect(#onError).to.equal(1)
expect(Rx.Observable.fromRange(3):count(error)).to.produce.error()
end)
end)
3 changes: 1 addition & 2 deletions tests/defaultIfEmpty.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
describe('defaultIfEmpty', function()
it('errors if the source errors', function()
local _, onError = observableSpy(Rx.Observable.throw():defaultIfEmpty(1))
expect(#onError).to.equal(1)
expect(Rx.Observable.throw():defaultIfEmpty(1)).to.produce.error()
end)

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

it('produces an error if its parent errors', function()
local _, onError = observableSpy(Rx.Observable.throw():distinct())
expect(#onError).to.equal(1)
expect(Rx.Observable.throw():distinct()).to.produce.error()
end)

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

describe('with the default comparator', function()
Expand Down Expand Up @@ -33,9 +32,7 @@ describe('distinctUntilChanged', function()
end)

it('calls onError if the comparator errors', function()
local onError = spy()
Rx.Observable.fromRange(2):distinctUntilChanged(error):subscribe(nil, onError, nil)
expect(#onError).to.equal(1)
expect(Rx.Observable.fromRange(2):distinctUntilChanged(error)).to.produce.error()
end)
end)
end)
5 changes: 2 additions & 3 deletions tests/elementAt.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
describe('elementAt', function()
it('errors when its parent errors', function()
local _, onError = observableSpy(Rx.Observable:throw():elementAt(0))
expect(#onError).to.equal(1)
expect(Rx.Observable:throw():elementAt(0)).to.produce.error()
end)

it('chains subscriptions', function()
Expand All @@ -22,7 +21,7 @@ describe('elementAt', function()
end)

it('errors if no index is specified', function()
expect(Rx.Observable.of(1):elementAt().subscribe).to.fail()
expect(function () Rx.Observable.of(1):elementAt() end).to.fail()
end)

it('produces no values if the specified index is less than one', function()
Expand Down
7 changes: 2 additions & 5 deletions tests/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ describe('filter', function()
end)

it('errors when its parent errors', function()
local _, onError = observableSpy(Rx.Observable.throw():filter())
expect(#onError).to.equal(1)
expect(Rx.Observable.throw():filter()).to.produce.error()
end)

it('calls onError if the predicate errors', function()
local onError = spy()
Rx.Observable.of(5):filter(error):subscribe(nil, onError, nil)
expect(#onError).to.equal(1)
expect(Rx.Observable.of(5):filter(error)).to.produce.error()
end)
end)
8 changes: 3 additions & 5 deletions tests/find.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
describe('find', 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:find().subscribe).to.fail()
expect(observable).to.produce.error()
expect(observable:find()).to.produce.error()
end)

it('calls onError if the predicate errors', function()
local onError = spy()
Rx.Observable.of(3):find(error):subscribe(nil, onError, nil)
expect(#onError).to.equal(1)
expect(Rx.Observable.of(3):find(error)).to.produce.error()
end)

it('uses the identity function as a predicate if none is specified', function()
Expand Down
4 changes: 2 additions & 2 deletions tests/first.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
describe('first', 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:first().subscribe).to.fail()
expect(observable).to.produce.error()
expect(observable:first()).to.produce.error()
end)

it('produces no elements if its parent produces no elements', function()
Expand Down
2 changes: 1 addition & 1 deletion tests/flatMap.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('flatMap', function()
it('produces an error if its parent errors', function()
local observable = Rx.Observable.of(''):flatMap(function(x) return x() end)
expect(observable.subscribe).to.fail()
expect(observable).to.produce.error()
end)

it('uses the identity function as the callback if none is specified', function()
Expand Down
4 changes: 1 addition & 3 deletions tests/flatMapLatest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ describe('flatMapLatest', function()
end)

it('produces an error if the callback errors', function()
local onError = spy()
Rx.Observable.fromRange(3):flatMapLatest(error):subscribe(nil, onError, nil)
expect(#onError).to.equal(1)
expect(Rx.Observable.fromRange(3):flatMapLatest(error)).to.produce.error()
end)

it('unsubscribes from the source and the projected observable', function()
Expand Down
Loading

0 comments on commit f9ff630

Please sign in to comment.