Cuckoo icon indicating copy to clipboard operation
Cuckoo copied to clipboard

Mock generation fails with certain property wrappers

Open hannessolo opened this issue 5 years ago • 3 comments

Expected Behavior

The run script successfully generates all mocks.

Current Behavior

The run script crashes with the message Fatal error: String index is out of bounds

Steps to Reproduce

  1. Create two files:
// UsefulClass.swift
class UsefulClass {
    @SomeWrapper(useValue: "foo")
    private var myString
    
}
// SomeWrapper.swift
@propertyWrapper
struct SomeWrapper {
    var wrappedValue: String

    init(useValue: String) {
        self.wrappedValue = useValue
    }
}

Compile test sources with the following script (Try to mock UsefulClass):

# Define output file. Change "${PROJECT_DIR}/${PROJECT_NAME}Tests" to your test's root source folder, if it's not the default name.
OUTPUT_FILE="${PROJECT_DIR}/${PROJECT_NAME}Tests/GeneratedMocks.swift"
echo "Generated Mocks File = ${OUTPUT_FILE}"

# Define input directory. Change "${PROJECT_DIR}/${PROJECT_NAME}" to your project's root source folder, if it's not the default name.
INPUT_DIR="${PROJECT_DIR}/${PROJECT_NAME}"
echo "Mocks Input Directory = ${INPUT_DIR}"

# Generate mock files, include as many input files as you'd like to create mocks for.
"${PODS_ROOT}/Cuckoo/run" generate --testable "${PROJECT_NAME}" \
--output "${OUTPUT_FILE}" \
"${INPUT_DIR}/UsefulClass.swift"

The script will produce the error.

The error can be avoided if you give additional type information:

class UsefulClass {
    @SomeWrapper(useValue: "foo")
    private var myString: String
    
}

Notice that here I give the variable myString an explicit type. This should not be required however, as the Swift compiler infers this type. With the explicit type, the script generates the mocks without any issue.

hannessolo avatar May 04 '20 13:05 hannessolo

Hey, @hannessolo. We're using SourceKitten to parse the code which only goes as far as syntactic analysis and therefore we have no way of inferring the type of the variable.

I suppose this could be worked around in some way, but at the moment it's not up there on our priority list.

MatyasKriz avatar May 08 '20 10:05 MatyasKriz

Thanks, makes sense. It just threw me off and took me a while to debug because the error message wasn't particularly useful (Fatal error: String index is out of bounds at first made me think that the source file is too long or something along those lines).

Perhaps a better error message could be implemented, although at first glance this may not be trivial - something along the lines of Type inference failed.

Otherwise, maybe the readme could be edited to have a section on when type inference works (simple instance variables etc) and when it fails (property wrappers and probably some other niche cases).

Anyway, thanks a lot for the fantastic library!

hannessolo avatar May 08 '20 10:05 hannessolo

Oh, yeah. I'm sorry about that. I can imagine that could've taken a while to debug.

Error and warning reporting is something we've been eager to add to Cuckoo for a while now. However, it requires quite a lot of changes as the library was created like most software just to serve a purpose and then it caught on and grew.

Adding a section to readme about type inference is a pretty good idea. I'll make sure to do that, thanks!

I think error logging (with filename and line no.) is about the most important feature we're missing at the moment just to avoid these bug hunts. Hopefully we'll have the time to add it soon.

We're glad you're enjoying Cuckoo and let us know if you have any other ideas for improvements! 🙂

MatyasKriz avatar May 08 '20 10:05 MatyasKriz