expo-modules-core 0.6.1 → 0.7.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.
Files changed (161) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +1 -1
  3. package/android/build.gradle +5 -5
  4. package/android/src/main/java/expo/modules/adapters/react/apploader/RNHeadlessAppLoader.kt +7 -1
  5. package/android/src/main/java/expo/modules/core/interfaces/ReactActivityLifecycleListener.java +23 -0
  6. package/android/src/main/java/expo/modules/kotlin/AppContext.kt +11 -1
  7. package/android/src/main/java/expo/modules/kotlin/DynamicExtenstions.kt +5 -3
  8. package/android/src/main/java/expo/modules/kotlin/KPromiseWrapper.kt +8 -2
  9. package/android/src/main/java/expo/modules/kotlin/KotlinInteropModuleRegistry.kt +13 -4
  10. package/android/src/main/java/expo/modules/kotlin/ModuleHolder.kt +7 -6
  11. package/android/src/main/java/expo/modules/kotlin/ModuleRegistry.kt +6 -1
  12. package/android/src/main/java/expo/modules/kotlin/Promise.kt +1 -1
  13. package/android/src/main/java/expo/modules/kotlin/callbacks/Callback.kt +5 -0
  14. package/android/src/main/java/expo/modules/kotlin/callbacks/ViewCallback.kt +28 -0
  15. package/android/src/main/java/expo/modules/kotlin/callbacks/ViewCallbackDelegate.kt +27 -0
  16. package/android/src/main/java/expo/modules/kotlin/defaultmodules/ErrorManagerModule.kt +25 -0
  17. package/android/src/main/java/expo/modules/kotlin/exception/CodedException.kt +68 -8
  18. package/android/src/main/java/expo/modules/kotlin/exception/ExceptionDecorator.kt +11 -0
  19. package/android/src/main/java/expo/modules/kotlin/methods/AnyMethod.kt +13 -14
  20. package/android/src/main/java/expo/modules/kotlin/modules/DefinitionMarker.kt +4 -0
  21. package/android/src/main/java/expo/modules/kotlin/modules/Module.kt +3 -2
  22. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionBuilder.kt +44 -4
  23. package/android/src/main/java/expo/modules/kotlin/records/Record.kt +39 -0
  24. package/android/src/main/java/expo/modules/kotlin/records/RecordTypeConverter.kt +14 -7
  25. package/android/src/main/java/expo/modules/kotlin/types/ArrayTypeConverter.kt +11 -5
  26. package/android/src/main/java/expo/modules/kotlin/types/ListTypeConverter.kt +10 -4
  27. package/android/src/main/java/expo/modules/kotlin/types/MapTypeConverter.kt +12 -6
  28. package/android/src/main/java/expo/modules/kotlin/types/PairTypeConverter.kt +29 -13
  29. package/android/src/main/java/expo/modules/kotlin/types/TypeConverter.kt +2 -1
  30. package/android/src/main/java/expo/modules/kotlin/views/CallbacksDefinition.kt +3 -0
  31. package/android/src/main/java/expo/modules/kotlin/views/GroupViewManagerWrapper.kt +22 -0
  32. package/android/src/main/java/expo/modules/kotlin/views/SimpleViewManagerWrapper.kt +22 -0
  33. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinition.kt +27 -2
  34. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinitionBuilder.kt +29 -1
  35. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerWrapperDelegate.kt +59 -2
  36. package/build/EventEmitter.d.ts +1 -0
  37. package/build/EventEmitter.d.ts.map +1 -0
  38. package/build/NativeModulesProxy.d.ts +1 -0
  39. package/build/NativeModulesProxy.d.ts.map +1 -0
  40. package/build/NativeModulesProxy.native.d.ts +1 -0
  41. package/build/NativeModulesProxy.native.d.ts.map +1 -0
  42. package/build/NativeModulesProxy.types.d.ts +1 -0
  43. package/build/NativeModulesProxy.types.d.ts.map +1 -0
  44. package/build/NativeViewManagerAdapter.d.ts +1 -0
  45. package/build/NativeViewManagerAdapter.d.ts.map +1 -0
  46. package/build/NativeViewManagerAdapter.native.d.ts +1 -0
  47. package/build/NativeViewManagerAdapter.native.d.ts.map +1 -0
  48. package/build/PermissionsHook.d.ts +1 -0
  49. package/build/PermissionsHook.d.ts.map +1 -0
  50. package/build/PermissionsInterface.d.ts +1 -0
  51. package/build/PermissionsInterface.d.ts.map +1 -0
  52. package/build/Platform.d.ts +1 -0
  53. package/build/Platform.d.ts.map +1 -0
  54. package/build/SyntheticPlatformEmitter.d.ts +1 -0
  55. package/build/SyntheticPlatformEmitter.d.ts.map +1 -0
  56. package/build/SyntheticPlatformEmitter.web.d.ts +1 -0
  57. package/build/SyntheticPlatformEmitter.web.d.ts.map +1 -0
  58. package/build/deprecate.d.ts +1 -0
  59. package/build/deprecate.d.ts.map +1 -0
  60. package/build/environment/browser.d.ts +1 -0
  61. package/build/environment/browser.d.ts.map +1 -0
  62. package/build/environment/browser.web.d.ts +1 -0
  63. package/build/environment/browser.web.d.ts.map +1 -0
  64. package/build/errors/CodedError.d.ts +1 -0
  65. package/build/errors/CodedError.d.ts.map +1 -0
  66. package/build/errors/UnavailabilityError.d.ts +1 -0
  67. package/build/errors/UnavailabilityError.d.ts.map +1 -0
  68. package/build/index.d.ts +3 -0
  69. package/build/index.d.ts.map +1 -0
  70. package/build/index.js +2 -0
  71. package/build/index.js.map +1 -1
  72. package/build/requireNativeModule.d.ts +16 -0
  73. package/build/requireNativeModule.d.ts.map +1 -0
  74. package/build/requireNativeModule.js +18 -0
  75. package/build/requireNativeModule.js.map +1 -0
  76. package/build/sweet/NativeErrorManager.d.ts +3 -0
  77. package/build/sweet/NativeErrorManager.d.ts.map +1 -0
  78. package/build/sweet/NativeErrorManager.js +3 -0
  79. package/build/sweet/NativeErrorManager.js.map +1 -0
  80. package/build/sweet/setUpErrorManager.fx.d.ts +2 -0
  81. package/build/sweet/setUpErrorManager.fx.d.ts.map +1 -0
  82. package/build/sweet/setUpErrorManager.fx.js +11 -0
  83. package/build/sweet/setUpErrorManager.fx.js.map +1 -0
  84. package/ios/AppDelegates/ExpoAppDelegate.swift +27 -8
  85. package/ios/JSI/ExpoModulesHostObject.h +33 -0
  86. package/ios/JSI/ExpoModulesHostObject.mm +40 -0
  87. package/ios/JSI/ExpoModulesProxySpec.h +4 -0
  88. package/ios/JSI/ExpoModulesProxySpec.mm +1 -3
  89. package/ios/JSI/JSIConversions.h +2 -0
  90. package/ios/JSI/JSIConversions.mm +9 -0
  91. package/ios/JSI/JSIInstaller.h +10 -0
  92. package/ios/JSI/JSIInstaller.mm +14 -2
  93. package/ios/JSI/JavaScriptObject.h +60 -0
  94. package/ios/JSI/JavaScriptObject.mm +93 -0
  95. package/ios/JSI/JavaScriptRuntime.h +54 -0
  96. package/ios/JSI/JavaScriptRuntime.mm +102 -0
  97. package/ios/ModuleRegistryAdapter/EXModuleRegistryAdapter.m +2 -12
  98. package/ios/NativeModulesProxy/EXComponentDataCompatibleWrapper.h +16 -0
  99. package/ios/NativeModulesProxy/EXComponentDataCompatibleWrapper.m +28 -0
  100. package/ios/NativeModulesProxy/EXNativeModulesProxy.mm +90 -66
  101. package/ios/RCTComponentData+Privates.h +12 -0
  102. package/ios/ReactDelegates/ExpoReactDelegate.swift +2 -2
  103. package/ios/ReactDelegates/ExpoReactDelegateHandler.swift +3 -3
  104. package/ios/ReactDelegates/ModulePriorities.swift +1 -1
  105. package/ios/Swift/AppContext.swift +38 -4
  106. package/ios/Swift/Arguments/ArgumentType.swift +4 -0
  107. package/ios/Swift/Arguments/Convertibles.swift +13 -13
  108. package/ios/Swift/Arguments/Types/EnumArgumentType.swift +11 -17
  109. package/ios/Swift/Arguments/Types/PromiseArgumentType.swift +1 -1
  110. package/ios/Swift/Arguments/Types/RawArgumentType.swift +2 -2
  111. package/ios/Swift/Conversions.swift +51 -56
  112. package/ios/Swift/EventListener.swift +8 -10
  113. package/ios/Swift/Events/Callback.swift +66 -0
  114. package/ios/Swift/Events/Event.swift +43 -0
  115. package/ios/Swift/Exceptions/ChainableException.swift +51 -0
  116. package/ios/Swift/{CodedError.swift → Exceptions/CodedError.swift} +1 -12
  117. package/ios/Swift/Exceptions/Exception.swift +62 -0
  118. package/ios/Swift/Exceptions/ExceptionOrigin.swift +28 -0
  119. package/ios/Swift/Exceptions/GenericException.swift +20 -0
  120. package/ios/Swift/Exceptions/UnexpectedException.swift +16 -0
  121. package/ios/Swift/Functions/AnyFunction.swift +11 -1
  122. package/ios/Swift/Functions/ConcreteFunction.swift +37 -16
  123. package/ios/Swift/JavaScriptUtils.swift +43 -0
  124. package/ios/Swift/ModuleHolder.swift +53 -14
  125. package/ios/Swift/ModuleRegistry.swift +4 -1
  126. package/ios/Swift/Modules/AnyModule.swift +0 -1
  127. package/ios/Swift/Modules/ModuleDefinition.swift +4 -13
  128. package/ios/Swift/Modules/ModuleDefinitionBuilder.swift +0 -1
  129. package/ios/Swift/Modules/ModuleDefinitionComponents.swift +0 -188
  130. package/ios/Swift/ModulesProvider.swift +0 -1
  131. package/ios/Swift/Objects/ObjectDefinition.swift +30 -0
  132. package/ios/Swift/Objects/ObjectDefinitionComponents.swift +208 -0
  133. package/ios/Swift/Promise.swift +8 -3
  134. package/ios/Swift/Records/AnyField.swift +7 -0
  135. package/ios/Swift/Records/Field.swift +24 -19
  136. package/ios/Swift/Records/FieldOption.swift +1 -1
  137. package/ios/Swift/Records/Record.swift +12 -4
  138. package/ios/Swift/SwiftInteropBridge.swift +39 -10
  139. package/ios/Swift/Views/AnyViewProp.swift +1 -1
  140. package/ios/Swift/Views/ComponentData.swift +95 -0
  141. package/ios/Swift/Views/ConcreteViewProp.swift +6 -8
  142. package/ios/Swift/Views/ViewFactory.swift +1 -1
  143. package/ios/Swift/Views/ViewManagerDefinition.swift +23 -2
  144. package/ios/Swift/Views/ViewManagerDefinitionBuilder.swift +0 -1
  145. package/ios/Swift/Views/ViewManagerDefinitionComponents.swift +26 -0
  146. package/ios/Swift/Views/ViewModuleWrapper.swift +5 -2
  147. package/ios/Tests/ArgumentTypeSpec.swift +3 -4
  148. package/ios/Tests/ConstantsSpec.swift +4 -4
  149. package/ios/Tests/ConvertiblesSpec.swift +33 -33
  150. package/ios/Tests/ExceptionsSpec.swift +112 -0
  151. package/ios/Tests/FunctionSpec.swift +20 -22
  152. package/ios/Tests/FunctionWithConvertiblesSpec.swift +2 -2
  153. package/ios/Tests/Mocks/ModuleMocks.swift +1 -1
  154. package/ios/Tests/Mocks/ModulesProviderMock.swift +0 -1
  155. package/ios/Tests/ModuleEventListenersSpec.swift +1 -1
  156. package/ios/Tests/RecordSpec.swift +7 -17
  157. package/package.json +3 -3
  158. package/src/index.ts +4 -0
  159. package/src/requireNativeModule.ts +29 -0
  160. package/src/sweet/NativeErrorManager.ts +2 -0
  161. package/src/sweet/setUpErrorManager.fx.ts +12 -0
@@ -3,7 +3,7 @@ import UIKit
3
3
  /**
4
4
  Type-erased protocol for view props classes.
5
5
  */
6
- public protocol AnyViewProp: AnyDefinition {
6
+ public protocol AnyViewProp: ViewManagerDefinitionComponent {
7
7
  /**
8
8
  Name of the view prop that JavaScript refers to.
9
9
  */
@@ -0,0 +1,95 @@
1
+ // Copyright 2021-present 650 Industries. All rights reserved.
2
+
3
+
4
+ /**
5
+ Custom component data extending `RCTComponentData`. Its main purpose is to handle event-based props (callbacks),
6
+ but it also simplifies capturing the view config so we can omit some reflections that React Native executes.
7
+ */
8
+ @objc(EXComponentData)
9
+ public final class ComponentData: EXComponentDataCompatibleWrapper {
10
+ /**
11
+ Weak pointer to the holder of a module that the component data was created for.
12
+ */
13
+ weak var moduleHolder: ModuleHolder?
14
+
15
+ /**
16
+ Initializer that additionally takes the original view module to have access to its definition.
17
+ */
18
+ @objc
19
+ public init(viewModule: ViewModuleWrapper, managerClass: ViewModuleWrapper.Type, bridge: RCTBridge) {
20
+ self.moduleHolder = viewModule.wrappedModuleHolder
21
+ super.init(managerClass: managerClass, bridge: bridge, eventDispatcher: bridge.eventDispatcher())
22
+ }
23
+
24
+ // MARK: RCTComponentData
25
+
26
+ /**
27
+ Creates a setter for the specific prop. For non-event props we just let React Native do its job.
28
+ Events are handled differently to conveniently use them in Swift.
29
+ */
30
+ public override func createPropBlock(_ propName: String, isShadowView: Bool) -> RCTPropBlockAlias {
31
+ // Expo Modules Core doesn't support shadow views yet, so fall back to the default implementation.
32
+ if isShadowView {
33
+ return super.createPropBlock(propName, isShadowView: isShadowView)
34
+ }
35
+
36
+ // If the prop is defined as an event, create our own event setter.
37
+ if moduleHolder?.viewManager?.eventNames.contains(propName) == true {
38
+ return createEventSetter(eventName: propName, bridge: self.manager?.bridge)
39
+ }
40
+
41
+ // Otherwise also fall back to the default implementation.
42
+ return super.createPropBlock(propName, isShadowView: isShadowView)
43
+ }
44
+
45
+ /**
46
+ The base `RCTComponentData` class does some Objective-C dynamic calls in this function, but we don't
47
+ need to do these slow operations since the Sweet API gives us necessary details without reflections.
48
+ */
49
+ public override func viewConfig() -> [String: Any] {
50
+ var propTypes: [String: Any] = [:]
51
+ var directEvents: [String] = []
52
+ let superClass: AnyClass? = managerClass.superclass()
53
+
54
+ if let eventNames = moduleHolder?.viewManager?.eventNames {
55
+ for eventName in eventNames {
56
+ directEvents.append(RCTNormalizeInputEventName(eventName))
57
+ propTypes[eventName] = "BOOL"
58
+ }
59
+ }
60
+
61
+ return [
62
+ "propTypes": propTypes,
63
+ "directEvents": directEvents,
64
+ "bubblingEvents": [String](),
65
+ "baseModuleName": superClass?.moduleName() as Any
66
+ ]
67
+ }
68
+ }
69
+
70
+ /**
71
+ Creates a setter for the event prop.
72
+ */
73
+ private func createEventSetter(eventName: String, bridge: RCTBridge?) -> RCTPropBlockAlias {
74
+ return { [weak bridge] (target: RCTComponent, value: Any) in
75
+ // Find view's property that is named as the prop and is wrapped by `Event`.
76
+ let child = Mirror(reflecting: target).children.first {
77
+ $0.label == "_\(eventName)"
78
+ }
79
+ guard let event = child?.value as? AnyEventInternal else {
80
+ return
81
+ }
82
+
83
+ // For callbacks React Native passes a bool value whether the prop is specified or not.
84
+ if value as? Bool == true {
85
+ event.settle { [weak target] (body: Any) in
86
+ if let target = target {
87
+ let componentEvent = RCTComponentEvent(name: eventName, viewTag: target.reactTag, body: ["payload": body])
88
+ bridge?.eventDispatcher().send(componentEvent)
89
+ }
90
+ }
91
+ } else {
92
+ event.invalidate()
93
+ }
94
+ }
95
+ }
@@ -36,22 +36,20 @@ public final class ConcreteViewProp<ViewType: UIView, PropType: AnyArgument>: An
36
36
  // Method's signature must be type-erased to conform to `AnyViewProp` protocol.
37
37
  // Given view must be castable to the generic `ViewType` type.
38
38
  guard let view = view as? ViewType else {
39
- throw IncompatibleViewError(propName: name, viewType: ViewType.self)
39
+ throw IncompatibleViewException((propName: name, viewType: ViewType.self))
40
40
  }
41
41
  guard let value = try propType.cast(value) as? PropType else {
42
- throw Conversions.CastingError<PropType>(value: value)
42
+ throw Conversions.CastingException<PropType>(value)
43
43
  }
44
44
  setter(view, value)
45
45
  }
46
46
  }
47
47
 
48
48
  /**
49
- An error that is thrown when the view passed to prop's setter doesn't match the type in setter's definition.
49
+ An exception that is thrown when the view passed to prop's setter doesn't match the type in setter's definition.
50
50
  */
51
- internal struct IncompatibleViewError: CodedError {
52
- let propName: String
53
- let viewType: UIView.Type
54
- var description: String {
55
- "Tried to set prop `\(propName)` on the view that isn't `\(viewType)`"
51
+ internal class IncompatibleViewException: GenericException<(propName: String, viewType: UIView.Type)> {
52
+ override var reason: String {
53
+ "Tried to set prop '\(param.propName)' on the view that isn't \(param.viewType)"
56
54
  }
57
55
  }
@@ -3,7 +3,7 @@ import UIKit
3
3
  /**
4
4
  A definition of the view factory that creates views.
5
5
  */
6
- internal struct ViewFactory: AnyDefinition {
6
+ internal struct ViewFactory: ViewManagerDefinitionComponent {
7
7
  typealias FactoryClosureType = () -> UIView
8
8
 
9
9
  let factory: FactoryClosureType
@@ -3,7 +3,7 @@ import UIKit
3
3
  /**
4
4
  The definition of the view manager. It's part of the module definition to scope only view-related definitions.
5
5
  */
6
- public struct ViewManagerDefinition: AnyDefinition {
6
+ public final class ViewManagerDefinition: ObjectDefinition {
7
7
  /**
8
8
  The view factory that lets us create views.
9
9
  */
@@ -14,13 +14,29 @@ public struct ViewManagerDefinition: AnyDefinition {
14
14
  */
15
15
  let props: [AnyViewProp]
16
16
 
17
- init(definitions: [AnyDefinition]) {
17
+ /**
18
+ Names of the events that the view can send to JavaScript.
19
+ */
20
+ let eventNames: [String]
21
+
22
+ /**
23
+ Default initializer receiving children definitions from the result builder.
24
+ */
25
+ override init(definitions: [AnyDefinition]) {
18
26
  self.factory = definitions
19
27
  .compactMap { $0 as? ViewFactory }
20
28
  .last
21
29
 
22
30
  self.props = definitions
23
31
  .compactMap { $0 as? AnyViewProp }
32
+
33
+ self.eventNames = Array(
34
+ definitions
35
+ .compactMap { ($0 as? EventsDefinition)?.names }
36
+ .joined()
37
+ )
38
+
39
+ super.init(definitions: definitions)
24
40
  }
25
41
 
26
42
  /**
@@ -39,3 +55,8 @@ public struct ViewManagerDefinition: AnyDefinition {
39
55
  }
40
56
  }
41
57
  }
58
+
59
+ /**
60
+ The protocol for definition components that can only be handled by the view manager builder.
61
+ */
62
+ public protocol ViewManagerDefinitionComponent: AnyDefinition {}
@@ -1,4 +1,3 @@
1
-
2
1
  #if swift(>=5.4)
3
2
  /**
4
3
  A result builder that captures scoped definitions specific for view managers, such as `ViewFactory` and `ConcreteViewProp`.
@@ -0,0 +1,26 @@
1
+ /// Here we implement the components exclusive for view managers.
2
+
3
+ // MARK: View factory
4
+
5
+ /**
6
+ Defines the factory creating a native view when the module is used as a view.
7
+ */
8
+ public func view(_ closure: @escaping () -> UIView) -> ViewManagerDefinitionComponent {
9
+ return ViewFactory(closure)
10
+ }
11
+
12
+ // MARK: Props
13
+
14
+ /**
15
+ Creates a view prop that defines its name and setter.
16
+ */
17
+ public func prop<ViewType: UIView, PropType: AnyArgument>(
18
+ _ name: String,
19
+ _ setter: @escaping (ViewType, PropType) -> Void
20
+ ) -> ViewManagerDefinitionComponent {
21
+ return ConcreteViewProp(
22
+ name: name,
23
+ propType: ArgumentType(PropType.self),
24
+ setter: setter
25
+ )
26
+ }
@@ -93,7 +93,7 @@ public final class ViewModuleWrapper: RCTViewManager, DynamicModuleWrapperProtoc
93
93
  */
94
94
  @objc
95
95
  public class func propConfig_proxiedProperties() -> [String] {
96
- return ["NSDictionary", "__custom__"];
96
+ return ["NSDictionary", "__custom__"]
97
97
  }
98
98
 
99
99
  /**
@@ -126,7 +126,7 @@ public final class ViewModuleWrapper: RCTViewManager, DynamicModuleWrapperProtoc
126
126
  let prefixedViewName = "ViewManagerAdapter_\(module.name())"
127
127
 
128
128
  return prefixedViewName.withCString { viewNamePtr in
129
- // Create a new meta class that inherits from `ViewModuleWrapper`. The class name passed here, doesn't work for Swift classes,
129
+ // Create a new class that inherits from `ViewModuleWrapper`. The class name passed here, doesn't work for Swift classes,
130
130
  // so we also have to override `moduleName` class method.
131
131
  let wrapperClass: AnyClass? = objc_allocateClassPair(ViewModuleWrapper.self, viewNamePtr, 0)
132
132
 
@@ -141,3 +141,6 @@ public final class ViewModuleWrapper: RCTViewManager, DynamicModuleWrapperProtoc
141
141
  }
142
142
  }
143
143
  }
144
+
145
+ // The direct event implementation can be cached and lazy-loaded (global and static variables are lazy by default in Swift).
146
+ let directEventBlockImplementation = imp_implementationWithBlock({ ["RCTDirectEventBlock"] } as @convention(block) () -> [String])
@@ -7,7 +7,6 @@ import Nimble
7
7
 
8
8
  class ArgumentTypeSpec: QuickSpec {
9
9
  override func spec() {
10
-
11
10
  it("casts primitives") {
12
11
  let type = ArgumentType(Int.self)
13
12
  let value = 123
@@ -34,7 +33,7 @@ class ArgumentTypeSpec: QuickSpec {
34
33
  let value: Double? = nil
35
34
  let anyValue = value as Any
36
35
 
37
- expect { try type.cast(anyValue) }.to(throwError(errorType: Conversions.NullCastError<Double>.self))
36
+ expect { try type.cast(anyValue) }.to(throwError(errorType: Conversions.NullCastException<Double>.self))
38
37
  }
39
38
 
40
39
  it("casts arrays") {
@@ -104,7 +103,7 @@ class ArgumentTypeSpec: QuickSpec {
104
103
 
105
104
  // "841" is not a raw value of any `IntTestEnum` case
106
105
  expect { try type.cast("string instead of int") }.to(throwError {
107
- expect($0).to(beAKindOf(EnumCastingError.self))
106
+ expect($0).to(beAKindOf(EnumCastingException.self))
108
107
  })
109
108
  }
110
109
 
@@ -113,7 +112,7 @@ class ArgumentTypeSpec: QuickSpec {
113
112
 
114
113
  // "841" is not a raw value of any `IntTestEnum` case
115
114
  expect { try type.cast(841) }.to(throwError {
116
- expect($0).to(beAKindOf(EnumNoSuchValueError.self))
115
+ expect($0).to(beAKindOf(EnumNoSuchValueException.self))
117
116
  })
118
117
  }
119
118
 
@@ -9,7 +9,7 @@ class ConstantsSpec: QuickSpec {
9
9
 
10
10
  it("takes closure resolving to dictionary") {
11
11
  let holder = mockModuleHolder(appContext) {
12
- $0.constants {
12
+ constants {
13
13
  return ["test": 123]
14
14
  }
15
15
  }
@@ -18,15 +18,15 @@ class ConstantsSpec: QuickSpec {
18
18
 
19
19
  it("takes the dictionary") {
20
20
  let holder = mockModuleHolder(appContext) {
21
- $0.constants(["test": 123])
21
+ constants(["test": 123])
22
22
  }
23
23
  expect(holder.getConstants()["test"] as? Int) == 123
24
24
  }
25
25
 
26
26
  it("merges multiple constants definitions") {
27
27
  let holder = mockModuleHolder(appContext) {
28
- $0.constants(["test": 456, "test2": 789])
29
- $0.constants(["test": 123])
28
+ constants(["test": 456, "test2": 789])
29
+ constants(["test": 123])
30
30
  }
31
31
  let consts = holder.getConstants()
32
32
  expect(consts["test"] as? Int) == 123
@@ -37,7 +37,7 @@ class ConvertiblesSpec: QuickSpec {
37
37
 
38
38
  it("throws when no string") {
39
39
  expect { try URL.convert(from: 29.5) }.to(
40
- throwError(errorType: Conversions.ConvertingError<URL>.self)
40
+ throwError(errorType: Conversions.ConvertingException<URL>.self)
41
41
  )
42
42
  }
43
43
  }
@@ -61,22 +61,22 @@ class ConvertiblesSpec: QuickSpec {
61
61
  }
62
62
 
63
63
  it("throws when array size is unexpected") { // different than two
64
- expect { try CGPoint.convert(from: []) }.to(throwError(errorType: Conversions.ConvertingError<CGPoint>.self))
65
- expect { try CGPoint.convert(from: [x]) }.to(throwError(errorType: Conversions.ConvertingError<CGPoint>.self))
66
- expect { try CGPoint.convert(from: [x, y, x]) }.to(throwError(errorType: Conversions.ConvertingError<CGPoint>.self))
64
+ expect { try CGPoint.convert(from: []) }.to(throwError(errorType: Conversions.ConvertingException<CGPoint>.self))
65
+ expect { try CGPoint.convert(from: [x]) }.to(throwError(errorType: Conversions.ConvertingException<CGPoint>.self))
66
+ expect { try CGPoint.convert(from: [x, y, x]) }.to(throwError(errorType: Conversions.ConvertingException<CGPoint>.self))
67
67
  }
68
68
 
69
69
  it("throws when dict is missing some keys") {
70
70
  expect { try CGPoint.convert(from: ["test": x]) }.to(throwError {
71
- expect($0).to(beAKindOf(Conversions.MissingKeysError<Double>.self))
72
- expect(($0 as! CodedError).description) == Conversions.MissingKeysError<Double>(keys: ["x", "y"]).description
71
+ expect($0).to(beAKindOf(Conversions.MissingKeysException<Double>.self))
72
+ expect(($0 as! CodedError).description) == Conversions.MissingKeysException<Double>(["x", "y"]).description
73
73
  })
74
74
  }
75
75
 
76
76
  it("throws when dict has uncastable keys") {
77
77
  expect { try CGPoint.convert(from: ["x": x, "y": "string"]) }.to(throwError {
78
- expect($0).to(beAKindOf(Conversions.CastingValuesError<Double>.self))
79
- expect(($0 as! CodedError).description) == Conversions.CastingValuesError<Double>(keys: ["y"]).description
78
+ expect($0).to(beAKindOf(Conversions.CastingValuesException<Double>.self))
79
+ expect(($0 as! CodedError).description) == Conversions.CastingValuesException<Double>(["y"]).description
80
80
  })
81
81
  }
82
82
  }
@@ -100,22 +100,22 @@ class ConvertiblesSpec: QuickSpec {
100
100
  }
101
101
 
102
102
  it("throws when array size is unexpected") { // different than two
103
- expect { try CGSize.convert(from: []) }.to(throwError(errorType: Conversions.ConvertingError<CGSize>.self))
104
- expect { try CGSize.convert(from: [width]) }.to(throwError(errorType: Conversions.ConvertingError<CGSize>.self))
105
- expect { try CGSize.convert(from: [width, height, width]) }.to(throwError(errorType: Conversions.ConvertingError<CGSize>.self))
103
+ expect { try CGSize.convert(from: []) }.to(throwError(errorType: Conversions.ConvertingException<CGSize>.self))
104
+ expect { try CGSize.convert(from: [width]) }.to(throwError(errorType: Conversions.ConvertingException<CGSize>.self))
105
+ expect { try CGSize.convert(from: [width, height, width]) }.to(throwError(errorType: Conversions.ConvertingException<CGSize>.self))
106
106
  }
107
107
 
108
108
  it("throws when dict is missing some keys") {
109
109
  expect { try CGSize.convert(from: ["width": width]) }.to(throwError {
110
- expect($0).to(beAKindOf(Conversions.MissingKeysError<Double>.self))
111
- expect(($0 as! CodedError).description) == Conversions.MissingKeysError<Double>(keys: ["height"]).description
110
+ expect($0).to(beAKindOf(Conversions.MissingKeysException<Double>.self))
111
+ expect(($0 as! CodedError).description) == Conversions.MissingKeysException<Double>(["height"]).description
112
112
  })
113
113
  }
114
114
 
115
115
  it("throws when dict has uncastable keys") {
116
116
  expect { try CGSize.convert(from: ["width": "test", "height": height]) }.to(throwError {
117
- expect($0).to(beAKindOf(Conversions.CastingValuesError<Double>.self))
118
- expect(($0 as! CodedError).description) == Conversions.CastingValuesError<Double>(keys: ["width"]).description
117
+ expect($0).to(beAKindOf(Conversions.CastingValuesException<Double>.self))
118
+ expect(($0 as! CodedError).description) == Conversions.CastingValuesException<Double>(["width"]).description
119
119
  })
120
120
  }
121
121
  }
@@ -139,22 +139,22 @@ class ConvertiblesSpec: QuickSpec {
139
139
  }
140
140
 
141
141
  it("throws when array size is unexpected") { // different than two
142
- expect { try CGVector.convert(from: []) }.to(throwError(errorType: Conversions.ConvertingError<CGVector>.self))
143
- expect { try CGVector.convert(from: [dx]) }.to(throwError(errorType: Conversions.ConvertingError<CGVector>.self))
144
- expect { try CGVector.convert(from: [dx, dy, dx]) }.to(throwError(errorType: Conversions.ConvertingError<CGVector>.self))
142
+ expect { try CGVector.convert(from: []) }.to(throwError(errorType: Conversions.ConvertingException<CGVector>.self))
143
+ expect { try CGVector.convert(from: [dx]) }.to(throwError(errorType: Conversions.ConvertingException<CGVector>.self))
144
+ expect { try CGVector.convert(from: [dx, dy, dx]) }.to(throwError(errorType: Conversions.ConvertingException<CGVector>.self))
145
145
  }
146
146
 
147
147
  it("throws when dict is missing some keys") {
148
148
  expect { try CGVector.convert(from: ["dx": dx]) }.to(throwError {
149
- expect($0).to(beAKindOf(Conversions.MissingKeysError<Double>.self))
150
- expect(($0 as! CodedError).description) == Conversions.MissingKeysError<Double>(keys: ["dy"]).description
149
+ expect($0).to(beAKindOf(Conversions.MissingKeysException<Double>.self))
150
+ expect(($0 as! CodedError).description) == Conversions.MissingKeysException<Double>(["dy"]).description
151
151
  })
152
152
  }
153
153
 
154
154
  it("throws when dict has uncastable keys") {
155
155
  expect { try CGVector.convert(from: ["dx": "dx", "dy": dy]) }.to(throwError {
156
- expect($0).to(beAKindOf(Conversions.CastingValuesError<Double>.self))
157
- expect(($0 as! CodedError).description) == Conversions.CastingValuesError<Double>(keys: ["dx"]).description
156
+ expect($0).to(beAKindOf(Conversions.CastingValuesException<Double>.self))
157
+ expect(($0 as! CodedError).description) == Conversions.CastingValuesException<Double>(["dx"]).description
158
158
  })
159
159
  }
160
160
  }
@@ -184,22 +184,22 @@ class ConvertiblesSpec: QuickSpec {
184
184
  }
185
185
 
186
186
  it("throws when array size is unexpected") { // different than four
187
- expect { try CGRect.convert(from: [x]) }.to(throwError(errorType: Conversions.ConvertingError<CGRect>.self))
188
- expect { try CGRect.convert(from: [x, y]) }.to(throwError(errorType: Conversions.ConvertingError<CGRect>.self))
189
- expect { try CGRect.convert(from: [x, y, width, height, y]) }.to(throwError(errorType: Conversions.ConvertingError<CGRect>.self))
187
+ expect { try CGRect.convert(from: [x]) }.to(throwError(errorType: Conversions.ConvertingException<CGRect>.self))
188
+ expect { try CGRect.convert(from: [x, y]) }.to(throwError(errorType: Conversions.ConvertingException<CGRect>.self))
189
+ expect { try CGRect.convert(from: [x, y, width, height, y]) }.to(throwError(errorType: Conversions.ConvertingException<CGRect>.self))
190
190
  }
191
191
 
192
192
  it("throws when dict is missing some keys") {
193
193
  expect { try CGRect.convert(from: ["x": x]) }.to(throwError {
194
- expect($0).to(beAKindOf(Conversions.MissingKeysError<Double>.self))
195
- expect(($0 as! CodedError).description) == Conversions.MissingKeysError<Double>(keys: ["y", "width", "height"]).description
194
+ expect($0).to(beAKindOf(Conversions.MissingKeysException<Double>.self))
195
+ expect(($0 as! CodedError).description) == Conversions.MissingKeysException<Double>(["y", "width", "height"]).description
196
196
  })
197
197
  }
198
198
 
199
199
  it("throws when dict has uncastable keys") {
200
200
  expect { try CGRect.convert(from: ["x": x, "y": nil, "width": width, "height": "\(height)"]) }.to(throwError {
201
- expect($0).to(beAKindOf(Conversions.CastingValuesError<Double>.self))
202
- expect(($0 as! CodedError).description) == Conversions.CastingValuesError<Double>(keys: ["y", "height"]).description
201
+ expect($0).to(beAKindOf(Conversions.CastingValuesException<Double>.self))
202
+ expect(($0 as! CodedError).description) == Conversions.CastingValuesException<Double>(["y", "height"]).description
203
203
  })
204
204
  }
205
205
  }
@@ -213,8 +213,8 @@ class ConvertiblesSpec: QuickSpec {
213
213
  }
214
214
  func testInvalidHexColor(_ hex: String) {
215
215
  expect { try CGColor.convert(from: hex) }.to(throwError {
216
- expect($0).to(beAKindOf(Conversions.InvalidHexColorError.self))
217
- expect(($0 as! CodedError).description) == Conversions.InvalidHexColorError(hex: hex).description
216
+ expect($0).to(beAKindOf(Conversions.InvalidHexColorException.self))
217
+ expect(($0 as! CodedError).description) == Conversions.InvalidHexColorException(hex).description
218
218
  })
219
219
  }
220
220
 
@@ -256,8 +256,8 @@ class ConvertiblesSpec: QuickSpec {
256
256
  it("throws when int overflows") {
257
257
  let hex = 0xBBAA88FF2
258
258
  expect { try CGColor.convert(from: hex) }.to(throwError {
259
- expect($0).to(beAKindOf(Conversions.HexColorOverflowError.self))
260
- expect(($0 as! CodedError).description) == Conversions.HexColorOverflowError(hex: UInt64(hex)).description
259
+ expect($0).to(beAKindOf(Conversions.HexColorOverflowException.self))
260
+ expect(($0 as! CodedError).description) == Conversions.HexColorOverflowException(UInt64(hex)).description
261
261
  })
262
262
  }
263
263
  }
@@ -0,0 +1,112 @@
1
+ // Copyright 2021-present 650 Industries. All rights reserved.
2
+
3
+ import Quick
4
+ import Nimble
5
+
6
+ @testable import ExpoModulesCore
7
+
8
+ final class ExceptionsSpec: QuickSpec {
9
+ override func spec() {
10
+ it("has name") {
11
+ let error = TestException()
12
+ expect(error.name) == "TestException"
13
+ }
14
+
15
+ it("has code") {
16
+ let error = TestException()
17
+ expect(error.code) == "ERR_TEST"
18
+ }
19
+
20
+ it("has reason") {
21
+ let error = TestException()
22
+ expect(error.reason) == "This is the test exception"
23
+ }
24
+
25
+ it("can be chained once") {
26
+ func throwable() throws {
27
+ do {
28
+ throw TestExceptionCause()
29
+ } catch {
30
+ throw TestException().causedBy(error)
31
+ }
32
+ }
33
+ expect { try throwable() }.to(throwError { error in
34
+ testChainedExceptionTypes(error: error, types: [TestException.self, TestExceptionCause.self])
35
+ })
36
+ }
37
+
38
+ it("can be chained twice") {
39
+ func throwable() throws {
40
+ do {
41
+ do {
42
+ throw TestExceptionCause()
43
+ } catch {
44
+ throw TestExceptionCause().causedBy(error)
45
+ }
46
+ } catch {
47
+ throw TestException().causedBy(error)
48
+ }
49
+ }
50
+ expect { try throwable() }.to(throwError { error in
51
+ testChainedExceptionTypes(error: error, types: [TestException.self, TestExceptionCause.self, TestExceptionCause.self])
52
+ })
53
+ }
54
+
55
+ it("includes cause description") {
56
+ func throwable() throws {
57
+ do {
58
+ throw TestExceptionCause()
59
+ } catch {
60
+ throw TestException().causedBy(error)
61
+ }
62
+ }
63
+ expect { try throwable() }.to(throwError { error in
64
+ if let error = error as? TestException, let cause = error.cause as? TestExceptionCause {
65
+ expect(error.description).to(contain(cause.description))
66
+ } else {
67
+ fail("Error and its cause are not of expected types.")
68
+ }
69
+ })
70
+ }
71
+
72
+ it("has root cause") {
73
+ let a = TestException()
74
+ let b = TestException().causedBy(a)
75
+ let c = TestException().causedBy(b)
76
+
77
+ expect(c.rootCause) === a
78
+ }
79
+ }
80
+ }
81
+
82
+ class TestException: Exception {
83
+ override var reason: String {
84
+ "This is the test exception"
85
+ }
86
+ }
87
+
88
+ class TestExceptionCause: Exception {
89
+ override var reason: String {
90
+ "This is the cause of the test exception"
91
+ }
92
+ }
93
+
94
+ /**
95
+ Tests whether the exception chain matches given types and their order.
96
+ */
97
+ private func testChainedExceptionTypes(error: Error, types: [Error.Type]) {
98
+ var next: Error? = error
99
+
100
+ for errorType in types {
101
+ let expectedErrorTypeName = String(describing: errorType)
102
+ let currentErrorTypeName = String(describing: type(of: next!))
103
+
104
+ expect(currentErrorTypeName).to(equal(expectedErrorTypeName), description: "The cause is not of type \(expectedErrorTypeName)")
105
+
106
+ if let chainableException = next as? ChainableException {
107
+ next = chainableException.cause
108
+ } else {
109
+ next = nil
110
+ }
111
+ }
112
+ }