expo-modules-core 0.9.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -5
- package/android/CMakeLists.txt +154 -0
- package/android/build.gradle +293 -5
- package/android/src/main/cpp/Exceptions.cpp +22 -0
- package/android/src/main/cpp/Exceptions.h +38 -0
- package/android/src/main/cpp/ExpoModulesHostObject.cpp +47 -0
- package/android/src/main/cpp/ExpoModulesHostObject.h +32 -0
- package/android/src/main/cpp/JNIFunctionBody.cpp +29 -0
- package/android/src/main/cpp/JNIFunctionBody.h +50 -0
- package/android/src/main/cpp/JNIInjector.cpp +19 -0
- package/android/src/main/cpp/JSIInteropModuleRegistry.cpp +122 -0
- package/android/src/main/cpp/JSIInteropModuleRegistry.h +96 -0
- package/android/src/main/cpp/JSIObjectWrapper.h +33 -0
- package/android/src/main/cpp/JSITypeConverter.h +84 -0
- package/android/src/main/cpp/JavaScriptModuleObject.cpp +138 -0
- package/android/src/main/cpp/JavaScriptModuleObject.h +122 -0
- package/android/src/main/cpp/JavaScriptObject.cpp +125 -0
- package/android/src/main/cpp/JavaScriptObject.h +131 -0
- package/android/src/main/cpp/JavaScriptRuntime.cpp +127 -0
- package/android/src/main/cpp/JavaScriptRuntime.h +87 -0
- package/android/src/main/cpp/JavaScriptValue.cpp +172 -0
- package/android/src/main/cpp/JavaScriptValue.h +78 -0
- package/android/src/main/cpp/MethodMetadata.cpp +230 -0
- package/android/src/main/cpp/MethodMetadata.h +92 -0
- package/android/src/main/java/expo/modules/adapters/react/NativeModulesProxy.java +2 -0
- package/android/src/main/java/expo/modules/core/errors/ContextDestroyedException.kt +7 -0
- package/android/src/main/java/expo/modules/interfaces/permissions/Permissions.java +30 -0
- package/android/src/main/java/expo/modules/kotlin/AppContext.kt +49 -1
- package/android/src/main/java/expo/modules/kotlin/ConcatIterator.kt +18 -0
- package/android/src/main/java/expo/modules/kotlin/KotlinInteropModuleRegistry.kt +15 -12
- package/android/src/main/java/expo/modules/kotlin/ModuleHolder.kt +39 -3
- package/android/src/main/java/expo/modules/kotlin/defaultmodules/ErrorManagerModule.kt +2 -2
- package/android/src/main/java/expo/modules/kotlin/exception/CodedException.kt +13 -0
- package/android/src/main/java/expo/modules/kotlin/exception/ExceptionDecorator.kt +2 -0
- package/android/src/main/java/expo/modules/kotlin/functions/AnyFunction.kt +19 -14
- package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunction.kt +29 -7
- package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionBuilder.kt +13 -13
- package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionComponent.kt +18 -0
- package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionWithPromiseComponent.kt +18 -0
- package/android/src/main/java/expo/modules/kotlin/functions/SuspendFunctionComponent.kt +56 -0
- package/android/src/main/java/expo/modules/kotlin/functions/SyncFunctionComponent.kt +28 -0
- package/android/src/main/java/expo/modules/kotlin/jni/CppType.kt +18 -0
- package/android/src/main/java/expo/modules/kotlin/jni/JNIFunctionBody.kt +39 -0
- package/android/src/main/java/expo/modules/kotlin/jni/JSIInteropModuleRegistry.kt +89 -0
- package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptModuleObject.kt +44 -0
- package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptObject.kt +113 -0
- package/android/src/main/java/expo/modules/kotlin/jni/JavaScriptValue.kt +35 -0
- package/android/src/main/java/expo/modules/kotlin/modules/Module.kt +15 -5
- package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionBuilder.kt +65 -111
- package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionData.kt +35 -2
- package/android/src/main/java/expo/modules/kotlin/providers/AppContextProvider.kt +14 -0
- package/android/src/main/java/expo/modules/kotlin/providers/CurrentActivityProvider.kt +22 -0
- package/android/src/main/java/expo/modules/kotlin/records/RecordTypeConverter.kt +19 -2
- package/android/src/main/java/expo/modules/kotlin/types/AnyType.kt +3 -2
- package/android/src/main/java/expo/modules/kotlin/types/ArrayTypeConverter.kt +7 -2
- package/android/src/main/java/expo/modules/kotlin/types/BasicTypeConverters.kt +68 -20
- package/android/src/main/java/expo/modules/kotlin/types/EnumTypeConverter.kt +50 -22
- package/android/src/main/java/expo/modules/kotlin/types/ListTypeConverter.kt +18 -2
- package/android/src/main/java/expo/modules/kotlin/types/MapTypeConverter.kt +18 -2
- package/android/src/main/java/expo/modules/kotlin/types/PairTypeConverter.kt +17 -2
- package/android/src/main/java/expo/modules/kotlin/types/TypeConverter.kt +43 -3
- package/android/src/main/java/expo/modules/kotlin/types/TypeConverterProvider.kt +5 -0
- package/build/NativeModulesProxy.native.d.ts.map +1 -1
- package/build/NativeModulesProxy.native.js +9 -3
- package/build/NativeModulesProxy.native.js.map +1 -1
- package/ios/AppDelegates/EXAppDelegatesLoader.m +1 -2
- package/ios/ExpoModulesCore.podspec +1 -1
- package/ios/JSI/EXJSIConversions.mm +6 -0
- package/ios/JSI/EXJSIInstaller.h +15 -21
- package/ios/JSI/EXJSIInstaller.mm +39 -3
- package/ios/JSI/EXJSIUtils.h +47 -3
- package/ios/JSI/EXJSIUtils.mm +88 -4
- package/ios/JSI/EXJavaScriptObject.h +11 -18
- package/ios/JSI/EXJavaScriptObject.mm +37 -18
- package/ios/JSI/EXJavaScriptRuntime.h +43 -9
- package/ios/JSI/EXJavaScriptRuntime.mm +70 -27
- package/ios/JSI/EXJavaScriptTypedArray.h +30 -0
- package/ios/JSI/EXJavaScriptTypedArray.mm +29 -0
- package/ios/JSI/EXJavaScriptValue.h +3 -2
- package/ios/JSI/EXJavaScriptValue.mm +17 -20
- package/ios/JSI/EXJavaScriptWeakObject.h +23 -0
- package/ios/JSI/EXJavaScriptWeakObject.mm +53 -0
- package/ios/JSI/EXObjectDeallocator.h +27 -0
- package/ios/JSI/ExpoModulesHostObject.h +3 -3
- package/ios/JSI/ExpoModulesHostObject.mm +4 -4
- package/ios/JSI/JavaScriptRuntime.swift +38 -1
- package/ios/JSI/JavaScriptValue.swift +7 -0
- package/ios/JSI/TypedArray.cpp +67 -0
- package/ios/JSI/TypedArray.h +46 -0
- package/ios/ModuleRegistryAdapter/EXModuleRegistryAdapter.m +0 -11
- package/ios/NativeModulesProxy/EXNativeModulesProxy.h +17 -10
- package/ios/NativeModulesProxy/EXNativeModulesProxy.mm +85 -77
- package/ios/NativeModulesProxy/NativeModulesProxyModule.swift +17 -0
- package/ios/Services/EXReactNativeEventEmitter.h +2 -2
- package/ios/Services/EXReactNativeEventEmitter.m +11 -6
- package/ios/Swift/AppContext.swift +208 -28
- package/ios/Swift/Arguments/AnyArgument.swift +18 -0
- package/ios/Swift/Arguments/{Types/EnumArgumentType.swift → EnumArgument.swift} +2 -17
- package/ios/Swift/Classes/ClassComponent.swift +95 -0
- package/ios/Swift/Classes/ClassComponentElement.swift +33 -0
- package/ios/Swift/Classes/ClassComponentElementsBuilder.swift +34 -0
- package/ios/Swift/Classes/ClassComponentFactories.swift +96 -0
- package/ios/Swift/DynamicTypes/AnyDynamicType.swift +44 -0
- package/ios/Swift/DynamicTypes/DynamicArrayType.swift +56 -0
- package/ios/Swift/DynamicTypes/DynamicConvertibleType.swift +27 -0
- package/ios/Swift/DynamicTypes/DynamicEnumType.swift +27 -0
- package/ios/Swift/DynamicTypes/DynamicOptionalType.swift +63 -0
- package/ios/Swift/DynamicTypes/DynamicRawType.swift +33 -0
- package/ios/Swift/DynamicTypes/DynamicSharedObjectType.swift +37 -0
- package/ios/Swift/DynamicTypes/DynamicType.swift +39 -0
- package/ios/Swift/DynamicTypes/DynamicTypedArrayType.swift +46 -0
- package/ios/Swift/Exceptions/CodedError.swift +1 -1
- package/ios/Swift/Exceptions/Exception.swift +8 -6
- package/ios/Swift/Exceptions/UnexpectedException.swift +2 -1
- package/ios/Swift/ExpoBridgeModule.m +5 -0
- package/ios/Swift/ExpoBridgeModule.swift +65 -0
- package/ios/Swift/Functions/AnyFunction.swift +33 -31
- package/ios/Swift/Functions/AsyncFunctionComponent.swift +196 -59
- package/ios/Swift/Functions/SyncFunctionComponent.swift +142 -58
- package/ios/Swift/JavaScriptUtils.swift +32 -57
- package/ios/Swift/Logging/LogHandlers.swift +39 -0
- package/ios/Swift/Logging/LogType.swift +62 -0
- package/ios/Swift/Logging/Logger.swift +198 -0
- package/ios/Swift/ModuleHolder.swift +19 -54
- package/ios/Swift/ModuleRegistry.swift +7 -1
- package/ios/Swift/Modules/AnyModule.swift +3 -3
- package/ios/Swift/ModulesProvider.swift +2 -0
- package/ios/Swift/Objects/JavaScriptObjectBuilder.swift +37 -0
- package/ios/Swift/Objects/ObjectDefinition.swift +74 -1
- package/ios/Swift/Objects/ObjectDefinitionComponents.swift +77 -68
- package/ios/Swift/Objects/PropertyComponent.swift +147 -0
- package/ios/Swift/Promise.swift +12 -3
- package/ios/Swift/Records/Field.swift +2 -2
- package/ios/Swift/SharedObjects/SharedObject.swift +20 -0
- package/ios/Swift/SharedObjects/SharedObjectRegistry.swift +129 -0
- package/ios/Swift/TypedArrays/AnyTypedArray.swift +11 -0
- package/ios/Swift/TypedArrays/ConcreteTypedArrays.swift +56 -0
- package/ios/Swift/TypedArrays/GenericTypedArray.swift +49 -0
- package/ios/Swift/TypedArrays/TypedArray.swift +80 -0
- package/ios/Swift/Utilities.swift +28 -0
- package/ios/Swift/Views/ConcreteViewProp.swift +3 -3
- package/ios/Swift/Views/ViewManagerDefinitionComponents.swift +2 -2
- package/ios/Tests/ClassComponentSpec.swift +210 -0
- package/ios/Tests/DynamicTypeSpec.swift +336 -0
- package/ios/Tests/EnumArgumentSpec.swift +48 -0
- package/ios/Tests/ExpoModulesSpec.swift +17 -3
- package/ios/Tests/FunctionSpec.swift +167 -118
- package/ios/Tests/Mocks/ModuleMocks.swift +1 -1
- package/ios/Tests/PropertyComponentSpec.swift +95 -0
- package/ios/Tests/SharedObjectRegistrySpec.swift +109 -0
- package/ios/Tests/TypedArraysSpec.swift +136 -0
- package/package.json +2 -2
- package/src/NativeModulesProxy.native.ts +13 -3
- package/src/ts-declarations/ExpoModules.d.ts +7 -0
- package/tsconfig.json +1 -1
- package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionWithPromise.kt +0 -15
- package/android/src/main/java/expo/modules/kotlin/functions/AsyncSuspendFunction.kt +0 -36
- package/ios/Swift/Arguments/AnyArgumentType.swift +0 -13
- package/ios/Swift/Arguments/ArgumentType.swift +0 -28
- package/ios/Swift/Arguments/Types/ArrayArgumentType.swift +0 -42
- package/ios/Swift/Arguments/Types/ConvertibleArgumentType.swift +0 -16
- package/ios/Swift/Arguments/Types/OptionalArgumentType.swift +0 -49
- package/ios/Swift/Arguments/Types/PromiseArgumentType.swift +0 -15
- package/ios/Swift/Arguments/Types/RawArgumentType.swift +0 -25
- package/ios/Swift/Functions/ConcreteFunction.swift +0 -103
- package/ios/Swift/SwiftInteropBridge.swift +0 -155
- package/ios/Tests/ArgumentTypeSpec.swift +0 -143
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-present 650 Industries. All rights reserved.
|
|
2
|
-
|
|
3
|
-
import Foundation
|
|
4
|
-
import React
|
|
5
|
-
|
|
6
|
-
@objc
|
|
7
|
-
public final class SwiftInteropBridge: NSObject {
|
|
8
|
-
let appContext: AppContext
|
|
9
|
-
|
|
10
|
-
var registry: ModuleRegistry {
|
|
11
|
-
appContext.moduleRegistry
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
internal init(appContext: AppContext) {
|
|
15
|
-
self.appContext = appContext
|
|
16
|
-
super.init()
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
@objc
|
|
20
|
-
public init(modulesProvider: ModulesProvider, legacyModuleRegistry: EXModuleRegistry) {
|
|
21
|
-
self.appContext = AppContext(withModulesProvider: modulesProvider, legacyModuleRegistry: legacyModuleRegistry)
|
|
22
|
-
super.init()
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
@objc
|
|
26
|
-
public func hasModule(_ moduleName: String) -> Bool {
|
|
27
|
-
return registry.has(moduleWithName: moduleName)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
@objc
|
|
31
|
-
public func setReactBridge(_ reactBridge: RCTBridge) {
|
|
32
|
-
appContext.reactBridge = reactBridge
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
@objc
|
|
36
|
-
public func callFunction(
|
|
37
|
-
_ functionName: String,
|
|
38
|
-
onModule moduleName: String,
|
|
39
|
-
withArgs args: [Any],
|
|
40
|
-
resolve: @escaping EXPromiseResolveBlock,
|
|
41
|
-
reject: @escaping EXPromiseRejectBlock
|
|
42
|
-
) {
|
|
43
|
-
registry
|
|
44
|
-
.get(moduleHolderForName: moduleName)?
|
|
45
|
-
.call(function: functionName, args: args) { value, error in
|
|
46
|
-
if let error = error {
|
|
47
|
-
reject(error.code, error.description, error)
|
|
48
|
-
} else if let error = error {
|
|
49
|
-
reject("ERR_UNKNOWN_ERROR", error.localizedDescription, error)
|
|
50
|
-
} else {
|
|
51
|
-
resolve(value)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
@objc
|
|
57
|
-
public func callFunctionSync(
|
|
58
|
-
_ functionName: String,
|
|
59
|
-
onModule moduleName: String,
|
|
60
|
-
withArgs args: [Any]
|
|
61
|
-
) -> Any? {
|
|
62
|
-
return registry
|
|
63
|
-
.get(moduleHolderForName: moduleName)?
|
|
64
|
-
.callSync(function: functionName, args: args)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
@objc
|
|
68
|
-
public func exportedFunctionNames() -> [String: [[String: Any]]] {
|
|
69
|
-
var constants = [String: [[String: Any]]]()
|
|
70
|
-
|
|
71
|
-
for holder in registry {
|
|
72
|
-
constants[holder.name] = holder.definition.functions.map({ functionName, function in
|
|
73
|
-
return [
|
|
74
|
-
"name": functionName,
|
|
75
|
-
"argumentsCount": function.argumentsCount,
|
|
76
|
-
"key": functionName
|
|
77
|
-
]
|
|
78
|
-
})
|
|
79
|
-
}
|
|
80
|
-
return constants
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
@objc
|
|
84
|
-
public func exportedModulesConstants() -> [String: Any] {
|
|
85
|
-
return registry.reduce(into: [String: Any]()) { acc, holder in
|
|
86
|
-
acc[holder.name] = holder.getConstants()
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
@objc
|
|
91
|
-
public func viewManagersMetadata() -> [String: Any] {
|
|
92
|
-
return registry.reduce(into: [String: Any]()) { acc, holder in
|
|
93
|
-
if let viewManager = holder.definition.viewManager {
|
|
94
|
-
acc[holder.name] = [
|
|
95
|
-
"propsNames": viewManager.props.map { $0.name }
|
|
96
|
-
]
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
Returns view modules wrapped by the base `ViewModuleWrapper` class.
|
|
103
|
-
*/
|
|
104
|
-
@objc
|
|
105
|
-
public func getViewManagers() -> [ViewModuleWrapper] {
|
|
106
|
-
return registry.compactMap { holder in
|
|
107
|
-
if holder.definition.viewManager != nil {
|
|
108
|
-
return ViewModuleWrapper(holder)
|
|
109
|
-
} else {
|
|
110
|
-
return nil
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
Sets the JSI runtime on the operating `AppContext`.
|
|
117
|
-
*/
|
|
118
|
-
@objc
|
|
119
|
-
public func setRuntime(_ runtime: JavaScriptRuntime?) {
|
|
120
|
-
appContext.runtime = runtime
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
@objc
|
|
124
|
-
public func getModuleNames() -> [String] {
|
|
125
|
-
return registry.getModuleNames()
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
@objc
|
|
129
|
-
public func getNativeModuleObject(_ moduleName: String) -> JavaScriptObject? {
|
|
130
|
-
return registry.get(moduleHolderForName: moduleName)?.javaScriptObject
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// MARK: - Events
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
Returns an array of event names supported by all Swift modules.
|
|
137
|
-
*/
|
|
138
|
-
@objc
|
|
139
|
-
public func getSupportedEvents() -> [String] {
|
|
140
|
-
return registry.reduce(into: [String]()) { events, holder in
|
|
141
|
-
events.append(contentsOf: holder.definition.eventNames)
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
Modifies listeners count for module with given name. Depending on the listeners count,
|
|
147
|
-
`onStartObserving` and `onStopObserving` are called.
|
|
148
|
-
*/
|
|
149
|
-
@objc
|
|
150
|
-
public func modifyEventListenersCount(_ moduleName: String, count: Int) {
|
|
151
|
-
registry
|
|
152
|
-
.get(moduleHolderForName: moduleName)?
|
|
153
|
-
.modifyListenersCount(count)
|
|
154
|
-
}
|
|
155
|
-
}
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-present 650 Industries. All rights reserved.
|
|
2
|
-
|
|
3
|
-
import ExpoModulesTestCore
|
|
4
|
-
|
|
5
|
-
@testable import ExpoModulesCore
|
|
6
|
-
|
|
7
|
-
class ArgumentTypeSpec: ExpoSpec {
|
|
8
|
-
override func spec() {
|
|
9
|
-
it("casts primitives") {
|
|
10
|
-
let type = ArgumentType(Int.self)
|
|
11
|
-
let value = 123
|
|
12
|
-
let anyValue = value as Any
|
|
13
|
-
|
|
14
|
-
expect(try type.cast(anyValue)).to(be(value))
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
it("casts optional types") {
|
|
18
|
-
let type = ArgumentType(Double?.self)
|
|
19
|
-
let value: Double? = nil
|
|
20
|
-
let anyValue = value as Any
|
|
21
|
-
let result = try type.cast(anyValue)
|
|
22
|
-
|
|
23
|
-
expect(result).to(beAKindOf(Double?.self))
|
|
24
|
-
|
|
25
|
-
// Since this `nil` is in fact of non-optional `Any` type, under the hood it's described as `Optional` enum.
|
|
26
|
-
// Simply checking `result == nil` does NOT work here, see `Optional.isNil` extension implementation.
|
|
27
|
-
expect(Optional.isNil(result)) == true
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
it("throws null cast error") {
|
|
31
|
-
let type = ArgumentType(Double.self) // non-optional (!)
|
|
32
|
-
let value: Double? = nil
|
|
33
|
-
let anyValue = value as Any
|
|
34
|
-
|
|
35
|
-
expect { try type.cast(anyValue) }.to(throwError(errorType: Conversions.NullCastException<Double>.self))
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
it("casts arrays") {
|
|
39
|
-
let type = ArgumentType([Double].self)
|
|
40
|
-
let value = 9.9
|
|
41
|
-
let anyValue = [value] as [Any]
|
|
42
|
-
let result = try type.cast(anyValue) as! [Any]
|
|
43
|
-
|
|
44
|
-
expect(result).to(beAKindOf([Double].self))
|
|
45
|
-
expect((result as! [Double]).first) == value
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
it("casts convertibles") {
|
|
49
|
-
let type = ArgumentType(ConvertibleTestStruct.self)
|
|
50
|
-
let value = "expo is the best"
|
|
51
|
-
let result = try type.cast(value)
|
|
52
|
-
|
|
53
|
-
expect(result).to(beAKindOf(ConvertibleTestStruct.self))
|
|
54
|
-
expect((result as! ConvertibleTestStruct).value) == value
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
it("casts array of convertibles") {
|
|
58
|
-
let type = ArgumentType([ConvertibleTestStruct].self)
|
|
59
|
-
let value = ["expo is the best"]
|
|
60
|
-
let result = try type.cast(value)
|
|
61
|
-
|
|
62
|
-
expect(result).to(beAKindOf([ConvertibleTestStruct].self))
|
|
63
|
-
expect((result as! [ConvertibleTestStruct]).first!.value) == value.first
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
it("casts array of array of convertibles") {
|
|
67
|
-
let type = ArgumentType([[ConvertibleTestStruct]].self)
|
|
68
|
-
let value = [["expo is the best"]]
|
|
69
|
-
let result = try type.cast(value)
|
|
70
|
-
|
|
71
|
-
expect(result).to(beAKindOf([[ConvertibleTestStruct]].self))
|
|
72
|
-
expect((result as! [[ConvertibleTestStruct]]).first!.first!.value) == value.first!.first
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
describe("EnumArgumentType") {
|
|
76
|
-
it("casts from String") {
|
|
77
|
-
let type = ArgumentType(StringTestEnum.self)
|
|
78
|
-
let input = "expo"
|
|
79
|
-
let output = try type.cast(input)
|
|
80
|
-
|
|
81
|
-
expect(output).to(beAKindOf(StringTestEnum.self))
|
|
82
|
-
expect(output as? StringTestEnum) == StringTestEnum.expo
|
|
83
|
-
expect(output as? StringTestEnum) == StringTestEnum(rawValue: input)
|
|
84
|
-
expect((output as! StringTestEnum).rawValue) == input
|
|
85
|
-
expect((output as! EnumArgument).anyRawValue).to(beAKindOf(String.self))
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
it("casts from Int") {
|
|
89
|
-
let type = ArgumentType(IntTestEnum.self)
|
|
90
|
-
let input: Int = -1
|
|
91
|
-
let output = try type.cast(input)
|
|
92
|
-
|
|
93
|
-
expect(output).to(beAKindOf(IntTestEnum.self))
|
|
94
|
-
expect(output as? IntTestEnum) == IntTestEnum.negative
|
|
95
|
-
expect(output as? IntTestEnum) == IntTestEnum(rawValue: input)
|
|
96
|
-
expect((output as! IntTestEnum).rawValue) == input
|
|
97
|
-
expect((output as! EnumArgument).anyRawValue).to(beAKindOf(Int.self))
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
it("throws casting error") {
|
|
101
|
-
let type = ArgumentType(IntTestEnum.self)
|
|
102
|
-
|
|
103
|
-
// "841" is not a raw value of any `IntTestEnum` case
|
|
104
|
-
expect { try type.cast("string instead of int") }.to(throwError {
|
|
105
|
-
expect($0).to(beAKindOf(EnumCastingException.self))
|
|
106
|
-
})
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
it("throws no such value error") {
|
|
110
|
-
let type = ArgumentType(IntTestEnum.self)
|
|
111
|
-
|
|
112
|
-
// "841" is not a raw value of any `IntTestEnum` case
|
|
113
|
-
expect { try type.cast(841) }.to(throwError {
|
|
114
|
-
expect($0).to(beAKindOf(EnumNoSuchValueException.self))
|
|
115
|
-
})
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
it("gets a list of all raw values") {
|
|
119
|
-
expect(StringTestEnum.allRawValues as? [String]) == ["hello", "expo"]
|
|
120
|
-
expect(IntTestEnum.allRawValues as? [Int]) == [-1, 1]
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
struct ConvertibleTestStruct: ConvertibleArgument {
|
|
127
|
-
let value: String
|
|
128
|
-
|
|
129
|
-
static func convert(from value: Any?) throws -> ConvertibleTestStruct {
|
|
130
|
-
guard let str = value as? String else { fatalError() }
|
|
131
|
-
return ConvertibleTestStruct(value: str)
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
enum StringTestEnum: String, EnumArgument {
|
|
136
|
-
case hello
|
|
137
|
-
case expo
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
enum IntTestEnum: Int, EnumArgument {
|
|
141
|
-
case negative = -1
|
|
142
|
-
case positive = 1
|
|
143
|
-
}
|