def test_find_swift_files(self):
     files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
     file_names = [file.split(os.sep)[-1] for file in files]
     assert "AwesomeRepository.swift" in file_names
     assert "AwesomeRepositoryImpl.swift" in file_names
     assert "ContainerBuilder.swift" in file_names
     assert "AppDelegate.swift" in file_names
    def test_write_mock_implementations(self):
        path = IosDebugTests.START_TEST_PROJECT_PATH
        files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
        with open(path + os.sep + DATA_FILE, "wb") as file:
            pickle.dump(str({KEY_MOCK_IMPLEMENTATIONS: ""}), file)
        path_to_content_dict = create_path_to_content_dict(files)
        repository_protocols = find_repository_protocols(path_to_content_dict)
        protocols_to_functions_map = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict)
        write_mock_implementations(
            path_to_content_dict,
            protocols_to_functions_map,
            IosDebugTests.START_TEST_PROJECT_PATH,
        )

        target_file = (IosDebugTests.START_TEST_PROJECT_PATH + os.sep +
                       "DebugModeTestApp" + os.sep + "AwesomeRepository.swift")
        with open(target_file, "r") as file:
            content = file.read()
            assert "// MARK: - Mock implementation" in content
            assert content.count("func doGenericAwesomeThings<T>") == 2
            assert (content.count(
                "func doAwesomeThingsAndReturnSingle() -> Single<String>") == 2
                    )

            assert ("""\
    container.register(AwesomeRepositoryImpl.self) { _ in
        AwesomeRepositoryImpl()
    }
""".strip() in content)
Beispiel #3
0
    def test_get_protocols_to_protocol_functions_dict(self):
        files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        repository_protocols = find_repository_protocols(path_to_content_dict)
        result = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict)
        awesome_function = result["AwesomeRepository"][0]
        assert awesome_function.name == "doAwesomeThings"
        assert awesome_function.params == None
        assert awesome_function.return_type == None

        awesome_generic_function = result["AwesomeRepository"][1]
        assert awesome_generic_function.name == "doGenericAwesomeThings"
        assert awesome_generic_function.generic_parameter_clause == "<T>"

        awesome_function_with_tuple_param = result["AwesomeRepository"][2]
        assert (awesome_function_with_tuple_param.name ==
                "doAwesomeThingsWithTupleParameter")
        assert (awesome_function_with_tuple_param.params[0] ==
                "tupleList: [(first: String, second: String)]")

        awesome_function_with_single = result["AwesomeRepository"][3]
        assert awesome_function_with_single.return_type == "Single<String>"

        awesome_function_with_single_tuple = result["AwesomeRepository"][5]
        assert (awesome_function_with_single_tuple.return_type ==
                "Single<(latest: String, minimal: String)>")
Beispiel #4
0
    def test_get_mock_settings_and_mock_cursor(self):
        files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        repository_protocols = find_repository_protocols(path_to_content_dict)
        protocols_to_functions_map = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict)

        registrations = get_registrations(repository_protocols)

        write_mock_implementations(
            path_to_content_dict,
            protocols_to_functions_map,
            IosDebugTests.START_TEST_PROJECT_PATH,
        )

        protocol_to_mocked_contents = get_protocol_to_mocked_content_dict(
            repository_protocols, path_to_content_dict)

        protocol_to_mock_function_variants = get_mock_functions_variants(
            protocol_to_mocked_contents)

        processed_mock_manager = get_mock_settings_and_mock_cursor(
            protocols_to_functions_map, protocol_to_mock_function_variants)

        assert (
            '"doAwesomeThings()": ["Original", "Mocked1", "Mocked2", "Mocked3", "Mocked4"]'
            in processed_mock_manager)
    def test_create_path_to_content_dict(self):
        target_file = (
            IosDebugTests.STOP_TEST_PROJECT_PATH
            + os.sep
            + "DebugModeTestApp"
            + os.sep
            + "AppDelegate.swift"
        )

        with open(target_file, "r") as file:
            content = file.read()
            assert (
                "let navigationController = ShakableNavigationController()" in content
            )

        files = find_swift_files(IosDebugTests.STOP_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        set_original_root_view_controller(
            files, path_to_content_dict, IosDebugTests.STOP_TEST_PROJECT_PATH
        )

        with open(target_file, "r") as file:
            content = file.read()
            assert (
                "let navigationController = UINavigationController(nibName: nil, bundle: nil)"
                in content
            )
Beispiel #6
0
 def test_create_path_to_content_dict(self):
     files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
     path_to_content_dict = create_path_to_content_dict(files)
     app_delegate_file_key = [
         key for key in path_to_content_dict.keys() if "AppDelegate" in key
     ][0]
     with open(app_delegate_file_key, "r") as file:
         content = file.read()
         assert content == path_to_content_dict[app_delegate_file_key]
 def test_get_protocol_to_mocked_content_dict(self):
     files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
     path_to_content_dict = create_path_to_content_dict(files)
     repository_protocols = find_repository_protocols(path_to_content_dict)
     protocols_to_functions_map = get_protocols_to_protocol_functions_dict(
         repository_protocols, path_to_content_dict)
     write_mock_implementations(
         path_to_content_dict,
         protocols_to_functions_map,
         IosDebugTests.START_TEST_PROJECT_PATH,
     )
     result = get_protocol_to_mocked_content_dict(repository_protocols,
                                                  path_to_content_dict)
     assert "AwesomeRepository" in result.keys()
    def test_create_path_to_content_dict(self):
        target_file = (IosDebugTests.STOP_TEST_PROJECT_PATH + os.sep +
                       "DebugModeTestApp" + os.sep + "ContainerBuilder.swift")

        with open(target_file, "r") as file:
            content = file.read()
            assert "registerMockAwesomeRepository(to: container)" in content

        files = find_swift_files(IosDebugTests.STOP_TEST_PROJECT_PATH)
        remove_mock_registrations(files)

        with open(target_file, "r") as file:
            content = file.read()
            assert "registerMockAwesomeRepository(to: container)" not in content
    def test_get_mock_settings_and_mock_cursor(self):
        files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        repository_protocols = find_repository_protocols(path_to_content_dict)
        protocols_to_functions_map = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict
        )
        result = get_original_registrations(
            path_to_content_dict, protocols_to_functions_map
        )

        assert (
            "container.register(AwesomeRepositoryImpl.self"
            in result["AwesomeRepository"]
        )
    def test_get_updated_mocked_implementation(self):
        files = find_swift_files(IosDebugTests.SYNC_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        repository_protocols = find_repository_protocols(path_to_content_dict)
        protocols_to_protocol_functions_dict = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict)

        stored_impl = get_stored_mock_implementation(
            IosDebugTests.SYNC_TEST_PROJECT_PATH, "AwesomeRepository")
        mock_functions_variants = get_mock_functions_variants(
            {"AwesomeRepository": stored_impl})

        result = get_updated_mocked_implementation(
            protocols_to_protocol_functions_dict["AwesomeRepository"],
            stored_impl,
            list(mock_functions_variants["AwesomeRepository"].keys()),
        )

        assert "justAnotherFunction(param: Bool) -> Bool {" in result
        assert "justAnotherFunction() -> Bool {" not in result
        assert "doAwesomeThings() {" not in result
Beispiel #11
0
    def test_store_mocked_implementation(self):
        files = find_swift_files(IosDebugTests.STOP_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        repository_protocols = find_repository_protocols(path_to_content_dict)
        protocols_to_functions_map = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict)
        write_mock_implementations(
            path_to_content_dict,
            protocols_to_functions_map,
            IosDebugTests.STOP_TEST_PROJECT_PATH,
        )
        protocol_to_mocked_contents = get_protocol_to_mocked_content_dict(
            repository_protocols, path_to_content_dict)

        store_mocked_implementation(protocol_to_mocked_contents,
                                    IosDebugTests.START_TEST_PROJECT_PATH)

        with open(IosDebugTests.START_TEST_PROJECT_PATH + os.sep + DATA_FILE,
                  "rb") as file:
            content = pickle.load(file)
            data = literal_eval(content)
            assert "mock_implementations" in data
Beispiel #12
0
    def test_create_path_to_content_dict(self):
        target_file = (
            IosDebugTests.STOP_TEST_PROJECT_PATH + os.sep + ".ios-debug_data.pkl"
        )

        files = find_swift_files(IosDebugTests.STOP_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        store_mocked_implementations(
            path_to_content_dict, IosDebugTests.STOP_TEST_PROJECT_PATH
        )

        with open(target_file, "rb") as file:
            content = pickle.load(file)
            dictionary = literal_eval(content)

            assert "mock_implementations" in dictionary
            assert "AwesomeRepository" in dictionary["mock_implementations"]
            assert (
                "func doAwesomeThings()"
                in dictionary["mock_implementations"]["AwesomeRepository"]
            )
            assert "Mocked4" in dictionary["mock_implementations"]["AwesomeRepository"]
    def test_add_mock_registrations_to_container_builder(self):
        files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        repository_protocols = find_repository_protocols(path_to_content_dict)
        protocols_to_functions_map = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict)

        registrations = get_registrations(repository_protocols)

        write_mock_implementations(
            path_to_content_dict,
            protocols_to_functions_map,
            IosDebugTests.START_TEST_PROJECT_PATH,
        )

        add_mock_registrations_to_container_builder(files, registrations)

        target_file = (IosDebugTests.START_TEST_PROJECT_PATH + os.sep +
                       "DebugModeTestApp" + os.sep + "ContainerBuilder.swift")
        with open(target_file, "r") as file:
            content = file.read()

            assert "registerMockAwesomeRepository(to: container)" in content
    def test_get_mock_functions_variants(self):
        files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        repository_protocols = find_repository_protocols(path_to_content_dict)
        protocols_to_functions_map = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict
        )
        write_mock_implementations(
            path_to_content_dict,
            protocols_to_functions_map,
            IosDebugTests.START_TEST_PROJECT_PATH,
        )

        protocol_to_mocked_contents = get_protocol_to_mocked_content_dict(
            repository_protocols, path_to_content_dict
        )

        result = get_mock_functions_variants(protocol_to_mocked_contents)[
            "AwesomeRepository"
        ]
        print("result")
        print(result)
        assert result["doAwesomeThings()"] == [
            '"Original"',
            '"Mocked1"',
            '"Mocked2"',
            '"Mocked3"',
            '"Mocked4"',
        ]

        assert result["doAwesomeThingsAndReturnSingle() -> Single<String>"] == [
            '"Original"',
            '"Mocked1"',
            '"Mocked2"',
            '"Mocked3"',
        ]
Beispiel #15
0
 def test_find_repository_protocols(self):
     files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
     path_to_content_dict = create_path_to_content_dict(files)
     repository_protocols = find_repository_protocols(path_to_content_dict)
     assert "AwesomeRepository" in repository_protocols
Beispiel #16
0
    def test_create_and_set_mock_manager(self):
        files = find_swift_files(IosDebugTests.START_TEST_PROJECT_PATH)
        path_to_content_dict = create_path_to_content_dict(files)
        repository_protocols = find_repository_protocols(path_to_content_dict)
        protocols_to_functions_map = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict
        )

        write_mock_implementations(
            path_to_content_dict,
            protocols_to_functions_map,
            IosDebugTests.START_TEST_PROJECT_PATH,
        )

        protocol_to_mocked_contents = get_protocol_to_mocked_content_dict(
            repository_protocols, path_to_content_dict
        )

        store_mocked_implementation(
            protocol_to_mocked_contents, IosDebugTests.START_TEST_PROJECT_PATH
        )

        protocol_to_protocol_functions = get_protocols_to_protocol_functions_dict(
            repository_protocols, path_to_content_dict
        )

        protocol_to_mocked_contents = get_protocol_to_mocked_content_dict(
            repository_protocols, path_to_content_dict
        )

        protocol_to_mock_function_variants = get_mock_functions_variants(
            protocol_to_mocked_contents
        )

        processed_mock_manager = get_mock_settings_and_mock_cursor(
            protocol_to_protocol_functions, protocol_to_mock_function_variants
        )

        create_and_set_mock_manager(
            files,
            path_to_content_dict,
            processed_mock_manager,
            IosDebugTests.START_TEST_PROJECT_PATH,
        )

        target_file = (
            IosDebugTests.START_TEST_PROJECT_PATH
            + os.sep
            + "DebugModeTestApp"
            + os.sep
            + "AppDelegate.swift"
        )
        with open(target_file, "r") as file:
            content = file.read()

            assert (
                "let navigationController = ShakableNavigationController() // !!! Don't edit this line while in debug mode !!!"
                in content
            )

            assert "self.window?.rootViewController = navigationController" in content