Sha256: 60ebf992395a3a6095613ed342b44825e6588ce280955296a6f339c2ea0f4012

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

require 'json'


module CouchPotato
  module RSpec
    class MapToProxy
      def initialize(input_ruby)
        @input_ruby = input_ruby
      end

      def to(*expected_ruby)
        MapToMatcher.new(expected_ruby, @input_ruby)
      end
    end

    class MapToMatcher
      include RunJS

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

      def matches?(view_spec)
        js = <<-JS
          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 exports = {};
            var pathArray = modulePath.split("/").slice(2);
            var result = lib;
            for (var i in pathArray) {
              result = result[pathArray[i]]
            }
            eval(result);
            return exports;
          }

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
couch_potato-1.2.0 lib/couch_potato/rspec/matchers/map_to_matcher.rb
couch_potato-1.1.4 lib/couch_potato/rspec/matchers/map_to_matcher.rb
couch_potato-1.1.2 lib/couch_potato/rspec/matchers/map_to_matcher.rb
couch_potato-1.1.1 lib/couch_potato/rspec/matchers/map_to_matcher.rb
couch_potato-1.1.0 lib/couch_potato/rspec/matchers/map_to_matcher.rb