Example #1
0
 def test_if_values_in_value_are_tuples_not_all_weights_are_numbers(self):
     """ If value is a tuple of less than 2 terms, it raises an error. """
     with self.assertRaises(ValueError):
         grammar.Or((
             (grammar.SingleTerm('terminal'), 2),
             (grammar.SingleTerm('terminal'), '8'),
         ))
Example #2
0
 def test_correct_construction(self):
     """ If value is correct, the term is correctly initialized. """
     value = (
         grammar.SingleTerm('terminal1'),
         'terminal2'
     )
     and_connector = grammar.And(*value)
     self.assertEquals(value[0], and_connector.value[0])
     self.assertEquals(grammar.SingleTerm(value[1]), and_connector.value[1])
Example #3
0
    def test_correct_construction_without_weights(self):
        """ If value correct without weights, term is correctly initialized. """
        value = (
            grammar.SingleTerm('t1'),
            't2',
        )

        or_connector = grammar.Or(*value)
        self.assertEquals(value[0], or_connector.value[0])
        self.assertEquals(grammar.SingleTerm(value[1]), or_connector.value[1])
        self.assertEquals(tuple(1 for _ in value), or_connector.weights)
Example #4
0
    def test_correct_construction_with_weights(self):
        """ If value correct with weights, term is correctly initialized. """
        value_and_weights = (
            (grammar.SingleTerm('t1'), 1),
            ('t2', 3),
        )
        value = tuple(t[0] for t in value_and_weights)
        weights = tuple(t[1] for t in value_and_weights)

        or_connector = grammar.Or(*value_and_weights)
        self.assertEquals(value[0], or_connector.value[0])
        self.assertEquals(grammar.SingleTerm(value[1]), or_connector.value[1])
        self.assertEquals(weights, or_connector.weights)
Example #5
0
    def test_repeated_productions_are_shrunk(self):
        """ If two or more productions has the same alpha, they're shrunk.

        This implies that if two or more productions has the same alpha, they're
        transformed in a single production with all the beta parts of the
        productions separated by Or's.
        """
        start_symbol = 'expr'
        productions = [
            grammar.Production(
                'expr',
                grammar.SingleTerm(str(i))
            ) for i in range(10)
            ]
        a_grammar = grammar.Grammar(start_symbol, productions)

        # The ten productions are transformed in One
        self.assertEquals(len(a_grammar.productions), 1)
        # The only term of the productions is an Or instance
        production = a_grammar.productions[0]
        self.assertEquals(start_symbol, production.variable)
        self.assertIsInstance(production.term, grammar.Or)
        # The Or term contains all the terms in initial productions.
        value = production.term.value
        for t in value:
            self.assertIn(t, [p.term for p in productions])
        for t in [p.term for p in productions]:
            self.assertIn(t, value)
Example #6
0
 def test_not_all_values_in_value_tuple_are_not_term_string_or_tuple(self):
     """ If value is a tuple of less than 2 terms, it raises an error. """
     invalid_values = (
         (grammar.SingleTerm('terminal'), 'a nice dog', 9),
         (1, 2, 3, 4, 5),
     )
     for value in invalid_values:
         with self.assertRaises(ValueError):
             grammar.Or(value)
Example #7
0
 def test_not_all_values_in_value_tuple_are_terms_or_strings(self):
     """ If elements in value are not Term, an error is raised. """
     invalid_values = (
         (grammar.SingleTerm('terminal'), 'a nice dog', 9),
         (1, 2, 3, 4, 5),
     )
     for value in invalid_values:
         with self.assertRaises(ValueError):
             grammar.And(value)
Example #8
0
 def test_value_is_a_tuple_of_less_than_two_terms(self):
     """ If value is a tuple of less than 2 terms, it raises an error. """
     invalid_values = (
         tuple(),
         grammar.SingleTerm('terminal'),
     )
     for value in invalid_values:
         with self.assertRaises(ValueError):
             grammar.Or(value)
Example #9
0
 def test_different_instances_with_same_content_are_equal(self):
     """ If two different instances has the same content are equal. """
     value = (
         grammar.SingleTerm('terminal1'),
         'terminal2'
     )
     self.assertEquals(
         grammar.And(*value),
         grammar.And(*value),
     )
Example #10
0
 def test_value_is_not_a_term_or_is_a_multiple_term(self):
     """ Param value should be a term other than Multiplier instance. """
     for invalid_value in (
         [],
         (),
         {},
         None,
         grammar.Multiplier(grammar.SingleTerm('t')),
     ):
         with self.assertRaises(ValueError):
             grammar.Multiplier(invalid_value)
Example #11
0
 def test_correct_construction_with_term_with_lower_upper_and_p(self):
     """ Correct construction with all params and a term instance. """
     value = grammar.SingleTerm('t')
     lower = 1
     upper = 7
     p = 0.75
     multiplier = grammar.Multiplier(value, lower=lower, upper=upper, p=p)
     self.assertEquals(value, multiplier.value)
     self.assertEquals(lower, multiplier.lower)
     self.assertEquals(upper, multiplier.upper)
     self.assertAlmostEqual(p, multiplier.p)
Example #12
0
    def test_correct_construction_with_term_without_anything(self):
        """ Correct construction without optional params and a term instance.

        That means that "lower", "upper" and "p" take the correct values.
        """
        value = grammar.SingleTerm('t')
        multiplier = grammar.Multiplier(value)
        self.assertEquals(value, multiplier.value)
        self.assertEquals(self.__default_lower, multiplier.lower)
        self.assertEquals(self.__default_upper, multiplier.upper)
        self.assertAlmostEqual(self.__default_p, multiplier.p)
Example #13
0
    def test_correct_construction_with_term_with_only_p(self):
        """ Correct construction with only "p" param and a term instance.

        That means that "lower" and "upper" take the correct values.
        """
        value = grammar.SingleTerm('t')
        p = 0.2
        multiplier = grammar.Multiplier(value, p=p)
        self.assertEquals(value, multiplier.value)
        self.assertEquals(self.__default_lower, multiplier.lower)
        self.assertEquals(self.__default_upper, multiplier.upper)
        self.assertAlmostEqual(p, multiplier.p)
Example #14
0
 def test_p_should_be_an_int_or_float(self):
     """ Value p should be a number between 0.0 and 1.0 both incl. """
     for invalid_p in (
         [],
         (),
         {},
         None,
         '7',
         -0.1,
         1.1,
     ):
         with self.assertRaises(ValueError):
             grammar.Multiplier(grammar.SingleTerm('t'), p=invalid_p)
Example #15
0
    def test_correct_construction_with_string_with_only_upper(self):
        """ Correct construction with only "upper" param and a string term.

        That means that "lower" and "p" take the correct values.
        """
        str_value = 't'
        value = grammar.SingleTerm(str_value)
        upper = 1
        multiplier = grammar.Multiplier(str_value, upper=upper)
        self.assertEquals(value, multiplier.value)
        self.assertEquals(self.__default_lower, multiplier.lower)
        self.assertEquals(upper, multiplier.upper)
        self.assertAlmostEqual(self.__default_p, multiplier.p)
Example #16
0
    def test_correct_construction_with_term_with_lower_and_upper(self):
        """ Correct construction with only "lower" and "upper" params and a
        term instance.

        That means that "p" takes the correct value.
        """
        value = grammar.SingleTerm('t')
        lower = 3
        upper = 5
        multiplier = grammar.Multiplier(value, lower=lower, upper=upper)
        self.assertEquals(value, multiplier.value)
        self.assertEquals(lower, multiplier.lower)
        self.assertEquals(upper, multiplier.upper)
        self.assertAlmostEqual(self.__default_p, multiplier.p)
Example #17
0
    def test_correct_construction_with_string_with_lower_and_p(self):
        """ Correct construction with only "lower" and "p" param and a
        string term.

        That means that "upper" takes the correct value.
        """
        str_value = 't'
        value = grammar.SingleTerm(str_value)
        lower = 7
        p = 0.75
        multiplier = grammar.Multiplier(str_value, lower=lower, p=p)
        self.assertEquals(value, multiplier.value)
        self.assertEquals(lower, multiplier.lower)
        self.assertEquals(self.__default_upper, multiplier.upper)
        self.assertAlmostEqual(p, multiplier.p)
Example #18
0
 def test_correct_construction_with_string_with_lower_upper_and_p(self):
     """ Correct construction with all optional params and a string term. """
     str_value = 't'
     value = grammar.SingleTerm(str_value)
     lower = 1
     upper = 7
     p = 0.75
     multiplier = grammar.Multiplier(
         str_value,
         lower=lower,
         upper=upper,
         p=p
     )
     self.assertEquals(value, multiplier.value)
     self.assertEquals(lower, multiplier.lower)
     self.assertEquals(upper, multiplier.upper)
     self.assertAlmostEqual(p, multiplier.p)
Example #19
0
 def test_value_is_not_a_tuple(self):
     """ If value is not a tuple, it raises an error. """
     with self.assertRaises(ValueError):
         grammar.Or(grammar.SingleTerm('terminal'))
Example #20
0
 def test_value_is_not_a_string(self):
     """ Param "value" should be a string. """
     for invalid_value in ([], (), {}, None, grammar.SingleTerm('hello')):
         with self.assertRaises(ValueError):
             grammar.SingleTerm(invalid_value)
Example #21
0
 def test_different_instances_with_same_content_are_equal(self):
     """ If two different instances has the same content are equal. """
     self.assertEquals(
         grammar.SingleTerm('t'),
         grammar.SingleTerm('t'),
     )
Example #22
0
 def test_correct_value_as_a_string(self):
     """ If value is a string, the term initializes correctly. """
     string_value = 'terminal'
     self.assertEquals(string_value, grammar.SingleTerm(string_value).value)