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