Sha256: 3b6c6ffa14055e5fe71a9d218bb84c1b7a52c006cbdd754b3389a01f0e1e99bd

Contents?: true

Size: 1.31 KB

Versions: 9

Compression:

Stored size: 1.31 KB

Contents

module CouchPotato
  module RSpec
    class ReduceToProxy
      def initialize(keys, values, rereduce = false)
        @keys, @values, @rereduce = keys, values, rereduce
      end

      def to(expected_ruby)
        ReduceToMatcher.new(expected_ruby, @keys, @values, @rereduce)
      end
    end

    class ReduceToMatcher
      include RunJS

      def initialize(expected_ruby, keys, values, rereduce = false)
        @expected_ruby, @keys, @values, @rereduce = expected_ruby, keys, values, rereduce
      end

      def matches?(view_spec)
        js = <<-JS
          sum = function(values) {
            var rv = 0;
            for (var i in values) {
              rv += values[i];
            }
            return rv;
          };

          var keys = #{@keys.to_json};
          var values = #{@values.to_json};
          var reduce = #{view_spec.reduce_function};
          JSON.stringify({result: reduce(keys, values, #{@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

9 entries across 9 versions & 1 rubygems

Version Path
couch_potato-1.4.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-1.3.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-1.2.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-1.1.4 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-1.1.2 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-1.1.1 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-1.1.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-1.0.1 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-1.0.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb