Skip to content

Commit

Permalink
fixed bug with ruby 1.8.7: str[i] returns a int instead of char, what…
Browse files Browse the repository at this point in the history
… caused wrong sign calculations on amounts. Added spec
  • Loading branch information
molpe committed Jan 13, 2012
1 parent f058ac1 commit 7ffe180
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 7 deletions.
1 change: 0 additions & 1 deletion autotest/discover.rb

This file was deleted.

9 changes: 5 additions & 4 deletions lib/norma43.rb
@@ -1,4 +1,5 @@
require "iconv"
require 'date'

module Norma43

Expand Down Expand Up @@ -34,7 +35,7 @@ def self.parse_header(line)
:account => account,
:begin_date => Date.strptime(line[20..25], DATE_FORMAT), #Date.from_nor43(line[20..25])
:end_date => Date.strptime(line[26..31], DATE_FORMAT),
:initial_balance => parse_amount(line[33..46], line[32]),
:initial_balance => parse_amount(line[33..46], line[32].chr),
:account_owner => line[51..76].strip,
:currency => line[47..49]
}
Expand All @@ -47,7 +48,7 @@ def self.parse_movement_main(line)
:operation => line[42..51],
:reference_1 => line[52..63],
:reference_2 => line[64..79].strip,
:amount => parse_amount(line[28..41], line[27]),
:amount => parse_amount(line[28..41], line[27].chr),
:office => line[6..9]
}
end
Expand All @@ -57,11 +58,11 @@ def self.parse_movement_optional(line)
end

def self.parse_end(line)
{:final_balance => parse_amount(line[59..72], line[28])}
{:final_balance => parse_amount(line[59..72], line[28].chr)}
end

def self.parse_amount(value, sign)
value.to_f / 100 * (sign == 1 ? -1 : 1)
value.to_f / 100 * (sign.to_i == 1 ? -1 : 1)
end

end
2 changes: 1 addition & 1 deletion lib/norma43/version.rb
@@ -1,3 +1,3 @@
module Norma43
VERSION = "0.2"
VERSION = "0.3"
end
23 changes: 23 additions & 0 deletions spec/data/test.n43
@@ -0,0 +1,23 @@
118888555511111111111103011103202000000010593169783THE EXPENDABLES REVAMP SL
22 0901110301110301040161000000000927000000000000000000000000
2301TRANSFERENC. A Silvester Stallone
22 0901110301110301040161000000001274090000000000000000000000
2301TRANSFERENC. A Mickey Rourke
22 0901110302110302040162000000000424800000000000001111111111
2301TRANSFERENC. Bruce Willis
22 0901110303110303040161000000000236000000000000000000000000
2301TRANSFERENC. A Charisma Carpenter
22 0901110304110304990801000000005000000000000000000000000000
2301APER IMPOSIC 123456789-00
22 09011103041103041203110000000011778100000000000987654321221234123412341234
2301TARJ.CREDITO Hank Amos
22 09011103041103041203110000000001882000000000000987654321116789678967896789
2301TRANSFERENC. Jet Li
22 0901110309110309040162000000001539900000000000123451234566
2301TRANSFERENC. Terry Crews
22 090111030911030904016200000000071154000000000000BA12345678
2301TRANSFERENC. David Zayas
22 0901110321110319170892000000000003030000000000000000000000
2301LIQ.INT.IMP. 123456789-00
3388885555111111111100006000000008803100000400000000267927200000000446933978
88999999999999999999000022
93 changes: 93 additions & 0 deletions spec/data/test.yml
@@ -0,0 +1,93 @@
:info:
:currency: "978"
:final_balance: 4469.33
:begin_date: 2011-03-01
:account:
:bank: "8888"
:office: "5555"
:number: "1111111111"
:control: ??
:end_date: 2011-03-20
:account_owner: THE EXPENDABLES REVAMP SL
:initial_balance: 10593.16
:movements:
- :concept: TRANSFERENC. A Silvester Stallone
:operation_date: 2011-03-01
:amount: -927.0
:value_date: 2011-03-01
:office: 0901
:operation: "0000000000"
:reference_1: "000000000000"
:reference_2: ""
- :concept: TRANSFERENC. A Mickey Rourke
:operation_date: 2011-03-01
:amount: -1274.09
:value_date: 2011-03-01
:office: 0901
:operation: "0000000000"
:reference_1: "000000000000"
:reference_2: ""
- :concept: TRANSFERENC. Bruce Willis
:operation_date: 2011-03-02
:amount: 424.8
:value_date: 2011-03-02
:office: 0901
:operation: "0000000000"
:reference_1: "001111111111"
:reference_2: ""
- :concept: TRANSFERENC. A Charisma Carpenter
:operation_date: 2011-03-03
:amount: -236.0
:value_date: 2011-03-03
:office: 0901
:operation: "0000000000"
:reference_1: "000000000000"
:reference_2: ""
- :concept: APER IMPOSIC 123456789-00
:operation_date: 2011-03-04
:amount: -5000.0
:value_date: 2011-03-04
:office: 0901
:operation: "0000000000"
:reference_1: "000000000000"
:reference_2: ""
- :concept: TARJ.CREDITO Hank Amos
:operation_date: 2011-03-04
:amount: -1177.81
:value_date: 2011-03-04
:office: 0901
:operation: "0000000000"
:reference_1: 098765432122
:reference_2: "1234123412341234"
- :concept: TRANSFERENC. Jet Li
:operation_date: 2011-03-04
:amount: -188.2
:value_date: 2011-03-04
:office: 0901
:operation: "0000000000"
:reference_1: 098765432111
:reference_2: "6789678967896789"
- :concept: TRANSFERENC. Terry Crews
:operation_date: 2011-03-09
:amount: 1539.9
:value_date: 2011-03-09
:office: 0901
:operation: "0000000000"
:reference_1: "123451234566"
:reference_2: ""
- :concept: TRANSFERENC. David Zayas
:operation_date: 2011-03-09
:amount: 711.54
:value_date: 2011-03-09
:office: 0901
:operation: "0000000000"
:reference_1: 00BA12345678
:reference_2: ""
- :concept: LIQ.INT.IMP. 123456789-00
:operation_date: 2011-03-21
:amount: 3.03
:value_date: 2011-03-19
:office: 0901
:operation: "0000000000"
:reference_1: "000000000000"
:reference_2: ""
22 changes: 22 additions & 0 deletions spec/lib/norma43_spec.rb
@@ -0,0 +1,22 @@
require 'spec_helper'
require 'lib/norma43'

describe Norma43 do

describe "parse" do

before :each do
@data = Norma43.read(File.join(File.dirname(__FILE__), "..", "data", "test.n43"))
end

specify { @data[:movements].size.should == 10 }
specify { @data[:info][:initial_balance].should == 10593.16 }
specify { @data[:info][:final_balance].should == 4469.33 }

#initial_balance + movements should be final balance. Float comparison with be_within
specify { (@data[:info][:initial_balance] + @data[:movements].inject( 0 ) { |sum,x| sum+x[:amount] }).should be_within(0.1).of(@data[:info][:final_balance]) }


end

end
1 change: 0 additions & 1 deletion spec/spec_helper.rb
@@ -1,7 +1,6 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
#require File.expand_path("../../config/environment", __FILE__)
require File.expand_path("../../spec/factories", __FILE__)

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Expand Down

0 comments on commit 7ffe180

Please sign in to comment.