def test_codegen_decision_only(self): base = abs_path('./specs/') sc = 'script-no-graph.py' ps = Parser(base + sc, base) ps.main(verbose=False) # compare file content with open(base + FIRST_SCRIPT, 'r') as f: actual = f.read() with open(base + sc, 'r') as f: lines = f.readlines()[7:] expected = ''.join(lines).replace('{{a}}', '1') self.assertEqual(actual, expected)
def test_codegen_graph_only(self): base = abs_path('./specs/') ps = Parser(base + 'script1-good.py', base) ps.main(verbose=False) # compare file content with open(os.path.join(base, FIRST_SCRIPT), 'r') as f: actual = f.read() with open(base + 'script1.py', 'r') as f: lines = f.read().split('\n') expected = '' for l in lines: if not l.strip().startswith('# --- '): expected += l + '\n' self.assertEqual(actual.strip(), expected.strip())
def test_constraint_5(self): """ Skip a block """ # first, skip a normal block base = abs_path('./specs/') ps = Parser(base + 'script3-5.py', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 8) # then, skip a decision block ps = Parser(base + 'script3-5.py', base) ps.spec['constraints'] = [{ "block": "B", "skippable": True, "condition": "a == if" }] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 6)
def test_constraint_4(self): """ Variable depends on variable """ base = abs_path('./specs/') ps = Parser(base + 'script3-4.py', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 4) # then, test index ps = Parser(base + 'script3-4.py', base) ps.spec['constraints'] = [{ "variable": "b", "index": 1, "condition": "a.index == 0" }, { "variable": "b", "index": 0, "condition": "a == else" }] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 4)
def test_eval(self): """ Evaluation of various conditions """ # expr and expr base = abs_path('./specs/') ps = Parser(base + 'script3-6.py', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 2) # expr or expr ps.spec['constraints'] = [{ "block": "D", "condition": "a == if or B == b1" }] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 6) # expr and (expr or expr) ps.spec['constraints'] = [{ "block": "D", "condition": "a == if and (B == b1 or B == b2)" }] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 4) # testing != ps.spec['constraints'] = [{"block": "D", "condition": "a != if"}] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 4) # testing >= ps.spec['constraints'] = [{"block": "D", "condition": "a.index >= 1"}] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 4) # testing index ps.spec['constraints'] = [{"block": "D", "condition": "b.index == 1"}] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 4) # testing option with integer type ps.spec['constraints'] = [{"block": "D", "condition": "b == 0"}] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 4) # testing option with float type ps.spec['constraints'] = [{"block": "D", "condition": "b == 1.5"}] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 4) # testing unmade decision ps.spec['constraints'] = [{"block": "A", "condition": "b.index == 0"}] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 0) # testing if the decision is made when the block depends on a variable # inside the block ps.spec['constraints'] = [{"block": "B", "condition": "b.index == 0"}] ps._parse_constraints() ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 0)
def test_r(self): base = abs_path('../example/fertility_r/') ps = Parser(base + 'template.R', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 120)
def test_code_gen(self): base = abs_path('../example/simple/') ps = Parser(base + 'template.py', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 6)
def test_constraint_inline(self): """ Inline constraints """ base = abs_path('./specs/') ps = Parser(base + 'script-inline-constraints.py', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 2)
def test_constraint_7(self): """ Linked decisions """ base = abs_path('./specs/') ps = Parser(base + 'script3-7.py', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 10)
def test_constraint_3(self): """ Normal block depends on block parameter """ base = abs_path('./specs/') ps = Parser(base + 'script3-3.py', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 8)
def test_constraint_1(self): """ Block options depend on block parameter """ base = abs_path('./specs/') ps = Parser(base + 'script3-1.py', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 6)
def test_script_6(self): base = abs_path('./specs/') ps = Parser(base + 'script2-block-param.py', base) ps.main(verbose=False) self.assertEqual(ps.wrangler.counter, 9)