Class: CouchPotato::RSpec::ReduceToMatcher

Inherits:
Object
  • Object
show all
Includes:
RunJS
Defined in:
lib/couch_potato/rspec/matchers/reduce_to_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected_ruby, docs, keys, rereduce = false) ⇒ ReduceToMatcher

Returns a new instance of ReduceToMatcher.



16
17
18
# File 'lib/couch_potato/rspec/matchers/reduce_to_matcher.rb', line 16

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

Instance Method Details

#failure_message_for_shouldObject



41
42
43
# File 'lib/couch_potato/rspec/matchers/reduce_to_matcher.rb', line 41

def failure_message_for_should
  "Expected to reduce to #{@expected_ruby.inspect} but got #{@actual_ruby.inspect}."
end

#failure_message_for_should_notObject



45
46
47
# File 'lib/couch_potato/rspec/matchers/reduce_to_matcher.rb', line 45

def failure_message_for_should_not
  "Expected not to reduce to #{@actual_ruby.inspect} but did."
end

#matches?(view_spec) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/couch_potato/rspec/matchers/reduce_to_matcher.rb', line 20

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