1- import io
2- import os
31import unittest
4- from unittest import mock
2+ from . utils import patch_files
53
64from fluent .runtime import FluentLocalization , FluentResourceLoader
75
8- ISFILE = os .path .isfile
9-
106
117class TestLocalization (unittest .TestCase ):
128 def test_init (self ):
@@ -15,21 +11,18 @@ def test_init(self):
1511 )
1612 self .assertTrue (callable (l10n .format_value ))
1713
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 ):
3022 l10n = FluentLocalization (
3123 ["de" , "fr" , "en" ], ["one.ftl" , "two.ftl" ], FluentResourceLoader ("{locale}" )
3224 )
25+ # Curious
3326 bundles_gen = l10n ._bundles ()
3427 bundle_de = next (bundles_gen )
3528 self .assertEqual (bundle_de .locales [0 ], "de" )
@@ -49,38 +42,30 @@ def test_bundles(self, codecs_open, isfile):
4942 self .assertEqual (l10n .format_value ("five" ), "exists" )
5043
5144
52- @mock .patch ("os.path.isfile" )
53- @mock .patch ("codecs.open" )
5445class 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 ):
6251 loader = FluentResourceLoader ("{locale}" )
6352 resources_list = list (loader .resources ("en" , ["one.ftl" , "two.ftl" ]))
6453 self .assertEqual (len (resources_list ), 1 )
6554 resources = resources_list [0 ]
6655 self .assertEqual (len (resources ), 2 )
6756
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 ):
7461 loader = FluentResourceLoader ("{locale}" )
7562 resources_list = list (loader .resources ("en" , ["one.ftl" , "two.ftl" ]))
7663 self .assertEqual (len (resources_list ), 1 )
7764 resources = resources_list [0 ]
7865 self .assertEqual (len (resources ), 1 )
7966
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 ):
8469 loader = FluentResourceLoader ("{locale}" )
8570 resources_list = list (loader .resources ("en" , ["one.ftl" , "two.ftl" ]))
8671 self .assertEqual (len (resources_list ), 0 )
0 commit comments