Add tests for keep=True

This commit is contained in:
Florian Bruhin 2014-11-05 07:58:28 +01:00
parent e68c28ebfc
commit 714ba530d3

View File

@ -27,77 +27,79 @@ from qutebrowser.utils import split
# Most tests copied from Python's shlex. # Most tests copied from Python's shlex.
# The original test data set was from shellwords, by Hartmut Goebel. # The original test data set was from shellwords, by Hartmut Goebel.
# Format: input/split|output|without|keep/split|output|with|keep/
test_data = r""" test_data = r"""
one two/one|two/ one two/one|two/one |two/
one "two three" four/one|two three|four/ one "two three" four/one|two three|four/one |"two three" |four/
one 'two three' four/one|two three|four/ one 'two three' four/one|two three|four/one |'two three' |four/
one "two\" three" four/one|two" three|four/ one "two\" three" four/one|two" three|four/one |"two\" three" |four/
one 'two'\'' three' four/one|two' three|four/ one 'two'\'' three' four/one|two' three|four/one |'two'\'' three' |four/
one "two three/one|two three/ one "two three/one|two three/one |"two three/
one 'two three/one|two three/ one 'two three/one|two three/one |'two three/
one\/one\/ one\/one\/one\/
one "two\/one|two\/ one "two\/one|two\/one |"two\/
foo bar/foo|bar/ foo bar/foo|bar/foo |bar/
foo bar/foo|bar/ foo bar/foo|bar/ foo |bar/
foo bar /foo|bar/ foo bar /foo|bar/ foo |bar /
foo bar bla fasel/foo|bar|bla|fasel/ foo bar bla fasel/foo|bar|bla|fasel/foo |bar |bla |fasel/
x y z xxxx/x|y|z|xxxx/ x y z xxxx/x|y|z|xxxx/x |y |z |xxxx/
\x bar/x|bar/ \x bar/x|bar/\x |bar/
\ x bar/ x|bar/ \ x bar/ x|bar/\ x |bar/
\ bar/ bar/ \ bar/ bar/\ bar/
foo \x bar/foo|x|bar/ foo \x bar/foo|x|bar/foo |\x |bar/
foo \ x bar/foo| x|bar/ foo \ x bar/foo| x|bar/foo |\ x |bar/
foo \ bar/foo| bar/ foo \ bar/foo| bar/foo |\ bar/
foo "bar" bla/foo|bar|bla/ foo "bar" bla/foo|bar|bla/foo |"bar" |bla/
"foo" "bar" "bla"/foo|bar|bla/ "foo" "bar" "bla"/foo|bar|bla/"foo" |"bar" |"bla"/
"foo" bar "bla"/foo|bar|bla/ "foo" bar "bla"/foo|bar|bla/"foo" |bar |"bla"/
"foo" bar bla/foo|bar|bla/ "foo" bar bla/foo|bar|bla/"foo" |bar |bla/
foo 'bar' bla/foo|bar|bla/ foo 'bar' bla/foo|bar|bla/foo |'bar' |bla/
'foo' 'bar' 'bla'/foo|bar|bla/ 'foo' 'bar' 'bla'/foo|bar|bla/'foo' |'bar' |'bla'/
'foo' bar 'bla'/foo|bar|bla/ 'foo' bar 'bla'/foo|bar|bla/'foo' |bar |'bla'/
'foo' bar bla/foo|bar|bla/ 'foo' bar bla/foo|bar|bla/'foo' |bar |bla/
blurb foo"bar"bar"fasel" baz/blurb|foobarbarfasel|baz/ blurb foo"bar"bar"fasel" baz/blurb|foobarbarfasel|baz/blurb |foo"bar"bar"fasel" |baz/
blurb foo'bar'bar'fasel' baz/blurb|foobarbarfasel|baz/ blurb foo'bar'bar'fasel' baz/blurb|foobarbarfasel|baz/blurb |foo'bar'bar'fasel' |baz/
""// ""//""/
''// ''//''/
foo "" bar/foo||bar/ foo "" bar/foo||bar/foo |"" |bar/
foo '' bar/foo||bar/ foo '' bar/foo||bar/foo |'' |bar/
foo "" "" "" bar/foo||||bar/ foo "" "" "" bar/foo||||bar/foo |"" |"" |"" |bar/
foo '' '' '' bar/foo||||bar/ foo '' '' '' bar/foo||||bar/foo |'' |'' |'' |bar/
\"/"/ \"/"/\"/
"\""/"/ "\""/"/"\""/
"foo\ bar"/foo\ bar/ "foo\ bar"/foo\ bar/"foo\ bar"/
"foo\\ bar"/foo\ bar/ "foo\\ bar"/foo\ bar/"foo\\ bar"/
"foo\\ bar\""/foo\ bar"/ "foo\\ bar\""/foo\ bar"/"foo\\ bar\""/
"foo\\" bar\"/foo\|bar"/ "foo\\" bar\"/foo\|bar"/"foo\\" |bar\"/
"foo\\ bar\" dfadf"/foo\ bar" dfadf/ "foo\\ bar\" dfadf"/foo\ bar" dfadf/"foo\\ bar\" dfadf"/
"foo\\\ bar\" dfadf"/foo\\ bar" dfadf/ "foo\\\ bar\" dfadf"/foo\\ bar" dfadf/"foo\\\ bar\" dfadf"/
"foo\\\x bar\" dfadf"/foo\\x bar" dfadf/ "foo\\\x bar\" dfadf"/foo\\x bar" dfadf/"foo\\\x bar\" dfadf"/
"foo\x bar\" dfadf"/foo\x bar" dfadf/ "foo\x bar\" dfadf"/foo\x bar" dfadf/"foo\x bar\" dfadf"/
\'/'/ \'/'/\'/
'foo\ bar'/foo\ bar/ 'foo\ bar'/foo\ bar/'foo\ bar'/
'foo\\ bar'/foo\\ bar/ 'foo\\ bar'/foo\\ bar/'foo\\ bar'/
"foo\\\x bar\" df'a\ 'df"/foo\\x bar" df'a\ 'df/ "foo\\\x bar\" df'a\ 'df"/foo\\x bar" df'a\ 'df/"foo\\\x bar\" df'a\ 'df"/
\"foo/"foo/ \"foo/"foo/\"foo/
\"foo\x/"foox/ \"foo\x/"foox/\"foo\x/
"foo\x"/foo\x/ "foo\x"/foo\x/"foo\x"/
"foo\ "/foo\ / "foo\ "/foo\ /"foo\ "/
foo\ xx/foo xx/ foo\ xx/foo xx/foo\ xx/
foo\ x\x/foo xx/ foo\ x\x/foo xx/foo\ x\x/
foo\ x\x\"/foo xx"/ foo\ x\x\"/foo xx"/foo\ x\x\"/
"foo\ x\x"/foo\ x\x/ "foo\ x\x"/foo\ x\x/"foo\ x\x"/
"foo\ x\x\\"/foo\ x\x\/ "foo\ x\x\\"/foo\ x\x\/"foo\ x\x\\"/
"foo\ x\x\\""foobar"/foo\ x\x\foobar/ "foo\ x\x\\""foobar"/foo\ x\x\foobar/"foo\ x\x\\""foobar"/
"foo\ x\x\\"\'"foobar"/foo\ x\x\'foobar/ "foo\ x\x\\"\'"foobar"/foo\ x\x\'foobar/"foo\ x\x\\"\'"foobar"/
"foo\ x\x\\"\'"fo'obar"/foo\ x\x\'fo'obar/ "foo\ x\x\\"\'"fo'obar"/foo\ x\x\'fo'obar/"foo\ x\x\\"\'"fo'obar"/
"foo\ x\x\\"\'"fo'obar" 'don'\''t'/foo\ x\x\'fo'obar|don't/ "foo\ x\x\\"\'"fo'obar" 'don'\''t'/foo\ x\x\'fo'obar|don't/"foo\ x\x\\"\'"fo'obar" 'don'\''t'/
"foo\ x\x\\"\'"fo'obar" 'don'\''t' \\/foo\ x\x\'fo'obar|don't|\/ "foo\ x\x\\"\'"fo'obar" 'don'\''t' \\/foo\ x\x\'fo'obar|don't|\/"foo\ x\x\\"\'"fo'obar" |'don'\''t' |\\/
'foo\ bar'/foo\ bar/ 'foo\ bar'/foo\ bar/'foo\ bar'/
'foo\\ bar'/foo\\ bar/ 'foo\\ bar'/foo\\ bar/'foo\\ bar'/
foo\ bar/foo bar/ foo\ bar/foo bar/foo\ bar/
foo#bar\nbaz/foo#bar|baz/ foo#bar\nbaz/foo#bar|baz/foo#bar\n|baz/
:-) ;-)/:-)|;-)/ :-) ;-)/:-)|;-)/:-) |;-)/
áéíóú/áéíóú/ áéíóú/áéíóú/áéíóú/
""" """
class SplitTests(unittest.TestCase): class SplitTests(unittest.TestCase):
@ -107,17 +109,26 @@ class SplitTests(unittest.TestCase):
def test_split(self): def test_split(self):
"""Test splitting.""" """Test splitting."""
for case in test_data.strip().splitlines(): for case in test_data.strip().splitlines():
cmd, out = case.split('/')[:-1] cmd, *out = case.split('/')[:-1]
cmd = cmd.replace(r'\n', '\n') cmd = cmd.replace(r'\n', '\n')
with self.subTest(cmd=cmd): with self.subTest(cmd=cmd):
items = split.split(cmd) items = split.split(cmd)
self.assertEqual(items, out.split('|')) self.assertEqual(items, out[0].split('|'))
def test_split_keep(self): def test_split_keep_original(self):
"""Test splitting with keep=True.""" """Test if splitting with keep=True yields the original string."""
for case in test_data.strip().splitlines(): for case in test_data.strip().splitlines():
cmd, _out = case.split('/')[:-1] cmd, *_out = case.split('/')[:-1]
cmd = cmd.replace(r'\n', '\n') cmd = cmd.replace(r'\n', '\n')
with self.subTest(cmd=cmd): with self.subTest(cmd=cmd):
items = split.split(cmd, keep=True) items = split.split(cmd, keep=True)
self.assertEqual(''.join(items), cmd) self.assertEqual(''.join(items), cmd)
def test_split_keep(self):
"""Test splitting with keep=True."""
for case in test_data.strip().splitlines():
cmd, *out = case.split('/')[:-1]
cmd = cmd.replace(r'\n', '\n')
with self.subTest(cmd=cmd):
items = split.split(cmd, keep=True)
self.assertEqual(items, out[1].split('|'))