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.6.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.5.7 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-rails2-0.5.10 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-rails2-0.5.9 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-rails2-0.5.8 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-rails2-0.5.7 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-rails2-0.5.6 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.5.6 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.5.5 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.5.4 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.5.3 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.5.2 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.5.1 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.5.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
davber_couch_potato-0.4.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.4.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.3.2 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.3.1 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
davber_couch_potato-0.3.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
couch_potato-0.3.0 lib/couch_potato/rspec/matchers/reduce_to_matcher.rb