Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 51 lines (42 sloc) 1.378 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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_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
Something went wrong with that request. Please try again.