Class: CouchPotato::RSpec::ReduceToMatcher

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ReduceToMatcher.



14
15
16
# File 'lib/couch_potato/rspec/matchers/reduce_to_matcher.rb', line 14

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

Instance Method Details

#failure_messageObject



39
40
41
# File 'lib/couch_potato/rspec/matchers/reduce_to_matcher.rb', line 39

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

#failure_message_when_negatedObject



43
44
45
# File 'lib/couch_potato/rspec/matchers/reduce_to_matcher.rb', line 43

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

#matches?(view_spec) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches?(view_spec)
  js = <<-JS
    (function() {
      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};
      return JSON.stringify({result: reduce(keys, values, #{@rereduce})});
    })()
  JS
  @actual_ruby = JSON.parse(ExecJS.eval(js))['result']
  @expected_ruby == @actual_ruby
end