[swift-evolution] [Pitch] @testable private members

Matthew Johnson matthew at anandabits.com
Sun Feb 19 07:39:14 CST 2017



Sent from my iPad

On Feb 19, 2017, at 12:21 AM, Brent Royal-Gordon <brent at architechies.com> wrote:

>> On Feb 18, 2017, at 10:14 AM, Matthew Johnson via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> When writing unit tests sometimes it is necessary to artificially elevate a member to `internal` in order to make it visible to unit tests where it could otherwise be `private` or `fileprivate`.  We could introduce an `@testable` attribute that could be applied anywhere an access modifier is used.  This attribute would elevate the access modifier to `internal` when the module is imported using the `@testable import MyModule` syntax in a test suite.
> 
>    extension MyModel {
>        public func load(fromJSON jsonData: Data) throws {
>            ...
>        }
>        
>        private func parse(_ json: Data) -> Any { … }
>    }
>    
>    extension MyModel {
>        public func load(fromPropertyList plistData: Data) throws {
>>        }
>        
>        private func parse(_ plist: Data) -> Any { … }
>    }
> 
> What happens when you try to test `MyModel.parse(_:)`?

With what I suggested you would not be able to unless you change the declarations to `@testable private`.

With Robert's alternative this would already be testable.

> 
> -- 
> Brent Royal-Gordon
> Architechies
> 



More information about the swift-evolution mailing list