MCPcopy Index your code
hub / github.com/python/cpython / test_str_format

Method test_str_format

Lib/test/test_format.py:358–430  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

356 "format argument 1: %g requires a real number, not str")
357
358 def test_str_format(self):
359 testformat("%r", "\u0378", "'\\u0378'") # non printable
360 testformat("%a", "\u0378", "'\\u0378'") # non printable
361 testformat("%r", "\u0374", "'\u0374'") # printable
362 testformat("%a", "\u0374", "'\\u0374'") # printable
363 testformat('%(x)r', {'x': 1}, '1')
364
365 # Test exception for unknown format characters, etc.
366 if verbose:
367 print('Testing exceptions')
368 test_exc('abc %b', 1, ValueError,
369 "unsupported format %b at position 4")
370 test_exc("abc %\nd", 1, ValueError,
371 "stray % at position 4 or unexpected format character U+000A at position 5")
372 test_exc("abc %\x1fd", 1, ValueError,
373 "stray % at position 4 or unexpected format character U+001F at position 5")
374 test_exc("abc %\x7fd", 1, ValueError,
375 "stray % at position 4 or unexpected format character U+007F at position 5")
376 test_exc("abc %\x80d", 1, ValueError,
377 "stray % at position 4 or unexpected format character U+0080 at position 5")
378 test_exc('abc %äd', 1, ValueError,
379 "stray % at position 4 or unexpected format character 'ä' (U+00E4) at position 5")
380 test_exc('abc %€d', 1, ValueError,
381 "stray % at position 4 or unexpected format character '€' (U+20AC) at position 5")
382 test_exc('no format', '1', TypeError,
383 "not all arguments converted during string formatting (required 0, got 1)")
384 test_exc('%r', (1, 2), TypeError,
385 "not all arguments converted during string formatting (required 1, got 2)")
386 test_exc('%(x)r %r', {'x': 1}, ValueError,
387 "format requires a parenthesised mapping key at position 6")
388 test_exc('%(x)*r', {'x': 1}, ValueError,
389 "* cannot be used with a parenthesised mapping key at position 0")
390 test_exc('%(x).*r', {'x': 1}, ValueError,
391 "* cannot be used with a parenthesised mapping key at position 0")
392 test_exc('%(x)d', {'x': '1'}, TypeError,
393 "format argument 'x': %d requires a real number, not str")
394 test_exc('%(x)x', {'x': '1'}, TypeError,
395 "format argument 'x': %x requires an integer, not str")
396 test_exc('%(x)g', {'x': '1'}, TypeError,
397 "format argument 'x': %g requires a real number, not str")
398 test_exc('%c', -1, OverflowError,
399 "format argument: %c argument not in range(0x110000)")
400 test_exc('%c', (-1,), OverflowError,
401 "format argument 1: %c argument not in range(0x110000)")
402 test_exc('%(x)c', {'x': -1}, OverflowError,
403 "format argument 'x': %c argument not in range(0x110000)")
404 test_exc('%c', sys.maxunicode+1, OverflowError,
405 "format argument: %c argument not in range(0x110000)")
406 test_exc('%c', 2**128, OverflowError,
407 "format argument: %c argument not in range(0x110000)")
408 test_exc('%c', 3.14, TypeError,
409 "format argument: %c requires an integer or a unicode character, not float")
410 test_exc('%c', (3.14,), TypeError,
411 "format argument 1: %c requires an integer or a unicode character, not float")
412 test_exc('%(x)c', {'x': 3.14}, TypeError,
413 "format argument 'x': %c requires an integer or a unicode character, not float")
414 test_exc('%c', 'ab', TypeError,
415 "format argument: %c requires an integer or a unicode character, not a string of length 2")

Callers

nothing calls this directly

Calls 3

TestFailedClass · 0.90
testformatFunction · 0.85
test_excFunction · 0.85

Tested by

no test coverage detected