observable.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 value-key pairs 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, key}) end
  84. expect(observable).to.produce(result)
  85. end)
  86. end)
  87. describe('with two arguments', function()
  88. it('returns an Observable that produces value-key pairs 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, 1}, {4, 2}, {5, 3}})
  92. end)
  93. end)
  94. end)
  95. describe('fromCoroutine', function()
  96. it('returns an Observable that produces a value whenever the first argument yields a value', function()
  97. local coroutine = coroutine.create(function()
  98. coroutine.yield(1)
  99. coroutine.yield(2)
  100. return 3
  101. end)
  102. local observable = Rx.Observable.fromCoroutine(coroutine)
  103. local onNext, onError, onComplete = observableSpy(observable)
  104. repeat Rx.scheduler:update()
  105. until Rx.scheduler:isEmpty()
  106. expect(onNext).to.equal({{1}, {2}, {3}})
  107. end)
  108. it('accepts a function as the first argument and wraps it into a coroutine', function()
  109. local coroutine = function()
  110. coroutine.yield(1)
  111. coroutine.yield(2)
  112. return 3
  113. end
  114. local observable = Rx.Observable.fromCoroutine(coroutine)
  115. local onNext, onError, onComplete = observableSpy(observable)
  116. repeat Rx.scheduler:update()
  117. until Rx.scheduler:isEmpty()
  118. expect(onNext).to.equal({{1}, {2}, {3}})
  119. end)
  120. end)
  121. describe('dump', function()
  122. end)
  123. end)