Sha256: 20ea951c2537942122ab5700d85601e6aeb71a888aec2733317e76f46cd52770

Contents?: true

Size: 1.48 KB

Versions: 11

Compression:

Stored size: 1.48 KB

Contents

# encoding: UTF-8

require File.expand_path("./helper", File.dirname(__FILE__))

class Post < Ohm::Model
  include Ohm::Typecast

  attribute :price, Integer
end

test "handles nil case correctly" do
  post = Post.create(:price => nil)
  post = Post[post.id]

  assert nil == post.price
end

test "handles empty string case correctly" do
  post = Post.create(:price => "")
  post = Post[post.id]

  assert "" == post.price.to_s
end

test "allows respond_to on an invalid integer" do
  post = Post.new(:price => "FooBar")

  assert_nothing_raised ArgumentError do
    post.price.respond_to?(:**)
  end

  assert ! post.price.respond_to?(:**)
end

test "falls back to String#respond_to? when invalid" do
  post = Post.new(:price => "FooBar")
  assert post.price.respond_to?(:capitalize)
end

test "allows for real arithmetic" do
  post = Post.create(:price => "3")
  post = Post[post.id]

  assert 6 == post.price + post.price
  assert 0 == post.price - post.price
  assert 9 == post.price * post.price
  assert 1 == post.price / post.price
end

test "raises when trying to do arithmetic ops on a non-int" do
  post = Post.create(:price => "FooBar")
  post = Post[post.id]

  assert_raise ArgumentError do
    post.price * post.price
  end
end

test "inspecting" do
  post = Post.new(:price => "50000")
  assert '"50000"' == post.price.inspect

  post.price = 'FooBar'
  assert '"FooBar"' == post.price.inspect
end

test "type is Fixnum" do
  post = Post.new(:price => 399)
  assert Fixnum == post.price.type
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ohm-contrib-0.1.2 test/typecast_integer_test.rb
ohm-contrib-0.1.1 test/typecast_integer_test.rb
ohm-contrib-0.1.0 test/typecast_integer_test.rb
ohm-contrib-0.0.42 test/typecast_integer_test.rb
ohm-contrib-0.0.41 test/typecast_integer_test.rb
ohm-contrib-0.0.40 test/typecast_integer_test.rb
ohm-contrib-0.0.39 test/typecast_integer_test.rb
ohm-contrib-0.0.38 test/typecast_integer_test.rb
ohm-contrib-0.0.37 test/typecast_integer_test.rb
ohm-contrib-0.0.36 test/typecast_integer_test.rb
ohm-contrib-0.0.35 test/typecast_integer_test.rb