pluck.lua 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. describe('pluck', function()
  2. it('returns the original observable if no key is specified', function()
  3. local observable = Rx.Observable.of(7)
  4. expect(observable:pluck()).to.equal(observable)
  5. end)
  6. it('retrieves the specified key from tables produced by the observable', function()
  7. local t = {
  8. { color = 'red' },
  9. { color = 'green' },
  10. { color = 'blue' }
  11. }
  12. expect(Rx.Observable.fromTable(t, ipairs):pluck('color')).to.produce('red', 'green', 'blue')
  13. end)
  14. it('supports numeric pluckage', function()
  15. local t = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
  16. expect(Rx.Observable.fromTable(t, ipairs):pluck(2)).to.produce(2, 5, 8)
  17. end)
  18. it('supports deep pluckage', function()
  19. local t = {
  20. { name = { first = 'Robert', last = 'Paulsen' } },
  21. { name = { first = 'Bond', last = 'James Bond' } }
  22. }
  23. expect(Rx.Observable.fromTable(t, ipairs):pluck('name', 'first')).to.produce('Robert', 'Bond')
  24. end)
  25. it('errors if a key does not exist', function()
  26. local california = {}
  27. expect(Rx.Observable.fromTable(california):pluck('water')).to.fail()
  28. end)
  29. it('respects metatables', function()
  30. local t = setmetatable({}, {__index = {a = 'b'}})
  31. expect(Rx.Observable.of(t):pluck('a')).to.produce('b')
  32. end)
  33. end)