From 04f884b14fd7cc396c9d442685df8489ae7e8a15 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 7 Mar 2012 05:38:28 -0600 Subject: Added test for value directives and annotations --- test/baseline/feat-value-expr.test | 103 +++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 test/baseline/feat-value-expr.test (limited to 'test/baseline/feat-value-expr.test') diff --git a/test/baseline/feat-value-expr.test b/test/baseline/feat-value-expr.test new file mode 100644 index 00000000..690febf9 --- /dev/null +++ b/test/baseline/feat-value-expr.test @@ -0,0 +1,103 @@ +;; A valuation function receives three arguments: +;; +;; 'source' A string identifying the commodity whose price +;; is being asked for (example: "EUR") +;; +;; 'date' The reference date the price should be relative. +;; +;; 'target' A string identifying the "target" commodity, or +;; the commodity the returned price should be in. +;; This argument is null if -V was used instead of -X. +;; +;; The valuation function should return an amount. If you've written your +;; function in Python, you can return something like Amount("$100"). If the +;; function returns an explicit value, that value is always used, regardless +;; of the commodity, the date, or the desired target commodity. + +define myfunc_seven(s, d, t) = 7 EUR + +;; In order to specific a fixed price, but still valuate that price into the +;; target commodity, use something like this: + +define myfunc_five(s, d, t) = market(5 EUR, d, t) + +;; The 'value' directive sets the valuation used for all commodities used in +;; the rest of the daat stream. This is the fallback, if nothing more +;; specific is found. + +value myfunc_seven + +;; You can set a specific valuation function on a per-commodity basis. +;; Instead of defining a function, you can also pass a lambda. + +commodity $ + value s, d, t -> 6 EUR + +;; Each account can also provide a default valuation function for any +;; commodities transferred to that account. + +account Expenses:Food5 + value myfunc_five + +;; The metadata field "Value", if found, overrides the valuation function on a +;; transaction-wide or per-posting basis. + += @XACT and Food + ; Value:: 8 EUR + (Equity) $1 + += @POST and Dining + (Expenses:Food9) $1 + ; Value:: 9 EUR + +;; Lastly, you can specify the valuation function/value for any specific +;; amount using the (( )) commodity annotation. + +2012-03-02 KFC + Expenses:Food2 $1 ((2 EUR)) + Assets:Cash2 + +2012-03-03 KFC + Expenses:Food3 $1 + ; Value:: 3 EUR + Assets:Cash3 + +2012-03-04 KFC + ; Value:: 4 EUR + Expenses:Food4 $1 + Assets:Cash4 + +2012-03-05 KFC + Expenses:Food5 $1 + Assets:Cash5 + +2012-03-06 KFC + Expenses:Food6 $1 + Assets:Cash6 + +2012-03-07 KFC + Expenses:Food7 1 CAD + Assets:Cas7 + +2012-03-08 XACT + Expenses:Food8 $1 + Assets:Cash8 + +2012-03-09 POST + Expenses:Dining9 $1 + Assets:Cash9 + +test reg -V food +12-Mar-02 KFC Expenses:Food2 2 EUR 2 EUR +12-Mar-03 KFC -1 EUR 1 EUR + Expenses:Food3 3 EUR 4 EUR +12-Mar-04 KFC -2 EUR 2 EUR + Expenses:Food4 4 EUR 6 EUR +12-Mar-05 KFC -3 EUR 3 EUR + Expenses:Food5 5 EUR 8 EUR +12-Mar-06 KFC -4 EUR 4 EUR + Expenses:Food6 6 EUR 10 EUR +12-Mar-07 KFC Expenses:Food7 7 EUR 17 EUR +12-Mar-08 XACT Expenses:Food8 8 EUR 25 EUR +12-Mar-09 POST (Expenses:Food9) 9 EUR 34 EUR +end test -- cgit v1.2.3 From 318b5f49ff07bdd906eb7c976b5c36c2eeb45872 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 9 Mar 2012 07:02:53 -0600 Subject: Fixed the behavior of fn_nail_down --- src/report.cc | 14 ++- test/baseline/feat-value-expr.test | 18 ++- test/baseline/opt-historical.test | 243 ++++++++++++++++++++++++++++++++----- 3 files changed, 234 insertions(+), 41 deletions(-) (limited to 'test/baseline/feat-value-expr.test') diff --git a/src/report.cc b/src/report.cc index 110d33ee..27d8bb2a 100644 --- a/src/report.cc +++ b/src/report.cc @@ -743,12 +743,20 @@ value_t report_t::fn_commodity(call_scope_t& args) value_t report_t::fn_nail_down(call_scope_t& args) { value_t arg0(args[0]); + value_t arg1(args[1]); + switch (arg0.type()) { case value_t::AMOUNT: { amount_t tmp(arg0.as_amount()); - if (tmp.has_commodity()) - tmp.set_commodity(tmp.commodity() - .nail_down(args[1].as_any())); + if (tmp.has_commodity() && ! arg1.is_null()) { + expr_t value_expr(is_expr(arg1) ? + as_expr(arg1) : expr_t::op_t::wrap_value(arg1 / arg0)); + std::ostringstream buf; + value_expr.print(buf); + value_expr.set_text(buf.str()); + + tmp.set_commodity(tmp.commodity().nail_down(value_expr)); + } return tmp; } diff --git a/test/baseline/feat-value-expr.test b/test/baseline/feat-value-expr.test index 690febf9..01f9780b 100644 --- a/test/baseline/feat-value-expr.test +++ b/test/baseline/feat-value-expr.test @@ -89,15 +89,11 @@ account Expenses:Food5 test reg -V food 12-Mar-02 KFC Expenses:Food2 2 EUR 2 EUR -12-Mar-03 KFC -1 EUR 1 EUR - Expenses:Food3 3 EUR 4 EUR -12-Mar-04 KFC -2 EUR 2 EUR - Expenses:Food4 4 EUR 6 EUR -12-Mar-05 KFC -3 EUR 3 EUR - Expenses:Food5 5 EUR 8 EUR -12-Mar-06 KFC -4 EUR 4 EUR - Expenses:Food6 6 EUR 10 EUR -12-Mar-07 KFC Expenses:Food7 7 EUR 17 EUR -12-Mar-08 XACT Expenses:Food8 8 EUR 25 EUR -12-Mar-09 POST (Expenses:Food9) 9 EUR 34 EUR +12-Mar-03 KFC Expenses:Food3 3 EUR 5 EUR +12-Mar-04 KFC Expenses:Food4 4 EUR 9 EUR +12-Mar-05 KFC Expenses:Food5 5 EUR 14 EUR +12-Mar-06 KFC Expenses:Food6 6 EUR 20 EUR +12-Mar-07 KFC Expenses:Food7 7 EUR 27 EUR +12-Mar-08 XACT Expenses:Food8 8 EUR 35 EUR +12-Mar-09 POST (Expenses:Food9) 9 EUR 44 EUR end test diff --git a/test/baseline/opt-historical.test b/test/baseline/opt-historical.test index 1cd141ab..9eb3558b 100644 --- a/test/baseline/opt-historical.test +++ b/test/baseline/opt-historical.test @@ -1,61 +1,250 @@ -2012-03-01 Broker - Assets:Stocks 10 APPL {$1} @ $1 +D EUR 2.000,00 + +P 2011-12-15 $ EUR 2 +P 2011-12-15 AAPL $5.00 + +2012-01-01 Broker + Assets:Stocks 10 AAPL {$1} @ $10 Equity -2012-03-02 Broker - Assets:Stocks 10 APPL {$1} @ $2 +P 2012-01-15 AAPL $15.00 + +2012-02-02 Broker + Assets:Stocks 10 AAPL {$2} @ $20 Equity +P 2012-02-15 AAPL $25.00 + 2012-03-03 Broker - Assets:Stocks 10 APPL {$1} @ $3 + Assets:Stocks 10 AAPL {$3} @ $30 Equity -2012-03-04 Broker - Assets:Stocks 10 APPL {$1} @ $4 +P 2012-03-15 AAPL $35.00 + +2012-04-04 Broker + Assets:Stocks 10 AAPL {$4} @ $40 Equity -2012-03-05 Broker - Assets:Stocks 10 APPL {$1} @ $5 +P 2012-04-15 AAPL $45.00 + +2012-05-05 Broker + Assets:Stocks 10 AAPL {$5} @ $50 Equity +P 2012-5-15 AAPL $55.00 + +test reg stocks +12-Jan-01 Broker Assets:Stocks 10 AAPL 10 AAPL +12-Feb-02 Broker Assets:Stocks 10 AAPL 20 AAPL +12-Mar-03 Broker Assets:Stocks 10 AAPL 30 AAPL +12-Apr-04 Broker Assets:Stocks 10 AAPL 40 AAPL +12-May-05 Broker Assets:Stocks 10 AAPL 50 AAPL +end test + test reg stocks -O -12-Mar-01 Broker Assets:Stocks 10 APPL 10 APPL -12-Mar-02 Broker Assets:Stocks 10 APPL 20 APPL -12-Mar-03 Broker Assets:Stocks 10 APPL 30 APPL -12-Mar-04 Broker Assets:Stocks 10 APPL 40 APPL -12-Mar-05 Broker Assets:Stocks 10 APPL 50 APPL +12-Jan-01 Broker Assets:Stocks 10 AAPL 10 AAPL +12-Feb-02 Broker Assets:Stocks 10 AAPL 20 AAPL +12-Mar-03 Broker Assets:Stocks 10 AAPL 30 AAPL +12-Apr-04 Broker Assets:Stocks 10 AAPL 40 AAPL +12-May-05 Broker Assets:Stocks 10 AAPL 50 AAPL end test test reg stocks -B -12-Mar-01 Broker Assets:Stocks $10 $10 -12-Mar-02 Broker Assets:Stocks $20 $30 -12-Mar-03 Broker Assets:Stocks $30 $60 -12-Mar-04 Broker Assets:Stocks $40 $100 -12-Mar-05 Broker Assets:Stocks $50 $150 +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Feb-02 Broker Assets:Stocks $200 $300 +12-Mar-03 Broker Assets:Stocks $300 $600 +12-Apr-04 Broker Assets:Stocks $400 $1000 +12-May-05 Broker Assets:Stocks $500 $1500 end test test reg stocks -I -12-Mar-01 Broker Assets:Stocks $10 $10 -12-Mar-02 Broker Assets:Stocks $10 $20 -12-Mar-03 Broker Assets:Stocks $10 $30 -12-Mar-04 Broker Assets:Stocks $10 $40 -12-Mar-05 Broker Assets:Stocks $10 $50 +12-Jan-01 Broker Assets:Stocks $10 $10 +12-Feb-02 Broker Assets:Stocks $20 $30 +12-Mar-03 Broker Assets:Stocks $30 $60 +12-Apr-04 Broker Assets:Stocks $40 $100 +12-May-05 Broker Assets:Stocks $50 $150 +end test + +test reg stocks -V +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Jan-15 Commodities revalued $50 $150 +12-Feb-02 Commodities revalued $50 $200 +12-Feb-02 Broker Assets:Stocks $200 $400 +12-Feb-15 Commodities revalued $100 $500 +12-Mar-03 Commodities revalued $100 $600 +12-Mar-03 Broker Assets:Stocks $300 $900 +12-Mar-15 Commodities revalued $150 $1050 +12-Apr-04 Commodities revalued $150 $1200 +12-Apr-04 Broker Assets:Stocks $400 $1600 +12-Apr-15 Commodities revalued $200 $1800 +12-May-05 Commodities revalued $200 $2000 +12-May-05 Broker Assets:Stocks $500 $2500 +end test + +test reg stocks -O -V +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Jan-15 Commodities revalued $50 $150 +12-Feb-02 Commodities revalued $50 $200 +12-Feb-02 Broker Assets:Stocks $200 $400 +12-Feb-15 Commodities revalued $100 $500 +12-Mar-03 Commodities revalued $100 $600 +12-Mar-03 Broker Assets:Stocks $300 $900 +12-Mar-15 Commodities revalued $150 $1050 +12-Apr-04 Commodities revalued $150 $1200 +12-Apr-04 Broker Assets:Stocks $400 $1600 +12-Apr-15 Commodities revalued $200 $1800 +12-May-05 Commodities revalued $200 $2000 +12-May-05 Broker Assets:Stocks $500 $2500 +end test + +test reg stocks -B -V +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Feb-02 Broker Assets:Stocks $200 $300 +12-Mar-03 Broker Assets:Stocks $300 $600 +12-Apr-04 Broker Assets:Stocks $400 $1000 +12-May-05 Broker Assets:Stocks $500 $1500 +end test + +test reg stocks -I -V +12-Jan-01 Broker Assets:Stocks $10 $10 +12-Feb-02 Broker Assets:Stocks $20 $30 +12-Mar-03 Broker Assets:Stocks $30 $60 +12-Apr-04 Broker Assets:Stocks $40 $100 +12-May-05 Broker Assets:Stocks $50 $150 +end test + +test reg stocks -X EUR +12-Jan-01 Broker Assets:Stocks EUR 200,00 EUR 200,00 +12-Feb-02 Commodities revalued EUR 200,00 EUR 400,00 +12-Feb-02 Broker Assets:Stocks EUR 400,00 EUR 800,00 +12-Mar-03 Commodities revalued EUR 400,00 EUR 1.200,00 +12-Mar-03 Broker Assets:Stocks EUR 600,00 EUR 1.800,00 +12-Apr-04 Commodities revalued EUR 600,00 EUR 2.400,00 +12-Apr-04 Broker Assets:Stocks EUR 800,00 EUR 3.200,00 +12-May-05 Commodities revalued EUR 800,00 EUR 4.000,00 +12-May-05 Broker Assets:Stocks EUR 1.000,00 EUR 5.000,00 +end test + +test reg stocks -O -X EUR +12-Jan-01 Broker Assets:Stocks EUR 200,00 EUR 200,00 +12-Feb-02 Commodities revalued EUR 200,00 EUR 400,00 +12-Feb-02 Broker Assets:Stocks EUR 400,00 EUR 800,00 +12-Mar-03 Commodities revalued EUR 400,00 EUR 1.200,00 +12-Mar-03 Broker Assets:Stocks EUR 600,00 EUR 1.800,00 +12-Apr-04 Commodities revalued EUR 600,00 EUR 2.400,00 +12-Apr-04 Broker Assets:Stocks EUR 800,00 EUR 3.200,00 +12-May-05 Commodities revalued EUR 800,00 EUR 4.000,00 +12-May-05 Broker Assets:Stocks EUR 1.000,00 EUR 5.000,00 +end test + +test reg stocks -B -X EUR +12-Jan-01 Broker Assets:Stocks EUR 200,00 EUR 200,00 +12-Feb-02 Broker Assets:Stocks EUR 400,00 EUR 600,00 +12-Mar-03 Broker Assets:Stocks EUR 600,00 EUR 1.200,00 +12-Apr-04 Broker Assets:Stocks EUR 800,00 EUR 2.000,00 +12-May-05 Broker Assets:Stocks EUR 1.000,00 EUR 3.000,00 +end test + +test reg stocks -I -X EUR +12-Jan-01 Broker Assets:Stocks EUR 20,00 EUR 20,00 +12-Feb-02 Broker Assets:Stocks EUR 40,00 EUR 60,00 +12-Mar-03 Broker Assets:Stocks EUR 60,00 EUR 120,00 +12-Apr-04 Broker Assets:Stocks EUR 80,00 EUR 200,00 +12-May-05 Broker Assets:Stocks EUR 100,00 EUR 300,00 +end test + +test reg stocks -H +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Feb-02 Broker Assets:Stocks $200 $300 +12-Mar-03 Broker Assets:Stocks $300 $600 +12-Apr-04 Broker Assets:Stocks $400 $1000 +12-May-05 Broker Assets:Stocks $500 $1500 end test test reg stocks -O -H +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Feb-02 Broker Assets:Stocks $200 $300 +12-Mar-03 Broker Assets:Stocks $300 $600 +12-Apr-04 Broker Assets:Stocks $400 $1000 +12-May-05 Broker Assets:Stocks $500 $1500 end test test reg stocks -B -H +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Feb-02 Broker Assets:Stocks $200 $300 +12-Mar-03 Broker Assets:Stocks $300 $600 +12-Apr-04 Broker Assets:Stocks $400 $1000 +12-May-05 Broker Assets:Stocks $500 $1500 end test test reg stocks -I -H +12-Jan-01 Broker Assets:Stocks $10 $10 +12-Feb-02 Broker Assets:Stocks $20 $30 +12-Mar-03 Broker Assets:Stocks $30 $60 +12-Apr-04 Broker Assets:Stocks $40 $100 +12-May-05 Broker Assets:Stocks $50 $150 end test -test reg stocks -O -V +test reg stocks -H -V +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Feb-02 Broker Assets:Stocks $200 $300 +12-Mar-03 Broker Assets:Stocks $300 $600 +12-Apr-04 Broker Assets:Stocks $400 $1000 +12-May-05 Broker Assets:Stocks $500 $1500 end test -test reg stocks -B -V +test reg stocks -O -H -V +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Feb-02 Broker Assets:Stocks $200 $300 +12-Mar-03 Broker Assets:Stocks $300 $600 +12-Apr-04 Broker Assets:Stocks $400 $1000 +12-May-05 Broker Assets:Stocks $500 $1500 end test -test reg stocks -I -V +test reg stocks -B -H -V +12-Jan-01 Broker Assets:Stocks $100 $100 +12-Feb-02 Broker Assets:Stocks $200 $300 +12-Mar-03 Broker Assets:Stocks $300 $600 +12-Apr-04 Broker Assets:Stocks $400 $1000 +12-May-05 Broker Assets:Stocks $500 $1500 +end test + +test reg stocks -I -H -V +12-Jan-01 Broker Assets:Stocks $10 $10 +12-Feb-02 Broker Assets:Stocks $20 $30 +12-Mar-03 Broker Assets:Stocks $30 $60 +12-Apr-04 Broker Assets:Stocks $40 $100 +12-May-05 Broker Assets:Stocks $50 $150 +end test + +test reg stocks -H -X EUR +12-Jan-01 Broker Assets:Stocks EUR 200,00 EUR 200,00 +12-Feb-02 Broker Assets:Stocks EUR 400,00 EUR 600,00 +12-Mar-03 Broker Assets:Stocks EUR 600,00 EUR 1.200,00 +12-Apr-04 Broker Assets:Stocks EUR 800,00 EUR 2.000,00 +12-May-05 Broker Assets:Stocks EUR 1.000,00 EUR 3.000,00 +end test + +test reg stocks -O -H -X EUR +12-Jan-01 Broker Assets:Stocks EUR 200,00 EUR 200,00 +12-Feb-02 Broker Assets:Stocks EUR 400,00 EUR 600,00 +12-Mar-03 Broker Assets:Stocks EUR 600,00 EUR 1.200,00 +12-Apr-04 Broker Assets:Stocks EUR 800,00 EUR 2.000,00 +12-May-05 Broker Assets:Stocks EUR 1.000,00 EUR 3.000,00 +end test + +test reg stocks -B -H -X EUR +12-Jan-01 Broker Assets:Stocks EUR 200,00 EUR 200,00 +12-Feb-02 Broker Assets:Stocks EUR 400,00 EUR 600,00 +12-Mar-03 Broker Assets:Stocks EUR 600,00 EUR 1.200,00 +12-Apr-04 Broker Assets:Stocks EUR 800,00 EUR 2.000,00 +12-May-05 Broker Assets:Stocks EUR 1.000,00 EUR 3.000,00 +end test + +test reg stocks -I -H -X EUR +12-Jan-01 Broker Assets:Stocks EUR 20,00 EUR 20,00 +12-Feb-02 Broker Assets:Stocks EUR 40,00 EUR 60,00 +12-Mar-03 Broker Assets:Stocks EUR 60,00 EUR 120,00 +12-Apr-04 Broker Assets:Stocks EUR 80,00 EUR 200,00 +12-May-05 Broker Assets:Stocks EUR 100,00 EUR 300,00 end test -- cgit v1.2.3