def test_formatter_placeholder_nonpermitted_chars(): fmt = au.Formatter({}) # Can't assess keys with !, which will be interpreted as a conversion flag. eq_(fmt.format("{key!r}", {"key!r": "value0"}, key="x"), "'x'") assert_raises(KeyError, fmt.format, "{key!r}", {"key!r": "value0"}) # Same for ":". eq_(fmt.format("{key:<5}", {"key:<5": "value0"}, key="x"), "x ") assert_raises(KeyError, fmt.format, "{key:<5}", {"key:<5": "value0"})
def test_formatter(): idx_to_name = {i: "col{}".format(i) for i in range(4)} values = {"col{}".format(i): "value{}".format(i) for i in range(4)} fmt = au.Formatter(idx_to_name) eq_(fmt.format("{0}", values), "value0") eq_(fmt.format("{0}", values), fmt.format("{col0}", values)) # Integer placeholders outside of `idx_to_name` don't work. assert_raises(KeyError, fmt.format, "{4}", values, 1, 2, 3, 4) # If the named placeholder is not in `values`, falls back to normal # formatting. eq_(fmt.format("{notinvals}", values, notinvals="ok"), "ok")
def test_formatter_no_mapping_arg(): fmt = au.Formatter({}) with assert_raises(ValueError) as cme: fmt.format("{0}", "not a mapping") # we provide that detail/element in a message assert_in("not a mapping", str(cme.value))
def test_formatter_no_idx_map(): fmt = au.Formatter({}) assert_raises(KeyError, fmt.format, "{0}", {"col0": "value0"})
def test_formatter_lower_case(): fmt = au.Formatter({0: "key"}) eq_(fmt.format("{key!l}", {"key": "UP"}), "up") eq_(fmt.format("{0!l}", {"key": "UP"}), "up") eq_(fmt.format("{other!s}", {}, other=[1, 2]), "[1, 2]")
def test_formatter_missing_arg(): fmt = au.Formatter({}, "NA") eq_(fmt.format("{here},{nothere}", {"here": "ok", "nothere": ""}), "ok,NA")
def test_formatter_placeholder_with_spaces(): fmt = au.Formatter({}) eq_(fmt.format("{with spaces}", {"with spaces": "value0"}), "value0")
def test_formatter_no_mapping_arg(): fmt = au.Formatter({}) assert_raises(ValueError, fmt.format, "{0}", "not a mapping")