فهرست منبع

Improve test coverage;

bjorn 8 سال پیش
والد
کامیت
f128b4844a
11فایلهای تغییر یافته به همراه51 افزوده شده و 19 حذف شده
  1. 2 1
      tests/average.lua
  2. 14 3
      tests/buffer.lua
  3. 6 0
      tests/catch.lua
  4. 5 0
      tests/concat.lua
  5. 2 1
      tests/contains.lua
  6. 2 3
      tests/count.lua
  7. 2 1
      tests/defaultIfEmpty.lua
  8. 2 3
      tests/distinct.lua
  9. 2 3
      tests/distinctUntilChanged.lua
  10. 12 1
      tests/elementAt.lua
  11. 2 3
      tests/filter.lua

+ 2 - 1
tests/average.lua

@@ -1,6 +1,7 @@
 describe('average', function()
   it('errors when its parent errors', function()
-    expect(Rx.Observable.throw():average().subscribe).to.fail()
+    local _, onError = observableSpy(Rx.Observable.throw():average())
+    expect(#onError).to.equal(1)
   end)
 
   it('produces a single value representing the average of the values produced by the source', function()

+ 14 - 3
tests/buffer.lua

@@ -1,8 +1,7 @@
 describe('buffer', 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:buffer().subscribe).to.fail()
+    local _, onError = observableSpy(Rx.Observable.throw():buffer())
+    expect(#onError).to.equal(1)
   end)
 
   it('fails if size is not specified', function()
@@ -24,4 +23,16 @@ describe('buffer', function()
     local observable = Rx.Observable.fromRange(5)
     expect(observable:buffer(2)).to.produce({{1, 2}, {3, 4}, {5}})
   end)
+
+  it('produces a partial buffer if the observable errors', function()
+    local observable = Rx.Observable.create(function(observer)
+      observer:onNext(1)
+      observer:onNext(2)
+      observer:onNext(3)
+      observer:onError('oops')
+    end)
+    local onNext, onError = observableSpy(observable:buffer(2))
+    expect(onNext).to.equal({{1, 2}, {3}})
+    expect(#onError).to.equal(1)
+  end)
 end)

+ 6 - 0
tests/catch.lua

@@ -35,4 +35,10 @@ describe('catch', function()
     Rx.Observable.throw():catch(handler):subscribe(nil, onError, nil)
     expect(#onError).to.equal(1)
   end)
+
+  it('calls onComplete when the parent completes', function()
+    local onComplete = spy()
+    Rx.Observable.throw():catch():subscribe(nil, nil, onComplete)
+    expect(#onComplete).to.equal(1)
+  end)
 end)

+ 5 - 0
tests/concat.lua

@@ -1,4 +1,9 @@
 describe('concat', function()
+  it('produces an error if its parent errors', function()
+    local _, onError = observableSpy(Rx.Observable.throw():concat())
+    expect(#onError).to.equal(1)
+  end)
+
   it('returns the first argument if it is the only argument', function()
     local observable = Rx.Observable.fromRange(1, 3):concat()
     expect(observable).to.produce(1, 2, 3)

+ 2 - 1
tests/contains.lua

@@ -1,6 +1,7 @@
 describe('contains', function()
   it('errors when its parent errors', function()
-    expect(Rx.Observable.throw():contains(1).subscribe).to.fail()
+    local _, onError = observableSpy(Rx.Observable.throw():contains(1))
+    expect(#onError).to.equal(1)
   end)
 
   it('returns false if the source Observable produces no values', function()

+ 2 - 3
tests/count.lua

@@ -1,8 +1,7 @@
 describe('count', function()
   it('passes through errors', function()
-    local observable = Rx.Observable.create(function(observer) observer:onError() end)
-    expect(observable.subscribe).to.fail()
-    expect(observable:count().subscribe).to.fail()
+    local _, onError = observableSpy(Rx.Observable.throw():count())
+    expect(#onError).to.equal(1)
   end)
 
   it('produces a single value representing the number of elements produced by the source', function()

+ 2 - 1
tests/defaultIfEmpty.lua

@@ -1,6 +1,7 @@
 describe('defaultIfEmpty', function()
   it('errors if the source errors', function()
-    expect(Rx.Observable.throw():defaultIfEmpty(1).subscribe).to.fail()
+    local _, onError = observableSpy(Rx.Observable.throw():defaultIfEmpty(1))
+    expect(#onError).to.equal(1)
   end)
 
   it('produces the values from the source unchanged if at least one value is produced', function()

+ 2 - 3
tests/distinct.lua

@@ -5,9 +5,8 @@ describe('distinct', function()
   end)
 
   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:distinct().subscribe).to.fail()
+    local _, onError = observableSpy(Rx.Observable.throw():distinct())
+    expect(#onError).to.equal(1)
   end)
 
   it('completes when its parent completes', function()

+ 2 - 3
tests/distinctUntilChanged.lua

@@ -1,8 +1,7 @@
 describe('distinctUntilChanged', 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:distinctUntilChanged().subscribe).to.fail()
+    local _, onError = observableSpy(Rx.Observable.throw():distinctUntilChanged())
+    expect(#onError).to.equal(1)
   end)
 
   describe('with the default comparator', function()

+ 12 - 1
tests/elementAt.lua

@@ -1,6 +1,7 @@
 describe('elementAt', function()
   it('errors when its parent errors', function()
-    expect(Rx.Observable.throw():elementAt(1).subscribe).to.fail()
+    local _, onError = observableSpy(Rx.Observable:throw():elementAt(0))
+    expect(#onError).to.equal(1)
   end)
 
   it('chains subscriptions', function()
@@ -10,6 +11,16 @@ describe('elementAt', function()
     expect(observable:elementAt(1):subscribe()).to.equal(subscription)
   end)
 
+  it('unsubscribes the subscription if the element is produced', function()
+    local subject = Rx.Subject.create()
+    local subscription = subject:elementAt(2):subscribe()
+    subscription.unsubscribe = spy()
+    subject:onNext('a')
+    expect(#subscription.unsubscribe).to.equal(0)
+    subject:onNext('b')
+    expect(#subscription.unsubscribe).to.equal(1)
+  end)
+
   it('errors if no index is specified', function()
     expect(Rx.Observable.of(1):elementAt().subscribe).to.fail()
   end)

+ 2 - 3
tests/filter.lua

@@ -17,9 +17,8 @@ describe('filter', function()
   end)
 
   it('errors when its parent errors', function()
-    local observable = Rx.Observable.of(''):map(function(x) return x() end)
-    expect(observable.subscribe).to.fail()
-    expect(observable:filter().subscribe).to.fail()
+    local _, onError = observableSpy(Rx.Observable.throw():filter())
+    expect(#onError).to.equal(1)
   end)
 
   it('calls onError if the predicate errors', function()