Skip to content

Commit

Permalink
[MOB-3028] Update tests and made the Language.make function thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
d4r1091 committed Dec 18, 2024
1 parent deb8b4a commit 9d779a9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,11 @@ struct EcosiaURLProvider {

public internal(set) static var current = make(for: .current)

private static let queue = DispatchQueue(label: "\(Bundle.main.bundleIdentifier!).LanguageQueue")
static func make(for locale: Locale) -> Self {
locale.withLanguage ?? .en
return queue.sync {
locale.withLanguage ?? .en
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Ecosia/Core/Language.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ public enum Language: String, Codable, CaseIterable {
}
}

private static let queue = DispatchQueue(label: "\(Bundle.ecosia.bundleIdentifier!).LanguageQueue")
static func make(for locale: Locale) -> Self {
locale.withLanguage ?? .en
return queue.sync {
locale.withLanguage ?? .en
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion EcosiaTests/Core/LanguageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import XCTest

final class LanguageTests: XCTestCase {
func testCurrent() {
func testSettingLanguageCurrentToUnexpectedLanguageReturnsDefault() {
Language.current = Language.make(for: Locale(identifier: "nz"))
XCTAssertEqual(.en, Language.current)
}

Expand Down
1 change: 1 addition & 0 deletions EcosiaTests/Core/LocalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import XCTest

final class LocalTests: XCTestCase {
func testCurrent() {
Language.current = .en
XCTAssertEqual("en-us", Language.current.locale.rawValue)
}

Expand Down

0 comments on commit 9d779a9

Please sign in to comment.