1
- import io
2
- import os
3
1
import unittest
4
- from unittest import mock
2
+ from . utils import patch_files
5
3
6
4
from fluent .runtime import FluentLocalization , FluentResourceLoader
7
5
8
- ISFILE = os .path .isfile
9
-
10
6
11
7
class TestLocalization (unittest .TestCase ):
12
8
def test_init (self ):
@@ -15,21 +11,18 @@ def test_init(self):
15
11
)
16
12
self .assertTrue (callable (l10n .format_value ))
17
13
18
- @mock .patch ("os.path.isfile" )
19
- @mock .patch ("codecs.open" )
20
- def test_bundles (self , codecs_open , isfile ):
21
- data = {
22
- "de/one.ftl" : "one = in German" ,
23
- "de/two.ftl" : "two = in German" ,
24
- "fr/two.ftl" : "three = in French" ,
25
- "en/one.ftl" : "four = exists" ,
26
- "en/two.ftl" : "five = exists" ,
27
- }
28
- isfile .side_effect = lambda p : p in data or ISFILE (p )
29
- codecs_open .side_effect = lambda p , _ , __ : io .StringIO (data [p ])
14
+ @patch_files ({
15
+ "de/one.ftl" : "one = in German" ,
16
+ "de/two.ftl" : "two = in German" ,
17
+ "fr/two.ftl" : "three = in French" ,
18
+ "en/one.ftl" : "four = exists" ,
19
+ "en/two.ftl" : "five = exists" ,
20
+ })
21
+ def test_bundles (self ):
30
22
l10n = FluentLocalization (
31
23
["de" , "fr" , "en" ], ["one.ftl" , "two.ftl" ], FluentResourceLoader ("{locale}" )
32
24
)
25
+ # Curious
33
26
bundles_gen = l10n ._bundles ()
34
27
bundle_de = next (bundles_gen )
35
28
self .assertEqual (bundle_de .locales [0 ], "de" )
@@ -49,38 +42,30 @@ def test_bundles(self, codecs_open, isfile):
49
42
self .assertEqual (l10n .format_value ("five" ), "exists" )
50
43
51
44
52
- @mock .patch ("os.path.isfile" )
53
- @mock .patch ("codecs.open" )
54
45
class TestResourceLoader (unittest .TestCase ):
55
- def test_all_exist (self , codecs_open , isfile ):
56
- data = {
57
- "en/one.ftl" : "one = exists" ,
58
- "en/two.ftl" : "two = exists" ,
59
- }
60
- isfile .side_effect = lambda p : p in data
61
- codecs_open .side_effect = lambda p , _ , __ : io .StringIO (data [p ])
46
+ @patch_files ({
47
+ "en/one.ftl" : "one = exists" ,
48
+ "en/two.ftl" : "two = exists" ,
49
+ })
50
+ def test_all_exist (self ):
62
51
loader = FluentResourceLoader ("{locale}" )
63
52
resources_list = list (loader .resources ("en" , ["one.ftl" , "two.ftl" ]))
64
53
self .assertEqual (len (resources_list ), 1 )
65
54
resources = resources_list [0 ]
66
55
self .assertEqual (len (resources ), 2 )
67
56
68
- def test_one_exists (self , codecs_open , isfile ):
69
- data = {
70
- "en/two.ftl" : "two = exists" ,
71
- }
72
- isfile .side_effect = lambda p : p in data
73
- codecs_open .side_effect = lambda p , _ , __ : io .StringIO (data [p ])
57
+ @patch_files ({
58
+ "en/two.ftl" : "two = exists" ,
59
+ })
60
+ def test_one_exists (self ):
74
61
loader = FluentResourceLoader ("{locale}" )
75
62
resources_list = list (loader .resources ("en" , ["one.ftl" , "two.ftl" ]))
76
63
self .assertEqual (len (resources_list ), 1 )
77
64
resources = resources_list [0 ]
78
65
self .assertEqual (len (resources ), 1 )
79
66
80
- def test_none_exist (self , codecs_open , isfile ):
81
- data = {}
82
- isfile .side_effect = lambda p : p in data
83
- codecs_open .side_effect = lambda p , _ , __ : io .StringIO (data [p ])
67
+ @patch_files ({})
68
+ def test_none_exist (self ):
84
69
loader = FluentResourceLoader ("{locale}" )
85
70
resources_list = list (loader .resources ("en" , ["one.ftl" , "two.ftl" ]))
86
71
self .assertEqual (len (resources_list ), 0 )
0 commit comments