Browse Source

Test Observable.pluck;

bjorn 10 years ago
parent
commit
3d85077084
2 changed files with 39 additions and 0 deletions
  1. 1 0
      tests/observable.lua
  2. 38 0
      tests/pluck.lua

+ 1 - 0
tests/observable.lua

@@ -165,4 +165,5 @@ describe('Observable', function()
   dofile('tests/min.lua')
   dofile('tests/merge.lua')
   dofile('tests/pack.lua')
+  dofile('tests/pluck.lua')
 end)

+ 38 - 0
tests/pluck.lua

@@ -0,0 +1,38 @@
+describe('pluck', function()
+  it('returns the original observable if no key is specified', function()
+    local observable = Rx.Observable.fromValue(7)
+    expect(observable:pluck()).to.equal(observable)
+  end)
+
+  it('retrieves the specified key from tables produced by the observable', function()
+    local t = {
+      { color = 'red' },
+      { color = 'green' },
+      { color = 'blue' }
+    }
+    expect(Rx.Observable.fromTable(t, ipairs):pluck('color')).to.produce('red', 'green', 'blue')
+  end)
+
+  it('supports numeric pluckage', function()
+    local t = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
+    expect(Rx.Observable.fromTable(t, ipairs):pluck(2)).to.produce(2, 5, 8)
+  end)
+
+  it('supports deep pluckage', function()
+    local t = {
+      { name = { first = 'Robert', last = 'Paulsen' } },
+      { name = { first = 'Bond', last = 'James Bond' } }
+    }
+    expect(Rx.Observable.fromTable(t, ipairs):pluck('name', 'first')).to.produce('Robert', 'Bond')
+  end)
+
+  it('errors if a key does not exist', function()
+    local california = {}
+    expect(Rx.Observable.fromTable(california):pluck('water')).to.fail()
+  end)
+
+  it('respects metatables', function()
+    local t = setmetatable({}, {__index = {a = 'b'}})
+    expect(Rx.Observable.fromValue(t):pluck('a')).to.produce('b')
+  end)
+end)