Skip to content

Commit

Permalink
ユニットテストもFileDict#loadに対応
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgto committed Dec 29, 2024
1 parent 10bab8b commit 8d49d01
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
37 changes: 23 additions & 14 deletions macSKKTests/FileDictTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ final class FileDictTests: XCTestCase {
let fileURL = Bundle(for: FileDictTests.self).url(forResource: "empty", withExtension: "txt")!
var cancellables: Set<AnyCancellable> = []

@MainActor func testLoadContainsBom() throws {
@MainActor func testLoadContainsBom() async throws {
let fileURL = Bundle(for: Self.self).url(forResource: "utf8-bom", withExtension: "txt")!
let dict = try FileDict(contentsOf: fileURL, type: .traditional(.utf8), readonly: true)
let dict = FileDict(contentsOf: fileURL, type: .traditional(.utf8), readonly: true)
try await dict.load(fileURL: fileURL)
XCTAssertEqual(dict.dict.entries, ["ゆにこーど": [Word("ユニコード")]])
}

@MainActor func testLoadJson() throws {
@MainActor func testLoadJson() async throws {
let expectation = XCTestExpectation()
NotificationCenter.default.publisher(for: notificationNameDictLoad).sink { notification in
if let loadEvent = notification.object as? DictLoadEvent {
Expand All @@ -28,13 +29,13 @@ final class FileDictTests: XCTestCase {
}
}.store(in: &cancellables)
let fileURL = Bundle(for: Self.self).url(forResource: "SKK-JISYO.test", withExtension: "json")!
let dict = try FileDict(contentsOf: fileURL, type: .json, readonly: true)
let dict = FileDict(contentsOf: fileURL, type: .json, readonly: true)
try await dict.load(fileURL: fileURL)
XCTAssertEqual(dict.dict.refer("", option: nil).map({ $0.word }).sorted(), ["", ""])
XCTAssertEqual(dict.dict.refer("あr", option: nil).map({ $0.word }).sorted(), ["在;注釈として解釈されない", ""])
wait(for: [expectation], timeout: 1.0)
}

@MainActor func testLoadJsonBroken() throws {
@MainActor func testLoadJsonBroken() async throws {
let expectation = XCTestExpectation()
NotificationCenter.default.publisher(for: notificationNameDictLoad).sink { notification in
if let loadEvent = notification.object as? DictLoadEvent {
Expand All @@ -44,12 +45,18 @@ final class FileDictTests: XCTestCase {
}
}.store(in: &cancellables)
let fileURL = Bundle(for: Self.self).url(forResource: "SKK-JISYO.broken", withExtension: "json")!
_ = try FileDict(contentsOf: fileURL, type: .json, readonly: true)
wait(for: [expectation], timeout: 1.0)
let dict = FileDict(contentsOf: fileURL, type: .json, readonly: true)
do {
try await dict.load(fileURL: fileURL)
XCTFail("エラーが発生するはずなのに発生していない")
} catch {
// OK
}
}

@MainActor func testAdd() throws {
let dict = try FileDict(contentsOf: fileURL, type: .traditional(.utf8), readonly: true)
@MainActor func testAdd() async throws {
let dict = FileDict(contentsOf: fileURL, type: .traditional(.utf8), readonly: true)
try await dict.load(fileURL: fileURL)
XCTAssertEqual(dict.entryCount, 0)
let word = Word("")
XCTAssertFalse(dict.hasUnsavedChanges)
Expand All @@ -58,17 +65,19 @@ final class FileDictTests: XCTestCase {
XCTAssertTrue(dict.hasUnsavedChanges)
}

@MainActor func testDelete() throws {
let dict = try FileDict(contentsOf: fileURL, type: .traditional(.utf8), readonly: true)
@MainActor func testDelete() async throws {
let dict = FileDict(contentsOf: fileURL, type: .traditional(.utf8), readonly: true)
try await dict.load(fileURL: fileURL)
dict.setEntries(["あr": [Word(""), Word("")]], readonly: true)
XCTAssertFalse(dict.delete(yomi: "あr", word: ""))
XCTAssertFalse(dict.hasUnsavedChanges)
XCTAssertTrue(dict.delete(yomi: "あr", word: ""))
XCTAssertTrue(dict.hasUnsavedChanges)
}

@MainActor func testSerialize() throws {
let dict = try FileDict(contentsOf: fileURL, type: .traditional(.utf8), readonly: false)
@MainActor func testSerialize() async throws {
let dict = FileDict(contentsOf: fileURL, type: .traditional(.utf8), readonly: false)
try await dict.load(fileURL: fileURL)
XCTAssertEqual(dict.serialize(),
[FileDict.headers[0], FileDict.okuriAriHeader, FileDict.okuriNashiHeader, ""].joined(separator: "\n"))
dict.add(yomi: "", word: Word("", annotation: Annotation(dictId: "testDict", text: "亜の注釈")))
Expand Down
10 changes: 5 additions & 5 deletions macSKKTests/MemoryDictTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MemoryDictTests: XCTestCase {
XCTAssertNil(dict.entries["から"])
}

func testAdd() throws {
@MainActor func testAdd() throws {
var dict = MemoryDict(entries: [:], readonly: false)
XCTAssertEqual(dict.entryCount, 0)
let word1 = Word("")
Expand Down Expand Up @@ -120,7 +120,7 @@ class MemoryDictTests: XCTestCase {
XCTAssertEqual(dict.refer("いt", option: nil), [Word("", okuri: "った"), Word("")])
}

func testDelete() throws {
@MainActor func testDelete() throws {
var dict = MemoryDict(entries: ["あr": [Word(""), Word("")], "": [Word(""), Word("")]], readonly: false)
XCTAssertFalse(dict.entries.isEmpty)
XCTAssertEqual(dict.okuriAriYomis, ["あr"])
Expand All @@ -145,14 +145,14 @@ class MemoryDictTests: XCTestCase {
XCTAssertTrue(dict.entries.isEmpty)
}

func testDeleteOkuriBlock() throws {
@MainActor func testDeleteOkuriBlock() throws {
var dict = MemoryDict(entries: ["あr": [Word("", okuri: ""), Word("", okuri: ""), Word("")]], readonly: false)
XCTAssertTrue(dict.delete(yomi: "あr", word: ""))
XCTAssertEqual(dict.refer("あr", option: nil), [], "あr を読みとして持つ変換候補が全て削除された")
XCTAssertEqual(dict.okuriAriYomis, [])
}

func testFindCompletion() throws {
@MainActor func testFindCompletion() throws {
var dict = MemoryDict(entries: [:], readonly: false)
XCTAssertNil(dict.findCompletion(prefix: ""), "辞書が空だとnil")
dict.add(yomi: "あいうえおか", word: Word("アイウエオカ"))
Expand All @@ -166,7 +166,7 @@ class MemoryDictTests: XCTestCase {
XCTAssertNil(dict.findCompletion(prefix: "だい"), "数値変換の読みはnil")
}

func testReferWithOption() {
@MainActor func testReferWithOption() {
let dict = MemoryDict(entries: ["あき>": [Word("空き")],
"あき": [Word("")],
">し": [Word("")],
Expand Down
10 changes: 5 additions & 5 deletions macSKKTests/UserDictTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Combine
@testable import macSKK

final class UserDictTests: XCTestCase {
func testRefer() throws {
@MainActor func testRefer() throws {
let dict1 = MemoryDict(entries: ["": [Word(""), Word("")]], readonly: true)
let dict2 = MemoryDict(entries: ["": [Word(""), Word("")]], readonly: true)
let userDict = try UserDict(dicts: [dict1, dict2],
Expand All @@ -32,7 +32,7 @@ final class UserDictTests: XCTestCase {
XCTAssertEqual(userDict.referDicts("").map({ $0.annotations.map({ $0.dictId }) }), [["dict1", "dict2"], [], []])
}

func testReferWithOption() throws {
@MainActor func testReferWithOption() throws {
let dict = MemoryDict(entries: ["あき>": [Word("空き")],
"あき": [Word("")],
">し": [Word("")],
Expand All @@ -54,7 +54,7 @@ final class UserDictTests: XCTestCase {
XCTAssertEqual(userDict.refer("", option: .prefix), [])
}

func testPrivateMode() throws {
@MainActor func testPrivateMode() throws {
let privateMode = CurrentValueSubject<Bool, Never>(false)
let userDict = try UserDict(dicts: [],
userDictEntries: ["": [Word("")]],
Expand All @@ -72,7 +72,7 @@ final class UserDictTests: XCTestCase {
XCTAssertTrue(userDict.delete(yomi: "", word: ""))
}

func testFindCompletionPrivateMode() throws {
@MainActor func testFindCompletionPrivateMode() throws {
let privateMode = CurrentValueSubject<Bool, Never>(true)
let ignoreUserDictInPrivateMode = CurrentValueSubject<Bool, Never>(false)
let dict1 = MemoryDict(entries: ["にほん": [Word("日本")], "にほ": [Word("2歩")]], readonly: false)
Expand All @@ -92,7 +92,7 @@ final class UserDictTests: XCTestCase {
XCTAssertEqual(userDict.findCompletion(prefix: ""), "にふ")
}

func testFindCompletionFromAllDicts() throws {
@MainActor func testFindCompletionFromAllDicts() throws {
let privateMode = CurrentValueSubject<Bool, Never>(false)
let ignoreUserDictInPrivateMode = CurrentValueSubject<Bool, Never>(false)
let findCompletionFromAllDicts = CurrentValueSubject<Bool, Never>(false)
Expand Down

0 comments on commit 8d49d01

Please sign in to comment.