Exemplo n.º 1
0
    def test_choices_negative(self):
        @dataclass
        class Options:
            small_integer: int = field(metadata=dict(choices=[1, 2, 3]))

        with NegativeTestHelper() as helper:
            parse_args(Options, ["--small-integer", "20"])
        self.assertIsNotNone(helper.exit_status,
                             "Expected an error while parsing")
Exemplo n.º 2
0
    def test_no_defaults_negative(self):
        @dataclass
        class Args:
            num_of_foo: int
            name: str

        with NegativeTestHelper() as helper:
            parse_args(Args, [])
        self.assertIsNotNone(helper.exit_status,
                             "Expected an error while parsing")
Exemplo n.º 3
0
    def test_basic(self):
        @dataclass
        class Opt:
            x: int = 42
            y: bool = False

        params = parse_args(Opt, [])
        self.assertEqual(42, params.x)
        self.assertEqual(False, params.y)
        params = parse_args(Opt, ["--x=10", "--y"])
        self.assertEqual(10, params.x)
        self.assertEqual(True, params.y)
Exemplo n.º 4
0
    def test_nargs_plus(self):
        @dataclass
        class Args:
            name: str
            friends: List[str] = field(metadata=dict(nargs="+"))

        args = ["--name", "Sam", "--friends", "pippin", "Frodo"]
        params = parse_args(Args, args)
        self.assertEqual("Sam", params.name)
        self.assertEqual(["pippin", "Frodo"], params.friends)

        args += ["Bilbo"]
        params = parse_args(Args, args)
        self.assertEqual("Sam", params.name)
        self.assertEqual(["pippin", "Frodo", "Bilbo"], params.friends)
Exemplo n.º 5
0
    def test_type(self):
        @dataclass
        class Options:
            name: str = field(metadata=dict(type=str.title))

        params = parse_args(Options, ["--name", "john doe"])
        self.assertEqual(params.name, "John Doe")
Exemplo n.º 6
0
    def test_choices(self):
        @dataclass
        class Options:
            small_integer: int = field(metadata=dict(choices=[1, 2, 3]))

        params = parse_args(Options, ["--small-integer", "2"])
        self.assertEqual(params.small_integer, 2)
Exemplo n.º 7
0
    def test_no_defaults(self):
        @dataclass
        class Args:
            num_of_foo: int
            name: str

        params = parse_args(Args, ["--num-of-foo=10", "--name", "Sam"])
        self.assertEqual(10, params.num_of_foo)
        self.assertEqual("Sam", params.name)
Exemplo n.º 8
0
    def test_positional(self):
        @dataclass
        class Options:
            x: int = field(metadata=dict(args=["-x", "--long-name"]))
            positional: str = field(metadata=dict(args=["positional"]))

        params = parse_args(Options, ["-x", "0", "POS_VALUE"])
        self.assertEqual(params.x, 0)
        self.assertEqual(params.positional, "POS_VALUE")
Exemplo n.º 9
0
    def test_nargs(self):
        @dataclass
        class Args:
            name: str
            friends: List[str] = field(metadata=dict(nargs=2))

        params = parse_args(Args,
                            ["--name", "Sam", "--friends", "pippin", "Frodo"])
        self.assertEqual("Sam", params.name)
        self.assertEqual(["pippin", "Frodo"], params.friends)
Exemplo n.º 10
0
    def test_default_factory(self):
        @dataclass
        class Parameters:
            cutoff_date: dt.datetime = field(
                default_factory=dt.datetime.now,
                metadata=dict(type=dt.datetime.fromisoformat))

        s_time = dt.datetime.now()
        params = parse_args(Parameters, [])
        e_time = dt.datetime.now()
        self.assertGreaterEqual(params.cutoff_date, s_time)
        self.assertLessEqual(params.cutoff_date, e_time)

        s_time = dt.datetime.now()
        params = parse_args(Parameters, [])
        e_time = dt.datetime.now()
        self.assertGreaterEqual(params.cutoff_date, s_time)
        self.assertLessEqual(params.cutoff_date, e_time)

        date = dt.datetime(2000, 1, 1)
        params = parse_args(Parameters, ["--cutoff-date", date.isoformat()])
        self.assertEqual(params.cutoff_date, date)
Exemplo n.º 11
0
    def test_default_factory_2(self):
        factory_calls = 0

        def factory_func():
            nonlocal factory_calls
            factory_calls += 1
            return f"Default Message: {factory_calls}"

        @dataclass
        class Parameters:
            message: str = field(default_factory=factory_func)

        params = parse_args(Parameters, [])
        self.assertEqual(params.message, "Default Message: 1")
        self.assertEqual(factory_calls, 1)

        params = parse_args(Parameters, ["--message", "User message"])
        self.assertEqual(params.message, "User message")
        self.assertEqual(factory_calls, 1)

        params = parse_args(Parameters, [])
        self.assertEqual(params.message, "Default Message: 2")
        self.assertEqual(factory_calls, 2)