Exemplo n.º 1
0
    def test_with_one_or_more_basics_with_other_separator(self):
        listsep = ';'
        parse_numbers2 = TypeBuilder.with_one_or_more(parse_number, listsep)
        self.assertEqual(parse_numbers2.pattern, r"(\d+)(\s*;\s*(\d+))*")

        listsep = ':'
        parse_numbers2 = TypeBuilder.with_one_or_more(parse_number, listsep)
        self.assertEqual(parse_numbers2.pattern, r"(\d+)(\s*:\s*(\d+))*")
Exemplo n.º 2
0
    def test_make_variant__with_color_or_person(self):
        type_converters = [parse_color, parse_person_choice]
        parse_variant2 = TypeBuilder.make_variant(type_converters)
        schema = "Variant2: {variant:Color_or_Person}"
        parser = parse.Parser(schema, dict(Color_or_Person=parse_variant2))

        # -- TYPE 1: Color
        self.assert_match(parser, "Variant2: red", "variant", Color.red)
        self.assert_match(parser, "Variant2: blue", "variant", Color.blue)

        # -- TYPE 2: Person
        self.assert_match(parser, "Variant2: Alice", "variant", "Alice")
        self.assert_match(parser, "Variant2: Bob", "variant", "Bob")
        self.assert_match(parser, "Variant2: Charly", "variant", "Charly")

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Variant2: __Alice__")
        self.assert_mismatch(parser, "Variant2: Alice ")
        self.assert_mismatch(parser, "Variant2: Alice2")
        self.assert_mismatch(parser, "Variant2: red2")

        # -- PERFORM TESTS:
        self.ensure_can_parse_all_enum_values(parser, parse_color,
                                              "Variant2: %s", "variant")

        self.ensure_can_parse_all_choices(parser, parse_person_choice,
                                          "Variant2: %s", "variant")
Exemplo n.º 3
0
    def test_make_enum_with_enum_class(self):
        """
        Use :meth:`parse_type.TypeBuilder.make_enum()` with enum34 classes.
        """
        class Color(Enum):
            red = 1
            green = 2
            blue = 3

        parse_color = TypeBuilder.make_enum(Color)
        parse_color.name = "Color"
        schema = "Answer: {color:Color}"
        parser = parse.Parser(schema, dict(Color=parse_color))

        # -- PERFORM TESTS:
        self.ensure_can_parse_all_enum_values(parser, parse_color,
                                              "Answer: %s", "color")

        # -- VALID:
        self.assert_match(parser, "Answer: red", "color", Color.red)
        self.assert_match(parser, "Answer: green", "color", Color.green)
        self.assert_match(parser, "Answer: blue", "color", Color.blue)

        # -- IGNORE-CASE: In parsing, calls type converter function !!!
        self.assert_match(parser, "Answer: RED", "color", Color.red)

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __RED__", "color")
        self.assert_mismatch(parser, "Answer: red ", "color")
        self.assert_mismatch(parser, "Answer: redx", "color")
        self.assert_mismatch(parser, "Answer: redx ZZZ", "color")
Exemplo n.º 4
0
    def test_make_enum_with_enum_class(self):
        """
        Use :meth:`parse_type.TypeBuilder.make_enum()` with enum34 classes.
        """
        class Color(Enum):
            red = 1
            green = 2
            blue = 3

        parse_color = TypeBuilder.make_enum(Color)
        parse_color.name = "Color"
        schema = "Answer: {color:Color}"
        parser = parse.Parser(schema, dict(Color=parse_color))

        # -- PERFORM TESTS:
        self.ensure_can_parse_all_enum_values(parser,
                parse_color, "Answer: %s", "color")

        # -- VALID:
        self.assert_match(parser, "Answer: red",   "color", Color.red)
        self.assert_match(parser, "Answer: green", "color", Color.green)
        self.assert_match(parser, "Answer: blue",  "color", Color.blue)

        # -- IGNORE-CASE: In parsing, calls type converter function !!!
        self.assert_match(parser, "Answer: RED", "color", Color.red)

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __RED__", "color")
        self.assert_mismatch(parser, "Answer: red ",    "color")
        self.assert_mismatch(parser, "Answer: redx",    "color")
        self.assert_mismatch(parser, "Answer: redx ZZZ", "color")
Exemplo n.º 5
0
    def test_make_variant__with_color_or_person(self):
        type_converters = [parse_color, parse_person_choice]
        parse_variant2 = TypeBuilder.make_variant(type_converters)
        schema = "Variant2: {variant:Color_or_Person}"
        parser = parse.Parser(schema, dict(Color_or_Person=parse_variant2))

        # -- TYPE 1: Color
        self.assert_match(parser, "Variant2: red",  "variant", Color.red)
        self.assert_match(parser, "Variant2: blue", "variant", Color.blue)

        # -- TYPE 2: Person
        self.assert_match(parser, "Variant2: Alice",  "variant", "Alice")
        self.assert_match(parser, "Variant2: Bob",    "variant", "Bob")
        self.assert_match(parser, "Variant2: Charly", "variant", "Charly")

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Variant2: __Alice__")
        self.assert_mismatch(parser, "Variant2: Alice ")
        self.assert_mismatch(parser, "Variant2: Alice2")
        self.assert_mismatch(parser, "Variant2: red2")

        # -- PERFORM TESTS:
        self.ensure_can_parse_all_enum_values(parser,
                    parse_color, "Variant2: %s", "variant")

        self.ensure_can_parse_all_choices(parser,
                    parse_person_choice, "Variant2: %s", "variant")
Exemplo n.º 6
0
 def test_make_variant__with_strict_raises_error_on_case_mismatch(self):
     # -- NEEDS:
     #  * re_opts=0 (IGNORECASE disabled)
     #  * strict=True, allow that an error is raised
     type_converters = [parse_number, parse_yesno]
     parse_variant = TypeBuilder.make_variant(type_converters,
                                              strict=True, re_opts=0)
     schema = "Variant: {variant:YesNo_or_Number}"
     parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
     self.assertRaises(AssertionError,  parser.parse, "Variant: YES")
Exemplo n.º 7
0
class step_definitons:
    def __init__(self):
        pass

    @Given('I go to the meeting')
    def given_step_impl(context):
        print('>>>>>> In given step implementation')

    # parsing with split and making as list
    def parse_list(parameters):
        param_list = parameters.split(",")
        # param_list = TypeBuilder.with_one_or_more(parameters, listsep=",")
        return param_list

    register_type(List=parse_list)

    @When('I meet {persons:List}')
    def step_impl(context, persons):
        print(">>>>> Step Implementation of list of parameters")
        for person in persons:
            print("Met with people: ", person)

    @When(u'I want to meet {a+}{names}')
    def step_impl(context, names):
        print(">>>>> Implementing cardinality.")
        print(type(names))
        for name in names:
            print('want to meet ppl: ', name)

# getting list using make choice predefined

    parse_list = TypeBuilder.make_choice(["Alice", "Bob", "Charly", "Dodo"])
    # register_type(mylist=parse_list)
    list_input = TypeBuilder.with_one_or_more(parse_list, listsep=",")
    register_type(list_names=list_input)

    @When('I test cardinality with {names:list_names}')
    def step_impl(context, names):
        print(
            ">>>>> Step Implementation of list of parameters with TypeBuilder")
        print(type(names))
        for name in names:
            print(">>>>>> Names in list: ", name)
    def test_with_one_or_more_enum(self):
        parse_color  = TypeBuilder.make_enum({"red": 1, "green":2, "blue": 3})
        parse_colors = TypeBuilder.with_one_or_more(parse_color)
        parse_colors.name = "Colors"

        extra_types = build_type_dict([ parse_colors ])
        schema = "List: {colors:Colors}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "List: green",      "colors", [ 2 ])
        self.assert_match(parser, "List: red, green", "colors", [ 1, 2 ])

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "List: ",   "colors")  # Zero items.
        self.assert_mismatch(parser, "List: x",  "colors")  # Not a Color.
        self.assert_mismatch(parser, "List: black", "colors")  # Unknown
        self.assert_mismatch(parser, "List: red,",  "colors")  # Trailing sep.
        self.assert_mismatch(parser, "List: a, b",  "colors")  # List of ...
    def test_with_zero_or_more__choice(self):
        parse_color  = TypeBuilder.make_choice(["red", "green", "blue"])
        parse_colors = TypeBuilder.with_zero_or_more(parse_color)
        parse_colors.name = "Colors0"

        extra_types = build_type_dict([ parse_colors ])
        schema = "List: {colors:Colors0}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "List: ",           "colors", [ ])
        self.assert_match(parser, "List: green",      "colors", [ "green" ])
        self.assert_match(parser, "List: red, green", "colors", [ "red", "green" ])

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "List: x",  "colors")  # Not a Color.
        self.assert_mismatch(parser, "List: black", "colors")  # Unknown
        self.assert_mismatch(parser, "List: red,",  "colors")  # Trailing sep.
        self.assert_mismatch(parser, "List: a, b",  "colors")  # List of ...
Exemplo n.º 10
0
 def test_make_variant__with_strict_raises_error_on_case_mismatch(self):
     # -- NEEDS:
     #  * re_opts=0 (IGNORECASE disabled)
     #  * strict=True, allow that an error is raised
     type_converters = [parse_number, parse_yesno]
     parse_variant = TypeBuilder.make_variant(type_converters,
                                              strict=True,
                                              re_opts=0)
     schema = "Variant: {variant:YesNo_or_Number}"
     parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
     self.assertRaises(AssertionError, parser.parse, "Variant: YES")
Exemplo n.º 11
0
    def test_make_choice__anycase_accepted_case_sensitity(self):
        # -- NOTE: strict=False => Disable errors due to case-mismatch.
        parse_choice = TypeBuilder.make_choice(["one", "two", "three"],
                                               strict=False)
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))

        # -- PERFORM TESTS:
        # NOTE: Parser uses re.IGNORECASE flag => Any case accepted.
        self.assert_match(parser, "Answer: one", "answer", "one")
        self.assert_match(parser, "Answer: TWO", "answer", "TWO")
        self.assert_match(parser, "Answer: Three", "answer", "Three")
Exemplo n.º 12
0
    def test_make_choice__anycase_accepted_case_sensitity(self):
        # -- NOTE: strict=False => Disable errors due to case-mismatch.
        parse_choice = TypeBuilder.make_choice(["one", "two", "three"],
                                               strict=False)
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))

        # -- PERFORM TESTS:
        # NOTE: Parser uses re.IGNORECASE flag => Any case accepted.
        self.assert_match(parser, "Answer: one",   "answer", "one")
        self.assert_match(parser, "Answer: TWO",   "answer", "TWO")
        self.assert_match(parser, "Answer: Three", "answer", "Three")
    def test_with_one_or_more_with_other_separator(self):
        parse_numbers2 = TypeBuilder.with_one_or_more(parse_number, listsep=';')
        parse_numbers2.name = "Numbers2"

        extra_types = build_type_dict([ parse_numbers2 ])
        schema = "List: {numbers:Numbers2}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "List: 1",       "numbers", [ 1 ])
        self.assert_match(parser, "List: 1; 2",    "numbers", [ 1, 2 ])
        self.assert_match(parser, "List: 1; 2; 3", "numbers", [ 1, 2, 3 ])
Exemplo n.º 14
0
 def test_make_variant__without_strict_may_return_none_on_case_mismatch(self):
     # -- NEEDS:
     #  * re_opts=0 (IGNORECASE disabled)
     #  * strict=False, otherwise an error is raised
     type_converters = [parse_number, parse_yesno]
     parse_variant = TypeBuilder.make_variant(type_converters, re_opts=0,
                                              strict=False)
     schema = "Variant: {variant:YesNo_or_Number}"
     parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
     result = parser.parse("Variant: No")
     self.assertNotEqual(result, None)
     self.assertEqual(result["variant"], None)
Exemplo n.º 15
0
    def test_make_choice__anycase_accepted_lowercase_enforced(self):
        # -- NOTE: strict=True => Enable errors due to case-mismatch.
        parse_choice = TypeBuilder.make_choice(["one", "two", "three"],
                            transform=lambda x: x.lower(), strict=True)
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))

        # -- PERFORM TESTS:
        # NOTE: Parser uses re.IGNORECASE flag
        # => Any case accepted, but result is in lower case.
        self.assert_match(parser, "Answer: one",   "answer", "one")
        self.assert_match(parser, "Answer: TWO",   "answer", "two")
        self.assert_match(parser, "Answer: Three", "answer", "three")
Exemplo n.º 16
0
    def test_with_optional(self):
        # -- ALIAS FOR: zero_or_one
        parse_opt_number = TypeBuilder.with_optional(parse_number)
        parse_opt_number.name = "OptionalNumber"

        extra_types = self.build_type_dict([ parse_opt_number ])
        schema = "Optional: {number:OptionalNumber}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "Optional: ",   "number", None)
        self.assert_match(parser, "Optional: 1",  "number", 1)
        self.assert_match(parser, "Optional: 42", "number", 42)
Exemplo n.º 17
0
    def test_make_choice2__with_transform(self):
        transform = lambda x: x.lower()
        parse_choice2 = TypeBuilder.make_choice2(["ZERO", "one", "Two"],
                                                 transform=transform)
        self.assertSequenceEqual(parse_choice2.choices, ["zero", "one", "two"])
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice2))

        # -- PERFORM TESTS:
        # NOTE: Parser uses re.IGNORECASE => Any case is accepted.
        self.assert_match(parser, "Answer: zERO", "answer", (0, "zero"))
        self.assert_match(parser, "Answer: ONE", "answer", (1, "one"))
        self.assert_match(parser, "Answer: Two", "answer", (2, "two"))
Exemplo n.º 18
0
    def test_make_choice2__with_transform(self):
        transform = lambda x: x.lower()
        parse_choice2 = TypeBuilder.make_choice2(["ZERO", "one", "Two"],
                                        transform=transform)
        self.assertSequenceEqual(parse_choice2.choices, ["zero", "one", "two"])
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice2))

        # -- PERFORM TESTS:
        # NOTE: Parser uses re.IGNORECASE => Any case is accepted.
        self.assert_match(parser, "Answer: zERO", "answer", (0, "zero"))
        self.assert_match(parser, "Answer: ONE", "answer",  (1, "one"))
        self.assert_match(parser, "Answer: Two", "answer",  (2, "two"))
Exemplo n.º 19
0
 def test_make_variant__without_strict_may_return_none_on_case_mismatch(
         self):
     # -- NEEDS:
     #  * re_opts=0 (IGNORECASE disabled)
     #  * strict=False, otherwise an error is raised
     type_converters = [parse_number, parse_yesno]
     parse_variant = TypeBuilder.make_variant(type_converters,
                                              re_opts=0,
                                              strict=False)
     schema = "Variant: {variant:YesNo_or_Number}"
     parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
     result = parser.parse("Variant: No")
     self.assertNotEqual(result, None)
     self.assertEqual(result["variant"], None)
Exemplo n.º 20
0
    def test_make_choice__anycase_accepted_lowercase_enforced(self):
        # -- NOTE: strict=True => Enable errors due to case-mismatch.
        parse_choice = TypeBuilder.make_choice(["one", "two", "three"],
                                               transform=lambda x: x.lower(),
                                               strict=True)
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))

        # -- PERFORM TESTS:
        # NOTE: Parser uses re.IGNORECASE flag
        # => Any case accepted, but result is in lower case.
        self.assert_match(parser, "Answer: one", "answer", "one")
        self.assert_match(parser, "Answer: TWO", "answer", "two")
        self.assert_match(parser, "Answer: Three", "answer", "three")
Exemplo n.º 21
0
    def test_with_many(self):
        # -- ALIAS FOR: one_or_more
        parse_numbers = TypeBuilder.with_many(parse_number)
        parse_numbers.name = "Numbers"

        extra_types = self.build_type_dict([ parse_numbers ])
        schema = "List: {numbers:Numbers}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "List: 1",       "numbers", [ 1 ])
        self.assert_match(parser, "List: 1, 2",    "numbers", [ 1, 2 ])
        self.assert_match(parser, "List: 1, 2, 3", "numbers", [ 1, 2, 3 ])

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "List: ",   "numbers")  # Zero items.
Exemplo n.º 22
0
    def test_make_variant__with_strict_and_compiled_raises_error_on_case_mismatch(self):
        # XXX re_opts=0 seems to work differently.
        # -- NEEDS:
        #  * re_opts=0 (IGNORECASE disabled)
        #  * strict=True, allow that an error is raised
        type_converters = [parse_number, parse_yesno]
        # -- ENSURE: coverage for cornercase.
        parse_number.matcher = re.compile(parse_number.pattern)

        parse_variant = TypeBuilder.make_variant(type_converters,
                                        compiled=True, re_opts=0, strict=True)
        schema = "Variant: {variant:YesNo_or_Number}"
        parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
        # XXX self.assertRaises(AssertionError,  parser.parse, "Variant: YES")
        result = parser.parse("Variant: Yes")
        self.assertNotEqual(result, None)
        self.assertEqual(result["variant"], True)
Exemplo n.º 23
0
    def test_make_choice(self):
        parse_choice = TypeBuilder.make_choice(["one", "two", "three"])
        parse_choice.name = "NumberWordChoice"
        extra_types = self.build_type_dict([ parse_choice ])
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "Answer: one", "answer", "one")
        self.assert_match(parser, "Answer: two", "answer", "two")
        self.ensure_can_parse_all_choices(parser,
                    parse_choice, "Answer: %s", "answer")

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __one__", "answer")
        self.assert_mismatch(parser, "Answer: one ",    "answer")
        self.assert_mismatch(parser, "Answer: one ZZZ", "answer")
Exemplo n.º 24
0
    def test_make_choice(self):
        parse_choice = TypeBuilder.make_choice(["one", "two", "three"])
        parse_choice.name = "NumberWordChoice"
        extra_types = build_type_dict([parse_choice])
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "Answer: one", "answer", "one")
        self.assert_match(parser, "Answer: two", "answer", "two")
        self.ensure_can_parse_all_choices(parser, parse_choice, "Answer: %s",
                                          "answer")

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __one__", "answer")
        self.assert_mismatch(parser, "Answer: one ", "answer")
        self.assert_mismatch(parser, "Answer: one ZZZ", "answer")
Exemplo n.º 25
0
    def test_with_zero_or_one(self):
        parse_opt_number = TypeBuilder.with_zero_or_one(parse_number)
        parse_opt_number.name = "OptionalNumber"

        extra_types = self.build_type_dict([ parse_opt_number ])
        schema = "Optional: {number:OptionalNumber}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "Optional: ",   "number", None)
        self.assert_match(parser, "Optional: 1",  "number", 1)
        self.assert_match(parser, "Optional: 42", "number", 42)

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Optional: x",   "number")  # Not a Number.
        self.assert_mismatch(parser, "Optional: -1",  "number")  # Negative.
        self.assert_mismatch(parser, "Optional: a, b", "number") # List of ...
Exemplo n.º 26
0
        def test_cardinality_field_with_zero_or_one(self):
            # -- SETUP:
            parse_person = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
            parse_person.name = "Person"      # For testing only.
            extra_types = build_type_dict([ parse_person ])
            schema = "Optional: {person:Person?}"
            parser = parse.Parser(schema, extra_types)

            # -- PERFORM TESTS:
            self.assert_match(parser, "Optional: ",      "person", None)
            self.assert_match(parser, "Optional: Alice", "person", "Alice")
            self.assert_match(parser, "Optional: Bob",   "person", "Bob")

            # -- PARSE MISMATCH:
            self.assert_mismatch(parser, "Optional: Anna", "person")  # Similar1.
            self.assert_mismatch(parser, "Optional: Boby", "person")  # Similar2.
            self.assert_mismatch(parser, "Optional: a",    "person")  # INVALID ...
Exemplo n.º 27
0
    def test_make_choice__with_transform(self):
        transform = lambda x: x.upper()
        parse_choice = TypeBuilder.make_choice(["ONE", "two", "Three"],
                                               transform)
        self.assertSequenceEqual(parse_choice.choices, ["ONE", "TWO", "THREE"])
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))

        # -- PERFORM TESTS:
        self.assert_match(parser, "Answer: one", "answer", "ONE")
        self.assert_match(parser, "Answer: two", "answer", "TWO")
        self.ensure_can_parse_all_choices(parser,
                    parse_choice, "Answer: %s", "answer")

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __one__", "answer")
        self.assert_mismatch(parser, "Answer: one ",    "answer")
        self.assert_mismatch(parser, "Answer: one ZZZ", "answer")
Exemplo n.º 28
0
    def test_make_choice2(self):
        parse_choice2 = TypeBuilder.make_choice2(["zero", "one", "two"])
        parse_choice2.name = "NumberWordChoice2"
        extra_types = self.build_type_dict([ parse_choice2 ])
        schema = "Answer: {answer:NumberWordChoice2}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "Answer: zero", "answer", (0, "zero"))
        self.assert_match(parser, "Answer: one",  "answer", (1, "one"))
        self.assert_match(parser, "Answer: two",  "answer", (2, "two"))
        self.ensure_can_parse_all_choices2(parser,
                parse_choice2, "Answer: %s", "answer")

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __one__", "answer")
        self.assert_mismatch(parser, "Answer: one ",    "answer")
        self.assert_mismatch(parser, "Answer: one ZZZ", "answer")
Exemplo n.º 29
0
        def test_without_cardinality_field(self):
            # -- IMPLCIT CARDINALITY: one
            # -- SETUP:
            parse_person = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
            parse_person.name = "Person"      # For testing only.
            extra_types = build_type_dict([ parse_person ])
            schema = "One: {person:Person}"
            parser = parse.Parser(schema, extra_types)

            # -- PERFORM TESTS:
            self.assert_match(parser, "One: Alice", "person", "Alice")
            self.assert_match(parser, "One: Bob",   "person", "Bob")

            # -- PARSE MISMATCH:
            self.assert_mismatch(parser, "One: ", "person")        # Missing.
            self.assert_mismatch(parser, "One: BAlice", "person")  # Similar1.
            self.assert_mismatch(parser, "One: Boby", "person")    # Similar2.
            self.assert_mismatch(parser, "One: a",    "person")    # INVALID ...
Exemplo n.º 30
0
    def test_make_choice__with_transform(self):
        transform = lambda x: x.upper()
        parse_choice = TypeBuilder.make_choice(["ONE", "two", "Three"],
                                               transform)
        self.assertSequenceEqual(parse_choice.choices, ["ONE", "TWO", "THREE"])
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))

        # -- PERFORM TESTS:
        self.assert_match(parser, "Answer: one", "answer", "ONE")
        self.assert_match(parser, "Answer: two", "answer", "TWO")
        self.ensure_can_parse_all_choices(parser, parse_choice, "Answer: %s",
                                          "answer")

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __one__", "answer")
        self.assert_mismatch(parser, "Answer: one ", "answer")
        self.assert_mismatch(parser, "Answer: one ZZZ", "answer")
Exemplo n.º 31
0
        def test_without_cardinality_field(self):
            # -- IMPLCIT CARDINALITY: one
            # -- SETUP:
            parse_person = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
            parse_person.name = "Person"  # For testing only.
            extra_types = build_type_dict([parse_person])
            schema = "One: {person:Person}"
            parser = parse.Parser(schema, extra_types)

            # -- PERFORM TESTS:
            self.assert_match(parser, "One: Alice", "person", "Alice")
            self.assert_match(parser, "One: Bob", "person", "Bob")

            # -- PARSE MISMATCH:
            self.assert_mismatch(parser, "One: ", "person")  # Missing.
            self.assert_mismatch(parser, "One: BAlice", "person")  # Similar1.
            self.assert_mismatch(parser, "One: Boby", "person")  # Similar2.
            self.assert_mismatch(parser, "One: a", "person")  # INVALID ...
Exemplo n.º 32
0
    def test_make_choice2__samecase_match_or_error(self):
        # -- NOTE: strict=True => Enable errors due to case-mismatch.
        parse_choice2 = TypeBuilder.make_choice2(["Zero", "one", "TWO"],
                                                 strict=True)
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice2))

        # -- PERFORM TESTS: Case matches.
        # NOTE: Parser uses re.IGNORECASE flag => Any case accepted.
        self.assert_match(parser, "Answer: Zero", "answer", (0, "Zero"))
        self.assert_match(parser, "Answer: one",  "answer", (1, "one"))
        self.assert_match(parser, "Answer: TWO",  "answer", (2, "TWO"))

        # -- PERFORM TESTS: EXACT-CASE MISMATCH
        case_mismatch_input_data = ["zero", "ZERO", "One", "ONE", "two" ]
        for input_value in case_mismatch_input_data:
            input_text = "Answer: %s" % input_value
            with self.assertRaises(ValueError):
                parser.parse(input_text)
Exemplo n.º 33
0
    def test_make_choice2__samecase_match_or_error(self):
        # -- NOTE: strict=True => Enable errors due to case-mismatch.
        parse_choice2 = TypeBuilder.make_choice2(["Zero", "one", "TWO"],
                                                 strict=True)
        schema = "Answer: {answer:NumberWordChoice}"
        parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice2))

        # -- PERFORM TESTS: Case matches.
        # NOTE: Parser uses re.IGNORECASE flag => Any case accepted.
        self.assert_match(parser, "Answer: Zero", "answer", (0, "Zero"))
        self.assert_match(parser, "Answer: one", "answer", (1, "one"))
        self.assert_match(parser, "Answer: TWO", "answer", (2, "TWO"))

        # -- PERFORM TESTS: EXACT-CASE MISMATCH
        case_mismatch_input_data = ["zero", "ZERO", "One", "ONE", "two"]
        for input_value in case_mismatch_input_data:
            input_text = "Answer: %s" % input_value
            with self.assertRaises(ValueError):
                parser.parse(input_text)
    def test_parse_with_optional_and_named_fields(self):
        parse_opt_number = TypeBuilder.with_optional(parse_number)
        parse_opt_number.name = "Number?"

        type_dict = build_type_dict([parse_opt_number, parse_number])
        schema = "Numbers: {number1:Number?} {number2:Number}"
        parser = parse.Parser(schema, type_dict)

        # -- CASE: Optional number is present
        result = parser.parse("Numbers: 34 12")
        expected = dict(number1=34, number2=12)
        self.assertIsNotNone(result)
        self.assertEqual(result.named, expected)

        # -- CASE: Optional number is missing
        result = parser.parse("Numbers:  12")
        expected = dict(number1=None, number2=12)
        self.assertIsNotNone(result)
        self.assertEqual(result.named, expected)
Exemplo n.º 35
0
    def test_with_one_or_more(self):
        parse_numbers = TypeBuilder.with_one_or_more(parse_number)
        parse_numbers.name = "Numbers"

        extra_types = self.build_type_dict([ parse_numbers ])
        schema = "List: {numbers:Numbers}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "List: 1",       "numbers", [ 1 ])
        self.assert_match(parser, "List: 1, 2",    "numbers", [ 1, 2 ])
        self.assert_match(parser, "List: 1, 2, 3", "numbers", [ 1, 2, 3 ])

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "List: ",   "numbers")  # Zero items.
        self.assert_mismatch(parser, "List: x",  "numbers")  # Not a Number.
        self.assert_mismatch(parser, "List: -1", "numbers")  # Negative.
        self.assert_mismatch(parser, "List: 1,", "numbers")  # Trailing sep.
        self.assert_mismatch(parser, "List: a, b", "numbers") # List of ...
Exemplo n.º 36
0
        def test_cardinality_field_with_one_or_more(self):
            # -- SETUP:
            parse_person = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
            parse_person.name = "Person"      # For testing only.
            extra_types = build_type_dict([ parse_person ])
            schema = "List: {persons:Person+}"
            parser = parse.Parser(schema, extra_types)

            # -- PERFORM TESTS:
            self.assert_match(parser, "List: Alice", "persons", [ "Alice" ])
            self.assert_match(parser, "List: Bob",   "persons", [ "Bob" ])
            self.assert_match(parser, "List: Bob, Alice",
                                      "persons", [ "Bob", "Alice" ])

            # -- PARSE MISMATCH:
            self.assert_mismatch(parser, "List: ",       "persons")  # Zero items.
            self.assert_mismatch(parser, "List: BAlice", "persons")  # Unknown1.
            self.assert_mismatch(parser, "List: Boby",   "persons")  # Unknown2.
            self.assert_mismatch(parser, "List: Alice,", "persons")  # Trailing,
            self.assert_mismatch(parser, "List: a, b",   "persons")  # List of...
    def test_parse_with_many_and_unnamed_fields(self):
        # -- ENSURE: Cardinality.one_or_more.group_count is correct
        # REQUIRES: ParserExt := parse_type.Parser with group_count support
        parse_many_numbers = TypeBuilder.with_many(parse_number)
        parse_many_numbers.name = "Number+"

        type_dict = build_type_dict([parse_many_numbers, parse_number])
        schema = "Numbers: {:Number+} {:Number}"
        parser = ParserExt(schema, type_dict)

        # -- CASE:
        result = parser.parse("Numbers: 1, 2, 3 42")
        expected = ([1, 2, 3], 42)
        self.assertIsNotNone(result)
        self.assertEqual(result.fixed, tuple(expected))

        result = parser.parse("Numbers: 3 43")
        expected = ([ 3 ], 43)
        self.assertIsNotNone(result)
        self.assertEqual(result.fixed, tuple(expected))
Exemplo n.º 38
0
    def test_make_variant__with_strict_and_compiled_raises_error_on_case_mismatch(
            self):
        # XXX re_opts=0 seems to work differently.
        # -- NEEDS:
        #  * re_opts=0 (IGNORECASE disabled)
        #  * strict=True, allow that an error is raised
        type_converters = [parse_number, parse_yesno]
        # -- ENSURE: coverage for cornercase.
        parse_number.matcher = re.compile(parse_number.pattern)

        parse_variant = TypeBuilder.make_variant(type_converters,
                                                 compiled=True,
                                                 re_opts=0,
                                                 strict=True)
        schema = "Variant: {variant:YesNo_or_Number}"
        parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
        # XXX self.assertRaises(AssertionError,  parser.parse, "Variant: YES")
        result = parser.parse("Variant: Yes")
        self.assertNotEqual(result, None)
        self.assertEqual(result["variant"], True)
Exemplo n.º 39
0
        def test_cardinality_field_with_zero_or_one(self):
            # -- SETUP:
            parse_person = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
            parse_person.name = "Person"  # For testing only.
            extra_types = build_type_dict([parse_person])
            schema = "Optional: {person:Person?}"
            parser = parse.Parser(schema, extra_types)

            # -- PERFORM TESTS:
            self.assert_match(parser, "Optional: ", "person", None)
            self.assert_match(parser, "Optional: Alice", "person", "Alice")
            self.assert_match(parser, "Optional: Bob", "person", "Bob")

            # -- PARSE MISMATCH:
            self.assert_mismatch(parser, "Optional: Anna",
                                 "person")  # Similar1.
            self.assert_mismatch(parser, "Optional: Boby",
                                 "person")  # Similar2.
            self.assert_mismatch(parser, "Optional: a",
                                 "person")  # INVALID ...
Exemplo n.º 40
0
    def test_make_choice2(self):
        # -- strict=False: Disable errors due to case mismatch.
        parse_choice2 = TypeBuilder.make_choice2(["zero", "one", "two"],
                                                 strict=False)
        parse_choice2.name = "NumberWordChoice2"
        extra_types = build_type_dict([parse_choice2])
        schema = "Answer: {answer:NumberWordChoice2}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.assert_match(parser, "Answer: zero", "answer", (0, "zero"))
        self.assert_match(parser, "Answer: one", "answer", (1, "one"))
        self.assert_match(parser, "Answer: two", "answer", (2, "two"))
        self.ensure_can_parse_all_choices2(parser, parse_choice2, "Answer: %s",
                                           "answer")

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __one__", "answer")
        self.assert_mismatch(parser, "Answer: one ", "answer")
        self.assert_mismatch(parser, "Answer: one ZZZ", "answer")
    def test_parse_with_optional_and_unnamed_fields(self):
        # -- ENSURE: Cardinality.optional.group_count is correct
        # REQUIRES: ParserExt := parse_type.Parser with group_count support
        parse_opt_number = TypeBuilder.with_optional(parse_number)
        parse_opt_number.name = "Number?"

        type_dict = build_type_dict([parse_opt_number, parse_number])
        schema = "Numbers: {:Number?} {:Number}"
        parser = ParserExt(schema, type_dict)

        # -- CASE: Optional number is present
        result = parser.parse("Numbers: 34 12")
        expected = (34, 12)
        self.assertIsNotNone(result)
        self.assertEqual(result.fixed, tuple(expected))

        # -- CASE: Optional number is missing
        result = parser.parse("Numbers:  12")
        expected = (None, 12)
        self.assertIsNotNone(result)
        self.assertEqual(result.fixed, tuple(expected))
    def test_parse_with_many0_and_unnamed_fields(self):
        # -- ENSURE: Cardinality.zero_or_more.group_count is correct
        # REQUIRES: ParserExt := parse_type.Parser with group_count support
        parse_many0_numbers = TypeBuilder.with_many0(parse_number)
        parse_many0_numbers.name = "Number*"

        type_dict = build_type_dict([parse_many0_numbers, parse_number])
        schema = "Numbers: {:Number*} {:Number}"
        parser = ParserExt(schema, type_dict)

        # -- CASE: Optional numbers are present
        result = parser.parse("Numbers: 1, 2, 3 42")
        expected = ([1, 2, 3], 42)
        self.assertIsNotNone(result)
        self.assertEqual(result.fixed, tuple(expected))

        # -- CASE: Optional numbers are missing := EMPTY-LIST
        result = parser.parse("Numbers:  43")
        expected = ([ ], 43)
        self.assertIsNotNone(result)
        self.assertEqual(result.fixed, tuple(expected))
Exemplo n.º 43
0
        def test_cardinality_field_with_one_or_more(self):
            # -- SETUP:
            parse_person = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
            parse_person.name = "Person"  # For testing only.
            extra_types = build_type_dict([parse_person])
            schema = "List: {persons:Person+}"
            parser = parse.Parser(schema, extra_types)

            # -- PERFORM TESTS:
            self.assert_match(parser, "List: Alice", "persons", ["Alice"])
            self.assert_match(parser, "List: Bob", "persons", ["Bob"])
            self.assert_match(parser, "List: Bob, Alice", "persons",
                              ["Bob", "Alice"])

            # -- PARSE MISMATCH:
            self.assert_mismatch(parser, "List: ", "persons")  # Zero items.
            self.assert_mismatch(parser, "List: BAlice",
                                 "persons")  # Unknown1.
            self.assert_mismatch(parser, "List: Boby", "persons")  # Unknown2.
            self.assert_mismatch(parser, "List: Alice,",
                                 "persons")  # Trailing,
            self.assert_mismatch(parser, "List: a, b", "persons")  # List of...
Exemplo n.º 44
0
    def test_make_enum(self):
        parse_nword = TypeBuilder.make_enum({"one": 1, "two": 2, "three": 3})
        parse_nword.name = "NumberAsWord"

        extra_types = self.build_type_dict([ parse_nword ])
        schema = "Answer: {number:NumberAsWord}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.ensure_can_parse_all_enum_values(parser,
            parse_nword, "Answer: %s", "number")

        # -- VALID:
        self.assert_match(parser, "Answer: one", "number", 1)
        self.assert_match(parser, "Answer: two", "number", 2)

        # -- IGNORE-CASE: In parsing, calls type converter function !!!
        self.assert_match(parser, "Answer: THREE", "number", 3)

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __one__", "number")
        self.assert_mismatch(parser, "Answer: one ",    "number")
        self.assert_mismatch(parser, "Answer: one_",    "number")
        self.assert_mismatch(parser, "Answer: one ZZZ", "number")
Exemplo n.º 45
0
    def test_make_enum_with_dict(self):
        parse_nword = TypeBuilder.make_enum({"one": 1, "two": 2, "three": 3})
        parse_nword.name = "NumberAsWord"

        extra_types = build_type_dict([parse_nword])
        schema = "Answer: {number:NumberAsWord}"
        parser = parse.Parser(schema, extra_types)

        # -- PERFORM TESTS:
        self.ensure_can_parse_all_enum_values(parser, parse_nword,
                                              "Answer: %s", "number")

        # -- VALID:
        self.assert_match(parser, "Answer: one", "number", 1)
        self.assert_match(parser, "Answer: two", "number", 2)

        # -- IGNORE-CASE: In parsing, calls type converter function !!!
        self.assert_match(parser, "Answer: THREE", "number", 3)

        # -- PARSE MISMATCH:
        self.assert_mismatch(parser, "Answer: __one__", "number")
        self.assert_mismatch(parser, "Answer: one ", "number")
        self.assert_mismatch(parser, "Answer: one_", "number")
        self.assert_mismatch(parser, "Answer: one ZZZ", "number")
 def test_with_cardinality_zero_or_one(self):
     parse_opt_number = TypeBuilder.with_cardinality(
             Cardinality.zero_or_one, parse_number)
     self.check_parse_number_with_zero_or_one(parse_opt_number)
 def test_with_cardinality_one(self):
     parse_number2 = TypeBuilder.with_cardinality(Cardinality.one, parse_number)
     assert parse_number2 is parse_number
 def test_with_cardinality_one_or_more(self):
     parse_many_numbers = TypeBuilder.with_cardinality(
             Cardinality.one_or_more, parse_number)
     self.check_parse_number_with_one_or_more(parse_many_numbers)
 def test_with_cardinality_optional(self):
     parse_opt_number = TypeBuilder.with_cardinality(
             Cardinality.optional, parse_number)
     self.check_parse_number_with_optional(parse_opt_number)
 def test_with_cardinality_many(self):
     parse_many_numbers = TypeBuilder.with_cardinality(
             Cardinality.many, parse_number)
     self.check_parse_number_with_many(parse_many_numbers)
 def test_with_cardinality_many0(self):
     parse_many0_numbers = TypeBuilder.with_cardinality(
             Cardinality.many0, parse_number)
     self.check_parse_number_with_zero_or_more(parse_many0_numbers)