observable.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. describe('Observable', function()
  2. describe('create', function()
  3. it('returns an Observable', function()
  4. local observable = Rx.Observable.create()
  5. expect(observable).to.be.an(Rx.Observable)
  6. end)
  7. it('sets _subscribe to the first argument it was passed', function()
  8. local subscribe = function() end
  9. local observable = Rx.Observable.create(subscribe)
  10. expect(observable._subscribe).to.equal(subscribe)
  11. end)
  12. end)
  13. describe('subscribe', function()
  14. it('passes the first argument to _subscribe if it is a table', function()
  15. local observable = Rx.Observable.fromValue()
  16. local observer = Rx.Observer.create()
  17. local function run() observable:subscribe(observer) end
  18. expect(spy(observable, '_subscribe', run)).to.equal({{observer}})
  19. end)
  20. it('creates a new Observer using the first three arguments and passes it to _subscribe if the first argument is not a table', function()
  21. local observable = Rx.Observable.fromValue()
  22. local a, b, c = function() end, function() end, function() end
  23. local function run() observable:subscribe(a, b, c) end
  24. local observer = spy(observable, '_subscribe', run)[1][1]
  25. expect(observer).to.be.an(Rx.Observer)
  26. expect(observer._onNext).to.equal(a)
  27. expect(observer._onError).to.equal(b)
  28. expect(observer._onComplete).to.equal(c)
  29. end)
  30. end)
  31. describe('fromValue', function()
  32. it('returns an Observable that produces the first argument and completes', function()
  33. local observable = Rx.Observable.fromValue(1, 2, 3)
  34. expect(observable).to.produce(1)
  35. end)
  36. it('returns an Observable that produces nil and completes if no arguments are passed', function()
  37. local observable = Rx.Observable.fromValue()
  38. expect(observable).to.produce(nil)
  39. end)
  40. end)
  41. describe('fromRange', function()
  42. it('errors if no arguments are provided', function()
  43. local run = function() Rx.Observable.fromRange():subscribe() end
  44. expect(run).to.fail()
  45. end)
  46. describe('with one argument', function()
  47. it('returns an Observable that produces elements sequentially from 1 to the first argument', function()
  48. local observable = Rx.Observable.fromRange(5)
  49. expect(observable).to.produce(1, 2, 3, 4, 5)
  50. end)
  51. it('returns an Observable that produces no elements if the first argument is less than one', function()
  52. local observable = Rx.Observable.fromRange(0)
  53. expect(observable).to.produce.nothing()
  54. end)
  55. end)
  56. describe('with two arguments', function()
  57. it('returns an Observable that produces elements sequentially from the first argument to the second argument', function()
  58. local observable = Rx.Observable.fromRange(1, 5)
  59. expect(observable).to.produce(1, 2, 3, 4, 5)
  60. end)
  61. it('returns an Observable that produces no elements if the first argument is greater than the second argument', function()
  62. local observable = Rx.Observable.fromRange(1, -5)
  63. expect(observable).to.produce.nothing()
  64. end)
  65. end)
  66. describe('with three arguments', function()
  67. it('returns an Observable that produces elements sequentially from the first argument to the second argument, incrementing by the third argument', function()
  68. local observable = Rx.Observable.fromRange(1, 5, 2)
  69. expect(observable).to.produce(1, 3, 5)
  70. end)
  71. end)
  72. end)
  73. describe('fromTable', function()
  74. it('errors if the first argument is not a table', function()
  75. local function run() Rx.Observable.fromTable():subscribe() end
  76. expect(run).to.fail()
  77. end)
  78. describe('with one argument', function()
  79. it('returns an Observable that produces values by iterating the table using pairs', function()
  80. local input = {foo = 'bar', 1, 2, 3}
  81. local observable = Rx.Observable.fromTable(input)
  82. local result = {}
  83. for key, value in pairs(input) do table.insert(result, {value}) end
  84. expect(observable).to.produce(result)
  85. end)
  86. end)
  87. describe('with two arguments', function()
  88. it('returns an Observable that produces values by iterating the table using the second argument', function()
  89. local input = {foo = 'bar', 3, 4, 5}
  90. local observable = Rx.Observable.fromTable(input, ipairs)
  91. expect(observable).to.produce(3, 4, 5)
  92. end)
  93. end)
  94. describe('with three arguments', function()
  95. it('returns an Observable that produces value-key pairs by iterating the table if the third argument is true', function()
  96. local input = {foo = 'bar', 3, 4, 5}
  97. local observable = Rx.Observable.fromTable(input, ipairs, true)
  98. expect(observable).to.produce({{3, 1}, {4, 2}, {5, 3}})
  99. end)
  100. end)
  101. end)
  102. describe('fromCoroutine', function()
  103. it('returns an Observable that produces a value whenever the first argument yields a value', function()
  104. local coroutine = coroutine.create(function()
  105. coroutine.yield(1)
  106. coroutine.yield(2)
  107. return 3
  108. end)
  109. Rx.scheduler = Rx.Scheduler.Cooperative.create()
  110. local observable = Rx.Observable.fromCoroutine(coroutine)
  111. local onNext, onError, onComplete = observableSpy(observable)
  112. repeat Rx.scheduler:update()
  113. until Rx.scheduler:isEmpty()
  114. expect(onNext).to.equal({{1}, {2}, {3}})
  115. end)
  116. it('accepts a function as the first argument and wraps it into a coroutine', function()
  117. local coroutine = function()
  118. coroutine.yield(1)
  119. coroutine.yield(2)
  120. return 3
  121. end
  122. Rx.scheduler = Rx.Scheduler.Cooperative.create()
  123. local observable = Rx.Observable.fromCoroutine(coroutine)
  124. local onNext, onError, onComplete = observableSpy(observable)
  125. repeat Rx.scheduler:update()
  126. until Rx.scheduler:isEmpty()
  127. expect(onNext).to.equal({{1}, {2}, {3}})
  128. end)
  129. end)
  130. describe('dump', function()
  131. end)
  132. dofile('tests/changes.lua')
  133. dofile('tests/combine.lua')
  134. dofile('tests/compact.lua')
  135. dofile('tests/concat.lua')
  136. dofile('tests/distinct.lua')
  137. dofile('tests/filter.lua')
  138. dofile('tests/find.lua')
  139. dofile('tests/first.lua')
  140. dofile('tests/flatten.lua')
  141. dofile('tests/last.lua')
  142. dofile('tests/map.lua')
  143. dofile('tests/max.lua')
  144. dofile('tests/min.lua')
  145. dofile('tests/merge.lua')
  146. dofile('tests/pack.lua')
  147. dofile('tests/pluck.lua')
  148. dofile('tests/reduce.lua')
  149. end)