Skip to content

Commit

Permalink
allow renaming of funds
Browse files Browse the repository at this point in the history
  • Loading branch information
mmitch committed Feb 7, 2024
1 parent 4e9c566 commit b7fc5d6
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 8 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ transaction file format

1. fund name

* `+RENAME some_fund`
Lines starting with `+RENAME` rename an existing fund. In later
lines it can only be used in transactions with the new name.

Rename attributes are (given in order, space-separated):

1. old fund name
2. new fund name

* `@@ 24.12.2021`
Lines starting with `@@` define the calendar day to use for the
next transactions.
Expand Down
13 changes: 13 additions & 0 deletions depot.pl
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,19 @@ sub import {
die "duplicate fund `$id' at line $.\n" if exists $funds{$id};
$funds{$id} = new Fund( id => $id );
}
elsif ($line =~ /
\+RENAME # fixed marker
\s+ # separator
([a-zA-Z0-9_]+) # source fund name
\s+ # separator
([a-zA-Z0-9_]+) # target fund name
/x) {
my ($from, $to) = ($1, $2);
die "rename: fund `$from' does not exist at line $.\n" unless exists $funds{$from};
die "rename: fund `$to' already exists at line $.\n" if exists $funds{$to};
$funds{$to} = delete $funds{$from};
$funds{$to}->{id} = $to;
}
elsif ($line =~ /
(\S+) # fund
\s+ # separator
Expand Down
8 changes: 8 additions & 0 deletions test/test-10-rename-fund.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.---------------------------------------------------------------------------------------------------------.
| ETF | Anteile | Wert | Kurs (seit Start) | Gewinn (relativ ) | Stand |
+--------+--------------+--------------+--------------------------+--------------------------+------------+
| fund_B | 0.316 St. | 35.72 EUR | 113.038 EUR ( +0.00%) | -1.01 EUR ( -2.75%) | 18.12.2020 |
| fund_Z | 3.700 St. | 69.83 EUR | 18.873 EUR ( +14.87%) | 2.41 EUR ( +3.58%) | 12.01.2021 |
+--------+--------------+--------------+--------------------------+--------------------------+------------+
| Depot | | 105.55 EUR | | 1.40 EUR ( +1.35%) | |
'--------+--------------+--------------+--------------------------+--------------------------+------------'
17 changes: 17 additions & 0 deletions test/test-10-rename-fund.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# define funds

+FUND fund_A
+FUND fund_B

# transactions per day

@@ 18.12.2020

fund_A +1,978 = 32,5 ! 1,21
fund_B +0,316 = 35,72 ! 1,01

+RENAME fund_A fund_Z

@@ 12.01.2021

fund_Z +1,722 = 32,5 ! 1,21
4 changes: 4 additions & 0 deletions test/test-11-rename-missing.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# define funds

+RENAME fund_A fund_Z

6 changes: 6 additions & 0 deletions test/test-12-rename-duplicate.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# define funds

+FUND fund_A
+FUND fund_Z

+RENAME fund_A fund_Z
9 changes: 9 additions & 0 deletions test/test-13-rename-old-gone.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# define funds

+FUND fund_A

+RENAME fund_A fund_Z

@@ 18.12.2020

fund_A +1,978 = 32,5 ! 1,21
20 changes: 12 additions & 8 deletions test/test-depot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ trap print_stats EXIT
export LANG=C

expect_success test-01-basic
expect_error test-02-undefined-fund "unknown fund \`missing_fund'"
expect_error test-03-empty-file "no funds found"
expect_error test-04-file-not-found "can't open \`test-04-file-not-found.input'"
expect_error test-05-unknown-line "unparseable line \`some random unparseable line'"
expect_error test-06-backwards-date "date \`23.11.1962' must be later than previous date"
expect_success test-07-default-mode '-default'
expect_success test-08-verbose-mode '-verbose'
expect_error test-09-duplicate-fund "duplicate fund \`fund_A'"
expect_error test-02-undefined-fund "unknown fund \`missing_fund'"
expect_error test-03-empty-file "no funds found"
expect_error test-04-file-not-found "can't open \`test-04-file-not-found.input'"
expect_error test-05-unknown-line "unparseable line \`some random unparseable line'"
expect_error test-06-backwards-date "date \`23.11.1962' must be later than previous date"
expect_success test-07-default-mode '-default'
expect_success test-08-verbose-mode '-verbose'
expect_error test-09-duplicate-fund "duplicate fund \`fund_A'"
expect_success test-10-rename-fund
expect_error test-11-rename-missing "rename: fund \`fund_A' does not exist"
expect_error test-12-rename-duplicate "rename: fund \`fund_Z' already exists"
expect_error test-13-rename-old-gone "unknown fund \`fund_A'"

0 comments on commit b7fc5d6

Please sign in to comment.