class TestPlaceholderCheck(TypeTestCase): p = placeholder(1) def test_simple(self): check = placeholder_check(string, 1) self.assertEqual(check('field', 'foo'), 'foo') self.assertEqual(check('field', self.p), self.p) self.assertEqual(check('field', 'foo' + self.p), 'foo' + self.p) with self.assertFieldError(('field', ), 'expected a string'): check('field', 1) def test_dict(self): check = placeholder_check(dict_shape({'foo': string}, 'a foo dict'), 1) self.assertEqual(check('field', {'foo': 'bar'}), {'foo': 'bar'}) self.assertEqual(check('field', {'foo': self.p}), {'foo': self.p}) self.assertEqual(check('field', {'foo': 'bar' + self.p}), {'foo': 'bar' + self.p}) with self.assertFieldError(('field', 'bad'), 'unexpected key'): check('field', {'bad': self.p}) def test_invalid_type(self): with self.assertFieldError(('field', ), 'expected a boolean'): placeholder_check(boolean, 1)('field', self.p)
def test_invalid(self): with self.assertFieldError(('field', )): string('field', 1) with self.assertFieldError(('field', )): string('field', None) with self.assertFieldError(('field', )): string('field', placeholder('foo'))
def test_placeholder_list(self): srcdir = Path('srcdir', '') srcdir_ph = placeholder(srcdir) self.assertShellArgs([srcdir_ph], [srcdir]) self.assertShellArgs(['foo', srcdir_ph, 'bar'], ['foo', srcdir, 'bar']) self.assertShellArgs([srcdir_ph + '/foo'], [(srcdir, '/foo')]) self.assertShellArgs([srcdir_ph + '/ foo'], [(srcdir, '/ foo')])
def test_ensure_path_placeholder(self): srcdir = placeholder(Path(Path.Base.srcdir, '')) self.assertEqual(Path.ensure_path(srcdir, Path.Base.srcdir), Path(Path.Base.srcdir, '')) self.assertEqual(Path.ensure_path(srcdir + '/foo', Path.Base.srcdir), Path(Path.Base.srcdir, 'foo')) subsrc = placeholder(Path(Path.Base.srcdir, 'subdir')) self.assertEqual(Path.ensure_path(subsrc, Path.Base.srcdir), Path(Path.Base.srcdir, 'subdir')) self.assertEqual(Path.ensure_path(subsrc + '/foo', Path.Base.srcdir), Path(Path.Base.srcdir, 'subdir/foo')) self.assertEqual(Path.ensure_path(subsrc + 'foo', Path.Base.srcdir), Path(Path.Base.srcdir, 'subdirfoo')) with self.assertRaises(ValueError): Path.ensure_path(srcdir + 'foo', Path.Base.srcdir) with self.assertRaises(ValueError): Path.ensure_path(srcdir + '/foo' + srcdir, Path.Base.srcdir)
def test_placeholder_string(self): srcdir = Path('srcdir', '') srcdir_ph = placeholder(srcdir) self.assertShellArgs(srcdir_ph, [srcdir]) self.assertShellArgs(srcdir_ph + ' foo', [srcdir, 'foo']) self.assertShellArgs('foo ' + srcdir_ph + ' bar', ['foo', srcdir, 'bar']) self.assertShellArgs(srcdir_ph + '/foo', [(srcdir, '/foo')]) self.assertShellArgs('"' + srcdir_ph + '/ foo"', [(srcdir, '/ foo')])
class TestPlaceholderFill(TypeTestCase): p = placeholder(1) def test_foo(self): check = placeholder_fill(string, 1, 'one') self.assertEqual(check('field', 'foo'), 'foo') self.assertEqual(check('field', self.p), 'one') self.assertEqual(check('field', 'foo' + self.p), 'fooone') def test_dict(self): check = placeholder_fill(dict_shape({'foo': string}, 'a foo dict'), 1, 'one') self.assertEqual(check('field', {'foo': 'bar'}), {'foo': 'bar'}) self.assertEqual(check('field', {'foo': self.p}), {'foo': 'one'}) self.assertEqual(check('field', {'foo': 'bar' + self.p}), {'foo': 'barone'}) with self.assertFieldError(('field', 'bad'), 'unexpected key'): check('field', {'bad': self.p}) def test_invalid_type(self): with self.assertFieldError(('field', ), 'expected a boolean'): placeholder_fill(boolean, 1, 'one')('field', self.p)
import os from unittest import TestCase from mopack.path import Path from mopack.placeholder import placeholder from mopack.shell import * srcdir = Path('srcdir', '') srcdir_ph = placeholder(srcdir) class TestSplitWindows(TestCase): def assertSplitEqual(self, value, expected, **kwargs): self.assertEqual(split_windows_str(value, **kwargs), expected) self.assertEqual(split_windows(value, **kwargs), ShellArguments(expected)) def test_single(self): self.assertSplitEqual('foo', ['foo']) self.assertSplitEqual(' foo', ['foo']) self.assertSplitEqual('foo ', ['foo']) self.assertSplitEqual(' foo ', ['foo']) def test_multiple(self): self.assertSplitEqual('foo bar baz', ['foo', 'bar', 'baz']) def test_backslash(self): self.assertSplitEqual(r'C:\path\to\file', [r'C:\path\to\file']) def test_quote(self): self.assertSplitEqual('foo "bar baz"', ['foo', 'bar baz'])
def test_valid(self): s = placeholder('foo') self.assertEqual(placeholder_string('field', 'foo'), 'foo') self.assertEqual(placeholder_string('field', s), s)