Skip to content

Commit

Permalink
Merge pull request #9 from JosephDuffy/improve-testing
Browse files Browse the repository at this point in the history
Improve testing
  • Loading branch information
JosephDuffy authored Feb 8, 2024
2 parents d6efa5f + 4243bc1 commit b17f467
Show file tree
Hide file tree
Showing 6 changed files with 917 additions and 164 deletions.
42 changes: 19 additions & 23 deletions Sources/HashableMacroMacros/Macros/HashableMacro+Hashable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ extension HashableMacro {

let baseModifiers = declaration.modifiers.filter({ modifier in
switch (modifier.name.tokenKind) {
case .keyword(.public):
case .keyword(.public), .keyword(.internal), .keyword(.fileprivate), .keyword(.package):
// Only public is truly needed, but we can be explicit with the others.
return true
case .keyword(.internal):
case .keyword(.open) where !finalHashInto:
return true
case .keyword(.fileprivate):
return true
case .keyword(.private):
// The added functions should never be private
case .keyword(.private), .keyword(.open):
// The added functions should never be private because they're in an extension. open
// is also ignored when `hash(into:)` is final.
return false
default:
return false
Expand Down Expand Up @@ -115,22 +115,6 @@ extension HashableMacro {
)
}

let baseModifiers = declaration.modifiers.filter({ modifier in
switch (modifier.name.tokenKind) {
case .keyword(.public):
return true
case .keyword(.internal):
return true
case .keyword(.fileprivate):
return true
case .keyword(.private):
// The added functions should never be private
return false
default:
return false
}
})

let equalsFunctionSignature = FunctionSignatureSyntax(
parameterClause: FunctionParameterClauseSyntax(
parameters: [
Expand Down Expand Up @@ -205,7 +189,19 @@ extension HashableMacro {
})
)

var equalsFunctionModifiers = baseModifiers
var equalsFunctionModifiers = DeclModifierListSyntax(declaration.modifiers.compactMap { modifier in
switch (modifier.name.tokenKind) {
case .keyword(.public), .keyword(.internal), .keyword(.fileprivate), .keyword(.package):
// Only public is truly needed, but we can be explicit with the others.
return modifier
case .keyword(.open):
// `==` is implicitly final; it cannot be open but it does need to be public.
return DeclModifierSyntax(name: .keyword(.public))
default:
// Anything else, e.g. private and final, should be discarded.
return nil
}
})
equalsFunctionModifiers.append(
DeclModifierSyntax(name: .keyword(.static))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@ extension HashableMacro {
in context: some MacroExpansionContext,
propertiesToHash: [TokenSyntax]
) -> DeclSyntax {
let baseModifiers = declaration.modifiers.filter({ modifier in
var hashPropertyModifiers = DeclModifierListSyntax(declaration.modifiers.compactMap { modifier in
switch (modifier.name.tokenKind) {
case .keyword(.public):
return true
case .keyword(.internal):
return true
case .keyword(.fileprivate):
return true
case .keyword(.private):
// The added functions should never be private
return false
case .keyword(.public), .keyword(.internal), .keyword(.fileprivate), .keyword(.package):
// Only public is truly needed, but we can be explicit with the others.
return modifier
case .keyword(.open):
// This property is final and cannot be open.
var modifier = modifier
modifier.name.tokenKind = .keyword(.public)
return modifier
default:
return false
// Anything else, e.g. private and final, should be discarded.
return nil
}
})

var hashPropertyModifiers = baseModifiers
hashPropertyModifiers.append(
DeclModifierSyntax(name: .keyword(.override))
)
Expand Down Expand Up @@ -109,18 +107,6 @@ extension HashableMacro {
propertiesToHash: [TokenSyntax],
isEqualToTypeFunctionName: IsEqualToTypeFunctionNameGeneration
) -> [DeclSyntax] {
let baseModifiers = declaration.modifiers.filter({ modifier in
switch (modifier.name.tokenKind) {
case .keyword(.public), .keyword(.internal), .keyword(.fileprivate), .keyword(.open):
return true
case .keyword(.private):
// The added functions should never be private
return false
default:
return false
}
})

var comparisons: InfixOperatorExprSyntax?

for propertyToken in propertiesToHash {
Expand Down Expand Up @@ -269,7 +255,16 @@ extension HashableMacro {
})
)

var equalsFunctionModifiers = baseModifiers
var equalsFunctionModifiers = declaration.modifiers.filter({ modifier in
switch (modifier.name.tokenKind) {
case .keyword(.open), .keyword(.public), .keyword(.internal), .keyword(.fileprivate), .keyword(.package):
// Only open and public are truly needed, but we can be explicit with the others.
return true
default:
// Anything else, e.g. private and final, should be discarded.
return false
}
})
equalsFunctionModifiers.append(
DeclModifierSyntax(name: .keyword(.override))
)
Expand Down Expand Up @@ -302,6 +297,25 @@ extension HashableMacro {
objectiveCName = .identifier(customName)
}

var isEqualTypedFunctionModifiers = DeclModifierListSyntax(declaration.modifiers.compactMap { modifier in
switch (modifier.name.tokenKind) {
case .keyword(.public), .keyword(.internal), .keyword(.fileprivate), .keyword(.package):
// Only public is truly needed, but we can be explicit with the others.
return modifier
case .keyword(.open):
// This function is final and cannot be open.
var modifier = modifier
modifier.name.tokenKind = .keyword(.public)
return modifier
default:
// Anything else, e.g. private and final, should be discarded.
return nil
}
})
isEqualTypedFunctionModifiers.append(
DeclModifierSyntax(name: .keyword(.final))
)

let isEqualTypedFunction = FunctionDeclSyntax(
attributes: AttributeListSyntax {
.attribute(
Expand All @@ -316,7 +330,7 @@ extension HashableMacro {
)
)
},
modifiers: baseModifiers,
modifiers: isEqualTypedFunctionModifiers,
name: .identifier("isEqual"),
signature: FunctionSignatureSyntax(
parameterClause: FunctionParameterClauseSyntax(
Expand Down
33 changes: 2 additions & 31 deletions Sources/HashableMacroMacros/Macros/HashableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,37 +294,9 @@ public struct HashableMacro: ExtensionMacro {
)
protocolExtensions.append(hashPropertyExtension)
protocolExtensions.append(isEqualImplementationExtension)
} else {
let hashableImplementationExtension = ExtensionDeclSyntax(
extendedType: type,
memberBlock: MemberBlockSyntax(
members: MemberBlockItemListSyntax(itemsBuilder: {
expansionForHashable(
of: node,
providingMembersOf: declaration,
in: context,
propertiesToHash: propertiesToHash
)
})
)
)
let equatableImplementationExtension = ExtensionDeclSyntax(
extendedType: type,
memberBlock: MemberBlockSyntax(
members: try MemberBlockItemListSyntax(itemsBuilder: {
try expansionForEquals(
of: node,
providingMembersOf: declaration,
in: context,
propertiesToHash: propertiesToHash
)
})
)
)
protocolExtensions.append(hashableImplementationExtension)
protocolExtensions.append(equatableImplementationExtension)
return protocolExtensions
}
#else
#endif
let hashableImplementationExtension = ExtensionDeclSyntax(
extendedType: type,
memberBlock: MemberBlockSyntax(
Expand Down Expand Up @@ -353,7 +325,6 @@ public struct HashableMacro: ExtensionMacro {
)
protocolExtensions.append(hashableImplementationExtension)
protocolExtensions.append(equatableImplementationExtension)
#endif

return protocolExtensions
}
Expand Down
54 changes: 50 additions & 4 deletions Tests/HashableMacroTests/HashableMacroAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ import HashableMacro
import Foundation

#if compiler(>=5.9.2)
@Hashable
public struct PublicStruct {
@Hashed
var hashableProperty: String
}

@Hashable
package struct PackageStruct {
@Hashed
var hashableProperty: String
}

@Hashable
internal struct ExplicitInternalStruct {
@Hashed
var hashableProperty: String
}

@Hashable
fileprivate struct FileprivateStruct {
@Hashed
var hashableProperty: String
}

@Hashable
private struct PrivateStruct {
@Hashed
var hashableProperty: String
}

@Hashable
struct HashableStructWithExplicitlyIncludedProperties {
@Hashed
Expand Down Expand Up @@ -113,6 +143,24 @@ public class HashableClassWithPrivateProperty: Hashable {
}
}

@Hashable(finalHashInto: false)
public class HashableClassWithNonFinalHashInto: Hashable {
@Hashed
let firstProperty: Int

@Hashed
let secondProperty: Int

@Hashed
private let privateProperty: Int

init(firstProperty: Int, secondProperty: Int, privateProperty: Int) {
self.firstProperty = firstProperty
self.secondProperty = secondProperty
self.privateProperty = privateProperty
}
}

/// A type that explicitly conforms to `Hashable`; the macro should not try to
/// add conformance (but it should still add the implementation required).
@Hashable
Expand Down Expand Up @@ -184,13 +232,11 @@ class NSObjectSubclass: NSObject {
}

@Hashable(isEqualToTypeFunctionName: .custom("isEqualToObject:"))
class NSObjectSubclassCustomEqualTo: NSObject {
public class NSObjectSubclassCustomEqualTo: NSObject {
@Hashed
var nsObjectSubclassProperty: String

init(
nsObjectSubclassProperty: String
) {
init(nsObjectSubclassProperty: String) {
self.nsObjectSubclassProperty = nsObjectSubclassProperty
}
}
Expand Down
Loading

0 comments on commit b17f467

Please sign in to comment.