def test_ask(self): with captured_stdout("\n") as stream: self.assertTrue(ask("Do you like cats?", default = True)) self.assertEqual(stream.getvalue(), "Do you like cats? [Y/n] ") with captured_stdout("\nyes") as stream: self.assertTrue(ask("Do you like cats?")) self.assertEqual(stream.getvalue(), "Do you like cats? (y/n) Invalid response, please try again\nDo you like cats? (y/n) ")
def test_choose(self): with captured_stdout("foo\n2\n") as stream: self.assertEqual(choose("What is your favorite color?", ["blue", "yellow", "green"]), "yellow") self.assertEqual(stream.getvalue(), "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: ") with captured_stdout("foo\n2\n") as stream: self.assertEqual(choose("What is your favorite color?", [("blue", 10), ("yellow", 11), ("green", 12)]), 11) self.assertEqual(stream.getvalue(), "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: ") with captured_stdout("foo\n\n") as stream: self.assertEqual(choose("What is your favorite color?", ["blue", "yellow", "green"], default = "yellow"), "yellow") self.assertEqual(stream.getvalue(), "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice [2]: Invalid choice, please try again\nChoice [2]: ")
def test_default_main(self): with captured_stdout() as stream: _, rc = Sample.run(["sample"], exit = False) self.assertEqual(rc, 1) self.assertIn("No sub-command given", stream.getvalue()) with captured_stdout() as stream: _, rc = Sample.run(["sample", "pimple"], exit = False) self.assertEqual(rc, 1) self.assertIn("Unknown sub-command 'pimple'", stream.getvalue()) with captured_stdout() as stream: _, rc = Sample.run(["sample", "mumble"], exit = False) self.assertEqual(rc, 1) self.assertIn("main() not implemented", stream.getvalue())
def test_defaults(self): class MainValidator(cli.Application): @cli.positional(int, int) def main(self, myint, myint2=2): print(repr(myint), repr(myint2)) with captured_stdout() as stream: _, rc = MainValidator.run(["prog", "1"], exit = False) self.assertEqual(rc, 0) self.assertEqual("1 2", stream.getvalue().strip()) with captured_stdout() as stream: _, rc = MainValidator.run(["prog", "1", "3"], exit = False) self.assertEqual(rc, 0) self.assertEqual("1 3", stream.getvalue().strip())
def test_env_var(self): with captured_stdout() as stream: _, rc = SimpleApp.run(["arg", "--bacon=10"], exit=False) self.assertEqual(rc, 0) self.assertIn("10", stream.getvalue()) with captured_stdout() as stream: with local.env( PLUMBUM_TEST_BACON='20', PLUMBUM_TEST_EGGS='raw', ): inst, rc = SimpleApp.run(["arg"], exit=False) self.assertEqual(rc, 0) self.assertIn("20", stream.getvalue()) self.assertEqual(inst.eggs, 'raw')
def test_prog(self): class MainValidator(cli.Application): @cli.positional(int, int, int) def main(self, myint, myint2, *mylist): print(repr(myint), myint2, mylist) with captured_stdout() as stream: _, rc = MainValidator.run(["prog", "1", "2", '3', '4', '5'], exit = False) self.assertEqual(rc, 0) self.assertEqual("1 2 (3, 4, 5)", stream.getvalue().strip())
def test_lazy_subcommand(self): class Foo(cli.Application): pass Foo.subcommand("lazy", "test_cli.LazyLoaded") with captured_stdout() as stream: _, rc = Foo.run(["foo", "lazy"], exit = False) self.assertEqual(rc, 0) self.assertIn("hello world", stream.getvalue())
def test_failure(self): class MainValidator(cli.Application): @cli.positional(int, int, int) def main(self, myint, myint2, *mylist): print(myint, myint2, mylist) with captured_stdout() as stream: _, rc = MainValidator.run(["prog", "1.2", "2", '3', '4', '5'], exit = False) self.assertEqual(rc, 2) value = stream.getvalue().strip() self.assertTrue("'int'>, not '1.2':" in value) self.assertTrue(" 'int'>, not '1.2':" in value) self.assertTrue('''ValueError("invalid literal for int() with base 10: '1.2'"''' in value)
def test_progress(self): with captured_stdout() as stream: for i in Progress.range(4, has_output=True, timer=False): print('hi') time.sleep(.5) stream.seek(0) output = """\ 0% complete 0% complete hi 25% complete hi 50% complete hi 75% complete hi 100% complete """ self.assertEqual(stream.read(), output)
def test_mandatory_env_var(self): with captured_stdout() as stream: _, rc = SimpleApp.run(["arg"], exit = False) self.assertEqual(rc, 2) self.assertIn("bacon is mandatory", stream.getvalue())
def test_unbind(self): with captured_stdout() as stream: _, rc = Sample.run(["sample", "--help"], exit = False) self.assertEqual(rc, 0) self.assertIn("--foo", stream.getvalue()) self.assertNotIn("--version", stream.getvalue())