def test_is_spare(self): r = solver.Rules( rule1=solver.Code(code_str='frame[0] < 10'), rule2=solver.Code(code_str='frame[0] + frame[1] == 10'), output=True) r.solve(start_bowling.is_spare, self)
def test_get_next_throw(self): i = Code() r = solver.Rules(before_last=(i < 9), output=solver.Code(code_str='game[i+1][0]')) r.add(last_bonus_thow=(i == 9), output=solver.Code(code_str='game[i][2]')) r.solve(start_bowling.get_next_throw, self)
def test_recursive_get_score(self): r = solver.Rules( solver.Code(code_str='i == len(game)'), output=0, default=solver.Code( code_str= 'recursive_get_score(game, i + 1) + get_frame_score(game[i], game, i)' )) r.solve(start_bowling.recursive_get_score, self)
def test_recursive_iteration(self): """ Will do recursive iteration, extremely cool!!! """ function = f.recursive_iteration array_len_0 = 'len(array) == 0' array_1 = 'array[1:]' args = {'array': s.Code(code_str=array_1)} out = s.Output(function, args) code = [ 'def {}(array):'.format(function.__name__), '', ' if {}:'.format(array_len_0), ' return 0', '', ' return {0}({1})'.format(function.__name__, array_1) ] r = Rules(r1=s.Code(code_str=array_len_0), output=0, default=out) solution = r.solve(function, self) self.assertEqual(solution.implementation, code)
def test_calling_another_function_with_args(self): """ Invoke function with arguments. """ function = f.another_call2 args = {'a': s.Code(code_str='a'), 'b': s.Code(code_str='b')} out_f = f.another_call code = [ 'def {}(a, b):'.format(function.__name__), '', ' if not a and b:', ' return {}(a, b)'.format(out_f.__name__), '', ' return False' ] r = Rules(a=False, b=True, output=out_f, output_args=args) # non-boolean output solution = r.solve(function) self.assertEqual(solution.implementation, code)
def test_factor_code_output(self): """ Tests that code output can be factored. """ function = f.factor_ordered_pieces_of_code output_code = '2*2' code1_str = 'isinstance(array[0], int)' code2_str = 'isinstance(array[1], int)' code = [ 'def ' + function.__name__ + '(array):', '', ' if ' + code1_str + ' or ' + code2_str + ':', ' return ' + output_code, '', ' return False' ] r = Rules(r1=s.Code(code_str=code1_str), output=s.Code(code_str=output_code)) r.add(s.Code(code_str=code2_str), output=s.Code(code_str=output_code)) solution = r.solve(function, self) self.assertEqual(solution.implementation, code)
def test_factor_ordered_pieces_with_redundancy(self): """Tests that string output is factored, when inputs are given in more than one addition.""" function = f.factor_ordered_pieces_with_redundancy right_str = 'factoring!!!' code0_str = 'isinstance(array[0], int)' code1_str = 'isinstance(array[1], int)' code = [ 'def {}(array):'.format(function.__name__), '', ' if {}:'.format(code1_str), " return \"{}\"".format(right_str), '', ' return False' ] r = Rules(r1=s.Code(code_str=code0_str), r2=s.Code(code_str=code1_str), output=right_str) r.add(s.Code(code_str=code1_str), output=right_str) solution = r.solve(function, self) self.assertEqual(solution.implementation, code)
def test_factor_ordered_pieces_of_code(self): """ Tests that string output is factored, when inputs are given in more than one addition. """ function = f.factor_ordered_pieces_of_code right_str = 'factoring!!!' code1_str = 'isinstance(array[0], int)' code2_str = 'isinstance(array[1], int)' code3_str = 'isinstance(array[2], int)' code = [ 'def ' + function.__name__ + '(array):', '', ' if ' + code1_str + ' and ' + code2_str + ' or ' + code3_str + ':', ' return ' + "\"" + right_str + "\"", '', ' return False' ] r = Rules(r1=s.Code(code_str=code1_str), r2=s.Code(code_str=code2_str), output=right_str) r.add(s.Code(code_str=code3_str), output=right_str) solution = r.solve(function, self) self.assertEqual(solution.implementation, code)
def test_right_code_input_order(self): """ For programmer convenience and to be able to use short circuiting. Code pieces on expressions will follow the same order as the input order. """ function = f.right_expression_order right_str = 'right order!!!' code1_str = 'len(array) > 1' code2_str = 'array[0]' code3_str = 'isinstance(array[0], int)' code = [ 'def ' + function.__name__ + '(array):', '', ' if ' + code1_str + ' and ' + code2_str + ' and ' + code3_str + ':', ' return ' + "\"" + right_str + "\"", '', ' return False' ] r = Rules(r1=s.Code(code_str=code1_str), r2=s.Code(code_str=code2_str), r3=s.Code(code_str=code3_str), output=right_str) solution = r.solve(function, self) self.assertEqual(solution.implementation, code)
def test_internal_code_arguments(self): """ Do logic with pieces of code that evaluate to boolean. """ function = f.with_internal_code_arg code = [ 'def ' + function.__name__ + '(a):', '', ' if isinstance(a, str):', ' return 2', '', ' return False' ] r = Rules(any_non_input_name=s.Code(code_str='isinstance(a, str)'), output=2) solution = r.solve(function, self) self.assertEqual(solution.implementation, code)
def test_calling_nested_functions(self): """ call nested functions. """ function = f.nested_call out_obj = s.Output(f.f, {'a': s.Output(f.g, {'a': s.Code(code_str='a')})}) code = [ 'def ' + function.__name__ + '(a):', '', ' if not a:', ' return ' + f.f.__name__ + '(' + f.g.__name__ + '(a))', '', ' return False' ] r = Rules(a=False, output=out_obj) solution = r.solve(function) self.assertEqual(solution.implementation, code)
def test_recursive_function(self): """ Will do recursion, extremely cool!!! """ function = f.recursive not_a = 'not a' args = {'a': s.Code(code_str=not_a)} out = s.Output(function, args) code = [ 'def {}(a):'.format(function.__name__), '', ' if {}:'.format(not_a), ' return 0', '', ' return {0}({1})'.format(function.__name__, not_a) ] r = Rules(a=False, output=0, default=out) solution = r.solve(function) self.assertEqual(solution.implementation, code)
def test_get_frame_score(self): r = solver.Rules( not_strike=solver.Code(code_str='not is_strike(frame)'), not_spare=solver.Code(code_str='not is_spare(frame)'), output=solver.Code(code_str='frame[0] + frame[1]')) r.add(is_spare=solver.Code(code_str='is_spare(frame)'), output=solver.Code( code_str='frame[0] + frame[1] + get_next_throw(i, game)')) r.add(is_strike=solver.Code(code_str='is_strike(frame)'), output=solver.Code( code_str='frame[0] + get_next_2_throws(i, game)')) r.solve(start_bowling.get_frame_score, self)
def test_get_next_2_throws(self): i = Code() r = solver.Rules( last_bonus_throw=(i == 9), output=solver.Code(code_str='game[i][1] + game[i][2]')) r.add(i == 8, solver.Code(code_str='is_strike(game[i+1])'), output=solver.Code(code_str='game[i+1][0] + game[i+1][1]')) r.add(next_is_not_strike=solver.Code( code_str='not is_strike(game[i+1])'), output=solver.Code(code_str='game[i+1][0] + game[i+1][1]')) r.add(next_is_strike=solver.Code(code_str='is_strike(game[i+1])'), output=solver.Code(code_str='game[i+1][0] + game[i+2][0]')) r.solve(start_bowling.get_next_2_throws, self)
def test_is_strike(self): r = solver.Rules(rule=solver.Code(code_str='frame[0] == 10'), output=True) r.solve(start_bowling.is_strike, self)