Brightify / Cuckoo

Boilerplate-free mocking framework for Swift!
MIT License
1.67k stars 174 forks source link

Bugfix: Accessibility.open init from token #511

Closed aim2120 closed 1 month ago

aim2120 commented 2 months ago

Description

The Accessibility initializer is not properly initializing the open case when for the version of swift-syntax used by the project. This was resulting in open class mocks being generated with the default (internal) access level.

Bug

With the library's current behavior, this code:

open class MyOpenClass {
  open func myFunc() { }
}

would generate this mock (simplified for readability):

class MockMyOpenClass {
  override func myFunc() { ... }
}

Fix

Match .keyword(.open) instead of .identifier("open") for the open access level.

With this change, the hypothetical class would be generated as:

public class MockMyOpenClass {
  public override func myFunc() { ... }
}

Tests

To create access level separation between mocks and test code, I've created CuckooMocks frameworks for each platform, each depended upon by each corresponding test target. The only change to existing tests is that this framework must be imported. More importantly, this separation allows for tests to prove that access levels of mocks are properly generated.

I'm open to feedback from the maintainer if they have differences in preference in this project structuring. If there were a way to determine access level from another method (e.g. reflection), I would opt for that, but it seems that this is really the only way with Swift to prove internal vs. public access levels for generated mocks.

MatyasKriz commented 1 month ago

I've squashed the commits and merged it. Thank you so much for this PR, mainly for the separation of mocks into its own PR, it will help with accessibility testing.