summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-06-21 18:07:27 +0100
committerJohn Wiegley <johnw@newartisans.com>2009-06-21 18:07:27 +0100
commit521b935aa8ef68ac3a8a1fe1a37eed39b41eec95 (patch)
treeb7e482b7ea2d7725387046fdd6523e0f15f1dfa3
parent7287aad336161eb9a143f188c92d462e62051374 (diff)
downloadfork-ledger-521b935aa8ef68ac3a8a1fe1a37eed39b41eec95.tar.gz
fork-ledger-521b935aa8ef68ac3a8a1fe1a37eed39b41eec95.tar.bz2
fork-ledger-521b935aa8ef68ac3a8a1fe1a37eed39b41eec95.zip
Restored --percent option, added baseline test
-rw-r--r--src/account.cc15
-rw-r--r--src/item.cc11
-rw-r--r--src/op.cc13
-rw-r--r--src/output.cc3
-rw-r--r--src/post.cc2
-rw-r--r--src/report.cc13
-rw-r--r--src/report.h9
-rw-r--r--src/session.cc6
-rw-r--r--test/baseline/opt-percent.test94
9 files changed, 157 insertions, 9 deletions
diff --git a/src/account.cc b/src/account.cc
index 58d9392f..c2628134 100644
--- a/src/account.cc
+++ b/src/account.cc
@@ -194,6 +194,10 @@ namespace {
return false;
}
+ value_t get_true(account_t&) {
+ return true;
+ }
+
value_t get_depth_spacer(account_t& account)
{
std::size_t depth = 0;
@@ -217,6 +221,10 @@ namespace {
value_t get_wrapper(call_scope_t& scope) {
return (*Func)(find_scope<account_t>(scope));
}
+
+ value_t get_parent(account_t& account) {
+ return value_t(static_cast<scope_t *>(account.parent));
+ }
}
expr_t::ptr_op_t account_t::lookup(const string& name)
@@ -243,9 +251,16 @@ expr_t::ptr_op_t account_t::lookup(const string& name)
return WRAP_FUNCTOR(get_wrapper<&get_depth_spacer>);
break;
+ case 'i':
+ if (name == "is_account")
+ return WRAP_FUNCTOR(get_wrapper<&get_true>);
+ break;
+
case 'p':
if (name == "partial_account")
return WRAP_FUNCTOR(get_partial_name);
+ else if (name == "parent")
+ return WRAP_FUNCTOR(get_wrapper<&get_parent>);
break;
case 's':
diff --git a/src/item.cc b/src/item.cc
index e54fa6be..9e297052 100644
--- a/src/item.cc
+++ b/src/item.cc
@@ -247,6 +247,10 @@ namespace {
return 0L;
}
+ value_t ignore(item_t&) {
+ return false;
+ }
+
template <value_t (*Func)(item_t&)>
value_t get_wrapper(call_scope_t& scope) {
return (*Func)(find_scope<item_t>(scope));
@@ -330,6 +334,11 @@ expr_t::ptr_op_t item_t::lookup(const string& name)
return WRAP_FUNCTOR(ledger::has_tag);
break;
+ case 'i':
+ if (name == "is_account")
+ return WRAP_FUNCTOR(get_wrapper<&ignore>);
+ break;
+
case 'm':
if (name == "meta")
return WRAP_FUNCTOR(ledger::get_tag);
@@ -343,6 +352,8 @@ expr_t::ptr_op_t item_t::lookup(const string& name)
case 'p':
if (name == "pending")
return WRAP_FUNCTOR(get_wrapper<&get_pending>);
+ else if (name == "parent")
+ return WRAP_FUNCTOR(get_wrapper<&ignore>);
break;
case 's':
diff --git a/src/op.cc b/src/op.cc
index f80d444a..f1b2fb6b 100644
--- a/src/op.cc
+++ b/src/op.cc
@@ -166,10 +166,15 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * locus)
call_scope_t call_args(scope);
if (value_t obj = left()->left()->as_function()(call_args)) {
if (obj.is_pointer()) {
- scope_t& objscope(obj.as_ref_lval<scope_t>());
- if (ptr_op_t member = objscope.lookup(right()->as_ident())) {
- result = member->calc(objscope);
- break;
+ if (obj.as_pointer_lval<scope_t>() == NULL) {
+ throw_(calc_error,
+ _("Left operand of . operator is NULL"));
+ } else {
+ scope_t& objscope(obj.as_ref_lval<scope_t>());
+ if (ptr_op_t member = objscope.lookup(right()->as_ident())) {
+ result = member->calc(objscope);
+ break;
+ }
}
}
}
diff --git a/src/output.cc b/src/output.cc
index c4317934..057733aa 100644
--- a/src/output.cc
+++ b/src/output.cc
@@ -209,7 +209,8 @@ void format_accounts::flush()
foreach (account_t * account, posted_accounts)
displayed += post_account(*account);
- if (! report.HANDLED(no_total) && displayed > 1) {
+ if (displayed > 1 &&
+ ! report.HANDLED(no_total) && ! report.HANDLED(percent)) {
bind_scope_t bound_scope(report, *report.session.master);
separator_format.format(out, bound_scope);
total_line_format.format(out, bound_scope);
diff --git a/src/post.cc b/src/post.cc
index 8eccf03d..9b416434 100644
--- a/src/post.cc
+++ b/src/post.cc
@@ -302,6 +302,8 @@ expr_t::ptr_op_t post_t::lookup(const string& name)
return WRAP_FUNCTOR(get_wrapper<&get_payee>);
else if (name == "primary")
return WRAP_FUNCTOR(get_wrapper<&get_commodity_is_primary>);
+ else if (name == "parent")
+ return WRAP_FUNCTOR(get_wrapper<&get_xact>);
break;
case 'r':
diff --git a/src/report.cc b/src/report.cc
index 8ccdfad1..1aa614ec 100644
--- a/src/report.cc
+++ b/src/report.cc
@@ -291,6 +291,13 @@ value_t report_t::fn_ansify_if(call_scope_t& scope)
}
}
+value_t report_t::fn_percent(call_scope_t& scope)
+{
+ interactive_t args(scope, "aa");
+ return (amount_t("100.00%") *
+ (args.get<amount_t>(0) / args.get<amount_t>(1)).number());
+}
+
namespace {
value_t fn_black(call_scope_t&) {
return string_value("black");
@@ -402,7 +409,7 @@ option_t<report_t> * report_t::lookup_option(const char * p)
{
switch (*p) {
case '%':
- OPT_CH(percentage);
+ OPT_CH(percent);
break;
case 'A':
OPT_CH(average);
@@ -568,7 +575,7 @@ option_t<report_t> * report_t::lookup_option(const char * p)
OPT(pager_);
else OPT(payee_as_account);
else OPT(pending);
- else OPT(percentage);
+ else OPT(percent);
else OPT_(period_);
else OPT(period_sort_);
else OPT(plot_amount_format_);
@@ -842,6 +849,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
}
else if (is_eq(p, "post"))
return WRAP_FUNCTOR(fn_false);
+ else if (is_eq(p, "percent"))
+ return MAKE_FUNCTOR(report_t::fn_percent);
break;
case 'q':
diff --git a/src/report.h b/src/report.h
index cad4b5ba..c091487b 100644
--- a/src/report.h
+++ b/src/report.h
@@ -155,6 +155,7 @@ public:
value_t fn_join(call_scope_t& scope);
value_t fn_format_date(call_scope_t& scope);
value_t fn_ansify_if(call_scope_t& scope);
+ value_t fn_percent(call_scope_t& scope);
#if 0
value_t fn_now(call_scope_t&) {
@@ -248,7 +249,7 @@ public:
HANDLER(pager_).report(out);
HANDLER(payee_as_account).report(out);
HANDLER(pending).report(out);
- HANDLER(percentage).report(out);
+ HANDLER(percent).report(out);
HANDLER(period_).report(out);
HANDLER(period_sort_).report(out);
HANDLER(plot_amount_format_).report(out);
@@ -582,7 +583,11 @@ public:
parent->HANDLER(limit_).on(string("--pending"), "pending");
});
- OPTION(report_t, percentage); // -%
+ OPTION_(report_t, percent, DO() { // -%
+ parent->HANDLER(total_)
+ .set_expr(string("--percent"),
+ "is_account&parent&parent.total&percent(total, parent.total)");
+ });
OPTION__
(report_t, period_, // -p
diff --git a/src/session.cc b/src/session.cc
index 2ae09e21..6b891e1b 100644
--- a/src/session.cc
+++ b/src/session.cc
@@ -81,6 +81,12 @@ session_t::session_t()
commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_NOMARKET);
else
assert(false);
+
+ // Add a "percentile" commodity
+ if (commodity_t * commodity = commodity_pool->create("%"))
+ commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_NOMARKET);
+ else
+ assert(false);
}
std::size_t session_t::read_journal(std::istream& in,
diff --git a/test/baseline/opt-percent.test b/test/baseline/opt-percent.test
new file mode 100644
index 00000000..1adb0a8a
--- /dev/null
+++ b/test/baseline/opt-percent.test
@@ -0,0 +1,94 @@
+bal --percent
+<<<
+2008/01/11 LIAT
+ Expenses:Travel:Airfare $40.00
+ Liabilities:MasterCard
+
+2008/01/14 cheaptickets.com
+ Expenses:Travel:Airfare $182.19
+ Liabilities:MasterCard
+
+2008/02/05 CTX
+ Expenses:Travel:Auto $240.38
+ Liabilities:MasterCard
+
+2008/02/05 UNITED
+ Expenses:Travel:Airfare $238.80
+ Liabilities:MasterCard
+
+2008/02/05 UNITED
+ Expenses:Travel:Airfare $238.80
+ Liabilities:MasterCard
+
+2008/02/22 BUDGET RENT-A-CAR
+ Expenses:Travel:Auto $40.59
+ Liabilities:MasterCard
+
+2008/03/16 IBERIA
+ Expenses:Travel:Airfare $1,231.60
+ Liabilities:MasterCard
+
+2008/03/16 IBERIA
+ Expenses:Travel:Airfare $1,231.60
+ Liabilities:MasterCard
+
+2008/04/03 AMERICAN
+ Expenses:Travel:Airfare $155.86
+ Liabilities:MasterCard
+
+2008/04/03 AMERICAN
+ Expenses:Travel:Airfare $155.86
+ Liabilities:MasterCard
+
+2008/04/30 UNITED
+ Expenses:Travel:Airfare $437.21
+ Liabilities:MasterCard
+
+2008/04/30 UNITED
+ Expenses:Travel:Airfare $437.21
+ Liabilities:MasterCard
+
+2008/08/08 BCIS I-131 FILING FEE-
+ Expenses:Travel:Passport $170.00
+ Liabilities:MasterCard
+
+2008/09/06 AMERICAN
+ Expenses:Travel:Airfare $912.60
+ Liabilities:MasterCard
+
+2008/09/06 AMERICAN
+ Expenses:Travel:Airfare $912.60
+ Liabilities:MasterCard
+
+2008/09/22 AGNT FEE
+ Expenses:Travel:Airfare $70.00
+ Liabilities:MasterCard
+
+2008/09/22 DELTA
+ Expenses:Travel:Airfare $806.20
+ Liabilities:MasterCard
+
+2008/09/22 DELTA
+ Expenses:Travel:Airfare $806.20
+ Liabilities:MasterCard
+
+2008/09/22 LIAT 1974 LIMITED
+ Expenses:Travel:Airfare $418.34
+ Liabilities:MasterCard
+
+2008/12/26 U.S. Department of State
+ Expenses:Travel:Passport $127.00
+ Assets:Checking
+
+2008/12/26 U.S. Department of State
+ Expenses:Travel:Passport $127.00
+ Assets:Checking
+>>>1
+ 100.00% Assets:Checking
+ 100.00% Expenses:Travel
+ 92.15% Airfare
+ 3.13% Auto
+ 4.72% Passport
+ 100.00% Liabilities:MasterCard
+>>>2
+=== 0