Browse Source

sum: added sum_regex_value_first

Fabian Peter Hammerle 7 years ago
parent
commit
006f87fd91
2 changed files with 19 additions and 0 deletions
  1. 3 0
      finoex/__init__.py
  2. 16 0
      tests/test_sum.py

+ 3 - 0
finoex/__init__.py

@@ -93,6 +93,9 @@ class Sum(ioex.calcex.Figure):
 
     value_regex = r"-?\d+([\.,]\d+)?"
     currency_regex = r"[^\d\s-]+"
+    sum_regex_value_first = r'\$?(?P<value>{}) (?P<currency>{})'.format(
+        value_regex, currency_regex,
+    )
 
     sum_regex = [
         r'^\$?(?P<value>{}) (?P<currency>{})$'.format(

+ 16 - 0
tests/test_sum.py

@@ -3,6 +3,7 @@ import pytest
 import copy
 import finoex
 import ioex
+import re
 
 
 @pytest.mark.parametrize(('params', 'kwargs', 'expected_value', 'expected_currency'), [
@@ -115,3 +116,18 @@ def test_parse_text(loc, text, expected_sum):
 def test_parse_text_fail(text):
     with pytest.raises(Exception):
         finoex.Sum.parse_text(text)
+
+
+@pytest.mark.parametrize(('haystack', 'expected_needles'), [
+    ["Preis: 0,50 €", [{'currency': '€', 'value': '0,50'}]],
+    ["Preis: 0,50 US$", [{'currency': 'US$', 'value': '0,50'}]],
+    ["Preis: 1234 ¥", [{'currency': '¥', 'value': '1234'}]],
+    ["price: 1.23 €", [{'currency': '€', 'value': '1.23'}]],
+    ["price: 1.23 US$", [{'currency': 'US$', 'value': '1.23'}]],
+])
+def test_sum_regex_value_first(haystack, expected_needles):
+    matches = re.finditer(
+        finoex.Sum.sum_regex_value_first,
+        haystack,
+    )
+    assert expected_needles == [m.groupdict() for m in matches]