Ejemplo n.º 1
0
 def test_type_dependency(self) -> None:
     # This one covered the 'has_method_with_parameter'.
     exp = ['class("B")', 'has_method_with_parameter("C","B")', 'interface("C")']
     rules = ['type("B", "C").' 'symbols("B"; "C").']
     raw_codegen = hh_codesynthesis.CodeGenerator()
     hh_codesynthesis.do_reasoning(additional_programs=rules, generator=raw_codegen)
     self.assertListEqual(sorted(str(raw_codegen).split()), exp)
Ejemplo n.º 2
0
    def test_extends_dependency_with_rule_extraction(self) -> None:
        exp = [
            'add_method("A","foo")',
            'add_method("B","foo")',
            'class("B")',
            'class("I")',
            'extends("A","T")',
            'extends("B","I")',
            'implements("B","A")',
            'interface("A")',
            'interface("T")',
        ]
        deps = """\
Extends A -> Type B
Extends I -> Type B
Extends T -> Type A
Method A::foo -> Type B
Type A -> Type B
Type I -> Type B
Type T -> Type A, Type B
"""
        raw_codegen = hh_codesynthesis.CodeGenerator()
        additional_programs = hh_codesynthesis.extract_logic_rules(deps.split("\n"))
        hh_codesynthesis.do_reasoning(
            additional_programs=additional_programs, generator=raw_codegen
        )
        self.assertListEqual(sorted(str(raw_codegen).split()), exp)
Ejemplo n.º 3
0
 def test_clingo_exception(self) -> None:
     deps = ["rule_without_period(symbol1, symbol2)"]
     raw_codegen = hh_codesynthesis.CodeGenerator()
     with self.assertRaises(expected_exception=RuntimeError, msg="parsing failed"):
         hh_codesynthesis.do_reasoning(
             additional_programs=deps, generator=raw_codegen
         )
Ejemplo n.º 4
0
 def test_fun_type_dependencies(self) -> None:
     # This one covered the 'invokes_function' with 'Type'.
     exp = [
         'class("A")',
         'funcs("Fn")',
         'interface("B")',
         'invokes_function("A","Fn")',
     ]
     rules = ['invoked_by("Fn", "A").' 'symbols("A"; "B").' 'funcs("Fn").']
     raw_codegen = hh_codesynthesis.CodeGenerator()
     hh_codesynthesis.do_reasoning(additional_programs=rules, generator=raw_codegen)
     self.assertListEqual(sorted(str(raw_codegen).split()), exp)
Ejemplo n.º 5
0
 def test_smethod_dependency_exception(self) -> None:
     # This one covered the unsatifiable part, that we can't find an answer.
     # Here we are forcing symbol("B") to get interface("B").
     rules = [
         'static_method("A", "foo", "B").',
         'interface("B").' 'symbols("A"; "B").',
     ]
     raw_codegen = hh_codesynthesis.CodeGenerator()
     with self.assertRaises(expected_exception=RuntimeError, msg="Unsatisfiable."):
         hh_codesynthesis.do_reasoning(
             additional_programs=rules, generator=raw_codegen
         )
Ejemplo n.º 6
0
 def test_smethod_fun_dependencies(self) -> None:
     # This one covered the 'invokes_static_method' with 'Fun', and we don't need to
     # pass the object as parameter, so that we directly invoke the static method.
     exp = [
         'add_static_method("B","foo")',
         'class("B")',
         'funcs("C")',
         'interface("A")',
         'invokes_static_method("C","B","foo")',
     ]
     rules = ['static_method("B", "foo", "C").' 'symbols("A"; "B").' 'funcs("C").']
     raw_codegen = hh_codesynthesis.CodeGenerator()
     hh_codesynthesis.do_reasoning(additional_programs=rules, generator=raw_codegen)
     self.assertListEqual(sorted(str(raw_codegen).split()), exp)
Ejemplo n.º 7
0
 def test_method_dependency(self) -> None:
     # This one covered the 'invokes_in_method', as well as the
     # 'has_method_with_parameter', since we need to pass the object as parameter,
     # then invoke its method.
     exp = [
         'add_method("B","foo")',
         'class("C")',
         'has_method_with_parameter("C","B")',
         'interface("B")',
         'invokes_in_method("C","B","foo")',
     ]
     rules = ['method("B", "foo", "C").' 'symbols("B"; "C").']
     raw_codegen = hh_codesynthesis.CodeGenerator()
     hh_codesynthesis.do_reasoning(additional_programs=rules, generator=raw_codegen)
     self.assertListEqual(sorted(str(raw_codegen).split()), exp)
Ejemplo n.º 8
0
 def test_interface_type_fun_dependency(self) -> None:
     # This one covered the 'has_parameter_and_argument' with <'Type', 'Fun'>.
     exp = [
         'class("B")',
         'funcs("Fn")',
         'has_parameter_and_argument("Fn","A","B")',
         'implements("B","A")',
         'interface("A")',
     ]
     rules = [
         'type("A", "Fn").'
         'symbols("A"; "B").'
         'funcs("Fn").'
         'extends_to("A","B").'
     ]
     raw_codegen = hh_codesynthesis.CodeGenerator()
     hh_codesynthesis.do_reasoning(additional_programs=rules, generator=raw_codegen)
     self.assertListEqual(sorted(str(raw_codegen).split()), exp)
Ejemplo n.º 9
0
 def test_method_type_extends_dependencies(self) -> None:
     # This one covered the 'override' in the "Extend" and "Method" edge.
     exp = [
         'add_method("B","foo")',
         'add_method("C","foo")',
         'class("C")',
         'implements("C","B")',
         'interface("B")',
     ]
     rules = [
         'extends_to("B", "C").',
         'method("B", "foo", "C").',
         'type("B", "C").',
         'symbols("B"; "C").',
     ]
     raw_codegen = hh_codesynthesis.CodeGenerator()
     hh_codesynthesis.do_reasoning(additional_programs=rules, generator=raw_codegen)
     self.assertListEqual(sorted(str(raw_codegen).split()), exp)
Ejemplo n.º 10
0
 def test_extends_dependency(self) -> None:
     exp = [
         'class("B")',
         'class("I")',
         'extends("A","T")',
         'extends("B","I")',
         'implements("B","A")',
         'interface("A")',
         'interface("T")',
     ]
     rules = [
         'extends_to("A", "B").',
         'extends_to("I", "B").',
         'extends_to("T", "A").',
         'symbols("A";"B";"I";"T").',
     ]
     raw_codegen = hh_codesynthesis.CodeGenerator()
     hh_codesynthesis.do_reasoning(additional_programs=rules, generator=raw_codegen)
     self.assertListEqual(sorted(str(raw_codegen).split()), exp)
Ejemplo n.º 11
0
 def test_class_method_fun_dependency(self) -> None:
     # This one covered the 'creates_in_body' with <'Method', 'Fun'>.
     exp = [
         'add_method("B","foo")',
         'class("B")',
         'creates_in_body("Fn","B")',
         'funcs("Fn")',
         'implements("B","A")',
         'interface("A")',
         'invokes_in_body("Fn","B","foo")',
     ]
     rules = [
         'method("B", "foo", "Fn").'
         'symbols("A"; "B").'
         'funcs("Fn").'
         'extends_to("A","B").'
     ]
     raw_codegen = hh_codesynthesis.CodeGenerator()
     hh_codesynthesis.do_reasoning(additional_programs=rules, generator=raw_codegen)
     self.assertListEqual(sorted(str(raw_codegen).split()), exp)