Browse files

add regression tests for floats, all pass

  • Loading branch information...
1 parent 4ab33ba commit 973b0b3e62c1c057164b1c0698c46a4c7282bb3e @databyte databyte committed Oct 16, 2012
Showing with 9 additions and 7 deletions.
  1. +3 −1 test/models/user.rb
  2. +6 −6 test/renderer_test.rb
View
4 test/models/user.rb
@@ -1,17 +1,19 @@
unless defined?(User)
class User
- attr_accessor :age, :city, :name, :first
+ attr_accessor :age, :city, :name, :first, :float
DEFAULT_AGE = 24
DEFAULT_CITY = 'irvine'
DEFAULT_NAME = 'rabl'
DEFAULT_FIRST = 'bob'
+ DEFAULT_FLOAT = 1234.56
def initialize(attributes={})
self.age = attributes[:age] || DEFAULT_AGE
self.city = attributes[:city] || DEFAULT_CITY
self.name = attributes[:name] || DEFAULT_NAME
self.first = attributes[:first] || DEFAULT_FIRST
+ self.float = attributes[:float] || DEFAULT_FLOAT
end
end
end
View
12 test/renderer_test.rb
@@ -218,40 +218,40 @@
File.open(tmp_path + "test.rabl", "w") do |f|
f.puts %q{
object @user
- attributes :age, :name
+ attributes :age, :name, :float
}
end
user = User.new(:name => 'ivan')
JSON.parse(Rabl::Renderer.json(user, 'test', :view_path => tmp_path))
- end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"ivan\"}}")
+ end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"ivan\",\"float\":1234.56}}")
end
context '.msgpack' do
asserts 'it renders msgpack' do
File.open(tmp_path + "test.rabl", "w") do |f|
f.puts %q{
object @user
- attributes :age, :name
+ attributes :age, :name, :float
}
end
user = User.new(:name => 'ivan')
Rabl::Renderer.msgpack(user, 'test', :view_path => tmp_path)
- end.equals "\x81\xA4user\x82\xA3age\x18\xA4name\xA4ivan"
+ end.equals "\x81\xA4user\x83\xA3age\x18\xA4name\xA4ivan\xA5float\xCB@\x93J=p\xA3\xD7\n"
end
context '.plist' do
asserts 'it renders xml' do
File.open(tmp_path + "test.rabl", "w") do |f|
f.puts %q{
object @user
- attributes :age, :name
+ attributes :age, :name, :float
}
end
user = User.new(:name => 'ivan')
Rabl::Renderer.plist(user, 'test', :view_path => tmp_path)
- end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>user</key>\n\t<dict>\n\t\t<key>age</key>\n\t\t<integer>24</integer>\n\t\t<key>name</key>\n\t\t<string>ivan</string>\n\t</dict>\n</dict>\n</plist>\n"
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>user</key>\n\t<dict>\n\t\t<key>age</key>\n\t\t<integer>24</integer>\n\t\t<key>float</key>\n\t\t<real>1234.56</real>\n\t\t<key>name</key>\n\t\t<string>ivan</string>\n\t</dict>\n</dict>\n</plist>\n"
end
end

1 comment on commit 973b0b3

@nesquena
Owner

Thanks! good to have some tests to make sure this isn't a general issue. I use floats in multiple of my APIs and oddly enough also never ran into this issue of floats becoming strings.

Please sign in to comment.