Browse Source

Test Observable.changes; Fix test runner; Change Observable.fromTable;

bjorn 10 years ago
parent
commit
82aa346555
4 changed files with 49 additions and 8 deletions
  1. 5 4
      rx.lua
  2. 30 0
      tests/changes.lua
  3. 13 3
      tests/observable.lua
  4. 1 1
      tests/runner.lua

+ 5 - 4
rx.lua

@@ -115,12 +115,13 @@ end
 --- Creates an Observable that produces values from a table.
 --- Creates an Observable that produces values from a table.
 -- @arg {table} table - The table used to create the Observable.
 -- @arg {table} table - The table used to create the Observable.
 -- @arg {function=pairs} iterator - An iterator used to iterate the table, e.g. pairs or ipairs.
 -- @arg {function=pairs} iterator - An iterator used to iterate the table, e.g. pairs or ipairs.
+-- @arg {boolean} keys - Whether or not to also emit the keys of the table.
 -- @returns {Observable}
 -- @returns {Observable}
-function Observable.fromTable(t, iterator)
+function Observable.fromTable(t, iterator, keys)
   iterator = iterator or pairs
   iterator = iterator or pairs
   return Observable.create(function(observer)
   return Observable.create(function(observer)
     for key, value in iterator(t) do
     for key, value in iterator(t) do
-      observer:onNext(value, key)
+      observer:onNext(value, keys and key or nil)
     end
     end
 
 
     observer:onComplete()
     observer:onComplete()
@@ -179,9 +180,9 @@ function Observable:changes(comparator)
     local first = true
     local first = true
     local currentValue = nil
     local currentValue = nil
 
 
-    local function onNext(value)
+    local function onNext(value, ...)
       if first or not comparator(value, currentValue) then
       if first or not comparator(value, currentValue) then
-        observer:onNext(value)
+        observer:onNext(value, ...)
         currentValue = value
         currentValue = value
         first = false
         first = false
       end
       end

+ 30 - 0
tests/changes.lua

@@ -0,0 +1,30 @@
+describe('changes', function()
+  describe('with the default comparator', function()
+    it('produces a value if it is the first value or different from the previous', function()
+      local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs):changes()
+      expect(observable).to.produce(1, 3, 1, 4, 5)
+    end)
+
+    it('produces the first value if it is nil', function()
+      local observable = Rx.Observable.fromValue(nil):changes()
+      expect(observable).to.produce({{}})
+    end)
+
+    it('produces multiple onNext arguments but only uses the first argument to check for equality', function()
+      local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs, true):changes()
+      expect(observable).to.produce({{1, 1}, {3, 3}, {1, 4}, {4, 5}, {5, 6}})
+    end)
+  end)
+
+  describe('with a custom comparator', function()
+    it('produces a value if it is the first value or the comparator returns false when passed the previous value and the current value', function()
+      local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs):changes(function(x, y) return x % 2 == y % 2 end)
+      expect(observable).to.produce(1, 4, 5)
+    end)
+
+    it('produces the first value if it is nil', function()
+      local observable = Rx.Observable.fromValue(nil):changes(function(x, y) return true end)
+      expect(observable).to.produce({{}})
+    end)
+  end)
+end)

+ 13 - 3
tests/observable.lua

@@ -89,19 +89,27 @@ describe('Observable', function()
     end)
     end)
 
 
     describe('with one argument', function()
     describe('with one argument', function()
-      it('returns an Observable that produces value-key pairs by iterating the table using pairs', function()
+      it('returns an Observable that produces values by iterating the table using pairs', function()
         local input = {foo = 'bar', 1, 2, 3}
         local input = {foo = 'bar', 1, 2, 3}
         local observable = Rx.Observable.fromTable(input)
         local observable = Rx.Observable.fromTable(input)
         local result = {}
         local result = {}
-        for key, value in pairs(input) do table.insert(result, {value, key}) end
+        for key, value in pairs(input) do table.insert(result, {value}) end
         expect(observable).to.produce(result)
         expect(observable).to.produce(result)
       end)
       end)
     end)
     end)
 
 
     describe('with two arguments', function()
     describe('with two arguments', function()
-      it('returns an Observable that produces value-key pairs by iterating the table using the second argument', function()
+      it('returns an Observable that produces values by iterating the table using the second argument', function()
         local input = {foo = 'bar', 3, 4, 5}
         local input = {foo = 'bar', 3, 4, 5}
         local observable = Rx.Observable.fromTable(input, ipairs)
         local observable = Rx.Observable.fromTable(input, ipairs)
+        expect(observable).to.produce(3, 4, 5)
+      end)
+    end)
+
+    describe('with three arguments', function()
+      it('returns an Observable that produces value-key pairs by iterating the table if the third argument is true', function()
+        local input = {foo = 'bar', 3, 4, 5}
+        local observable = Rx.Observable.fromTable(input, ipairs, true)
         expect(observable).to.produce({{3, 1}, {4, 2}, {5, 3}})
         expect(observable).to.produce({{3, 1}, {4, 2}, {5, 3}})
       end)
       end)
     end)
     end)
@@ -139,4 +147,6 @@ describe('Observable', function()
 
 
   describe('dump', function()
   describe('dump', function()
   end)
   end)
+
+  dofile('tests/changes.lua')
 end)
 end)

+ 1 - 1
tests/runner.lua

@@ -51,7 +51,7 @@ lust.paths['nothing'] = {
 table.insert(lust.paths['to'], 'produce')
 table.insert(lust.paths['to'], 'produce')
 
 
 if arg[1] then
 if arg[1] then
-  arg[1] = arg[1]:gsub('^(tests/).+', ''):gsub('%.lua$', '')
+  arg[1] = arg[1]:gsub('^(tests/)', ''):gsub('%.lua$', '')
   dofile('tests/' .. arg[1] .. '.lua')
   dofile('tests/' .. arg[1] .. '.lua')
 else
 else
   local files = {
   local files = {