def test_partial_string_cont(): t, cont = PartialString.cont() t = cont(t, "x: {0}\ny: {1}\n{-1}", object(), object()) t = cont(t, "z: {0}\n{-1}", object()) t = cont(t, "") key = t.holes assert (t.format({ key[0]: "line1", key[1]: "line2", key[2]: "line3" }) == 'x: line1\ny: line2\nz: line3\n') assert t.size(hole_size=5) == 27
def test_partial_string_escape(): tmpl = PartialString.numl( "find {0} -name {1} " + PartialString.escape("-exec ls -la {} \;"), 2) assert tmpl == PartialString('find {0} -name {1} -exec ls -la {{}} \\;', *tmpl.holes) assert tmpl.size() == 33 assert tmpl.size(4) == 39 assert tmpl == PartialString.numl("find {0} -name {1} -exec ls -la {2} \;", 3).pformat({2: "{}"}) assert (tmpl.pformatl("my{}path", "my{}file") == PartialString( 'find my{{}}path -name my{{}}file -exec ls -la {{}} \\;')) assert (tmpl.formatl( "my{}path", "my{}file") == 'find my{}path -name my{}file -exec ls -la {} \\;') esc = PartialString("{{}} {0}", None) assert esc.pformat({None: PartialString.of(None)}) == esc assert esc.format({None: "0"}) == "{} 0" with pytest.raises(ValueError): PartialString("{}")
def test_partial_string_numl(): tmpl = PartialString.numl("{0} {1} {2}", 2, object()) assert tmpl.holes[:2] == (0, 1) assert tmpl.pformatl("(1)", "(2)", "(o)") == PartialString('(1) (2) (o)')
def test_partial_string(): a, b = object(), object() tmpl = PartialString("{0} {1}", a, b) assert tmpl.holes == (a, b) assert tmpl.format({a: "Hello,", b: "World!"}) == 'Hello, World!' assert tmpl.pformat({a: "Hello,"}) == PartialString('Hello, {0}', b) assert tmpl.pformat({b: "World!"}) == PartialString('{0} World!', a) assert tmpl.base_len, tmpl.num_holes == (1, 2) assert tmpl.size(hole_size=33) == 67 assert tmpl.pformat({a: PartialString('{0}', b)}) == PartialString('{0} {0}', b) assert tmpl.pformat({a: tmpl}) == PartialString('{0} {1} {1}', a, b) assert tmpl.pformat({b: tmpl}) == PartialString('{0} {0} {1}', a, b) PartialString("{1}", a, b) with pytest.raises(IndexError): PartialString("{0} {1} {2}", a, b)