{"id":17690295,"url":"https://github.com/uncommon/fakedmacro","last_synced_at":"2025-03-30T22:14:37.376Z","repository":{"id":248870250,"uuid":"829250560","full_name":"Uncommon/FakedMacro","owner":"Uncommon","description":"Swift macro that creates test fakes for protocols","archived":false,"fork":false,"pushed_at":"2024-09-05T21:36:05.000Z","size":81,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-06T02:46:48.527Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Uncommon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-16T04:16:44.000Z","updated_at":"2024-09-05T21:36:08.000Z","dependencies_parsed_at":"2024-10-24T14:29:36.096Z","dependency_job_id":"beec3af2-479d-4358-86be-bb7758eecee6","html_url":"https://github.com/Uncommon/FakedMacro","commit_stats":null,"previous_names":["uncommon/fakedmacro"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Uncommon%2FFakedMacro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Uncommon%2FFakedMacro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Uncommon%2FFakedMacro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Uncommon%2FFakedMacro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Uncommon","download_url":"https://codeload.github.com/Uncommon/FakedMacro/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246385414,"owners_count":20768672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-24T11:50:31.561Z","updated_at":"2025-03-30T22:14:37.355Z","avatar_url":"https://github.com/Uncommon.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `@Faked` Macro\n\nThe `@Faked` macro makes it more convenient to create implementations of your protocols for tests and previews.\n\nWhen attached to a protocol, this macro creates a child protocol prefixed with `Empty` that has implementations of all properties and functions returning default values, such as zero, `nil`, or empty arrays. It also creates a concrete type prefixed with `Null` - either a `struct` or a `class` depedning on whether the original protocol inherits from `AnyObject` - which inherits from the `Empty` protocol.\n\nFor your tests and previews, you can inherit from the `Empty` protocol so that you only have to implement the members needed for that context, and the rest are covered by the \"empty\" implementations.\n\nFor example, this:\n\n```swift\n@Faked protocol Thing {\n    var x: Int { get }\n    func perform()\n}\n```\nexpands to this:\n```swift\nprotocol Thing {\n    var x: Int { get }\n    func perform()\n}\nprotocol EmptyThing: Thing {\n    var x: Int { get }\n    func perform()\n}\nextension EmptyThing {\n    var x: Int { 0 }\n    func perform() {}\n}\nstruct NullThing: EmptyThing {}\n```\n\nThen in some test where only the `perform()` function matters, you can create this test-specific struct:\n\n```swift\nstruct FakeThing: EmptyThing {\n    func perform() {\n        // some fake implementation\n    }\n}\n```\n\n### Associated types\n\nIf the protocol has associated types, you can to specify which concrete types to use in the \"Null\" type. By default, a \"Null\" prefix will be added. This is done with the `types` parameter:\n\n```swift\n@Faked(types: [\"X\": Int.self, \"Y\": String.self])\nprotocol Thing {\n    associatedtype X\n    associatedtype Y\n    associatedType Z\n    func intFunc() -\u003e Int\n}\n```\n\nThe resulting concrete type will be:\n\n```swift\nstruct NullThing: EmptyThing {\n    typealias X = Int\n    typealias Y = String\n    typealias Z = NullZ  // not specified, defaults to \"Null\" prefix\n}\n```\n\n### Creating extensions\n\nIf you want to create an extension to an Empty protocol generated by `@Faked`, there is currently a limitation in the Swift compiler such that the extension must be in a different file from where the macro is used. If you try to do it in the same file, the compiler will not recognize that the Empty protocol exists.\n\nYou can, however, conform types to that protocol within the same file.\n\n### Implementation note\n\nNotice than in the example above, `EmptyThing` duplicates all the members from `Thing`. This is because of an implementation detail: `@Faked` is a two-stage macro. `@Faked` itself is a \"peer macro\", creating `EmptyThing` and `NullThing` as _peers_ of the original protocol. It also attaches a second macro, `@Thing_Imp`, to `EmptyThing`. `@Thing_Imp` is an \"extension macro\", and only extension macros may create extensions (and only of the protocol they're attached to), so it creates the extension with the default implementations. Since `@Thing_Imp` can't see anything outside the protocol it's attached to, all the members of `Thing` must be duplicated in `EmptyThing` so the second macro can see them.\n\nUnfortunately, as of Xcode 15.4 (and 16 beta), expanding a nested macro doesn't work.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funcommon%2Ffakedmacro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funcommon%2Ffakedmacro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funcommon%2Ffakedmacro/lists"}