Skip to content

Commit

Permalink
some smaller fixes + mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
mindreframer committed Nov 23, 2014
1 parent 4e88024 commit 66d4c1d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api_view.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "require_pattern"
spec.add_development_dependency "minitest", '~> 5.4.3'
spec.add_development_dependency "mocha"
spec.add_development_dependency "minitest-reporters"
spec.add_development_dependency "oj"
spec.add_development_dependency "multi_json"
Expand All @@ -31,4 +32,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "allocation_stats"
spec.add_development_dependency "bixby-bench"
spec.add_development_dependency "coco"

end
5 changes: 5 additions & 0 deletions lib/api_view/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ def self.convert(obj)
obj
end
end

# delegate to class method
def convert
self.class.convert(object)
end
end
end
4 changes: 2 additions & 2 deletions lib/api_view/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def skip_serialization=(value)
#
# @param [Object] obj
# @return [Object]
def convert(obj, options=nil)
def convert(obj, options={})
return obj if is_basic_type?(obj)
return convert_hash(obj, options) if obj.kind_of?(Hash)
return convert_enumerable(obj, options) if obj.respond_to?(:map)
Expand Down Expand Up @@ -88,7 +88,7 @@ def to_json(obj)

# Returns an XML representation of the data object
def to_xml(obj)
obj.to_xml()
obj.to_xml
end

# Returns a guess at the format in this scope
Expand Down
27 changes: 25 additions & 2 deletions test/engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,36 @@
res = ApiView::Engine.render('hey')
res.must_equal 'hey'
end

it "can deduct format from scope (request object)" do
scope = OpenStruct.new(request: OpenStruct.new(format: 'xml'))
obj = [{a: 'hash'}]
Array.any_instance.stubs(:to_xml).returns('to_xml_works')
res = ApiView::Engine.render(obj, scope, {})
res.must_equal('to_xml_works')
end
end

describe 'render' do
it "just works" do
describe 'convert' do
it "works basic data types" do
res = ApiView::Engine.convert('hey')
res.must_equal 'hey'
end

it "works with hashes" do
res = ApiView::Engine.convert({a: 'hash'})
res.must_equal({:a=>"hash"})
end

it "works with arrays" do
res = ApiView::Engine.convert([{a: 'hash'}])
res.must_equal([{:a=>"hash"}])
end

it "works with arrays and extra params" do
res = ApiView::Engine.convert([{a: 'hash'}], {format: 'json'})
res.must_equal([{:a=>"hash"}])
end
end
end

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'coco' # code coverage, needs to be on top! see .coco.yml for more details
Bundler.setup(:default, :development)
require 'minitest'
require 'mocha/mini_test'
require "minitest/autorun"

require 'minitest/reporters'
Expand Down

0 comments on commit 66d4c1d

Please sign in to comment.