XCTest Ambiguous type name

Hi

I've create a couple of files that are in the build target and "checked" the box to include these files in the test target.

Here are the files in the build and test targets.

DefaultValue+Extension.swift

extension Bool {
   public enum False: DefaultValue {
      public static let defaultValue = false
   }

   public enum True: DefaultValue {
      public static let defaultValue = true
   }
}

extension String {
   public enum Empty: DefaultValue {
      public static let defaultValue = ""
   }
}

DefaultValuePropertyWrapper.swift

public protocol DefaultValue {
   associatedtype Value: Decodable
   static var defaultValue: Value {
      get
   }
}


@propertyWrapper
public struct Default<T: DefaultValue> {
   public var wrappedValue: T.Value
   
   public init() {
      self.wrappedValue = T.defaultValue
   }
}

extension Default: Decodable {
   public init(from decoder: Decoder) throws {
      let container = try decoder.singleValueContainer()
      self.wrappedValue = try container.decode(T.Value.self)
   }
}

extension Default: Encodable where T.Value: Encodable {
   public func encode(to encoder: Encoder) throws {
      var container = encoder.singleValueContainer()
      try container.encode(self.wrappedValue)
   }
}

extension Default: Equatable where T.Value: Equatable {}
extension Default: Hashable where T.Value: Hashable{}

extension KeyedDecodingContainer {
   public func decode<T>(_ type: Default<T>.Type, forKey key: Key) throws -> Default<T> where T: DefaultValue {
   try decodeIfPresent(type, forKey: key) ?? .init()
   }
}

Running the unit test I encounter a several errors which I haven't been able to resolve. Here is my swift file with only the test target checked.

DefaultValueTest.swift

import XCTest
@testable import MyFramework

class DefaultValueTests: XCTestCase {
   override func setUpWithError() throws {
   }
   
   override func tearDownWithError() throws {
   }

   struct Person: Decodable {
      let userId: Int
      let name: String
      @Default<String.Empty> var nickName: String
      @Default<Bool.False> var isEnabled: Bool
      @Default<Bool.True> var isAdmin: Bool
   }

   func testDefaultValues() throws {
      let json = """
      {
         "userId": 1,
         "name": "John"
      }
      """

      do {
         let result = try JSONDecoder().decode(Person.self, from: json.data(using: .utf8)!)

         XCTAssertTrue(result.nickname.isEmpty)
         XCTAssertFalse(result.isEnabled)
         XCTAssertTrue(result.isAdmin)
      }
      catch let error {
         print("Error: \(error.localizedDescription)")
         XCTFail()
     }
}       

The Person structure in DefaultValueTest.swift complains of the following:

  • Type DefaultValueTests.Person does not conform to protocol Decodable
  • nickName Ambiguous type name 'Empty' in 'String'
  • isEnabled Ambiguous type name 'False' in 'Bool'
  • isAdmin Ambiguous type name 'True' in 'Bool'

When I build the framework and use it another project, everything works as expected...

Appreciate any advice 🙏

Replies

I managed to get the errors to go away. I "unchecked" DefaultValue+Extension.swift and DefaultValuePropertyWrapper.swift from being part of the test target and in the DefaultValueTest.swift file replaced @testable import MyFramework with import MyFramework.

Compiles and the tests run successfully now 😅