RxLua/tests/distinctUntilChanged.lua
Junseong Jang f9ff630135 Add an assertion like 'expect(observable).to.produce.error()' to test '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
2019-03-28 03:04:58 +09:00

39 lines
1.7 KiB
Lua

describe('distinctUntilChanged', function()
it('produces an error if its parent errors', function()
expect(Rx.Observable.throw():distinctUntilChanged()).to.produce.error()
end)
describe('with the default comparator', function()
it('produces a value if it is the first value or different from the previous', function()
local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs):distinctUntilChanged()
expect(observable).to.produce(1, 3, 1, 4, 5)
end)
it('produces the first value if it is nil', function()
local observable = Rx.Observable.of(nil):distinctUntilChanged()
expect(observable).to.produce({{}})
end)
it('produces multiple onNext arguments but only uses the first argument to check for equality', function()
local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs, true):distinctUntilChanged()
expect(observable).to.produce({{1, 1}, {3, 3}, {1, 4}, {4, 5}, {5, 6}})
end)
end)
describe('with a custom comparator', function()
it('produces a value if it is the first value or the comparator returns false when passed the previous value and the current value', function()
local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs):distinctUntilChanged(function(x, y) return x % 2 == y % 2 end)
expect(observable).to.produce(1, 4, 5)
end)
it('produces the first value if it is nil', function()
local observable = Rx.Observable.of(nil):distinctUntilChanged(function(x, y) return true end)
expect(observable).to.produce({{}})
end)
it('calls onError if the comparator errors', function()
expect(Rx.Observable.fromRange(2):distinctUntilChanged(error)).to.produce.error()
end)
end)
end)