Bladeren bron

fix regex when removing spaces in brackets when the sentence is json (#399)

Nicolas Marcq 7 jaren geleden
bovenliggende
commit
e563393c96
2 gewijzigde bestanden met toevoegingen van 15 en 2 verwijderingen
  1. 14 1
      Tests/test_utils.py
  2. 1 1
      kalliope/core/Utils/Utils.py

+ 14 - 1
Tests/test_utils.py

@@ -226,6 +226,15 @@ class TestUtils(unittest.TestCase):
                          expected_result,
                          "Fail to remove spaces in two brackets")
 
+        # test with json
+        sentence = "{\"params\": {\"apikey\": \"ISNOTMYPASSWORD\", " \
+                   "\"query\": \"met le chauffage a {{ valeur }} degres\"}}"
+        expected_result = "{\"params\": {\"apikey\": \"ISNOTMYPASSWORD\", " \
+                          "\"query\": \"met le chauffage a {{valeur}} degres\"}}"
+        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
+                         expected_result,
+                         "Fail to remove spaces in two brackets")
+
     def test_encode_text_utf8(self):
         """
         Test encoding the text in utf8
@@ -236,4 +245,8 @@ class TestUtils(unittest.TestCase):
         expected_sentence = "kâllìöpé"
 
         self.assertEqual(Utils.encode_text_utf8(text=sentence),
-                         expected_sentence)
+                         expected_sentence)
+
+
+if __name__ == '__main__':
+    unittest.main()

+ 1 - 1
kalliope/core/Utils/Utils.py

@@ -273,7 +273,7 @@ class Utils(object):
         :return: the sentence without any spaces in brackets
         """
 
-        pattern = '\s+(?=[^\{\{\}\}]*\}\})'
+        pattern = '(?<=\{\{)\s+|\s+(?=\}\})'
         # Remove white spaces (if any) between the variable and the double brace then split
         if not isinstance(sentence, six.text_type):
             sentence = str(sentence)