diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2013-08-01 23:00:53 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2013-08-02 22:32:29 -0700 |
commit | e2540283a9e25748c20bde296919040c93a927db (patch) | |
tree | 41aefc5b45c87fd9762656e240d82aa714074ee9 /attoconf/tests | |
parent | d53d9bf0e55fddd814ad99ec89134a7228390c65 (diff) | |
download | attobuild-e2540283a9e25748c20bde296919040c93a927db.tar.gz attobuild-e2540283a9e25748c20bde296919040c93a927db.tar.bz2 attobuild-e2540283a9e25748c20bde296919040c93a927db.tar.xz attobuild-e2540283a9e25748c20bde296919040c93a927db.zip |
Add flesh to the skeleton
Diffstat (limited to 'attoconf/tests')
-rw-r--r-- | attoconf/tests/__init__.py | 0 | ||||
-rw-r--r-- | attoconf/tests/test_core.py | 129 | ||||
-rw-r--r-- | attoconf/tests/test_help.py | 195 | ||||
-rw-r--r-- | attoconf/tests/test_version.py | 94 |
4 files changed, 418 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..88bf616 --- /dev/null +++ b/attoconf/tests/test_core.py @@ -0,0 +1,129 @@ +# 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/>. + +from __future__ import print_function, division, absolute_import + +import unittest + +from attoconf.core import Project, Build + +from cStringIO 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', + 'display help you should never ever ever care about', True) + proj.add_option('--foo', init='asdf', + type=str, check=None, + help='set frob target', hidden=False) + proj.add_option('--bar', init=None, + type=str, 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 + --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 + --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.assertEquals(build.project.srcdir, 'foo') + self.assertEquals(build.bindir, 'bar') + self.assertEquals(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, None) + 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=None, + type=str, check=check_foo, + help='help for string foo', hidden=False) + proj.add_option('--bar', init=0, + type=int, check=check_bar, + help='help for int bar', hidden=False) + proj.add_option('--qux', init=None, + type=int, check=check_qux, + help='help for int qux', hidden=False) + proj.add_option('VAR', init='', + type=str, check=check_var, + help='help for string VAR', hidden=False) + + build = Build(proj, '.') + build.configure(['--alias'], {'VAR': 'value'}) + self.assertEqual(build.vars, + { + 'FOO': ('B', 'command-line'), + 'BAR': (1, 'command-line'), + 'QUX': (None, 'default'), + 'VAR': ('value', 'environment'), + }) diff --git a/attoconf/tests/test_help.py b/attoconf/tests/test_help.py new file mode 100644 index 0000000..ea42c28 --- /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/>. + +from __future__ import print_function, division, absolute_import + +import unittest + +from attoconf.help import Help, HelpSection, put_line_in_width + +from cStringIO 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_version.py b/attoconf/tests/test_version.py new file mode 100644 index 0000000..1ce855d --- /dev/null +++ b/attoconf/tests/test_version.py @@ -0,0 +1,94 @@ +# 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/>. + +from __future__ import print_function, division, absolute_import + +import unittest + +from attoconf.version import require_version, string as version_string + +class TestVersion(unittest.TestCase): + def test_parse(self): + # don't do this + name, version, dist = version_string.split(' ', 2) + self.assertEqual(name, 'attoconf') + 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) |