mirror of
				https://github.com/stripe/stripe-ruby.git
				synced 2025-11-04 00:01:47 -05:00 
			
		
		
		
	When constructing an object using .construct_from treat keys that are strings the same as keys which are symbols by calling Util's symbolize_names on an input hash. This makes guarantees around consistency a little better. Fixes #151.
		
			
				
	
	
		
			36 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			36 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
require File.expand_path('../../test_helper', __FILE__)
 | 
						|
 | 
						|
module Stripe
 | 
						|
  class StripeObjectTest < Test::Unit::TestCase
 | 
						|
    should "implement #respond_to correctly" do
 | 
						|
      obj = Stripe::StripeObject.construct_from({ :id => 1, :foo => 'bar' })
 | 
						|
      assert obj.respond_to?(:id)
 | 
						|
      assert obj.respond_to?(:foo)
 | 
						|
      assert !obj.respond_to?(:baz)
 | 
						|
    end
 | 
						|
 | 
						|
    should "marshal be insensitive to strings vs. symbols when constructin" do
 | 
						|
      obj = Stripe::StripeObject.construct_from({ :id => 1, 'name' => 'Stripe' })
 | 
						|
      assert_equal 1, obj[:id]
 | 
						|
      assert_equal 'Stripe', obj[:name]
 | 
						|
    end
 | 
						|
 | 
						|
    should "marshal a stripe object correctly" do
 | 
						|
      obj = Stripe::StripeObject.construct_from({ :id => 1, :name => 'Stripe' }, {:api_key => 'apikey'})
 | 
						|
      m = Marshal.load(Marshal.dump(obj))
 | 
						|
      assert_equal 1, m.id
 | 
						|
      assert_equal 'Stripe', m.name
 | 
						|
      expected_hash = {:api_key => 'apikey'}
 | 
						|
      assert_equal expected_hash, m.instance_variable_get('@opts')
 | 
						|
    end
 | 
						|
 | 
						|
    should "recursively call to_hash on its values" do
 | 
						|
      nested_hash = { :id => 7, :foo => 'bar' }
 | 
						|
      nested = Stripe::StripeObject.construct_from(nested_hash)
 | 
						|
      obj = Stripe::StripeObject.construct_from({ :id => 1, :nested => nested, :list => [nested] })
 | 
						|
      expected_hash = { :id => 1, :nested => nested_hash, :list => [nested_hash] }
 | 
						|
      assert_equal expected_hash, obj.to_hash
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 |