require 'spec_helper' describe Rents::Currency do describe "Operator Format value" do # setup it vars to have the conversion tested, value: 1999 (nineteen ninety nine) pt_br = '1.999,00' en_us = '1,999.00' # Values expected operator_value = '199900' operator_expected_value = '199900' integer_value = '199900'.to_i decimal_value = BigDecimal.new '1999.00' decimal_expected_value = BigDecimal.new '1999.00' operator_with_cents = '300055' # R$ 3.000,55 non_cents_expected = '00' cents_expected = '55' # Values Converted to operator format pt_br_operator_converted = Rents::Currency.to_operator_str pt_br en_us_operator_converted = Rents::Currency.to_operator_str en_us decimal_converted = Rents::Currency.to_decimal operator_value big_decimal_converted = Rents::Currency.to_operator_str decimal_value integer_converted = Rents::Currency.to_operator_str integer_value cents_received = Rents::Currency.get_cents operator_with_cents non_cents_received = Rents::Currency.get_cents operator_value float_with_cents = 1000.55 float_without_cents = 1000.0 context 'Decimal to Operator' do # Convert PT_BR it 'PT_BR should be convertible' do expect(pt_br_operator_converted).to be_a String expect(pt_br_operator_converted.index('.')).to be_nil expect(pt_br_operator_converted.index(',')).to be_nil expect(pt_br_operator_converted).to be_integer expect(pt_br_operator_converted).to be_equals operator_expected_value end # Convert EN_US it 'EN_US should be convertible' do expect(en_us_operator_converted).to be_a String expect(en_us_operator_converted.index('.')).to be_nil expect(en_us_operator_converted.index(',')).to be_nil expect(en_us_operator_converted).to be_integer expect(en_us_operator_converted).to be_equals operator_expected_value end # To operator passing a BigDecimal instance it 'should have a Operator format based on BigDecimal instance' do expect(big_decimal_converted).to be_a String expect(big_decimal_converted.index('.')).to be_nil expect(big_decimal_converted.index(',')).to be_nil expect(big_decimal_converted).to be_integer expect(big_decimal_converted).to be_equals operator_expected_value end end context 'Integer to Operator' do it 'should have a Operator format based on Integer instance' do expect(integer_converted).to be_a String expect(integer_converted.index('.')).to be_nil expect(integer_converted.index(',')).to be_nil expect(integer_converted).to be_integer expect(integer_converted).to be_equals operator_expected_value end end context 'Operator to Decimal' do # Convert from the operator to BigDecimal instance, like rails does/need it 'should be the BigDecimal expected' do expect(decimal_converted).to be_a BigDecimal expect(decimal_converted).to be == decimal_expected_value end # Convert operator with cents to float correctly it 'should convert with cents correctly' do end end context 'Methods to work in operator format' do it 'should retrieve existent cents' do expect(cents_received).to be_equals cents_expected end it 'should retrieve the non existent cents' do expect(non_cents_received).to be_equals non_cents_expected end it 'should have cents' do expect(Rents::Currency.have_cents?(operator_with_cents)).to be_truthy end it 'should not have cents' do expect(Rents::Currency.have_cents?(operator_value)).to_not be_truthy end it '(RECEIVING FLOAT) should have cents' do expect(Rents::Currency.have_cents?(float_with_cents)).to be_truthy end it '(RECEIVING FLOAT) should not have cents' do expect(Rents::Currency.have_cents?(float_without_cents)).to_not be_truthy end end end end