Repository navigation
Expand file tree
/
Copy pathcpp-only.harness.ts
More file actions
410 lines (355 loc) · 13.3 KB
/
Copy pathcpp-only.harness.ts
File metadata and controls
410 lines (355 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import { describe, expect, it } from 'react-native-harness'
import { NitroModules } from 'react-native-nitro-modules'
import type {
Base,
Child,
Powertrain,
TestObjectCpp,
TestObjectSwiftKotlin,
} from 'react-native-nitro-test'
import { HybridSomeExternalObject } from 'react-native-nitro-test-external'
import { TEST_CUSTOM_TYPE, TEST_PERSON } from './utils/fixtures'
import { debugOnly, itIf } from './utils/helpers'
// `rawJsiFunc` is registered as a raw JSI method in `HybridTestObjectCpp.hpp`,
// so it is not part of the `TestObjectCpp` spec.
interface WithRawJsiFunc {
rawJsiFunc(...args: unknown[]): unknown
}
const POWERTRAINS: Powertrain[] = ['electric', 'gas', 'hybrid']
const IS_DEBUG = NitroModules.buildType === 'debug'
describe('C++ only', () => {
describe('TestObjectCpp', () => {
const testObject =
NitroModules.createHybridObject<TestObjectCpp>('TestObjectCpp')
const swiftKotlinObject =
NitroModules.createHybridObject<TestObjectSwiftKotlin>(
'TestObjectSwiftKotlin'
)
const rawObject = testObject as TestObjectCpp & WithRawJsiFunc
it('getVariantTuple(...) converts Float2', () => {
expect(testObject.getVariantTuple([10, 20])).toEqual([10, 20])
})
it('getVariantTuple(...) converts Float3', () => {
expect(testObject.getVariantTuple([10, 20, 30])).toEqual([10, 20, 30])
})
it('getVariantTuple(...) throws at wrong size (5 items)', () => {
expect(() =>
// @ts-expect-error
testObject.getVariantTuple([10, 20, 30, 40, 50])
).toThrow(`${testObject.name}.getVariantTuple(...): Cannot convert`)
})
it('getVariantTuple(...) throws at wrong type (string)', () => {
// @ts-expect-error
expect(() => testObject.getVariantTuple('hello')).toThrow(
`${testObject.name}.getVariantTuple(...): Cannot convert`
)
})
it('getVariantTuple(...) throws at wrong type (string[])', () => {
expect(() =>
// @ts-expect-error
testObject.getVariantTuple(['hello', 'world'])
).toThrow(`${testObject.name}.getVariantTuple(...): Cannot convert`)
})
it("someTuple roundtrips [55, 'hello']", () => {
testObject.someTuple = [55, 'hello']
expect(testObject.someTuple).toEqual([55, 'hello'])
})
it('flip([10, 20, 30]) reverses the tuple', () => {
expect(testObject.flip([10, 20, 30])).toEqual([30, 20, 10])
})
it('flip([10, 20]) throws at wrong size', () => {
const flipTwoItems = () =>
testObject.flip(
// @ts-expect-error
[10, 20]
)
expect(flipTwoItems).toThrow(
`${testObject.name}.flip(...): The given JS Array has 2 items, but ${debugOnly('std::tuple<double, double, double>')}`
)
expect(flipTwoItems).toThrow('expects 3 items')
})
it('passTuple(...) returns the same tuple', () => {
expect(testObject.passTuple([13, 'hello', true])).toEqual([
13,
'hello',
true,
])
})
it('bounceCustomType(...) returns the same value', () => {
expect(testObject.bounceCustomType(TEST_CUSTOM_TYPE)).toBe(
TEST_CUSTOM_TYPE
)
})
it('bounceAnyHybrid(...) returns the same object', () => {
expect(testObject.bounceAnyHybrid(HybridSomeExternalObject)).toBe(
HybridSomeExternalObject
)
})
it('bounceAnyHybrid(...) result does not equal a different object', () => {
const external = testObject.bounceAnyHybrid(HybridSomeExternalObject)
expect(external.equals(testObject)).toBe(false)
})
it('rawJsiFunc(...) returns its arguments as an array', () => {
const result = rawObject.rawJsiFunc(55, false, 'hello', { obj: true })
expect(result).toEqual([55, false, 'hello', { obj: true }])
})
it('getVariantHybridEnum(...) bounces a HybridObject', () => {
expect(testObject.getVariantHybridEnum(testObject)).toBe(testObject)
})
it('getVariantHybridEnum(...) bounces every Powertrain', () => {
for (const powertrain of POWERTRAINS) {
expect(testObject.getVariantHybridEnum(powertrain), powertrain).toBe(
powertrain
)
}
})
it('getVariantHybridEnum(...) throws for an unknown string', () => {
expect(() =>
// @ts-expect-error
testObject.getVariantHybridEnum('diesel')
).toThrow(
`${testObject.name}.getVariantHybridEnum(...): Cannot convert "diesel" to any type in ${debugOnly('variant<std::shared_ptr<margelo::nitro::test::HybridTestObjectCppSpec>, margelo::nitro::test::Powertrain>!')}`
)
})
it('getVariantHybridEnum(...) throws for a TestObjectSwiftKotlin', () => {
expect(() =>
// @ts-expect-error
testObject.getVariantHybridEnum(swiftKotlinObject)
).toThrow(
`${testObject.name}.getVariantHybridEnum(...): Cannot convert "${String(swiftKotlinObject)}" to any type in `
)
})
it('getVariantHybridEnum(...) throws for a Person', () => {
expect(() =>
// @ts-expect-error
testObject.getVariantHybridEnum(TEST_PERSON)
).toThrow(
`${testObject.name}.getVariantHybridEnum(...): Cannot convert "[object Object]" to any type in `
)
})
it('getVariantTuple(...) checks every item, not just the first', () => {
expect(() =>
testObject.getVariantTuple(
// @ts-expect-error
[10, 'twenty']
)
).toThrow(
`${testObject.name}.getVariantTuple(...): Cannot convert "10,twenty" to any type in `
)
})
it('getVariantTuple(...) throws for an empty array', () => {
expect(() =>
// @ts-expect-error
testObject.getVariantTuple([])
).toThrow(
`${testObject.name}.getVariantTuple(...): Cannot convert "" to any type in `
)
})
it('getVariantTuple(...) keeps NaN and Infinity', () => {
expect(testObject.getVariantTuple([NaN, Infinity])).toEqual([
NaN,
Infinity,
])
})
it('someTuple roundtrips NaN and an empty string', () => {
testObject.someTuple = [NaN, '']
expect(testObject.someTuple).toEqual([NaN, ''])
})
it('someTuple throws for 1 item', () => {
const setOneItem = () => {
// @ts-expect-error
testObject.someTuple = [55]
}
expect(setOneItem).toThrow(
`${testObject.name}.someTuple: The given JS Array has 1 items, but ${debugOnly('std::tuple<double, std::string>')}`
)
expect(setOneItem).toThrow('expects 2 items')
})
it('someTuple throws for a number in place of the string', () => {
expect(() => {
// @ts-expect-error
testObject.someTuple = [55, 66]
}).toThrow(
`${testObject.name}.someTuple: Value is a number, expected a String`
)
})
it('someTuple keeps its value after a failed set', () => {
testObject.someTuple = [13, 'before']
expect(() => {
// @ts-expect-error
testObject.someTuple = [55, 66]
}).toThrow(`${testObject.name}.someTuple: `)
expect(testObject.someTuple).toEqual([13, 'before'])
})
it('flip(...) keeps NaN, Infinity and -0', () => {
expect(testObject.flip([NaN, Infinity, -0])).toEqual([-0, Infinity, NaN])
})
it('flip(...) throws for 4 items', () => {
const flipFourItems = () =>
testObject.flip(
// @ts-expect-error
[1, 2, 3, 4]
)
expect(flipFourItems).toThrow(
`${testObject.name}.flip(...): The given JS Array has 4 items, but ${debugOnly('std::tuple<double, double, double>')}`
)
expect(flipFourItems).toThrow('expects 3 items')
})
it('flip(...) throws for an empty array', () => {
expect(() =>
// @ts-expect-error
testObject.flip([])
).toThrow(`${testObject.name}.flip(...): The given JS Array has 0 items`)
})
it('flip(...) throws for a string item', () => {
expect(() =>
testObject.flip(
// @ts-expect-error
[1, 'two', 3]
)
).toThrow(
`${testObject.name}.flip(...): Value is a string, expected a number`
)
})
it('flip(...) throws for a non-array object', () => {
expect(() =>
// @ts-expect-error
testObject.flip({ 0: 1, 1: 2, 2: 3, length: 3 })
).toThrow(
`${testObject.name}.flip(...): Object is an object, expected an array`
)
})
it('flip(...) throws for a string', () => {
expect(() =>
// @ts-expect-error
testObject.flip('abc')
).toThrow(
`${testObject.name}.flip(...): Value is a string, expected an Object`
)
})
it('passTuple(...) bounces falsy values', () => {
expect(testObject.passTuple([0, '', false])).toEqual([0, '', false])
})
it('passTuple(...) throws for a string in place of the boolean', () => {
expect(() =>
testObject.passTuple(
// @ts-expect-error
[13, 'hello', 'true']
)
).toThrow(
`${testObject.name}.passTuple(...): Value is a string, expected a boolean`
)
})
it('passTuple(...) throws for a number in place of the string', () => {
expect(() =>
testObject.passTuple(
// @ts-expect-error
[13, 42, true]
)
).toThrow(
`${testObject.name}.passTuple(...): Value is a number, expected a String`
)
})
it('passTuple(...) throws for 4 items', () => {
const passFourItems = () =>
testObject.passTuple(
// @ts-expect-error
[13, 'hello', true, 'extra']
)
expect(passFourItems).toThrow(
`${testObject.name}.passTuple(...): The given JS Array has 4 items, but ${debugOnly('std::tuple<double, std::string, bool>')}`
)
expect(passFourItems).toThrow('expects 3 items')
})
it('bounceCustomType(...) bounces an empty string', () => {
expect(testObject.bounceCustomType('')).toBe('')
})
it('bounceCustomType(...) bounces a unicode string', () => {
expect(testObject.bounceCustomType('héllo 👋 世界')).toBe('héllo 👋 世界')
})
it('bounceCustomType(...) keeps an embedded NUL character', () => {
expect(testObject.bounceCustomType('a\0b')).toBe('a\0b')
})
it('bounceCustomType(...) throws for a number', () => {
expect(() =>
// @ts-expect-error
testObject.bounceCustomType(55)
).toThrow(
`${testObject.name}.bounceCustomType(...): Value is a number, expected a String`
)
})
it('bounceAnyHybrid(...) bounces itself', () => {
expect(testObject.bounceAnyHybrid(testObject)).toBe(testObject)
})
it('bounceAnyHybrid(...) bounces a TestObjectSwiftKotlin', () => {
expect(testObject.bounceAnyHybrid(swiftKotlinObject)).toBe(
swiftKotlinObject
)
})
it('bounceAnyHybrid(...) bounces a C++ Child', () => {
const child = testObject.createChild()
expect(testObject.bounceAnyHybrid(child)).toBe(child)
})
it('bounceAnyHybrid(...) bounces a Swift/Kotlin Child', () => {
const child = NitroModules.createHybridObject<Child>('Child')
expect(testObject.bounceAnyHybrid(child)).toBe(child)
})
it('bounceAnyHybrid(...) bounces a Swift/Kotlin Base', () => {
const base = NitroModules.createHybridObject<Base>('Base')
expect(testObject.bounceAnyHybrid(base)).toBe(base)
})
it('bounceAnyHybrid(...) throws for a plain object', () => {
expect(() =>
// @ts-expect-error
testObject.bounceAnyHybrid({})
).toThrow(
`${testObject.name}.bounceAnyHybrid(...): Cannot convert "[object Object]" to NativeState<${debugOnly('margelo::nitro::HybridObject>! It does not have a NativeState!')}`
)
})
itIf(IS_DEBUG)('bounceAnyHybrid(...) throws for undefined', () => {
expect(() =>
// @ts-expect-error
testObject.bounceAnyHybrid(undefined)
).toThrow(
`${testObject.name}.bounceAnyHybrid(...): Cannot convert "undefined" to NativeState<margelo::nitro::HybridObject>! It is undefined!`
)
})
it('rawJsiFunc() returns an empty array without arguments', () => {
expect(rawObject.rawJsiFunc()).toEqual([])
})
it('rawJsiFunc(...) keeps undefined and null arguments', () => {
expect(rawObject.rawJsiFunc(undefined, null)).toStrictEqual([
undefined,
null,
])
})
it('rawJsiFunc(...) passes objects by reference', () => {
const object = { some: 'object' }
const [result] = rawObject.rawJsiFunc(object) as unknown[]
expect(result).toBe(object)
})
it('rawJsiFunc(...) passes functions by reference', () => {
const func = () => {}
const [result] = rawObject.rawJsiFunc(func) as unknown[]
expect(result).toBe(func)
})
itIf(IS_DEBUG)(
'rawJsiFunc(...) throws for a `this` without NativeState',
() => {
expect(() => rawObject.rawJsiFunc.call({})).toThrow(
'Cannot call hybrid function `HybridTestObjectCpp.rawJsiFunc(...)` - `this` does not have a NativeState!'
)
}
)
itIf(IS_DEBUG)(
'rawJsiFunc(...) throws for a different HybridObject as `this`',
() => {
expect(() =>
rawObject.rawJsiFunc.call(HybridSomeExternalObject)
).toThrow(
"Cannot call hybrid function `HybridTestObjectCpp.rawJsiFunc(...)` - `this` has a NativeState, but it's the wrong type!"
)
}
)
})
})