Skip to content

Commit

Permalink
[test]: Added missing tests for Klarna::API.parse_flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
grimen committed Oct 16, 2011
1 parent 10b374a commit 4fc2a71
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/klarna/api.rb
Expand Up @@ -140,9 +140,9 @@ def validate_arg(value, cast_to, format_expression, strip_expression = nil, &blo

def parse_flags(constant_name, flags)
if flags.is_a?(Hash)
flags = flags.sum { |k,v|
::Klarna::API.const_get(constant_name.to_s.upcase.to_sym)[k.to_s.upcase.to_sym]
}
flags = flags.sum do |k, v|
v ? ::Klarna::API.const_get(constant_name.to_s.upcase.to_sym)[k.to_s.upcase.to_sym] : 0
end
end
flags.to_i
end
Expand Down
35 changes: 34 additions & 1 deletion test/klarna/api_test.rb
Expand Up @@ -98,7 +98,40 @@
assert_respond_to ::Klarna::API, :validate_arg
end

# it 'should...'
# TODO: Add missing specs.
end

describe '.parse_flags' do
it 'should be defined' do
assert_respond_to ::Klarna::API, :parse_flags
end

it 'should calculate value for one flag properly' do
assert_equal 0, ::Klarna::API.parse_flags(:GOODS, :IS_SHIPMENT => false)
assert_equal 8, ::Klarna::API.parse_flags(:GOODS, :IS_SHIPMENT => true)
end

it 'should calculate value for multiple flags properly using hash (bitwise OR - a.k.a sum)' do
# Maybe overkill, but here we go... ;)

# Uppercase
assert_equal 0, ::Klarna::API.parse_flags(:GOODS, :IS_SHIPMENT => false, :IS_HANDLING => false, :INC_VAT => false)
assert_equal 8, ::Klarna::API.parse_flags(:GOODS, :IS_SHIPMENT => true, :IS_HANDLING => false, :INC_VAT => false)
assert_equal 16, ::Klarna::API.parse_flags(:GOODS, :IS_SHIPMENT => false, :IS_HANDLING => true, :INC_VAT => false)
assert_equal 32, ::Klarna::API.parse_flags(:GOODS, :IS_SHIPMENT => false, :IS_HANDLING => false, :INC_VAT => true)
assert_equal 24, ::Klarna::API.parse_flags(:GOODS, :IS_SHIPMENT => true, :IS_HANDLING => true, :INC_VAT => false)
assert_equal 48, ::Klarna::API.parse_flags(:GOODS, :IS_SHIPMENT => false, :IS_HANDLING => true, :INC_VAT => true)
assert_equal 56, ::Klarna::API.parse_flags(:GOODS, :IS_SHIPMENT => true, :IS_HANDLING => true, :INC_VAT => true)

# Lowercase (for readability)
assert_equal 0, ::Klarna::API.parse_flags(:goods, :is_shipment => false, :is_handling => false, :inc_vat => false)
assert_equal 8, ::Klarna::API.parse_flags(:goods, :is_shipment => true, :is_handling => false, :inc_vat => false)
assert_equal 16, ::Klarna::API.parse_flags(:goods, :is_shipment => false, :is_handling => true, :inc_vat => false)
assert_equal 32, ::Klarna::API.parse_flags(:goods, :is_shipment => false, :is_handling => false, :inc_vat => true)
assert_equal 24, ::Klarna::API.parse_flags(:goods, :is_shipment => true, :is_handling => true, :inc_vat => false)
assert_equal 48, ::Klarna::API.parse_flags(:goods, :is_shipment => false, :is_handling => true, :inc_vat => true)
assert_equal 56, ::Klarna::API.parse_flags(:goods, :is_shipment => true, :is_handling => true, :inc_vat => true)
end
end

describe '.digest' do
Expand Down

0 comments on commit 4fc2a71

Please sign in to comment.