diff options
Diffstat (limited to 'attoconf/tests')
-rw-r--r-- | attoconf/tests/__init__.py | 0 | ||||
-rw-r--r-- | attoconf/tests/test_core.py | 139 | ||||
-rw-r--r-- | attoconf/tests/test_help.py | 195 | ||||
-rw-r--r-- | attoconf/tests/test_types.py | 62 | ||||
-rw-r--r-- | attoconf/tests/test_version.py | 95 |
5 files changed, 491 insertions, 0 deletions
diff --git a/attoconf/tests/__init__.py b/attoconf/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/attoconf/tests/__init__.py diff --git a/attoconf/tests/test_core.py b/attoconf/tests/test_core.py new file mode 100644 index 0000000..93aeaaf --- /dev/null +++ b/attoconf/tests/test_core.py @@ -0,0 +1,139 @@ +# Copyright 2013-2014 Ben Longbons <b.r.longbons@gmail.com> +# +# This file is part of attoconf. +# +# attoconf is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# attoconf is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with attoconf. If not, see <http://www.gnu.org/licenses/>. + + + +import unittest + +from attoconf.core import Project, Build +from attoconf.types import uint, shell_word, shell_partial_word, maybe + +import os +from io import StringIO +import sys + +class ReplacingStdout(object): + __slots__ = ('old', 'new') + def __init__(self, new): + self.old = None + self.new = new + def __enter__(self): + self.old = sys.stdout + sys.stdout = self.new + def __exit__(self, type, value, traceback): + sys.stdout = self.old + del self.old + +class TestProject(unittest.TestCase): + def test_help(self): + proj = Project('foo') + proj.add_help('General:', False) + proj.add_alias('--help', ['--help=default'], + help='display standard help, then exit', hidden=False) + proj.add_option('--help', init='none', + type=proj.do_help, check=None, + help='display some subset of help', hidden=False, + help_var='TYPE') + proj.help.add_option('--help=hidden', + help='display help you should never ever ever care about', + hidden=True) + proj.add_option('--foo', init='asdf', + type=shell_word, check=None, + help='set frob target', hidden=False) + proj.add_option('--bar', init='', + type=maybe(shell_word), check=None, + help='set frob source', hidden=False, + help_def='FOO') + + build = Build(proj, 'bar') + + out = StringIO() + with ReplacingStdout(out): + with self.assertRaises(SystemExit): + build.apply_arg('--help') + self.assertEqual(out.getvalue(), ''' +General: + --help display standard help, then exit + --help=TYPE display some subset of help [none] + --foo=FOO set frob target [asdf] + --bar=BAR set frob source [FOO] + +'''[1:]) + + out = StringIO() + with ReplacingStdout(out): + with self.assertRaises(SystemExit): + build.apply_arg('--help=hidden') + self.assertEqual(out.getvalue(), ''' +General: + --help display standard help, then exit + --help=TYPE display some subset of help [none] + --help=hidden display help you should never ever ever care about + --foo=FOO set frob target [asdf] + --bar=BAR set frob source [FOO] + +'''[1:]) + + def test_path(self): + proj = Project('foo/') + build = Build(proj, 'bar/') + self.assertEqual(build.project.srcdir, 'foo') + self.assertEqual(build.builddir, 'bar') + self.assertEqual(build.relative_source(), '../foo') + + def test_configure(self): + def check_foo(bld, FOO): + self.assertEqual(FOO, 'B') + def check_bar(bld, BAR): + self.assertEqual(BAR, 1) + def check_qux(bld, QUX): + self.assertEqual(QUX, '') + def check_var(bld, VAR): + self.assertEqual(VAR, 'value') + + proj = Project('.') + proj.add_alias('--alias', ['--foo=A', '--bar=1', '--foo=B'], + help=None, hidden=False) + proj.add_option('--foo', init='X', + type=shell_word, check=check_foo, + help='help for string foo', hidden=False) + proj.add_option('--bar', init=0, + type=uint, check=check_bar, + help='help for int bar', hidden=False) + proj.add_option('--qux', init='', + type=maybe(uint), check=check_qux, + help='help for int qux', hidden=False, + help_def='auto') + proj.add_option('VAR', init='', + type=shell_partial_word, check=check_var, + help='help for string VAR', hidden=False) + + build = Build(proj, '.') + build.configure(['--alias'], + { + 'VAR': 'value', + 'QUX': 'a', + '--qux': 'b', + }) + os.remove('config.status') + self.assertEqual(build.vars, + { + 'FOO': 'B', + 'BAR': 1, + 'QUX': '', + 'VAR': 'value', + }) diff --git a/attoconf/tests/test_help.py b/attoconf/tests/test_help.py new file mode 100644 index 0000000..60a9e49 --- /dev/null +++ b/attoconf/tests/test_help.py @@ -0,0 +1,195 @@ +# Copyright 2013 Ben Longbons <b.r.longbons@gmail.com> +# +# This file is part of attoconf. +# +# attoconf is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# attoconf is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with attoconf. If not, see <http://www.gnu.org/licenses/>. + + + +import unittest + +from attoconf.help import Help, HelpSection, put_line_in_width + +from io import StringIO + +class TestHelpSection(unittest.TestCase): + def test_basic(self): + sec = HelpSection() + sec.add_text('foo', False) + sec.add_option('--foo', 'FOO', True) + sec.add_text('bar', True) + sec.add_option('--bar', 'BAR', False) + self.assertEqual(sec.headers, [(False, 'foo'), (True, 'bar')]) + self.assertEqual(sec.options, [(True, '--foo', 'FOO'), (False, '--bar', 'BAR')]) + + out = StringIO() + sec.print(out, False, float('inf')) + self.assertEqual(out.getvalue(), '''foo + --bar BAR +''') + + out = StringIO() + sec.print(out, True, float('inf')) + self.assertEqual(out.getvalue(), '''foo +bar + --foo FOO + --bar BAR +''') + + def test_width(self): + out = StringIO() + put_line_in_width(out, 'foo bar baz', float('inf'), 0) + self.assertEqual(out.getvalue(), 'foo bar baz\n') + + out = StringIO() + put_line_in_width(out, 'foo bar baz', 10, 0) + self.assertEqual(out.getvalue(), 'foo bar\nbaz\n') + + out = StringIO() + put_line_in_width(out, 'foo bar baz', 10, 2) + self.assertEqual(out.getvalue(), 'foo bar\n baz\n') + + out = StringIO() + put_line_in_width(out, ' foo bar baz', 10, 0) + self.assertEqual(out.getvalue(), ' foo bar\nbaz\n') + + out = StringIO() + put_line_in_width(out, ' foo bar baz', 10, 0) + self.assertEqual(out.getvalue(), ' foo\nbar baz\n') + + out = StringIO() + put_line_in_width(out, ' foo bar baz', 10, 3) + self.assertEqual(out.getvalue(), ' foo\n bar baz\n') + + out = StringIO() + put_line_in_width(out, ' foo bar baz', 10, 4) + self.assertEqual(out.getvalue(), ' foo\n bar\n baz\n') + + out = StringIO() + put_line_in_width(out, 'really-long-string', float('inf'), 0) + self.assertEqual(out.getvalue(), 'really-long-string\n') + + out = StringIO() + put_line_in_width(out, ' really-long-string', float('inf'), 0) + self.assertEqual(out.getvalue(), ' really-long-string\n') + + out = StringIO() + put_line_in_width(out, 'really-long-string', 10, 0) + self.assertEqual(out.getvalue(), 'really-long-string\n') + + out = StringIO() + put_line_in_width(out, ' really-long-string', 10, 0) + self.assertEqual(out.getvalue(), ' really-long-string\n') + + out = StringIO() + put_line_in_width(out, 'short really-long-string', float('inf'), 0) + self.assertEqual(out.getvalue(), 'short really-long-string\n') + + out = StringIO() + put_line_in_width(out, ' short really-long-string', float('inf'), 0) + self.assertEqual(out.getvalue(), ' short really-long-string\n') + + out = StringIO() + put_line_in_width(out, 'short really-long-string', 10, 0) + self.assertEqual(out.getvalue(), 'short\nreally-long-string\n') + + out = StringIO() + put_line_in_width(out, ' short really-long-string', 10, 0) + self.assertEqual(out.getvalue(), ' short\nreally-long-string\n') + + out = StringIO() + put_line_in_width(out, 'short really-long-string', 10, 1) + self.assertEqual(out.getvalue(), 'short\n really-long-string\n') + + out = StringIO() + put_line_in_width(out, ' short really-long-string', 10, 1) + self.assertEqual(out.getvalue(), ' short\n really-long-string\n') + + def test_print(self): + sec = HelpSection() + sec.add_option('--abcdefghijklmnopqrstuvwxyz', + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', False) + + out = StringIO() + sec.print(out, False, 80) + self.assertEqual(out.getvalue(), ''' + --abcdefghijklmnopqrstuvwxyz + ABCDEFGHIJKLMNOPQRSTUVWXYZ +'''[1:]) + + sec.add_option('--foo', 'FOO', False) + sec.add_option('--frob', 'FROB', False) + + out = StringIO() + sec.print(out, False, 80) + self.assertEqual(out.getvalue(), ''' + --abcdefghijklmnopqrstuvwxyz + ABCDEFGHIJKLMNOPQRSTUVWXYZ + --foo FOO + --frob FROB +'''[1:]) + + out = StringIO() + sec.print(out, False, float('inf')) + self.assertEqual(out.getvalue(), ''' + --abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ + --foo FOO + --frob FROB +'''[1:]) + + +class TestHelp(unittest.TestCase): + def test_print(self): + help = Help() + help.add_option('--invisible', 'You can\'t see this', True) + help.add_text('General:', False) + help.add_option('--help', 'show this', False) + help.add_option('--version', 'show that', True) + help.add_text('Other stuff:', False) + help.add_text('with long header:', True) + help.add_option('--foo-lets-make-it-long-just-because', 'I\'m bored', False) + help.add_text('Visibility:', False) + help.add_option('--sneaky', 'This may be surprising. It also works as a demonstration of multi-line wrapping, just because.', True) + + out = StringIO() + help.print(out, True, 80) + self.assertEqual(out.getvalue(), ''' + --invisible You can't see this + +General: + --help show this + --version show that + +Other stuff: +with long header: + --foo-lets-make-it-long-just-because + I'm bored + +Visibility: + --sneaky This may be surprising. It also works as a demonstration of + multi-line wrapping, just because. + +'''[1:]) + + out = StringIO() + help.print(out, False, 80) + self.assertEqual(out.getvalue(), ''' +General: + --help show this + +Other stuff: + --foo-lets-make-it-long-just-because + I'm bored + +'''[1:]) diff --git a/attoconf/tests/test_types.py b/attoconf/tests/test_types.py new file mode 100644 index 0000000..14926ef --- /dev/null +++ b/attoconf/tests/test_types.py @@ -0,0 +1,62 @@ +# Copyright 2013 Ben Longbons <b.r.longbons@gmail.com> +# +# This file is part of attoconf. +# +# attoconf is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# attoconf is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with attoconf. If not, see <http://www.gnu.org/licenses/>. + + + +import unittest + +from attoconf.types import enum, ShellList + +class TestEnum(unittest.TestCase): + def test_stuff(self): + foobar = enum('foo', 'bar') + foobar('foo') + foobar('bar') + with self.assertRaisesRegex(ValueError, "'baz' not in {foo, bar}"): + foobar('baz') + +class TestShell(unittest.TestCase): + def test_str(self): + sh0 = ShellList('\\ ') + self.assertEqual("' '", str(sh0)) + self.assertEqual([' '], sh0.list) + sh1 = ShellList(' foo ') + self.assertEqual('foo', str(sh1)) + self.assertEqual(['foo'], sh1.list) + sh2 = ShellList(' "foo bar " baz') + self.assertEqual("'foo bar ' baz", str(sh2)) + self.assertEqual(['foo bar ', 'baz'], sh2.list) + sh3 = ShellList(""" "foo\\ bar\\"" 'baz\\ qux' ''\\''' frob\\ it """) + self.assertEqual("""'foo\\ bar"' 'baz\\ qux' ''"'"'' 'frob it'""", str(sh3)) + self.assertEqual(['foo\\ bar"', 'baz\\ qux', "'", 'frob it'], sh3.list) + + def test_list(self): + sh1 = ShellList(['foo']) + self.assertEqual('foo', str(sh1)) + sh2 = ShellList(['foo bar ', 'baz']) + self.assertEqual("'foo bar ' baz", str(sh2)) + sh3 = ShellList(['foo\\ bar"', 'baz\\ qux', "'", 'frob it']) + self.assertEqual('\'foo\\ bar"\' \'baz\\ qux\' \'\'"\'"\'\' \'frob it\'', str(sh3)) + + def test_add(self): + sh0 = ShellList('') + self.assertEqual(str(sh0 + sh0), '') + self.assertEqual((sh0 + sh0).list, []) + sh1 = ShellList(['foo bar', 'baz']) + self.assertEqual(str(sh0 + sh1), "'foo bar' baz") + self.assertEqual((sh0 + sh1).list, sh1.list) + self.assertEqual(str(sh1 + sh1), "'foo bar' baz 'foo bar' baz") diff --git a/attoconf/tests/test_version.py b/attoconf/tests/test_version.py new file mode 100644 index 0000000..9967283 --- /dev/null +++ b/attoconf/tests/test_version.py @@ -0,0 +1,95 @@ +# Copyright 2013 Ben Longbons <b.r.longbons@gmail.com> +# +# This file is part of attoconf. +# +# attoconf is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# attoconf is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with attoconf. If not, see <http://www.gnu.org/licenses/>. + + + +import unittest + +from attoconf.version import require_version, full_version, part_version + +class TestVersion(unittest.TestCase): + def test_parse(self): + # don't do this + name, version, dist = full_version.split(' ', 2) + self.assertEqual(name, 'attoconf') + self.assertEqual(version, part_version) + major, minor, patch = [int(x) for x in version.split('.')] + assert dist.startswith('(') + assert dist.endswith(')') + dist = dist[1:-1] + assert '(' not in dist + assert ')' not in dist + assert '\n' not in dist + from attoconf._version import distributor + self.assertEqual(dist, distributor) + + def test_check(self): + from attoconf._version import major, minor, patch + with self.assertRaises(SystemExit): + require_version(major - 1, minor - 1, patch - 1) + with self.assertRaises(SystemExit): + require_version(major - 1, minor - 1, patch + 0) + with self.assertRaises(SystemExit): + require_version(major - 1, minor - 1, patch + 1) + with self.assertRaises(SystemExit): + require_version(major - 1, minor + 0, patch - 1) + with self.assertRaises(SystemExit): + require_version(major - 1, minor + 0, patch + 0) + with self.assertRaises(SystemExit): + require_version(major - 1, minor + 0, patch + 1) + with self.assertRaises(SystemExit): + require_version(major - 1, minor + 1, patch - 1) + with self.assertRaises(SystemExit): + require_version(major - 1, minor + 1, patch + 0) + with self.assertRaises(SystemExit): + require_version(major - 1, minor + 1, patch + 1) + if 1: + require_version(major + 0, minor - 1, patch - 1) + if 1: + require_version(major + 0, minor - 1, patch + 0) + if 1: + require_version(major + 0, minor - 1, patch + 1) + if 1: + require_version(major + 0, minor + 0, patch - 1) + if 1: + require_version(major + 0, minor + 0, patch + 0) + with self.assertRaises(SystemExit): + require_version(major + 0, minor + 0, patch + 1) + with self.assertRaises(SystemExit): + require_version(major + 0, minor + 1, patch - 1) + with self.assertRaises(SystemExit): + require_version(major + 0, minor + 1, patch + 0) + with self.assertRaises(SystemExit): + require_version(major + 0, minor + 1, patch + 1) + with self.assertRaises(SystemExit): + require_version(major + 1, minor - 1, patch - 1) + with self.assertRaises(SystemExit): + require_version(major + 1, minor - 1, patch + 0) + with self.assertRaises(SystemExit): + require_version(major + 1, minor - 1, patch + 1) + with self.assertRaises(SystemExit): + require_version(major + 1, minor + 0, patch - 1) + with self.assertRaises(SystemExit): + require_version(major + 1, minor + 0, patch + 0) + with self.assertRaises(SystemExit): + require_version(major + 1, minor + 0, patch + 1) + with self.assertRaises(SystemExit): + require_version(major + 1, minor + 1, patch - 1) + with self.assertRaises(SystemExit): + require_version(major + 1, minor + 1, patch + 0) + with self.assertRaises(SystemExit): + require_version(major + 1, minor + 1, patch + 1) |