From 64d1afba83c2506acf06a6f6b861af08342f6590 Mon Sep 17 00:00:00 2001 From: ssig33 Date: Tue, 28 Oct 2025 16:26:55 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E8=87=AA=E5=8B=95?= =?UTF-8?q?=E5=8C=96=E3=81=A8=E5=93=81=E8=B3=AA=E6=8B=85=E4=BF=9D=E3=81=AE?= =?UTF-8?q?=E5=BC=B7=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CalendarServiceに対するユニットテスト(ロード、保存の挙動)を実装 - Makefileにtestターゲットを追加し、コマンドラインからテスト実行を可能化 - Xcodeプロジェクト(yml)にテストターゲット(OreCalendarTests)とtest定義を追加 --- Makefile | 5 +++- Tests/CalendarServiceTests.swift | 50 ++++++++++++++++++++++++++++++++ project.yml | 12 ++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 Tests/CalendarServiceTests.swift diff --git a/Makefile b/Makefile index 4e122dd..2bb7150 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: generate build all +.PHONY: generate build all test all: build @@ -7,6 +7,9 @@ generate: build: generate xcodebuild -scheme OreCalendar -destination 'platform=macOS' -derivedDataPath build build +test: generate + xcodebuild test -scheme OreCalendar -destination 'platform=macOS' -derivedDataPath build + release: build rm -rf ~/Applications/OreCalendar.app ; true mv build/Build/Products/Release/OreCalendar.app ~/Applications/OreCalendar.app diff --git a/Tests/CalendarServiceTests.swift b/Tests/CalendarServiceTests.swift new file mode 100644 index 0000000..18bc8d4 --- /dev/null +++ b/Tests/CalendarServiceTests.swift @@ -0,0 +1,50 @@ +import XCTest +import EventKit +@testable import OreCalendar + +@MainActor +final class CalendarServiceTests: XCTestCase { + var service: CalendarService! + let testKey = "selectedCalendars" + + override func setUp() async throws { + try await super.setUp() + service = CalendarService() + UserDefaults.standard.removeObject(forKey: testKey) + } + + override func tearDown() async throws { + UserDefaults.standard.removeObject(forKey: testKey) + service = nil + try await super.tearDown() + } + + func testLoadDefaultSelectedCalendars_withSavedCalendars() async throws { + let savedCalendarIDs = ["calendar1", "calendar2", "calendar3"] + UserDefaults.standard.set(savedCalendarIDs, forKey: testKey) + + service.loadDefaultSelectedCalendars() + + XCTAssertEqual(service.selectedCalendars, Set(savedCalendarIDs)) + } + + func testLoadDefaultSelectedCalendars_withoutSavedCalendars() async throws { + UserDefaults.standard.removeObject(forKey: testKey) + + service.loadDefaultSelectedCalendars() + + let expectedCalendarIDs = Set(service.availableCalendars.map { $0.calendarIdentifier }) + XCTAssertEqual(service.selectedCalendars, expectedCalendarIDs) + } + + func testSaveSelectedCalendars() async throws { + let testCalendarIDs = ["cal1", "cal2", "cal3"] + service.selectedCalendars = Set(testCalendarIDs) + + service.saveSelectedCalendars() + + let savedCalendars = UserDefaults.standard.array(forKey: testKey) as? [String] + XCTAssertNotNil(savedCalendars) + XCTAssertEqual(Set(savedCalendars!), Set(testCalendarIDs)) + } +} diff --git a/project.yml b/project.yml index 920a7d8..9ad51f6 100644 --- a/project.yml +++ b/project.yml @@ -11,6 +11,9 @@ schemes: config: Release archive: config: Release + test: + targets: + - OreCalendarTests targets: OreCalendar: platform: macOS @@ -29,4 +32,13 @@ targets: ASSETCATALOG_COMPILER_APPICON_NAME: AppIcon INFOPLIST_KEY_NSCalendarsUsageDescription: "カレンダーのイベントを表示するためにアクセスが必要です" INFOPLIST_KEY_NSCalendarsFullAccessUsageDescription: "カレンダーのイベントを表示するためにフルアクセスが必要です" + OreCalendarTests: + platform: macOS + type: bundle.unit-test + sources: + - Tests + dependencies: + - target: OreCalendar + settings: + GENERATE_INFOPLIST_FILE: true