Skip to content

Commit

Permalink
Replace print_r with JSON.stringify to handle dates in JS runtime specs
Browse files Browse the repository at this point in the history
  • Loading branch information
andymorris committed May 17, 2013
1 parent 94ff05b commit b60c41c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 69 deletions.
3 changes: 1 addition & 2 deletions lib/couch_potato/rspec/matchers/list_as_matcher.rb
Expand Up @@ -20,7 +20,6 @@ def initialize(expected_ruby, results_ruby)

def matches?(view_spec)
js = <<-JS
#{File.read(File.dirname(__FILE__) + '/print_r.js')}
#{File.read(File.dirname(__FILE__) + '/json2.js')}
var results = #{@results_ruby.to_json};
var listed = '';
Expand All @@ -33,7 +32,7 @@ def matches?(view_spec)
listed = listed + text;
};
list();
print_r(JSON.parse(listed));
JSON.stringify(JSON.parse(listed));
JS

@actual_ruby = JSON.parse(run_js(js))
Expand Down
3 changes: 1 addition & 2 deletions lib/couch_potato/rspec/matchers/map_to_matcher.rb
Expand Up @@ -23,15 +23,14 @@ def initialize(expected_ruby, input_ruby)

def matches?(view_spec)
js = <<-JS
#{File.read(File.dirname(__FILE__) + '/print_r.js')}
var doc = #{@input_ruby.to_json};
var map = #{view_spec.map_function};
var result = [];
var emit = function(key, value) {
result.push([key, value]);
};
map(doc);
print_r(result);
JSON.stringify(result);
JS
@actual_ruby = JSON.parse(run_js(js))
@expected_ruby == @actual_ruby
Expand Down
60 changes: 0 additions & 60 deletions lib/couch_potato/rspec/matchers/print_r.js

This file was deleted.

4 changes: 1 addition & 3 deletions lib/couch_potato/rspec/matchers/reduce_to_matcher.rb
Expand Up @@ -19,8 +19,6 @@ def initialize(expected_ruby, docs, keys, rereduce = false)

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) {
Expand All @@ -32,7 +30,7 @@ def matches?(view_spec)
var docs = #{@docs.to_json};
var keys = #{@keys.to_json};
var reduce = #{view_spec.reduce_function};
print_r({result: reduce(docs, keys, #{@rereduce})});
JSON.stringify({result: reduce(docs, keys, #{@rereduce})});
JS
@actual_ruby = JSON.parse(run_js(js))['result']
@expected_ruby == @actual_ruby
Expand Down
19 changes: 17 additions & 2 deletions spec/unit/rspec_matchers_spec.rb
Expand Up @@ -30,6 +30,11 @@
@view_spec.should_not map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2], ['male', 'person'])
end
end

it "should work with date emit values" do
spec = stub(:map_function => "function(doc) { emit(null, new Date(1368802800000)); }")
spec.should map({}).to([nil, "2013-05-17T15:00:00.000Z"])
end

describe "failing specs" do
before(:each) do
Expand Down Expand Up @@ -68,6 +73,11 @@
it "should not pass if the given function returns different javascript" do
@view_spec.should_not reduce([], [1, 2, 3]).to(7)
end

it "should work with date return values" do
spec = stub(:reduce_function => "function() { return new Date(1368802800000); }")
spec.should reduce([], []).to("2013-05-17T15:00:00.000Z")
end

describe "rereduce" do
it "should pass if the given function return the expected javascript" do
Expand Down Expand Up @@ -107,7 +117,12 @@
it "should not pass if the function does not return the expected json" do
@view_spec.should_not list({'rows' => [{:text => 'hello'}]}).as([{'text' => 'hello there'}])
end


it "should work with date values" do
spec = stub(:list_function => "function() { send(JSON.stringify([{date: new Date(1368802800000)}])); }")
spec.should list({"rows" => [{}]}).as([{"date" => "2013-05-17T15:00:00.000Z"}])
end

describe "failing specs" do
it "should have a nice error message for failing should" do
lambda {
Expand All @@ -121,4 +136,4 @@
}.should raise_error('Expected to not list as [{"text"=>"hello world"}] but did.')
end
end
end
end

0 comments on commit b60c41c

Please sign in to comment.