Sha256: 116c6aad2dace0cc46e6266d7cf3ef4eb3a66a824b2b26b2e69d8097a2adf037

Contents?: true

Size: 1.4 KB

Versions: 39

Compression:

Stored size: 1.4 KB

Contents

module CouchPotato
  module RSpec
    class ReduceToProxy
      def initialize(docs, keys, rereduce = false)
        @docs, @keys, @rereduce = docs, keys, rereduce
      end
      
      def to(expected_ruby)
        ReduceToMatcher.new(expected_ruby, @docs, @keys, @rereduce)
      end
    end
    
    class ReduceToMatcher
      include RunJS
      
      def initialize(expected_ruby, docs, keys, rereduce = false)
        @expected_ruby, @docs, @keys, @rereduce = expected_ruby, docs, keys, rereduce
      end
      
      def matches?(view_spec)
        js = <<-JS
          #{File.read(File.dirname(__FILE__) + '/print_r.js')}
          
          sum = function(values) {
            var rv = 0;
            for (var i in values) {
              rv += values[i];
            }
            return rv;
          };
          
          var docs = #{@docs.to_json};
          var keys = #{@keys.to_json};
          var reduce = #{view_spec.reduce_function};
          print(print_r({result: reduce(docs, keys, #{@rereduce})}));
        JS
        @actual_ruby = JSON.parse(run_js(js))['result']
        @expected_ruby == @actual_ruby
      end
      
      def failure_message_for_should
        "Expected to reduce to #{@expected_ruby.inspect} but got #{@actual_ruby.inspect}."
      end
      
      def failure_message_for_should_not
        "Expected not to reduce to #{@actual_ruby.inspect} but did."
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 3 rubygems

Version Path
couch_potato-0.2.32 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.31 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.30 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.29 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.28 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.27 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.26 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.25 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.24 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.23 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.22 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.21 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.20 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.19 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.18 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.17 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.16 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.15 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.2.14 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb