From 2258e7a44ea550c709d9c429a973d66add31e5ec Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sat, 24 Apr 2021 15:42:01 -0700 Subject: [PATCH] xaccAccountTypes was backwards vs. the documentation. The unit tests and dialog-account were similarly backwards, but the use in gnucash/import-export/ofx followed the docs. --- gnucash/gnome-utils/dialog-account.c | 4 ++-- libgnucash/engine/Account.cpp | 13 +++++++++++-- libgnucash/engine/test/utest-Account.cpp | 11 +++++------ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/gnucash/gnome-utils/dialog-account.c b/gnucash/gnome-utils/dialog-account.c index 43f66a78a3..508e191db2 100644 --- a/gnucash/gnome-utils/dialog-account.c +++ b/gnucash/gnome-utils/dialog-account.c @@ -589,7 +589,7 @@ make_children_compatible (AccountWindow *aw) account = aw_get_account (aw); g_return_if_fail (account); - if (xaccAccountTypesCompatible (xaccAccountGetType (account), aw->type)) + if (xaccAccountTypesCompatible (aw->type, xaccAccountGetType (account))) return; set_children_types (account, aw->type); @@ -709,7 +709,7 @@ verify_children_compatible (AccountWindow *aw) if (!account) return FALSE; - if (xaccAccountTypesCompatible (xaccAccountGetType (account), aw->type)) + if (xaccAccountTypesCompatible (aw->type, xaccAccountGetType (account))) return TRUE; if (gnc_account_n_children(account) == 0) diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index f820cbdba8..b82180d07a 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -4509,8 +4509,17 @@ gboolean xaccAccountTypesCompatible (GNCAccountType parent_type, GNCAccountType child_type) { - return ((xaccParentAccountTypesCompatibleWith (parent_type) & - (1 << child_type)) + /* ACCT_TYPE_NONE isn't compatible with anything, even ACCT_TYPE_NONE. */ + if (parent_type == ACCT_TYPE_NONE || child_type == ACCT_TYPE_NONE) + return FALSE; + + /* ACCT_TYPE_ROOT can't have a parent account, and asking will raise + * an error. */ + if (child_type == ACCT_TYPE_ROOT) + return FALSE; + + return ((xaccParentAccountTypesCompatibleWith (child_type) & + (1 << parent_type)) != 0); } diff --git a/libgnucash/engine/test/utest-Account.cpp b/libgnucash/engine/test/utest-Account.cpp index 30cb137234..35f411322f 100644 --- a/libgnucash/engine/test/utest-Account.cpp +++ b/libgnucash/engine/test/utest-Account.cpp @@ -2251,9 +2251,8 @@ test_xaccAccountType_Compatibility (void) auto check2 = test_error_struct_new(logdomain, loglevel, msg2); gint loghandler; - for (type = ACCT_TYPE_BANK; type < NUM_ACCOUNT_TYPES; type = ++type) + for (type = ACCT_TYPE_BANK; type < NUM_ACCOUNT_TYPES; ++type) { - GNCAccountType child; if (type == ACCT_TYPE_ROOT) { loghandler = g_log_set_handler (logdomain, loglevel, @@ -2278,11 +2277,11 @@ test_xaccAccountType_Compatibility (void) g_assert_cmpint (compat, == , equity_compat); else if (type == ACCT_TYPE_TRADING) g_assert_cmpint (compat, == , trading_compat); - for (child = ACCT_TYPE_NONE; child < ACCT_TYPE_LAST; child = ++child) - if (1 << child & compat) - g_assert (xaccAccountTypesCompatible (type, child)); + for (auto parent = ACCT_TYPE_NONE; parent < ACCT_TYPE_LAST; ++parent) + if (1 << parent & compat) + g_assert (xaccAccountTypesCompatible (parent, type)); else - g_assert (!xaccAccountTypesCompatible (type, child)); + g_assert (!xaccAccountTypesCompatible (parent, type)); compat = xaccAccountTypesCompatibleWith (type); if (type <= ACCT_TYPE_LIABILITY ||