Skip to content

Support generic test function #202

Closed as not planned
Closed as not planned
@Kyle-Ye

Description

@Kyle-Ye

Description

We have some generic interface in the codebase. And I'd request generic function support for @Test macro.

The following is a simplified version of my use case.

protocol Key {
	associated Value
	static var defaultValue: Value
	static func reduce(value: inout Value, nextValue: () -> Value)
}
struct AddIntKey: Key {
    static var defaultValue: Int { 0 }
    
    static func reduce(value: inout Int, nextValue: () -> Int) {
        value += nextValue()
    }
}
struct MultipleIntKey: Key {
    static var defaultValue: Int { 0 }
    
    static func reduce(value: inout Int, nextValue: () -> Int) {
        value *= nextValue()
    }
}

Expected behavior

@Test(arguments: [ // ✅
    (AddIntKey.self, [1, 2], 3)
	(MultipleIntKey.self, [1, 2], 2)
])
func reduce<Key: PreferenceKey>(
    type: Key.Type, values: [Key.Value], expectedValues: Key.Value
) {
	...
}

Actual behavior

@Test(arguments: [ // ❌: The @Test attribute cannot be applied to a generic function.
    (AddIntKey.self, [1, 2], 3)
	(MultipleIntKey.self, [1, 2], 2)
])
func reduce<Key: PreferenceKey>(
    type: Key.Type, values: [Key.Value], expectedValues: Key.Value
) {
	...
}

Steps to reproduce

No response

swift-testing version/commit hash

No response

Swift & OS version (output of swift --version && uname -a)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestwontfix❌ This will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions