def testHelpWithClass(self): with self.assertRaisesFireExit(0, 'SYNOPSIS.*ARG1'): core.Fire(tc.InstanceVars, command=['--', '--help']) with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*ARG1'): core.Fire(tc.InstanceVars, command=['--help']) with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*ARG1'): core.Fire(tc.InstanceVars, command=['-h'])
def testSetParseFnsOnlySomeTypes(self): self.assertEqual( core.Fire(PartialParseFn, command=['example4', '10', '10']), ('10', 10)) self.assertEqual( core.Fire(PartialParseFn, command=['example5', '10', '10']), (10, '10'))
def testCallable(self): with self.assertOutputMatches(stdout=r"foo:\s+foo\s+", stderr=None): core.Fire(tc.CallableWithKeywordArgument(), command=["--foo=foo"]) with self.assertOutputMatches(stdout=r"foo\s+", stderr=None): core.Fire(tc.CallableWithKeywordArgument(), command=["print_msg", "foo"]) with self.assertOutputMatches(stdout=r"", stderr=None): core.Fire(tc.CallableWithKeywordArgument(), command=[])
def testSetParseFnsOnlySomeTypes(self): self.assertEqual( core.Fire(PartialParseFn, command=["example4", "10", "10"]), ("10", 10)) self.assertEqual( core.Fire(PartialParseFn, command=["example5", "10", "10"]), (10, "10"))
def testHelpWithClass(self): with self.assertRaisesFireExit(0, "SYNOPSIS.*ARG1"): core.Fire(tc.InstanceVars, command=["--", "--help"]) with self.assertRaisesFireExit(0, "INFO:.*SYNOPSIS.*ARG1"): core.Fire(tc.InstanceVars, command=["--help"]) with self.assertRaisesFireExit(0, "INFO:.*SYNOPSIS.*ARG1"): core.Fire(tc.InstanceVars, command=["-h"])
def testCallable(self): with self.assertOutputMatches(stdout=r'foo:\s+foo\s+', stderr=None): core.Fire(tc.CallableWithKeywordArgument(), command=['--foo=foo']) with self.assertOutputMatches(stdout=r'foo\s+', stderr=None): core.Fire(tc.CallableWithKeywordArgument(), command=['print_msg', 'foo']) with self.assertOutputMatches(stdout=r'', stderr=None): core.Fire(tc.CallableWithKeywordArgument(), command=[])
def testSetParseFnsNamedDefaultsFromFire(self): # Fire should use the decorator to know how to parse string arguments. self.assertEqual(core.Fire(WithDefaults, command=["example2"]), (10, int)) self.assertEqual(core.Fire(WithDefaults, command=["example2", "10"]), (10, float)) self.assertEqual(core.Fire(WithDefaults, command=["example2", "13"]), (13, float)) self.assertEqual(core.Fire(WithDefaults, command=["example2", "14.0"]), (14, float))
def testHelpWithNamespaceCollision(self): # Tests cases when calling the help shortcut should not show help. with self.assertOutputMatches(stdout='DESCRIPTION.*', stderr=None): core.Fire(tc.WithHelpArg, command=['--help', 'False']) with self.assertOutputMatches(stdout='help in a dict', stderr=None): core.Fire(tc.WithHelpArg, command=['dictionary', '__help']) with self.assertOutputMatches(stdout='{}', stderr=None): core.Fire(tc.WithHelpArg, command=['dictionary', '--help']) with self.assertOutputMatches(stdout='False', stderr=None): core.Fire(tc.function_with_help, command=['False'])
def testHelpWithNamespaceCollision(self): # Tests cases when calling the help shortcut should not show help. with self.assertOutputMatches(stdout="DESCRIPTION.*", stderr=None): core.Fire(tc.WithHelpArg, command=["--help", "False"]) with self.assertOutputMatches(stdout="help in a dict", stderr=None): core.Fire(tc.WithHelpArg, command=["dictionary", "__help"]) with self.assertOutputMatches(stdout="{}", stderr=None): core.Fire(tc.WithHelpArg, command=["dictionary", "--help"]) with self.assertOutputMatches(stdout="False", stderr=None): core.Fire(tc.function_with_help, command=["False"])
def testSetParseFnsDefaultsFromFire(self): # Fire should use the decorator to know how to parse string arguments. self.assertEqual(core.Fire(WithDefaults, command=['example1']), (10, int)) self.assertEqual(core.Fire(WithDefaults, command=['example1', '10']), (10, float)) self.assertEqual(core.Fire(WithDefaults, command=['example1', '13']), (13, float)) self.assertEqual(core.Fire(WithDefaults, command=['example1', '14.0']), (14, float))
def testHelpWithMember(self): with self.assertRaisesFireExit(0, 'SYNOPSIS.*capitalize'): core.Fire(tc.TypedProperties, command=['gamma', '--', '--help']) with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*capitalize'): core.Fire(tc.TypedProperties, command=['gamma', '--help']) with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*capitalize'): core.Fire(tc.TypedProperties, command=['gamma', '-h']) with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*delta'): core.Fire(tc.TypedProperties, command=['delta', '--help']) with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*echo'): core.Fire(tc.TypedProperties, command=['echo', '--help'])
def testHelpWithMember(self): with self.assertRaisesFireExit(0, "SYNOPSIS.*capitalize"): core.Fire(tc.TypedProperties, command=["gamma", "--", "--help"]) with self.assertRaisesFireExit(0, "INFO:.*SYNOPSIS.*capitalize"): core.Fire(tc.TypedProperties, command=["gamma", "--help"]) with self.assertRaisesFireExit(0, "INFO:.*SYNOPSIS.*capitalize"): core.Fire(tc.TypedProperties, command=["gamma", "-h"]) with self.assertRaisesFireExit(0, "INFO:.*SYNOPSIS.*delta"): core.Fire(tc.TypedProperties, command=["delta", "--help"]) with self.assertRaisesFireExit(0, "INFO:.*SYNOPSIS.*echo"): core.Fire(tc.TypedProperties, command=["echo", "--help"])
def testHelpWithMember(self): with self.assertRaisesFireExit(0, 'Usage:.*upper'): core.Fire(tc.TypedProperties, command=['gamma', '--', '--help']) with self.assertRaisesFireExit(0, 'INFO:.*Usage:.*upper'): core.Fire(tc.TypedProperties, command=['gamma', '--help']) with self.assertRaisesFireExit(0, 'INFO:.*Usage:.*upper'): core.Fire(tc.TypedProperties, command=['gamma', '-h']) with self.assertRaisesFireExit(0, 'INFO:.*Usage:.*delta'): core.Fire(tc.TypedProperties, command=['delta', '--help']) with self.assertRaisesFireExit(0, 'INFO:.*Usage:.*echo'): core.Fire(tc.TypedProperties, command=['echo', '--help'])
def testInteractiveModeVariables(self, mock_embed): core.Fire(tc.WithDefaults, command=["double", "2", "--", "-i"]) self.assertTrue(mock_embed.called) (variables, verbose), unused_kwargs = mock_embed.call_args self.assertFalse(verbose) self.assertEqual(variables["result"], 4) self.assertIsInstance(variables["self"], tc.WithDefaults) self.assertIsInstance(variables["trace"], trace.FireTrace)
def testLruCacheDecorator(self): self.assertEqual( core.Fire( tc.py3.lru_cache_decorated, # pytype: disable=module-attr command=["foo"], ), "foo", )
def testLruCacheDecoratorBoundArg(self): self.assertEqual( core.Fire( tc.py3.LruCacheDecoratedMethod, # pytype: disable=module-attr command=["lru_cache_in_class", "foo"], ), "foo", )
def testSetParseFn(self): self.assertEqual( core.Fire( WithVarArgs, command=['example7', '1', '--arg2=2', '3', '4', '--kwarg=5']), ('1', '2', ('3', '4'), { 'kwarg': '5' }))
def testInteractiveModeVariables(self, mock_embed): core.Fire(tc.WithDefaults, 'double 2 -- -i') self.assertTrue(mock_embed.called) (variables, verbose), unused_kwargs = mock_embed.call_args self.assertFalse(verbose) self.assertEqual(variables['result'], 4) self.assertIsInstance(variables['self'], tc.WithDefaults) self.assertIsInstance(variables['trace'], trace.FireTrace)
def testInteractiveModeVariablesWithName(self, mock_embed): core.Fire(tc.WithDefaults, command=['double', '2', '--', '-i', '-v'], name='D') self.assertTrue(mock_embed.called) (variables, verbose), unused_kwargs = mock_embed.call_args self.assertTrue(verbose) self.assertEqual(variables['result'], 4) self.assertIsInstance(variables['self'], tc.WithDefaults) self.assertEqual(variables['D'], tc.WithDefaults) self.assertIsInstance(variables['trace'], trace.FireTrace)
def testSetParseFn(self): self.assertEqual( core.Fire( WithVarArgs, command=["example7", "1", "--arg2=2", "3", "4", "--kwarg=5"], ), ("1", "2", ("3", "4"), { "kwarg": "5" }), )
def testSetParseFnsForKeywordArgs(self): self.assertEqual(core.Fire(E, 'example6'), ('default', 0)) self.assertEqual(core.Fire(E, 'example6 --herring "red"'), ('default', 0)) self.assertEqual(core.Fire(E, 'example6 --mode train'), ('train', 0)) self.assertEqual(core.Fire(E, 'example6 --mode 3'), ('3', 0)) self.assertEqual(core.Fire(E, 'example6 --mode -1 --count 10'), ('-1', 10)) self.assertEqual(core.Fire(E, 'example6 --count -2'), ('default', -2))
def testCustomSerialize(self): def serialize(x): if isinstance(x, list): return ', '.join(str(xi) for xi in x) if isinstance(x, dict): return ', '.join('{}={!r}'.format(k, v) for k, v in sorted(x.items())) if x == 'special': return ['SURPRISE!!', "I'm a list!"] return x ident = lambda x: x with self.assertOutputMatches(stdout='a, b', stderr=None): _ = core.Fire(ident, command=['[a,b]'], serialize=serialize) with self.assertOutputMatches(stdout='a=5, b=6', stderr=None): _ = core.Fire(ident, command=['{a:5,b:6}'], serialize=serialize) with self.assertOutputMatches(stdout='asdf', stderr=None): _ = core.Fire(ident, command=['asdf'], serialize=serialize) with self.assertOutputMatches(stdout="SURPRISE!!\nI'm a list!\n", stderr=None): _ = core.Fire(ident, command=['special'], serialize=serialize) with self.assertRaises(core.FireError): core.Fire(ident, command=['asdf'], serialize=55)
def testSetParseFnsForKeywordArgs(self): self.assertEqual(core.Fire(WithKwargs, command=['example6']), ('default', 0)) self.assertEqual( core.Fire(WithKwargs, command=['example6', '--herring', '"red"']), ('default', 0)) self.assertEqual( core.Fire(WithKwargs, command=['example6', '--mode', 'train']), ('train', 0)) self.assertEqual( core.Fire(WithKwargs, command=['example6', '--mode', '3']), ('3', 0)) self.assertEqual( core.Fire(WithKwargs, command=['example6', '--mode', '-1', '--count', '10']), ('-1', 10)) self.assertEqual( core.Fire(WithKwargs, command=['example6', '--count', '-2']), ('default', -2))
def testSetParseFnsForKeywordArgs(self): self.assertEqual(core.Fire(WithKwargs, command=["example6"]), ("default", 0)) self.assertEqual( core.Fire(WithKwargs, command=["example6", "--herring", '"red"']), ("default", 0), ) self.assertEqual( core.Fire(WithKwargs, command=["example6", "--mode", "train"]), ("train", 0)) self.assertEqual( core.Fire(WithKwargs, command=["example6", "--mode", "3"]), ("3", 0)) self.assertEqual( core.Fire(WithKwargs, command=["example6", "--mode", "-1", "--count", "10"]), ("-1", 10), ) self.assertEqual( core.Fire(WithKwargs, command=["example6", "--count", "-2"]), ("default", -2), )
def testSetParseFnsFnWithPositionalArgs(self): self.assertEqual(core.Fire(double, command=["5"]), 10)
def testSetParseFnsPositionalArgs(self): self.assertEqual(core.Fire(NoDefaults, command=["quadruple", "5"]), 20)
def testSetParseFnsNamedArgs(self): self.assertEqual(core.Fire(NoDefaults, command=["double", "2"]), 4) self.assertEqual(core.Fire(NoDefaults, command=["triple", "4"]), 12.0)
def testSetParseFnsPositionalAndNamed(self): self.assertEqual(core.Fire(MixedArguments, ["example3", "10", "10"]), (10, "10"))
def testSetParseFnsNamedArgs(self): self.assertEqual(core.Fire(NoDefaults, command=['double', '2']), 4) self.assertEqual(core.Fire(NoDefaults, command=['triple', '4']), 12.0)
def testSetParseFnsPositionalAndNamed(self): self.assertEqual(core.Fire(MixedArguments, ['example3', '10', '10']), (10, '10'))