Class: CouchPotato::RSpec::ReduceToMatcher
- Inherits:
-
Object
- Object
- CouchPotato::RSpec::ReduceToMatcher
- Includes:
- RSpec::Matchers::Composable
- Defined in:
- lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
Instance Method Summary collapse
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(expected_ruby, keys, values, rereduce = false) ⇒ ReduceToMatcher
constructor
A new instance of ReduceToMatcher.
- #matches?(view_spec) ⇒ Boolean
Constructor Details
#initialize(expected_ruby, keys, values, 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, keys, values, rereduce = false) @expected_ruby, @keys, @values, @rereduce = expected_ruby, keys, values, rereduce end |
Instance Method Details
#failure_message ⇒ Object
41 42 43 |
# File 'lib/couch_potato/rspec/matchers/reduce_to_matcher.rb', line 41 def "Expected to reduce to #{@expected_ruby.inspect} but got #{@actual_ruby.inspect}." end |
#failure_message_when_negated ⇒ Object
45 46 47 |
# File 'lib/couch_potato/rspec/matchers/reduce_to_matcher.rb', line 45 def "Expected not to reduce to #{@actual_ruby.inspect} but did." end |
#matches?(view_spec) ⇒ 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 = " (function() {\n sum = function(values) {\n var rv = 0;\n for (var i in values) {\n rv += values[i];\n }\n return rv;\n };\n\n var keys = \#{@keys.to_json};\n var values = \#{@values.to_json};\n var reduce = \#{view_spec.reduce_function};\n return JSON.stringify({result: reduce(keys, values, \#{@rereduce})});\n })()\n JS\n @actual_ruby = JSON.parse(ExecJS.eval(js))['result']\n values_match? @expected_ruby, @actual_ruby\nend\n" |