-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathData+JSValueTests.swift
More file actions
55 lines (47 loc) · 1.81 KB
/
Data+JSValueTests.swift
File metadata and controls
55 lines (47 loc) · 1.81 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
import XCTest
import Foundation
import JavaScriptFoundationCompat
import JavaScriptKit
final class DataJSValueTests: XCTestCase {
func testDataToJSValue() {
let data = Data([0x00, 0x01, 0x02, 0x03])
let jsValue = data.jsValue
let uint8Array = JSTypedArray<UInt8>(from: jsValue)
XCTAssertEqual(uint8Array?.lengthInBytes, 4)
XCTAssertEqual(uint8Array?[0], 0x00)
XCTAssertEqual(uint8Array?[1], 0x01)
XCTAssertEqual(uint8Array?[2], 0x02)
XCTAssertEqual(uint8Array?[3], 0x03)
}
func testJSValueToData() {
let jsValue = JSTypedArray<UInt8>([0x00, 0x01, 0x02, 0x03]).jsValue
let data = Data.construct(from: jsValue)
XCTAssertEqual(data, Data([0x00, 0x01, 0x02, 0x03]))
}
func testDataToJSValue_withLargeData() {
let data = Data(repeating: 0x00, count: 1024 * 1024)
let jsValue = data.jsValue
let uint8Array = JSTypedArray<UInt8>(from: jsValue)
XCTAssertEqual(uint8Array?.lengthInBytes, 1024 * 1024)
}
func testJSValueToData_withLargeData() {
let jsValue = JSTypedArray<UInt8>(Array(repeating: 0x00, count: 1024 * 1024)).jsValue
let data = Data.construct(from: jsValue)
XCTAssertEqual(data?.count, 1024 * 1024)
}
func testDataToJSValue_withEmptyData() {
let data = Data()
let jsValue = data.jsValue
let uint8Array = JSTypedArray<UInt8>(from: jsValue)
XCTAssertEqual(uint8Array?.lengthInBytes, 0)
}
func testJSValueToData_withEmptyData() {
let jsValue = JSTypedArray<UInt8>([]).jsValue
let data = Data.construct(from: jsValue)
XCTAssertEqual(data, Data())
}
func testJSValueToData_withInvalidJSValue() {
let data = Data.construct(from: JSObject().jsValue)
XCTAssertNil(data)
}
}