def test_lookup_definitions(self): from pysd.translation.vensim.vensim2py import get_equation_components self.assertEqual( get_equation_components(r"table([(0,-1)-(45,1)],(0,0),(5,0))"), { "expr": "([(0,-1)-(45,1)],(0,0),(5,0))", "kind": "lookup", "subs": [], "subs_compatibility": {}, "real_name": "table", "keyword": None, }, ) self.assertEqual( get_equation_components(r"table2 ([(0,-1)-(45,1)],(0,0),(5,0))"), { "expr": "([(0,-1)-(45,1)],(0,0),(5,0))", "kind": "lookup", "subs": [], "subs_compatibility": {}, "real_name": "table2", "keyword": None, }, )
def test_pathological_names(self): from pysd.translation.vensim.vensim2py import get_equation_components self.assertEqual( get_equation_components(r'"silly-string" = 25'), { "expr": "25", "kind": "component", "subs": [], "subs_compatibility": {}, "real_name": '"silly-string"', "keyword": None, }, ) self.assertEqual( get_equation_components(r'"pathological\\-string" = 25'), { "expr": "25", "kind": "component", "subs": [], "subs_compatibility": {}, "real_name": r'"pathological\\-string"', "keyword": None, }, )
def test_whitespace_handling(self): """ Whitespaces should be shortened to a single space """ from pysd.translation.vensim.vensim2py import get_equation_components self.assertEqual( get_equation_components(r"""constant\t = \t25\t """), { "expr": "25", "kind": "component", "subs": [], "subs_compatibility": {}, "real_name": "constant", "keyword": None, }, ) # test eliminating vensim's line continuation character self.assertEqual( get_equation_components(r"""constant [Sub1, \\ Sub2] = 10, 12; 14, 16;"""), { "expr": "10, 12; 14, 16;", "kind": "component", "subs": ["Sub1", "Sub2"], "subs_compatibility": {}, "real_name": "constant", "keyword": None, }, )
def test_subscript_references(self): from pysd.translation.vensim.vensim2py import get_equation_components self.assertEqual( get_equation_components( r"constant [Sub1, Sub2] = 10, 12; 14, 16;"), { "expr": "10, 12; 14, 16;", "kind": "component", "subs": ["Sub1", "Sub2"], "subs_compatibility": {}, "real_name": "constant", "keyword": None, }, ) self.assertEqual( get_equation_components(r"function [Sub1] = other function[Sub1]"), { "expr": "other function[Sub1]", "kind": "component", "subs": ["Sub1"], "subs_compatibility": {}, "real_name": "function", "keyword": None, }, ) self.assertEqual( get_equation_components( r'constant ["S1,b", "S1,c"] = 1, 2; 3, 4;'), { "expr": "1, 2; 3, 4;", "kind": "component", "subs": ['"S1,b"', '"S1,c"'], "subs_compatibility": {}, "real_name": "constant", "keyword": None, }, ) self.assertEqual( get_equation_components( r'constant ["S1=b", "S1=c"] = 1, 2; 3, 4;'), { "expr": "1, 2; 3, 4;", "kind": "component", "subs": ['"S1=b"', '"S1=c"'], "subs_compatibility": {}, "real_name": "constant", "keyword": None, }, )
def test_get_equation_components_error(self): from pysd.translation.vensim.vensim2py import get_equation_components defi = "NIF: NF<x-x>NF" try: get_equation_components(defi) self.assertFail() except ValueError as err: self.assertIn( "\nError when parsing definition:\n\t %s\n\n" "probably used definition is invalid or not integrated..." "\nSee parsimonious output above." % defi, err.args[0], )
def test_subscript_definition_parsing(self): from pysd.translation.vensim.vensim2py import get_equation_components self.assertEqual( get_equation_components(r"""Sub1: Entry 1, Entry 2, Entry 3 """), { "expr": None, "kind": "subdef", "subs": ["Entry 1", "Entry 2", "Entry 3"], "subs_compatibility": {}, "real_name": "Sub1", "keyword": None, }, ) with self.assertRaises(ValueError) as err: get_equation_components(r"""Sub2: (1-3) """) self.assertIn("A numeric range must contain at least one letter.", str(err.exception)) with self.assertRaises(ValueError) as err: get_equation_components(r"""Sub2: (a1-a1) """) self.assertIn( "The number of the first subscript value must be " "lower than the second subscript value in a " "subscript numeric range.", str(err.exception)) with self.assertRaises(ValueError) as err: get_equation_components(r"""Sub2: (a1-b3) """) self.assertIn("Only matching names ending in numbers are valid.", str(err.exception))
def test_basics(self): from pysd.translation.vensim.vensim2py import get_equation_components self.assertEqual( get_equation_components(r'constant = 25'), { 'expr': '25', 'kind': 'component', 'subs': [], 'subs_compatibility': {}, 'real_name': 'constant', 'keyword': None })
def test_equals_handling(self): """ Parse cases with equal signs within the expression """ from pysd.translation.vensim.vensim2py import get_equation_components self.assertEqual( get_equation_components(r"Boolean = IF THEN ELSE(1 = 1, 1, 0)"), { "expr": "IF THEN ELSE(1 = 1, 1, 0)", "kind": "component", "subs": [], "subs_compatibility": {}, "real_name": "Boolean", "keyword": None, }, )