[gnome-calculator/wip/currency-conversion-plugins] Added currency-provider tests
- From: Robert Roth <robertroth src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator/wip/currency-conversion-plugins] Added currency-provider tests
- Date: Fri, 15 Jan 2021 09:13:24 +0000 (UTC)
commit 43c4d44563e90fd089c6073b8286aafa5dd2e534
Author: Robert Roth <robert roth off gmail com>
Date: Fri Jan 15 11:13:11 2021 +0200
Added currency-provider tests
lib/currency-provider.vala | 19 ++++--
lib/currency.vala | 4 +-
tests/meson.build | 11 ++++
tests/test-currency-providers.vala | 123 +++++++++++++++++++++++++++++++++++++
tests/test-equation.vala | 9 ---
5 files changed, 151 insertions(+), 15 deletions(-)
---
diff --git a/lib/currency-provider.vala b/lib/currency-provider.vala
index 53c7c4ce..d4d53b55 100644
--- a/lib/currency-provider.vala
+++ b/lib/currency-provider.vala
@@ -26,6 +26,7 @@ abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
private bool loading;
private bool loaded;
private List<Currency> currencies;
+ public CurrencyManager currency_manager {get; construct;}
public void update_rates (bool asyncLoad = true) {
debug ("Updating %s rates ".printf(source_name));
@@ -57,7 +58,7 @@ abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
protected Currency? get_currency (string name)
{
- return CurrencyManager.get_default ().get_currency (name);
+ return currency_manager.get_currency (name);
}
protected virtual void do_load_rates () {
@@ -265,7 +266,7 @@ class ImfCurrencyProvider : AbstractCurrencyProvider {
if (c == null && value != null)
{
debug ("Using IMF rate of %s for %s", tokens[value_index], symbol);
- c = CurrencyManager.get_default ().add_currency (symbol, source_name);
+ c = currency_manager.add_currency (symbol, source_name);
value = value.reciprocal ();
if (c != null)
c.set_value (value);
@@ -278,6 +279,11 @@ class ImfCurrencyProvider : AbstractCurrencyProvider {
}
base.do_load_rates ();
}
+
+ public ImfCurrencyProvider (CurrencyManager _currency_manager)
+ {
+ Object(currency_manager: _currency_manager);
+ }
}
@@ -362,7 +368,7 @@ class EcbCurrencyProvider : AbstractCurrencyProvider {
if (name != null && value != null && get_currency (name) == null)
{
debug ("Using ECB rate of %s for %s", value, name);
- var c = CurrencyManager.get_default ().add_currency (name, source_name);
+ var c = currency_manager.add_currency (name, source_name);
var r = mp_set_from_string (value);
var v = eur_rate.get_value ();
v = v.multiply (r);
@@ -373,10 +379,15 @@ class EcbCurrencyProvider : AbstractCurrencyProvider {
private void set_ecb_fixed_rate (string name, string value, Currency eur_rate)
{
debug ("Using ECB fixed rate of %s for %s", value, name);
- var c = CurrencyManager.get_default ().add_currency (name, source_name + "#fixed");
+ var c = currency_manager.add_currency (name, source_name + "#fixed");
var r = mp_set_from_string (value);
var v = eur_rate.get_value ();
v = v.divide (r);
c.set_value (v);
}
+
+ public EcbCurrencyProvider (CurrencyManager _currency_manager)
+ {
+ Object(currency_manager: _currency_manager);
+ }
}
diff --git a/lib/currency.vala b/lib/currency.vala
index 351bd38e..bcde9f16 100644
--- a/lib/currency.vala
+++ b/lib/currency.vala
@@ -104,8 +104,8 @@ public class CurrencyManager : Object
default_currency_manager.currencies.append (new Currency ("VEF", _("Venezuelan BolĂvar"), "Bs F"));
default_currency_manager.currencies.append (new Currency ("ZAR", _("South African Rand"), "R"));
- default_currency_manager.providers.append (new ImfCurrencyProvider ());
- default_currency_manager.providers.append (new EcbCurrencyProvider ());
+ default_currency_manager.providers.append (new ImfCurrencyProvider (default_currency_manager));
+ default_currency_manager.providers.append (new EcbCurrencyProvider (default_currency_manager));
/* Start downloading the rates if they are outdated. */
foreach (var p in default_currency_manager.providers) {
p.updated.connect ( () => { default_currency_manager.updated (); });
diff --git a/tests/meson.build b/tests/meson.build
index e76a591a..f9c4f1a7 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -30,6 +30,17 @@ test_equation = executable('test-equation', test_equation_sources,
)
test('Equation test', test_equation)
+test_currency_sources = [
+ 'test-currency-providers.vala',
+]
+test_currency = executable('test-currency-providers', test_currency_sources,
+ dependencies: gnome_calculator_tests_deps,
+ link_with: [libcalculator, lib_mpfrg],
+ include_directories: gnome_calculator_tests_includes,
+)
+test('Currency test', test_currency)
+
+
test_number_sources = [
'test-number.vala',
]
diff --git a/tests/test-currency-providers.vala b/tests/test-currency-providers.vala
new file mode 100644
index 00000000..d38f085d
--- /dev/null
+++ b/tests/test-currency-providers.vala
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2021 Robert Roth.
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+
+private int number_base = 10;
+private int wordlen = 32;
+private AngleUnit angle_units = AngleUnit.DEGREES;
+
+private int fail_count = 0;
+private int pass_count = 0;
+
+private string error_code_to_string (ErrorCode error)
+{
+ if (error == ErrorCode.MP)
+ return "ErrorCode.MP(\"%s\")".printf (Number.error);
+
+ return mp_error_code_to_string (error);
+}
+
+private class TestConversion : Equation
+{
+ public TestConversion (string equation)
+ {
+ base (equation);
+ }
+
+ public override Number? convert (Number x, string x_units, string z_units)
+ {
+ return UnitManager.get_default ().convert_by_symbol (x, x_units, z_units);
+ }
+}
+
+private void test (string expression, string expected, ErrorCode expected_error)
+{
+ var equation = new TestConversion (expression);
+ equation.base = number_base;
+ equation.wordlen = wordlen;
+ equation.angle_units = angle_units;
+
+ ErrorCode error;
+ uint representation_base;
+ var result = equation.parse (out representation_base, out error);
+
+ if (result == null)
+ {
+ if (error == expected_error)
+ {
+ /*stdout.printf ("PASS: '%s' -> error %s\n", expression, error_code_to_string (error));*/
+ pass_count++;
+ }
+ else if (expected_error == ErrorCode.NONE)
+ {
+ stdout.printf ("*FAIL: '%s' -> error %s, expected result %s\n", expression, error_code_to_string
(error), expected);
+ fail_count++;
+ }
+ else
+ {
+ stdout.printf ("*FAIL: '%s' -> error %s, expected error %s\n", expression, error_code_to_string
(error), error_code_to_string (expected_error));
+ fail_count++;
+ }
+ }
+ else
+ {
+ var serializer = new Serializer (DisplayFormat.FIXED, number_base, 9);
+ serializer.set_representation_base (representation_base);
+ var result_str = serializer.to_string (result);
+
+ if (expected_error != ErrorCode.NONE)
+ {
+ stdout.printf ("*FAIL: '%s' -> %s, expected error %s\n", expression, result_str,
error_code_to_string (expected_error));
+ fail_count++;
+ }
+ else if (result_str != expected)
+ {
+ stdout.printf ("*FAIL: '%s' -> '%s', expected '%s'\n", expression, result_str, expected);
+ fail_count++;
+ }
+ else
+ {
+ /*stdout.printf ("PASS: '%s' -> '%s'\n", expression, result_str);*/
+ pass_count++;
+ }
+ }
+}
+
+
+private void test_imf_provider ()
+{
+}
+
+private void test_ecb_provider ()
+{
+}
+
+private void test_currency_conversions ()
+{
+ CurrencyManager.get_default (false).refresh_interval = 3600;
+ CurrencyManager.get_default ().refresh_sync ();
+
+ test ("1 EUR in EUR", "1", 0);
+}
+
+public int main (string[] args)
+{
+ Intl.setlocale (LocaleCategory.ALL, "C");
+
+ test_currency_conversions ();
+ test_imf_provider ();
+ test_ecb_provider ();
+
+ if (fail_count == 0)
+ stdout.printf ("Passed all %i tests\n", pass_count);
+ else
+ stdout.printf ("Failed %i/%d tests\n", fail_count, pass_count + fail_count);
+
+ return fail_count;
+}
diff --git a/tests/test-equation.vala b/tests/test-equation.vala
index df4df2d9..d2e6806a 100644
--- a/tests/test-equation.vala
+++ b/tests/test-equation.vala
@@ -723,14 +723,6 @@ private void test_base_conversion ()
test ("0xa in dec", "10", 0);
}
-private void test_currency_conversions ()
-{
- CurrencyManager.get_default (false).refresh_interval = 3600;
- CurrencyManager.get_default ().refresh_sync ();
-
- test ("1 EUR in EUR", "1", 0);
-}
-
private void test_precedence ()
{
number_base = 10;
@@ -827,7 +819,6 @@ public int main (string[] args)
test_conversions ();
test_equations ();
test_base_conversion ();
- test_currency_conversions ();
test_precedence ();
test_custom_functions ();
test_bit_shift ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]