def test_retrieving_existing_global_object(self): objects = { 'bool': BoolVar(), 'traffic': TrafficLightVar(), } st = Namespace(objects) requested_object = st.get_object("bool") eq_(requested_object, BoolVar())
def test_retrieving_existing_2nd_level_object(self): sub_objects = { 'bool': BoolVar(), 'traffic': TrafficLightVar(), } global_objects = { 'foo': TrafficLightVar(), } sub_namespace = {'sub1': Namespace(sub_objects)} st = Namespace(global_objects, sub_namespace) requested_object = st.get_object("bool", ["sub1"]) eq_(requested_object, BoolVar())
class TestEvaluableParser(object): """Tests for the evaluable parser.""" global_objects = { 'bool': BoolVar(), 'message': String("Hello world"), 'foo': PermissiveFunction, } traffic_objects = { 'traffic_light': TrafficLightVar(), 'pedestrians_crossing_road': PedestriansCrossingRoad(), 'drivers_awaiting_green_light': DriversAwaitingGreenLightVar(), 'traffic_violation': TrafficViolationFunc, } root_namespace = Namespace(global_objects, { 'traffic': Namespace(traffic_objects), }) parser = EvaluableParser(Grammar(), root_namespace) #{ Tests for the parse action that makes the variables def test_existing_variable_without_namespace(self): parse_tree = self.parser("~ bool") eq_(parse_tree.root_node, Not(self.global_objects['bool'])) def test_existing_variable_with_namespace(self): parse_tree = self.parser('traffic:traffic_light == "green"') expected_node = Equal(self.traffic_objects['traffic_light'], String("green")) eq_(parse_tree.root_node, expected_node) def test_non_existing_variable_without_namespace(self): assert_raises(ScopeError, self.parser, "~ non_existing_var") def test_non_existing_variable_with_namespace(self): assert_raises(ScopeError, self.parser, "~ traffic:non_existing_var") def test_variable_in_non_existing_namespace(self): assert_raises(ScopeError, self.parser, "~ bar:foo") def test_function_instead_of_variable(self): # "foo" is a function, so it cannot be used as a variable (without # parenthesis): try: self.parser('~ foo') except BadExpressionError, exc: eq_('"foo" represents a function, not a variable', unicode(exc)) else:
def test_retrieving_non_existing_object(self): global_objects = { 'foo': TrafficLightVar(), 'traffic-violation': TrafficViolationFunc(String("pedestrians")), } st = Namespace(global_objects, ) assert_raises(ScopeError, st.get_object, "bool")
def test_retrieving_object_in_non_existing_subtable(self): global_objects = { 'foo': TrafficLightVar(), 'traffic-violation': TrafficViolationFunc(String("pedestrians")), } st = Namespace(global_objects, ) assert_raises(ScopeError, st.get_object, "foo", ["doesn't", "exist"])
def test_retrieving_existing_3rd_level_object(self): third_level_objects = { 'bool': BoolVar(), } second_level_objects = { 'traffic': TrafficLightVar(), } global_objects = { 'foo': TrafficLightVar(), 'traffic-violation': TrafficViolationFunc(String("pedestrians")), } third_level_namespaces = {'sub2': Namespace(third_level_objects)} second_level_namespaces = { 'sub1': Namespace(second_level_objects, third_level_namespaces)} st = Namespace(global_objects, second_level_namespaces) requested_object = st.get_object("bool", ["sub1", "sub2"]) eq_(requested_object, BoolVar())
def test_retrieving_existing_3rd_level_object(self): third_level_objects = { 'bool': BoolVar(), } second_level_objects = { 'traffic': TrafficLightVar(), } global_objects = { 'foo': TrafficLightVar(), 'traffic-violation': TrafficViolationFunc(String("pedestrians")), } third_level_namespaces = {'sub2': Namespace(third_level_objects)} second_level_namespaces = { 'sub1': Namespace(second_level_objects, third_level_namespaces) } st = Namespace(global_objects, second_level_namespaces) requested_object = st.get_object("bool", ["sub1", "sub2"]) eq_(requested_object, BoolVar())
def test_subnamespaces_are_optional(self): """Namespaces may not have sub-namespaces.""" st = Namespace(objects={}) eq_(st.subnamespaces, {})