diff --git a/Package.swift b/Package.swift index ec9e602..c0fb425 100644 --- a/Package.swift +++ b/Package.swift @@ -22,7 +22,8 @@ let package = Package( .package(url: "https://github.com/what3words/w3w-swift-design.git", "1.0.0" ..< "2.0.0"), .package(url: "git@github.com:what3words/w3w-swift-design-swiftui.git", "1.0.0" ..< "2.0.0"), .package(url: "git@github.com:w3w-internal/w3w-swift-app-events.git", "5.0.0" ..< "6.0.0"), - .package(url: "https://github.com/what3words/w3w-swift-presenters.git", "1.0.0" ..< "2.0.0") + .package(url: "https://github.com/what3words/w3w-swift-presenters.git", "1.0.0" ..< "2.0.0"), + .package(url: "git@github.com:w3w-internal/w3w-swift-app-types.git", "5.0.0"..<"6.0.0"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. @@ -35,7 +36,8 @@ let package = Package( .product(name: "W3WSwiftDesign", package: "w3w-swift-design"), .product(name: "W3WSwiftDesignSwiftUI", package: "w3w-swift-design-swiftui"), .product(name: "W3WSwiftPresenters", package: "w3w-swift-presenters"), - .product(name: "W3WSwiftAppEvents", package: "w3w-swift-app-events") + .product(name: "W3WSwiftAppEvents", package: "w3w-swift-app-events"), + .product(name: "W3WSwiftAppTypes", package: "w3w-swift-app-types") ], resources: [.process("Resources")] ), diff --git a/Sources/W3WSwiftComponentsOcr/Lib/Ocr/W3WOcrDataTypes.swift b/Sources/W3WSwiftComponentsOcr/Lib/Ocr/W3WOcrDataTypes.swift index 7f47407..2fc8677 100644 --- a/Sources/W3WSwiftComponentsOcr/Lib/Ocr/W3WOcrDataTypes.swift +++ b/Sources/W3WSwiftComponentsOcr/Lib/Ocr/W3WOcrDataTypes.swift @@ -124,6 +124,27 @@ public struct W3WOcrLanguage { //}: Hashable { public static let english = W3WOcrLanguage(name: "English", nativeName: "English", code: "en") } +public extension W3WRfcLanguageProtocol { + /// convert W3WRfcLanguage to W3WOcrLanguage + func toW3WOcrLanguage() -> W3WOcrLanguage? { + guard let langCode, !langCode.isEmpty else { return nil } + if let language = self as? W3WRfcLanguage { + return W3WOcrLanguage(name: language.name , nativeName: language.nativeName, code: langCode) + } else { + return W3WOcrLanguage(name: LanguageUtils.getLanguageName(forLocale: langCode, inLocale: "en"), + nativeName: LanguageUtils.getLanguageName(forLocale: langCode, inLocale: langCode), + code: langCode) + } + } +} + +@available(iOS 13.0.0, *) +@available(watchOS 6.0.0, *) +extension W3WOcrLanguage: W3WRfcLanguageConvertable { + public func toRfcLanguage() -> some W3WRfcLanguageProtocol { + return W3WRfcLanguage(from: code) + } +} // as in the Sdk public struct W3WOcrSuggestion: W3WSuggestion { diff --git a/Tests/w3w-swift-components-ocrTests/w3w_swift_components_ocrTests.swift b/Tests/w3w-swift-components-ocrTests/w3w_swift_components_ocrTests.swift deleted file mode 100644 index 0c44cc5..0000000 --- a/Tests/w3w-swift-components-ocrTests/w3w_swift_components_ocrTests.swift +++ /dev/null @@ -1,112 +0,0 @@ -import XCTest -import W3WSwiftCore -@testable import W3WSwiftComponentsOcr - - -final class w3w_swift_components_ocrTests: XCTestCase { - - // fake API to inject into tests - var api = FakeApi() - - - /// Basic test to make sure it works from begining to end - @available(iOS 13.0, *) - func testWithAutosuggest() { - let expectation = self.expectation(description: "testWithAutosuggest") - - // make OCR view controller - let ocrVC = W3WOcrViewController(ocr: W3WOcrNative(api), w3w: api) - ocrVC.camera?.set(crop: CGRect(x: 0.0, y: 0.0, width: 400.0, height: 300.0)) - XCTAssertEqual(ocrVC.state, .idle) - - // wait for image to be recognised - ocrVC.onReceiveRawSuggestions = { rawSuggestions in - XCTAssertEqual(ocrVC.state, .scanning) - XCTAssertFalse(rawSuggestions.isEmpty) - } - - ocrVC.onSuggestions = { suggestions in - XCTAssertEqual(ocrVC.state, .scanned) - guard let firstSuggestion = suggestions.first else { - XCTFail("Suggestions should not be empty") - return - } - XCTAssertEqual(firstSuggestion.nearestPlace, "Bayswater, UK") - XCTAssertEqual(firstSuggestion.language?.code, "en") - XCTAssertEqual(firstSuggestion.country?.code, "GB") - XCTAssertEqual(firstSuggestion.distanceToFocus?.kilometers, 1.0) - expectation.fulfill() - } - - ocrVC.start() - XCTAssertEqual(ocrVC.state, .detecting) - - // The OCR simulated camera cycles through 4 possibilities, and the FakeApi tries - // to match a real twa to the input. This usually succeed within a couple seconds - // but we use a long timeout to make a false negative a million to one chance. - waitForExpectations(timeout: 300.0, handler: nil) - } - - @available(iOS 13.0, *) - func testWithoutAutosuggest() { - let expectation = self.expectation(description: "testWithoutAutosuggest") - - // make OCR view controller - let ocrVC = W3WOcrViewController(ocr: W3WOcrNative(api)) - ocrVC.camera?.set(crop: CGRect(x: 0.0, y: 0.0, width: 400.0, height: 300.0)) - XCTAssertEqual(ocrVC.state, .idle) - - // wait for image to be recognised - ocrVC.onReceiveRawSuggestions = { rawSuggestions in - XCTAssertEqual(ocrVC.state, .scanning) - XCTAssertFalse(rawSuggestions.isEmpty) - } - - ocrVC.onSuggestions = { suggestions in - XCTAssertEqual(ocrVC.state, .scanned) - guard let firstSuggestion = suggestions.first else { - XCTFail("Suggestions should not be empty") - return - } - XCTAssertEqual(firstSuggestion.nearestPlace, "Bayswater, UK") - XCTAssertEqual(firstSuggestion.language?.code, "en") - XCTAssertEqual(firstSuggestion.country?.code, "GB") - XCTAssertEqual(firstSuggestion.distanceToFocus?.kilometers, 1.0) - expectation.fulfill() - } - - ocrVC.start() - XCTAssertEqual(ocrVC.state, .detecting) - - // The OCR simulated camera cycles through 4 possibilities, and the FakeApi tries - // to match a real twa to the input. This usually succeed within a couple seconds - // but we use a long timeout to make a false negative a million to one chance. - waitForExpectations(timeout: 300.0, handler: nil) - } - - @available(iOS 13.0, *) - func testWithError() { - let expectation = self.expectation(description: "testWithError") - expectation.assertForOverFulfill = false - - // make OCR view controller - let api = FakeApiWithError() - let ocrVC = W3WOcrViewController(ocr: W3WOcrNative(api)) - ocrVC.camera?.set(crop: CGRect(x: 0.0, y: 0.0, width: 400.0, height: 300.0)) - XCTAssertEqual(ocrVC.state, .idle) - - ocrVC.onError = { error in - XCTAssertEqual(error.description, "Fake error") - XCTAssertEqual(ocrVC.state, .error) - expectation.fulfill() - } - - ocrVC.start() - XCTAssertEqual(ocrVC.state, .detecting) - - // The OCR simulated camera cycles through 4 possibilities, and the FakeApi tries - // to match a real twa to the input. This usually succeed within a couple seconds - // but we use a long timeout to make a false negative a million to one chance. - waitForExpectations(timeout: 300.0, handler: nil) - } -}