From 29813f93e5b7aa918e1dc091128e3ce6ddda5ae0 Mon Sep 17 00:00:00 2001 From: zonble Date: Tue, 18 Jan 2022 17:05:51 +0800 Subject: [PATCH] Adds test cases. --- .../continuous-integration-workflow.yml | 4 +- McBopomofo.xcodeproj/project.pbxproj | 162 ++++++++++++++++++ .../xcschemes/McBopomofo.xcscheme | 10 ++ McBopomofoTests/PreferencesTests.swift | 158 +++++++++++++++++ Source/InputMethodController.mm | 4 +- Source/Preferences.swift | 27 ++- 6 files changed, 359 insertions(+), 6 deletions(-) create mode 100644 McBopomofoTests/PreferencesTests.swift diff --git a/.github/workflows/continuous-integration-workflow.yml b/.github/workflows/continuous-integration-workflow.yml index f52cf8cd..b73aaa0e 100644 --- a/.github/workflows/continuous-integration-workflow.yml +++ b/.github/workflows/continuous-integration-workflow.yml @@ -3,7 +3,7 @@ on: [push] jobs: build: - name: Build + name: Build and Test runs-on: macOS-latest env: DEVELOPER_DIR: /Applications/Xcode.app/Contents/Developer @@ -17,6 +17,8 @@ jobs: run: xcodebuild -scheme McBopomofo -configuration Release build - name: Build run: xcodebuild -scheme McBopomofoInstaller -configuration Release build + - name: Test + run: xcodebuild -scheme McBopomofo -configuration Debug test - name: Test CandidateUI run: swift test working-directory: Packages/CandidateUI diff --git a/McBopomofo.xcodeproj/project.pbxproj b/McBopomofo.xcodeproj/project.pbxproj index 64f9430c..e4a61870 100644 --- a/McBopomofo.xcodeproj/project.pbxproj +++ b/McBopomofo.xcodeproj/project.pbxproj @@ -55,6 +55,7 @@ D47F7DCE278BFB57002F9DD7 /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DCD278BFB57002F9DD7 /* PreferencesWindowController.swift */; }; D47F7DD0278C0897002F9DD7 /* NonModalAlertWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DCF278C0897002F9DD7 /* NonModalAlertWindowController.swift */; }; D47F7DD3278C1263002F9DD7 /* UserOverrideModel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DD2278C1263002F9DD7 /* UserOverrideModel.cpp */; }; + D485D3B92796A8A000657FF3 /* PreferencesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D485D3B82796A8A000657FF3 /* PreferencesTests.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -72,6 +73,13 @@ remoteGlobalIDString = 6A0D4EA115FC0D2D00ABF4B3; remoteInfo = McBopomofo; }; + D485D3BA2796A8A000657FF3 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 6A0D4E9415FC0CFA00ABF4B3 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 6A0D4EA115FC0D2D00ABF4B3; + remoteInfo = McBopomofo; + }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ @@ -191,6 +199,8 @@ D47F7DCF278C0897002F9DD7 /* NonModalAlertWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonModalAlertWindowController.swift; sourceTree = ""; }; D47F7DD1278C1263002F9DD7 /* UserOverrideModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UserOverrideModel.h; sourceTree = ""; }; D47F7DD2278C1263002F9DD7 /* UserOverrideModel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UserOverrideModel.cpp; sourceTree = ""; }; + D485D3B62796A8A000657FF3 /* McBopomofoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = McBopomofoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + D485D3B82796A8A000657FF3 /* PreferencesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -218,6 +228,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + D485D3B32796A8A000657FF3 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -226,6 +243,7 @@ children = ( D427F766278C9CBD004A2160 /* Packages */, 6A0D4EC215FC0D3C00ABF4B3 /* Source */, + D485D3B72796A8A000657FF3 /* McBopomofoTests */, 6A0D4EA515FC0D2D00ABF4B3 /* Frameworks */, 6A0D4EA315FC0D2D00ABF4B3 /* Products */, ); @@ -236,6 +254,7 @@ children = ( 6A0D4EA215FC0D2D00ABF4B3 /* McBopomofo.app */, 6ACA41CB15FC1D7500935EF6 /* McBopomofoInstaller.app */, + D485D3B62796A8A000657FF3 /* McBopomofoTests.xctest */, ); name = Products; sourceTree = ""; @@ -431,6 +450,14 @@ name = Packages; sourceTree = ""; }; + D485D3B72796A8A000657FF3 /* McBopomofoTests */ = { + isa = PBXGroup; + children = ( + D485D3B82796A8A000657FF3 /* PreferencesTests.swift */, + ); + path = McBopomofoTests; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXLegacyTarget section */ @@ -499,17 +526,40 @@ productReference = 6ACA41CB15FC1D7500935EF6 /* McBopomofoInstaller.app */; productType = "com.apple.product-type.application"; }; + D485D3B52796A8A000657FF3 /* McBopomofoTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = D485D3BE2796A8A000657FF3 /* Build configuration list for PBXNativeTarget "McBopomofoTests" */; + buildPhases = ( + D485D3B22796A8A000657FF3 /* Sources */, + D485D3B32796A8A000657FF3 /* Frameworks */, + D485D3B42796A8A000657FF3 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + D485D3BB2796A8A000657FF3 /* PBXTargetDependency */, + ); + name = McBopomofoTests; + productName = McBopomofoTests; + productReference = D485D3B62796A8A000657FF3 /* McBopomofoTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ 6A0D4E9415FC0CFA00ABF4B3 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 1320; LastUpgradeCheck = 1310; TargetAttributes = { 6A0D4EA115FC0D2D00ABF4B3 = { LastSwiftMigration = 1240; }; + D485D3B52796A8A000657FF3 = { + CreatedOnToolsVersion = 13.2.1; + TestTargetID = 6A0D4EA115FC0D2D00ABF4B3; + }; }; }; buildConfigurationList = 6A0D4E9715FC0CFA00ABF4B3 /* Build configuration list for PBXProject "McBopomofo" */; @@ -531,6 +581,7 @@ 6A0D4EA115FC0D2D00ABF4B3 /* McBopomofo */, 6ACA41CA15FC1D7500935EF6 /* McBopomofoInstaller */, 6A38BC2115FC12FD00A8A51F /* Data */, + D485D3B52796A8A000657FF3 /* McBopomofoTests */, ); }; /* End PBXProject section */ @@ -569,6 +620,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + D485D3B42796A8A000657FF3 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ @@ -625,6 +683,14 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + D485D3B22796A8A000657FF3 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + D485D3B92796A8A000657FF3 /* PreferencesTests.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ @@ -638,6 +704,11 @@ target = 6A0D4EA115FC0D2D00ABF4B3 /* McBopomofo */; targetProxy = 6ACA420015FC1DCC00935EF6 /* PBXContainerItemProxy */; }; + D485D3BB2796A8A000657FF3 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 6A0D4EA115FC0D2D00ABF4B3 /* McBopomofo */; + targetProxy = D485D3BA2796A8A000657FF3 /* PBXContainerItemProxy */; + }; /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ @@ -1044,6 +1115,88 @@ }; name = Release; }; + D485D3BC2796A8A000657FF3 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + BUNDLE_LOADER = "$(TEST_HOST)"; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_STYLE = Automatic; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = dwarf; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GENERATE_INFOPLIST_FILE = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + MARKETING_VERSION = 1.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + PRODUCT_BUNDLE_IDENTIFIER = com.cerence.McBopomofoTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_EMIT_LOC_STRINGS = NO; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/McBopomofo.app/Contents/MacOS/McBopomofo"; + }; + name = Debug; + }; + D485D3BD2796A8A000657FF3 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + BUNDLE_LOADER = "$(TEST_HOST)"; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_STYLE = Automatic; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GENERATE_INFOPLIST_FILE = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + MARKETING_VERSION = 1.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + PRODUCT_BUNDLE_IDENTIFIER = com.cerence.McBopomofoTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + SWIFT_EMIT_LOC_STRINGS = NO; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 5.0; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/McBopomofo.app/Contents/MacOS/McBopomofo"; + }; + name = Release; + }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -1083,6 +1236,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + D485D3BE2796A8A000657FF3 /* Build configuration list for PBXNativeTarget "McBopomofoTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + D485D3BC2796A8A000657FF3 /* Debug */, + D485D3BD2796A8A000657FF3 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ /* Begin XCSwiftPackageProductDependency section */ diff --git a/McBopomofo.xcodeproj/xcshareddata/xcschemes/McBopomofo.xcscheme b/McBopomofo.xcodeproj/xcshareddata/xcschemes/McBopomofo.xcscheme index 3b2c3c69..494e3900 100644 --- a/McBopomofo.xcodeproj/xcshareddata/xcschemes/McBopomofo.xcscheme +++ b/McBopomofo.xcodeproj/xcshareddata/xcschemes/McBopomofo.xcscheme @@ -28,6 +28,16 @@ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" shouldUseLaunchSchemeArgsEnv = "YES"> + + + + b ? a : b; } { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-result" - [Preferences toogleHalfWidthPunctuationEnabled]; + [Preferences toggleHalfWidthPunctuationEnabled]; #pragma GCC diagnostic pop } - (void)togglePhraseReplacementEnabled:(id)sender { - BOOL enabled = [Preferences tooglePhraseReplacementEnabled]; + BOOL enabled = [Preferences togglePhraseReplacementEnabled]; McBopomofoLM *lm = [LanguageModelManager languageModelMcBopomofo]; lm->setPhraseReplacementEnabled(enabled); } diff --git a/Source/Preferences.swift b/Source/Preferences.swift index 538484b8..6c0cc7a5 100644 --- a/Source/Preferences.swift +++ b/Source/Preferences.swift @@ -55,7 +55,6 @@ private let kChineseConversionEngineKey = "ChineseConversionEngine" private let kChineseConversionStyle = "ChineseConversionStyle" private let kDefaultCandidateListTextSize: CGFloat = 16 -private let kMinKeyLabelSize: CGFloat = 10 private let kMinCandidateListTextSize: CGFloat = 12 private let kMaxCandidateListTextSize: CGFloat = 196 @@ -205,6 +204,28 @@ struct ComposingBufferSize { // MARK: - class Preferences: NSObject { + static func reset() { + let defaults = UserDefaults.standard + defaults.removeObject(forKey: kKeyboardLayoutPreferenceKey) + defaults.removeObject(forKey: kBasisKeyboardLayoutPreferenceKey) + defaults.removeObject(forKey: kFunctionKeyKeyboardLayoutPreferenceKey) + defaults.removeObject(forKey: kFunctionKeyKeyboardLayoutOverrideIncludeShiftKey) + defaults.removeObject(forKey: kCandidateListTextSizeKey) + defaults.removeObject(forKey: kSelectPhraseAfterCursorAsCandidatePreferenceKey) + defaults.removeObject(forKey: kUseHorizontalCandidateListPreferenceKey) + defaults.removeObject(forKey: kComposingBufferSizePreferenceKey) + defaults.removeObject(forKey: kChooseCandidateUsingSpaceKey) + defaults.removeObject(forKey: kChineseConversionEnabledKey) + defaults.removeObject(forKey: kHalfWidthPunctuationEnabledKey) + defaults.removeObject(forKey: kEscToCleanInputBufferKey) + defaults.removeObject(forKey: kCandidateTextFontName) + defaults.removeObject(forKey: kCandidateKeyLabelFontName) + defaults.removeObject(forKey: kCandidateKeys) + defaults.removeObject(forKey: kPhraseReplacementEnabledKey) + defaults.removeObject(forKey: kChineseConversionEngineKey) + defaults.removeObject(forKey: kChineseConversionStyle) + } + @UserDefault(key: kKeyboardLayoutPreferenceKey, defaultValue: 0) @objc static var keyboardLayout: Int @@ -247,7 +268,7 @@ class Preferences: NSObject { @UserDefault(key: kHalfWidthPunctuationEnabledKey, defaultValue: false) @objc static var halfWidthPunctuationEnabled: Bool - @objc static func toogleHalfWidthPunctuationEnabled() -> Bool { + @objc static func toggleHalfWidthPunctuationEnabled() -> Bool { halfWidthPunctuationEnabled = !halfWidthPunctuationEnabled return halfWidthPunctuationEnabled; } @@ -326,7 +347,7 @@ class Preferences: NSObject { @UserDefault(key: kPhraseReplacementEnabledKey, defaultValue: false) @objc static var phraseReplacementEnabled: Bool - @objc static func tooglePhraseReplacementEnabled() -> Bool { + @objc static func togglePhraseReplacementEnabled() -> Bool { phraseReplacementEnabled = !phraseReplacementEnabled return phraseReplacementEnabled; }