Class: CouchPotato::RSpec::MapToMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(expected_ruby, input_ruby) ⇒ MapToMatcher

Returns a new instance of MapToMatcher.



17
18
19
20
# File 'lib/couch_potato/rspec/matchers/map_to_matcher.rb', line 17

def initialize(expected_ruby, input_ruby)
  @expected_ruby = expected_ruby
  @input_ruby = input_ruby
end

Instance Method Details

#failure_messageObject



52
53
54
# File 'lib/couch_potato/rspec/matchers/map_to_matcher.rb', line 52

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

#failure_message_when_negatedObject



56
57
58
# File 'lib/couch_potato/rspec/matchers/map_to_matcher.rb', line 56

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

#matches?(view_spec) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/couch_potato/rspec/matchers/map_to_matcher.rb', line 22

def matches?(view_spec)
  js = <<-JS
    (function() {
      var doc = #{@input_ruby.to_json};
      var map = #{view_spec.map_function};
      var lib = #{view_spec.respond_to?(:lib) && view_spec.lib.to_json};
      var result = [];
      var require = function(modulePath) {
        var module = {exports: {}};
        var exports = module.exports;
        var pathArray = modulePath.split("/").slice(2);
        var result = lib;
        for (var i in pathArray) {
          result = result[pathArray[i]];
        }
        eval(result);
        return module.exports;
      }

      var emit = function(key, value) {
        result.push([key, value]);
      };
      map(doc);
      return JSON.stringify(result);
    })()
  JS
  @actual_ruby = JSON.parse(ExecJS.eval(js))
  @expected_ruby == @actual_ruby
end