Ejemplo n.º 1
0
 def test_checking_logical_support(self):
     """Sets don't have logical support."""
     op = Set(Number(9), String("carla"))
     op.check_logical_support()
     ok_(op(None))
     ok_(Set(Number(0))(None))
     ok_(not Set()(None))
Ejemplo n.º 2
0
 def test_representation_without_namespace(self):
     # With Unicode:
     func = PlaceholderFunction(u"aquí", None, Number(1), String("hi"))
     eq_('<Placeholder function call "aquí"(<Number 1.0>, <String "hi">)>',
         repr(func))
     # With ASCII:
     func = PlaceholderFunction("here", None, Number(1), String("hi"))
     eq_(u'<Placeholder function call "here"(<Number 1.0>, <String "hi">)>',
         repr(func))
Ejemplo n.º 3
0
 def test_instantiation(self):
     """All the members of a set must be operands"""
     # Instantiation with operands:
     Set(Number(3), String("hola"))
     # Instantiation with a non-operand value:
     try:
         Set(Number(3), "hola")
         assert False, "InvalidOperationError exception not raised"
     except InvalidOperationError as exc:
         assert 'Item "hola" is not an operand' in six.text_type(exc)
Ejemplo n.º 4
0
 def test_string_without_symbol_table(self):
     # With ASCII characters:
     bind1 = Bind("pi", Number(3.1416))
     bind1_as_unicode = six.text_type(bind1)
     eq_(bind1_as_unicode, 'Operand 3.1416 bound as "pi"')
     eq_(bind1_as_unicode, six.text_type(bind1))
     # With non-ASCII characters:
     bind2 = Bind(u"pí", Number(3.1416))
     bind2_as_unicode = six.text_type(bind2)
     eq_(bind2_as_unicode, u'Operand 3.1416 bound as "pí"')
     eq_(six.text_type(bind2), 'Operand 3.1416 bound as "pí"')
Ejemplo n.º 5
0
 def test_representation_with_namespace(self):
     # With Unicode:
     func = PlaceholderFunction(u"aquí", ["a", "d"], Number(1), String("hi"))
     eq_('<Placeholder function call "aquí"(<Number 1.0>, <String "hi">) ' \
         'at namespace="a:d">',
         repr(func))
     # With ASCII:
     func = PlaceholderFunction("here", ["a", "d"], Number(1), String("hi"))
     eq_(u'<Placeholder function call "here"(<Number 1.0>, <String "hi">) ' \
         'at namespace="a:d">',
         repr(func))
Ejemplo n.º 6
0
 def test_constructor_with_many_arguments(self):
     PermissiveFunction(
         Number(0),
         Number(1),
         Number(2),
         Number(3),
         Number(4),
         Number(5),
         Number(6),
         Number(7),
         Number(8),
         Number(9),
     )
Ejemplo n.º 7
0
 def test_less_than(self):
     # With an integer constant:
     op = Number(10)
     ok_(op.less_than(11, None))
     ok_(op.less_than(10.00001, None))
     assert_false(op.less_than(9.99999, None))
     # With a float constant:
     op = Number(10.00)
     ok_(op.less_than(11, None))
     ok_(op.less_than(10.00001, None))
     assert_false(op.less_than(9.99999, None))
     # With everything but a number:
     assert_raises(InvalidOperationError, op.less_than, "ten", None)
Ejemplo n.º 8
0
    def test_checking_duplicate_object_global_names(self):
        """
        Two objects cannot have the same global names in the same table.

        """
        st = SymbolTable(
            "global",
            (
                Bind("e", Number(2.7183), es_VE=u"número e"),
                Bind("pi", Number(3.1416)),
                Bind("e", Number(2.71828), es_ES=u"número e"),
            ),
        )
        assert_raises(ScopeError, st.validate_scope)
Ejemplo n.º 9
0
 def test_representation(self):
     set1 = Set(Number(3), Number(5))
     try:
         eq_(repr(set1), "<Set <Number 3.0>, <Number 5.0>>")
     except AssertionError:
         eq_(repr(set1), "<Set <Number 5.0>, <Number 3.0>>")
     # Now with an empty set:
     set2 = Set()
     eq_(repr(set2), "<Set>")
     # Now with Unicode stuff in it:
     set3 = Set(String(u"España"), String(u"carabobeño"))
     try:
         eq_(repr(set3), '<Set <String "España">, <String "carabobeño">>')
     except AssertionError:
         eq_(repr(set3), '<Set <String "carabobeño">, <String "España">>')
Ejemplo n.º 10
0
 def test_string_with_symbol_table(self):
     # With ASCII characters:
     bind1 = Bind("pi", Number(3.1416))
     SymbolTable("global", [bind1])
     bind1_as_unicode = six.text_type(bind1)
     eq_('Operand 3.1416 bound as "pi" (in Symbol table global)',
         bind1_as_unicode)
     eq_(six.text_type(bind1), bind1_as_unicode)
     # With non-ASCII characters:
     bind2 = Bind(u"pí", Number(3.1416))
     SymbolTable("global", [bind2])
     bind2_as_unicode = six.text_type(bind2)
     eq_(u'Operand 3.1416 bound as "pí" (in Symbol table global)',
         bind2_as_unicode)
     eq_('Operand 3.1416 bound as "pí" (in Symbol table global)',
         six.text_type(bind2))
Ejemplo n.º 11
0
    def test_constructor(self):
        """
        Placeholder functions should contain an attribute which represents the
        name of the function in question and another one which represents the
        arguments passed.

        """
        func1 = PlaceholderFunction(u"PAÍS", None)
        eq_(func1.name, u"país")
        eq_(func1.arguments, ())

        func2 = PlaceholderFunction("country", None,
                                    PlaceholderFunction("city", None),
                                    Number(2))
        eq_(func2.name, "country")
        eq_(func2.arguments, (PlaceholderFunction("city", None), Number(2)))
Ejemplo n.º 12
0
 def test_constructor_with_minimum_arguments(self):
     func = PermissiveFunction(String("this-is-arg0"))
     args = {
         'arg0': String("this-is-arg0"),
         'oarg0': Set(),
         'oarg1': Number(1),
     }
     eq_(func.arguments, args)
Ejemplo n.º 13
0
 def test_constructor_with_one_optional_argument(self):
     func = PermissiveFunction(String("this-is-arg0"),
                               String("this-is-oarg0"))
     args = {
         'arg0': String("this-is-arg0"),
         'oarg0': String("this-is-oarg0"),
         'oarg1': Number(1),
     }
     eq_(func.arguments, args)
Ejemplo n.º 14
0
 def test_checking_valid_table(self):
     st = SymbolTable(
         "global",
         # Bindings/global objects:
         (
             Bind("bool", BoolVar(), es="booleano"),
             Bind("traffic", TrafficLightVar(), es=u"tráfico"),
         ),
         # Sub-tables:
         SymbolTable(
             "maths",
             (
                 Bind("pi", Number(3.1416)),
                 Bind("e", Number(2.7183)),
             ),
         ),
     )
     eq_(st.validate_scope(), None)
Ejemplo n.º 15
0
 def test_equality(self):
     op = Set(Number(3), String("hola"))
     set1 = {3, "hola"}
     set2 = {3}
     set3 = {3, "hola", "something else"}
     set4 = {"nothing to do"}
     # Comparing them...
     ok_(op.equals(set1, None), "The constant equals %s" % op.constant_value)
     assert_false(op.equals(set2, None))
     assert_false(op.equals(set3, None))
     assert_false(op.equals(set4, None))
Ejemplo n.º 16
0
 def test_parsing_with_localized_grammars(self):
     castilian_grammar = Grammar(decimal_separator=",",
                                 thousands_separator=".")
     mgr = ConvertibleParseManager(Grammar(), es=castilian_grammar)
     parse_tree = mgr.parse(u"tráfico:peatones_cruzando_calle <= 3,00",
                            "es")
     expected_tree = ConvertibleParseTree(
         LessEqual(
             PlaceholderVariable("peatones_cruzando_calle", (u"tráfico", )),
             Number(3.0)))
     eq_(parse_tree, expected_tree)
Ejemplo n.º 17
0
 def test_parsing_with_localized_grammars(self):
     castilian_grammar = Grammar(decimal_separator=",",
                                 thousands_separator=".")
     mgr = EvaluableParseManager(self.symbol_table,
                                 Grammar(),
                                 es=castilian_grammar)
     parse_tree = mgr.parse(u"tráfico:peatones_cruzando_calle <= 3,00",
                            "es")
     expected_tree = EvaluableParseTree(
         LessEqual(PedestriansCrossingRoad(), Number(3.0)))
     eq_(parse_tree, expected_tree)
Ejemplo n.º 18
0
 def test_adding_grammar(self):
     """It should be possible to add grammars after instantiation."""
     castilian_grammar = Grammar(decimal_separator=",",
                                 thousands_separator=".")
     mgr = EvaluableParseManager(self.symbol_table, Grammar())
     mgr.add_parser("es", castilian_grammar)
     parse_tree = mgr.parse(u'tráfico:peatones_cruzando_calle <= 3,00',
                            "es")
     expected_tree = EvaluableParseTree(
         LessEqual(PedestriansCrossingRoad(), Number(3.0)))
     eq_(parse_tree, expected_tree)
Ejemplo n.º 19
0
 def test_non_operands_as_arguments(self):
     """Placeholder functions reject non-operands as arguments."""
     assert_raises(BadCallError, PlaceholderFunction, "func", (),
                   Number(3), Number(6), 1)
     assert_raises(BadCallError, PlaceholderFunction, "func", (), 2,
                   Number(6), Number(3))
     assert_raises(BadCallError, PlaceholderFunction, "func", (),
                   Number(6), 3, Number(3))
Ejemplo n.º 20
0
    def test_equivalence(self):
        """
        Two constant sets A and B are equivalent if each element in A is
        equivalent to one element in B.

        """
        set1 = Set(String("hi"), Set(Number(3), String("hello")))
        set2 = Set(String("hi"), Number(3), String("hello"))
        set3 = Set(Set(String("hello"), Number(3)), String("hi"))
        set4 = Set(String("hi"), String("hello"))

        set1.check_equivalence(set3)
        set3.check_equivalence(set1)
        assert_raises(AssertionError, set1.check_equivalence, set2)
        assert_raises(AssertionError, set1.check_equivalence, set4)
        assert_raises(AssertionError, set2.check_equivalence, set1)
        assert_raises(AssertionError, set2.check_equivalence, set3)
        assert_raises(AssertionError, set2.check_equivalence, set4)
        assert_raises(AssertionError, set3.check_equivalence, set2)
        assert_raises(AssertionError, set3.check_equivalence, set4)
        assert_raises(AssertionError, set4.check_equivalence, set1)
        assert_raises(AssertionError, set4.check_equivalence, set2)
        assert_raises(AssertionError, set4.check_equivalence, set3)

        ok_(set1 == set3)
        ok_(set3 == set1)
        ok_(set1 != set2)
        ok_(set1 != set4)
        ok_(set2 != set1)
        ok_(set2 != set3)
        ok_(set2 != set4)
        ok_(set3 != set2)
        ok_(set3 != set4)
        ok_(set4 != set1)
        ok_(set4 != set2)
        ok_(set4 != set3)
Ejemplo n.º 21
0
    def test_equivalence(self):
        """
        Two constant numbers are equivalent if they represent the same number.

        """
        number1 = Number(22)
        number2 = Number(23)
        number3 = Number(22)

        number1.check_equivalence(number3)
        number3.check_equivalence(number1)
        assert_raises(AssertionError, number1.check_equivalence, number2)
        assert_raises(AssertionError, number2.check_equivalence, number1)
        assert_raises(AssertionError, number2.check_equivalence, number3)
        assert_raises(AssertionError, number3.check_equivalence, number2)

        ok_(number1 == number3)
        ok_(number3 == number1)
        ok_(number1 != number2)
        ok_(number2 != number1)
        ok_(number2 != number3)
        ok_(number3 != number2)
Ejemplo n.º 22
0
    def test_parsing_with_defined_grammar_but_no_available_translations(self):
        """
        When an expression is written in an supported grammar but there are no
        translated bindings, the default names must be used along with the
        custom grammar.

        """
        french_grammar = Grammar(decimal_separator=",",
                                 thousands_separator=".")
        mgr = EvaluableParseManager(self.symbol_table,
                                    Grammar(),
                                    fr=french_grammar)
        # French grammar is not supported:
        parse_tree = mgr.parse("traffic:pedestrians_crossing_road <= 3,0",
                               "fr")
        expected_tree = EvaluableParseTree(
            LessEqual(PedestriansCrossingRoad(), Number(3)))
        eq_(parse_tree, expected_tree)
Ejemplo n.º 23
0
    def test_parsing_with_undefined_grammar_but_available_translations(self):
        """
        When an expression is written in an unsupported grammar, a parser
        based on the generic grammar must be created and used.

        The respective translated bindings must be used if available.

        """
        log_handler = LoggingHandlerFixture()
        mgr = EvaluableParseManager(self.symbol_table, Grammar())
        # Castilian grammar is not supported:
        parse_tree = mgr.parse(u"tráfico:peatones_cruzando_calle <= 3.0", "es")
        expected_tree = EvaluableParseTree(
            LessEqual(PedestriansCrossingRoad(), Number(3)))
        eq_(parse_tree, expected_tree)
        # Checking the log:
        info = "Generated parser for unknown grammar %s" % repr(u'es')

        ok_(info in log_handler.handler.messages['info'])
        log_handler.undo()
Ejemplo n.º 24
0
    def test_parsing_with_undefined_grammar_and_no_translated_bindings(self):
        """
        When an expression is written in an unsupported grammar, a parser
        based on the generic grammar must be created and used.

        If there are no translated bindings, the default names must be used.

        """
        log_handler = LoggingHandlerFixture()
        mgr = EvaluableParseManager(self.symbol_table, Grammar())
        # French grammar is not supported:
        parse_tree = mgr.parse("traffic:pedestrians_crossing_road <= 3.0",
                               "fr")
        expected_tree = EvaluableParseTree(
            LessEqual(PedestriansCrossingRoad(), Number(3)))
        eq_(parse_tree, expected_tree)
        # Checking the log:
        info = "Generated parser for unknown grammar %s" % repr('fr')
        ok_(info in log_handler.handler.messages['info'])
        log_handler.undo()
Ejemplo n.º 25
0
class PermissiveFunction(Function):
    """
    A mock function operator which accepts any type of arguments.

    """

    operations = set(["boolean"])

    required_arguments = ("arg0", )

    optional_arguments = OrderedDict([('oarg0', Set()), ('oarg1', Number(1))])

    def check_arguments(self):
        """Do nothing -- Allow any kind of arguments."""
        pass

    def to_python(self, context):
        return self.arguments

    def __call__(self, context):
        return True
Ejemplo n.º 26
0
    def test_checking_duplicate_object_localized_names(self):
        """
        Two objects cannot have the same localized names in the same table.

        """
        st1 = SymbolTable(
            "global",
            (
                Bind("e", Number(2.7183), es_VE=u"número e"),
                Bind("pi", Number(3.1416)),
                Bind("eulers-number", Number(2.71828), es_VE=u"número e"),
            ),
        )
        st2 = SymbolTable(
            "global",
            (
                Bind("e", Number(2.7183), es_VE=u"número e"),
                Bind("pi", Number(3.1416)),
                # These object will be called "número e" in Spanish too:
                Bind(u"número e", Number(2.71828)),
            ),
        )
        assert_raises(ScopeError, st1.validate_scope)
        assert_raises(ScopeError, st2.validate_scope)
Ejemplo n.º 27
0
 def test_checking_logical_support(self):
     """Numbers have logical support."""
     op = Number(9)
     op.check_logical_support()
     ok_(Number(9)(None))
     assert_false(Number(0)(None))
Ejemplo n.º 28
0
 def test_belongs_to(self):
     op = Set(String("arepa"), Number(4))
     ok_(op.belongs_to(4, None))
     ok_(op.belongs_to(4.00, None))
     ok_(op.belongs_to("arepa", None))
     assert_false(op.belongs_to("something else", None))
Ejemplo n.º 29
0
 def test_representation(self):
     number = Number(4)
     eq_(repr(number), "<Number 4.0>")
Ejemplo n.º 30
0
 def test_python_value(self):
     op = Set(Number(10), Number(1), String("paola"))
     eq_(op.to_python(None), {10, 1, "paola"})