[gnome-calculator/wip/currency-conversion-plugins] Added imf currency provider test



commit b8eede7059f50b30a03ddd0497bc7796a392f0ee
Author: Robert Roth <robert roth off gmail com>
Date:   Fri Jan 15 13:00:34 2021 +0200

    Added imf currency provider test

 lib/currency-provider.vala         | 14 +++++++---
 lib/currency.vala                  |  9 +++++--
 tests/test-currency-providers.vala | 55 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 71 insertions(+), 7 deletions(-)
---
diff --git a/lib/currency-provider.vala b/lib/currency-provider.vala
index d4d53b55..8bf27cde 100644
--- a/lib/currency-provider.vala
+++ b/lib/currency-provider.vala
@@ -5,9 +5,11 @@ public interface CurrencyProvider : Object {
     public abstract void update_rates (bool asyncLoad = true);
 
     public abstract void set_refresh_interval (int interval, bool asyncLoad = true);
+
+    public abstract void clear ();
 }
 
-abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
+public abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
 
     public abstract string rate_filepath {owned get ;}
 
@@ -28,6 +30,10 @@ abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
     private List<Currency> currencies;
     public CurrencyManager currency_manager {get; construct;}
 
+    public void clear () {
+        FileUtils.remove (rate_filepath);
+    }
+
     public void update_rates (bool asyncLoad = true) {
         debug ("Updating %s rates ".printf(source_name));
 
@@ -142,7 +148,7 @@ abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
 
 }
 
-class ImfCurrencyProvider : AbstractCurrencyProvider {
+public class ImfCurrencyProvider : AbstractCurrencyProvider {
     public override string rate_filepath { owned get {
         return Path.build_filename (Environment.get_user_cache_dir (), "gnome-calculator", "rms_five.xls"); 
} }
 
@@ -283,11 +289,12 @@ class ImfCurrencyProvider : AbstractCurrencyProvider {
     public ImfCurrencyProvider (CurrencyManager _currency_manager)
     {
         Object(currency_manager: _currency_manager);
+        _currency_manager.add_provider (this);
     }
 }
 
 
-class EcbCurrencyProvider : AbstractCurrencyProvider {
+public class EcbCurrencyProvider : AbstractCurrencyProvider {
     public override string rate_filepath { owned get {
         return Path.build_filename (Environment.get_user_cache_dir (), "gnome-calculator", 
"eurofxref-daily.xml"); } }
 
@@ -389,5 +396,6 @@ class EcbCurrencyProvider : AbstractCurrencyProvider {
     public EcbCurrencyProvider (CurrencyManager _currency_manager)
     {
         Object(currency_manager: _currency_manager);
+        _currency_manager.add_provider (this);
     }
 }
diff --git a/lib/currency.vala b/lib/currency.vala
index bcde9f16..9663868f 100644
--- a/lib/currency.vala
+++ b/lib/currency.vala
@@ -26,11 +26,16 @@ public class CurrencyManager : Object
 
     public signal void updated ();
 
+    public void add_provider (CurrencyProvider provider) {
+        providers.append (provider);
+    }
+
     public void refresh_sync () {
         foreach (var p in default_currency_manager.providers) {
             p.set_refresh_interval(_refresh_interval, false);
         }
     }
+
     public void refresh_async () {
         foreach (var p in default_currency_manager.providers) {
             p.set_refresh_interval(_refresh_interval, true);
@@ -104,8 +109,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));
-        default_currency_manager.providers.append (new EcbCurrencyProvider (default_currency_manager));
+        new ImfCurrencyProvider (default_currency_manager);
+        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/test-currency-providers.vala b/tests/test-currency-providers.vala
index d38f085d..7bde4b37 100644
--- a/tests/test-currency-providers.vala
+++ b/tests/test-currency-providers.vala
@@ -36,6 +36,49 @@ private class TestConversion : Equation
     }
 }
 
+private void test_currency (CurrencyManager currency_manager, string currency_str, string? expected, 
ErrorCode expected_error)
+{
+    var result_currency = currency_manager.get_currency (currency_str);
+
+    uint representation_base = 10;
+
+    if (result_currency == null)
+    {
+        if (expected == null)
+        {
+            pass_count++;
+        }
+        else
+        {
+            stdout.printf ("*FAIL: '%s' currency should not be available\n", currency_str);
+            fail_count++;
+        }
+    }
+    else
+    {
+        var serializer = new Serializer (DisplayFormat.FIXED, number_base, 9);
+        serializer.set_representation_base (representation_base);
+        var result = result_currency.get_value ();
+        var result_str = serializer.to_string (result);
+
+        if (expected_error != ErrorCode.NONE)
+        {
+            stdout.printf ("*FAIL: '%s' -> %s, expected error %s\n", currency_str, result_str, 
error_code_to_string (expected_error));
+            fail_count++;
+        }
+        else if (result_str != expected)
+        {
+            stdout.printf ("*FAIL: '%s' -> '%s', expected '%s'\n", currency_str, result_str, expected);
+            fail_count++;
+        }
+        else
+        {
+            /*stdout.printf ("PASS: '%s' -> '%s'\n", expression, result_str);*/
+            pass_count++;
+        }
+    }
+}
+
 private void test (string expression, string expected, ErrorCode expected_error)
 {
     var equation = new TestConversion (expression);
@@ -92,6 +135,14 @@ private void test (string expression, string expected, ErrorCode expected_error)
 
 private void test_imf_provider ()
 {
+    var currency_manager = new CurrencyManager();
+    var imf_provider = new ImfCurrencyProvider (currency_manager);
+
+    imf_provider.clear ();
+    test_currency (currency_manager, "EUR", null, 0);
+    // set refresh interval and sync update
+    imf_provider.set_refresh_interval (3600, false);
+
 }
 
 private void test_ecb_provider ()
@@ -102,7 +153,7 @@ private void test_currency_conversions ()
 {
     CurrencyManager.get_default (false).refresh_interval = 3600;
     CurrencyManager.get_default ().refresh_sync ();
-    
+
     test ("1 EUR in EUR", "1", 0);
 }
 
@@ -110,9 +161,9 @@ public int main (string[] args)
 {
     Intl.setlocale (LocaleCategory.ALL, "C");
 
-    test_currency_conversions ();
     test_imf_provider ();
     test_ecb_provider ();
+    test_currency_conversions ();
 
     if (fail_count == 0)
         stdout.printf ("Passed all %i tests\n", pass_count);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]