Exemple #1
0
 def test_dict_del_fail(self) -> None:
     def f(a: Dict[str, int]) -> None:
         '''
         post[a]: True
         '''
         del a["42"]
     self.assertEqual(*check_exec_err(f))
Exemple #2
0
    def test_string_formatting_literal(self) -> None:
        def f(o: object) -> str:
            ''' post: True '''
            return 'object of type {typ} with repr {zzzzz}'.format(typ=type(o),
                                                                   rep=repr(o))

        self.assertEqual(*check_exec_err(f))
Exemple #3
0
    def test_typevar_bounds_fail(self) -> None:
        T = TypeVar('T')

        def f(x: T) -> int:
            ''' post:True '''
            return x + 1  # type: ignore
        self.assertEqual(*check_exec_err(f))
Exemple #4
0
 def test_tuple_with_uniform_values_fail(self) -> None:
     def f(a: Tuple[int, ...]) -> float:
         '''
         post: True
         '''
         return sum(a) / len(a)
     self.assertEqual(*check_exec_err(f))
Exemple #5
0
 def test_max_fail(self) -> None:
     def f(l: List[int]) -> int:
         '''
         post: _ in l
         '''
         return max(l)
     self.assertEqual(*check_exec_err(f))
Exemple #6
0
    def test_string_formatting_literal(self) -> None:
        def f(o: object) -> str:
            """ post: True """
            return "object of type {typ} with repr {zzzzz}".format(  # type: ignore
                typ=type(o), rep=repr(o))

        self.assertEqual(*check_exec_err(f))
    def test_date_plus_delta_overflow_err(self) -> None:
        def f(delta: datetime.timedelta) -> datetime.date:
            """
            post: _.year != -9999
            """
            return datetime.date(2000, 1, 1) + delta

        self.assertEqual(*check_exec_err(f))
 def test_item_delete_type_error(self) -> None:
     def f(l: List[float]) -> None:
         '''
         pre: len(l) == 0
         post: True
         '''
         del l[1.0]
     self.assertEqual(*check_exec_err(f, 'TypeError'))
 def test_mismatched_types(self) -> None:
     def f(x: float, y: list) -> float:
         '''
         pre: x == 1.0 and y == []
         post: _ == 1
         '''
         return x + y
     self.assertEqual(*check_exec_err(f, 'TypeError: unsupported operand type'))
Exemple #10
0
 def test_index_error(self) -> None:
     def f(l: List[int], idx: int) -> int:
         '''
         pre: idx >= 0 and len(l) > 2
         post: True
         '''
         return l[idx]
     self.assertEqual(*check_exec_err(f))
Exemple #11
0
    def test_item_delete_type_error(self) -> None:
        def f(l: List[float]) -> None:
            """
            pre: len(l) == 0
            post: True
            """
            del l[1.0]  # type: ignore

        self.assertEqual(*check_exec_err(f, "TypeError"))
Exemple #12
0
 def test_string_formatting_varfmt(self) -> None:
     def f(fmt: str) -> str:
         '''
         # NOTE: with a iteration-base, pure python implementation of format, we wouldn't need this precondition:
         pre: '{}' in fmt
         post: True
         '''
         return fmt.format(ver=sys.version, platform=sys.platform)
     self.assertEqual(*check_exec_err(f))
Exemple #13
0
    def test_index_error(self) -> None:
        def f(l: List[int], idx: int) -> int:
            """
            pre: idx >= 0 and len(l) > 2
            post: True
            """
            return l[idx]

        self.assertEqual(*check_exec_err(f, "IndexError"))
 def test_index_err(self) -> None:
     def f(s1: str, s2: str) -> int:
         '''
         pre: s1 == 'aba'
         pre: 'ab' in s2
         post: True
         '''
         return s1.index(s2)
     # index() raises ValueError when a match isn't found:
     self.assertEqual(*check_exec_err(f, 'ValueError'))
Exemple #15
0
    def test_mismatched_types(self) -> None:
        def f(x: float, y: list) -> float:
            """
            pre: x == 1.0 and y == []
            post: _ == 1
            """
            return x + y  # type: ignore

        self.assertEqual(
            *check_exec_err(f, "TypeError: unsupported operand type"))
Exemple #16
0
 def test_nondeterminisim_detected(self) -> None:
     _GLOBAL_THING = [True]
     def f(i: int) -> int:
         ''' post: True '''
         if i > 0:
             _GLOBAL_THING[0] = not _GLOBAL_THING[0]
         else:
             _GLOBAL_THING[0] = not _GLOBAL_THING[0]
         if _GLOBAL_THING[0]:
             return -i if i < 0 else i
         else:
             return -i if i < 0 else i
     self.assertEqual(*check_exec_err(f, 'NotDeterministic'))
Exemple #17
0
        def test_icontract_nesting(self):
            @icontract.require(lambda name: name.startswith("a"))
            def innerfn(name: str):
                pass

            @icontract.ensure(lambda: True)
            @icontract.require(lambda name: len(name) > 0)
            def outerfn(name: str):
                innerfn("00" + name)

            self.assertEqual(*check_exec_err(
                outerfn,
                message_prefix="PreconditionFailed",
                optionset=AnalysisOptionSet(
                    analysis_kind=[AnalysisKind.icontract]),
            ))
Exemple #18
0
 def test_comparison_type_error(self) -> None:
     def f(a: List[Set], b: str):
         ''' post: True '''
         return a <= b
     self.assertEqual(*check_exec_err(f, 'TypeError'))
Exemple #19
0
    def test_enforced_fn_preconditions(self) -> None:
        def f(x: int) -> bool:
            """ post: _ == True """
            return bool(fibb(x)) or True

        self.assertEqual(*check_exec_err(f))
 def test_list_index(self) -> None:
     def f(i: int) -> int:
         ''' post: True '''
         return [0, 1, 2].index(i)
     self.assertEqual(*check_exec_err(f, 'ValueError: 3 is not in list'))
 def test_index_type_error(self) -> None:
     def f(l: List[int]) -> int:
         ''' post: True '''
         return l[0.0:]
     self.assertEqual(*check_exec_err(f, 'TypeError'))
 def test_item_delete_oob(self) -> None:
     def f(l: List[float]) -> None:
         ''' post: True '''
         del l[1]
     self.assertEqual(*check_exec_err(f, 'IndexError'))
Exemple #23
0
    def test_comparison_type_error(self) -> None:
        def f(a: List[Set], b: str):
            """ post: True """
            return a <= b  # type: ignore

        self.assertEqual(*check_exec_err(f, "TypeError"))
Exemple #24
0
    def test_item_delete_oob(self) -> None:
        def f(l: List[float]) -> None:
            """ post: True """
            del l[1]

        self.assertEqual(*check_exec_err(f, "IndexError"))
Exemple #25
0
    def test_list_index(self) -> None:
        def f(i: int) -> int:
            """ post: True """
            return [0, 1, 2].index(i)

        self.assertEqual(*check_exec_err(f, "ValueError: 3 is not in list"))
Exemple #26
0
    def test_index_type_error(self) -> None:
        def f(l: List[int]) -> int:
            """ post: True """
            return l[0.0:]  # type: ignore

        self.assertEqual(*check_exec_err(f, "TypeError"))