@@ -96,21 +96,55 @@ def test_checks_fuzzy(self):
9696 # the file has 11 errors (with the fuzzy string)
9797 self .assertEqual (len (result [0 ][1 ]), 11 )
9898
99- def test_replace_formatters (self ):
100- """Test removal of formatters in a string."""
101- self .assertEqual (replace_formatters ('%' , '' , 'c' ), '' )
102- self .assertEqual (replace_formatters ('\\ ' , '' , 'c' ), '\\ ' )
103- self .assertEqual (replace_formatters ('%s' , ' ' , 'c' ), ' ' )
104- self .assertEqual (replace_formatters ('%.02f' , ' ' , 'c' ), ' ' )
105- self .assertEqual (replace_formatters ('%!%s%!' , '' , 'c' ), '%!%!' )
106- self .assertEqual (replace_formatters ('%.02!' , ' ' , 'c' ), '%.02!' )
99+ def test_replace_formatters_c (self ):
100+ """Test removal of formatters in a C string."""
101+ self .assertEqual (replace_formatters ('%s' , 'c' ), '' )
102+ self .assertEqual (replace_formatters ('%%' , 'c' ), '%' )
103+ self .assertEqual (replace_formatters ('%.02f' , 'c' ), '' )
104+ self .assertEqual (replace_formatters ('%!%s%!' , 'c' ), '%!%!' )
105+ self .assertEqual (replace_formatters ('%.02!' , 'c' ), '%.02!' )
107106 self .assertEqual (
108- replace_formatters ('%.3fThis is a %stest' , ' ' , ' c' ),
109- ' This is a test' )
107+ replace_formatters ('%.3fThis is a %stest' , 'c' ),
108+ 'This is a test' )
110109 self .assertEqual (
111- replace_formatters ('%.3fTest%s%d%%%.03f%luhere% s' , '' , ' c' ),
110+ replace_formatters ('%.3fTest%s%d%%%.03f%luhere% s' , 'c' ),
112111 'Test%here' )
113112
113+ def test_replace_formatters_python (self ):
114+ """Test removal of formatters in a python string."""
115+ # str.__mod__()
116+ self .assertEqual (replace_formatters ('%s' , 'python' ), '' )
117+ self .assertEqual (replace_formatters ('%b' , 'python' ), '' )
118+ self .assertEqual (replace_formatters ('%%' , 'python' ), '%' )
119+ self .assertEqual (replace_formatters ('%.02f' , 'python' ), '' )
120+ self .assertEqual (replace_formatters ('%(sth)s' , 'python' ), 'sth' )
121+ self .assertEqual (replace_formatters ('%(sth)02f' , 'python' ), 'sth' )
122+ # str.format()
123+ conditions = [
124+ (
125+ 'First, thou shalt count to {0}' , 'First, thou shalt count to ' ,
126+ 'References first positional argument' ),
127+ (
128+ 'Bring me a {}' , 'Bring me a ' ,
129+ 'Implicitly references the first positional argument' ),
130+ ('From {} to {}' , 'From to ' , 'Same as "From {0} to {1}"' ),
131+ (
132+ 'My quest is {name}' , 'My quest is ' ,
133+ 'References keyword argument \' name\' ' ),
134+ (
135+ 'Weight in tons {0.weight}' , 'Weight in tons ' ,
136+ '\' weight\' attribute of first positional arg' ),
137+ (
138+ 'Units destroyed: {players[0]}' , 'Units destroyed: ' ,
139+ 'First element of keyword argument \' players\' .' ),
140+ ]
141+ for condition in conditions :
142+ self .assertEqual (
143+ replace_formatters (condition [0 ], 'python' ),
144+ condition [1 ],
145+ condition [2 ],
146+ )
147+
114148 def test_spelling_id (self ):
115149 """Test spelling on source messages (English) of gettext files."""
116150 po_check = PoCheck ()
0 commit comments