1 import ExpoModulesTestCore 2 3 @testable import ExpoModulesCore 4 5 final class SharedRefSpec: ExpoSpec { 6 override func spec() { 7 let appContext = AppContext.create() 8 let runtime = try! appContext.runtime 9 10 beforeSuite { 11 appContext.moduleRegistry.register(moduleType: FirstModule.self) 12 appContext.moduleRegistry.register(moduleType: SecondModule.self) 13 } 14 15 it("is a shared object") { 16 expect(SharedRef<UIImage>.self is SharedObject.Type) 17 } 18 19 it("has dynamic type for shared objects") { 20 let dynamicType = ~SharedRef<UIImage>.self 21 22 expect(dynamicType is DynamicSharedObjectType) == true 23 } 24 25 it("creates shared data") { 26 let result = try runtime.eval("expo.modules.FirstModule.createSharedData('\(sharedDataString)')") 27 28 expect(result.kind) == .object 29 } 30 31 it("shares Data object") { 32 let result = try runtime.eval([ 33 "sharedData = expo.modules.FirstModule.createSharedData('\(sharedDataString)')", 34 "expo.modules.SecondModule.stringFromSharedData(sharedData)" 35 ]) 36 37 expect(result.kind) == .string 38 expect(try result.asString()) == sharedDataString 39 } 40 } 41 } 42 43 private let sharedDataString = "I can be shared among independent modules" 44 45 private class FirstModule: Module { 46 public func definition() -> ModuleDefinition { 47 Function("createSharedData") { (string: String) -> SharedRef<Data> in 48 let data = Data(string.utf8) 49 return SharedRef<Data>(data) 50 } 51 } 52 } 53 54 private class SecondModule: Module { 55 public func definition() -> ModuleDefinition { 56 Function("stringFromSharedData") { (data: SharedRef<Data>) -> String in 57 return String(decoding: data.pointer, as: UTF8.self) 58 } 59 } 60 } 61