|
1 |
| -package io.scalajs.nodejs.buffer |
| 1 | +package nodejs.buffer |
2 | 2 |
|
3 |
| -import io.scalajs.collection.Iterator.Entry |
4 |
| -import io.scalajs.nodejs.buffer |
| 3 | +import io.scalajs.nodejs.buffer.Buffer |
5 | 4 | import org.scalatest.FunSpec
|
6 | 5 |
|
7 | 6 | import scala.scalajs.js
|
8 |
| -import scala.scalajs.js.typedarray.{ArrayBuffer, DataView, Uint8Array} |
9 | 7 |
|
10 | 8 | /**
|
11 | 9 | * Buffer Tests
|
12 | 10 | */
|
13 | 11 | class BufferTest extends FunSpec {
|
14 |
| - |
15 |
| - describe("Buffer") { |
16 |
| - describe("instance members") { |
17 |
| - it("should sort buffers") { |
18 |
| - val buf1 = Buffer.from("ABC") |
19 |
| - val buf2 = Buffer.from("BCD") |
20 |
| - val buf3 = Buffer.from("ABCD") |
21 |
| - |
22 |
| - assert(buf1.compare(buf1) == 0) |
23 |
| - assert(buf1.compare(buf2) == -1) |
24 |
| - assert(buf1.compare(buf3) == -1) |
25 |
| - assert(buf2.compare(buf1) == 1) |
26 |
| - assert(buf2.compare(buf3) == 1) |
27 |
| - } |
28 |
| - |
29 |
| - it("should support iterating entries [classic]") { |
30 |
| - val buf = Buffer.from("Hello!") |
31 |
| - val it = buf.entries() |
32 |
| - var result: Entry[js.Any] = null |
33 |
| - do { |
34 |
| - result = it.next() |
35 |
| - if (!result.done) info(s"value: ${result.value}") |
36 |
| - } while (!result.done) |
37 |
| - } |
38 |
| - |
39 |
| - it("should support iterating entries [Scala]") { |
40 |
| - val buf = Buffer.from("Hello!") |
41 |
| - for (value <- buf.entries()) info(s"value: $value") |
42 |
| - } |
43 |
| - |
44 |
| - it("should support buffer property") { |
45 |
| - val arrayBuffer = new ArrayBuffer(16) |
46 |
| - val buf = Buffer.from(arrayBuffer) |
47 |
| - assert(buf.buffer === arrayBuffer) |
48 |
| - } |
49 |
| - |
50 |
| - it("should support byteOffset property") { |
51 |
| - import scala.scalajs.js.typedarray.Int8Array |
52 |
| - val nodeBuffer = Buffer.from(js.Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) |
53 |
| - val typedarray = new Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length) |
54 |
| - assert(typedarray.mkString == new Int8Array(js.Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)).mkString) |
55 |
| - } |
56 |
| - |
57 |
| - it("should support byteLength for specific types") { |
58 |
| - assert(Buffer.byteLength("\u00bd + \u00bc = \u00be") == 12) |
59 |
| - assert(Buffer.byteLength("\u00bd + \u00bc = \u00be", "utf8") == 12) |
60 |
| - assert(Buffer.byteLength(Buffer.alloc(12)) == 12) |
61 |
| - assert(Buffer.byteLength(new Uint8Array(12)) == 12) |
62 |
| - assert(Buffer.byteLength(new DataView(new ArrayBuffer(12))) == 12) |
63 |
| - assert(Buffer.byteLength(new ArrayBuffer(12)) == 12) |
64 |
| - } |
65 |
| - |
66 |
| - it("should support fill") { |
67 |
| - val otherBuf = Buffer.from("abcdef") |
68 |
| - assert(Buffer.alloc(10).fill(otherBuf).toString() == "abcdefabcd") |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - describe("class members") { |
73 |
| - it("should support fields") { |
74 |
| - assert(Buffer.poolSize > 0) |
75 |
| - } |
76 |
| - |
77 |
| - it("should create buffers from strings") { |
78 |
| - val bufA = Buffer.from("Hello ") |
79 |
| - info(s"bufA => ${bufA.toString()}") |
80 |
| - val bufB = Buffer.from("World") |
81 |
| - info(s"bufB => ${bufB.toString()}") |
82 |
| - val bufC = bufA + bufB |
83 |
| - info(s"bufC => ${bufC.toString()}, length = ${bufC.byteLength()}") |
84 |
| - |
85 |
| - assert(bufA.toString() == "Hello ") |
86 |
| - assert(bufB.toString() == "World") |
87 |
| - assert(bufC.byteLength() == 11) |
88 |
| - } |
89 |
| - |
90 |
| - it("should create buffers from buffers") { |
91 |
| - val buffer = Buffer.from("hello") |
92 |
| - assert(Buffer.from(buffer).toString() == "hello") |
93 |
| - |
94 |
| - // TODO: when Scala.js added TypedArray.from |
95 |
| - // val uints = Uint8Array.from(???) |
96 |
| - // assert(Buffer.from(uints).toString() == "worlds") |
97 |
| - } |
98 |
| - |
99 |
| - it("should support concat") { |
100 |
| - val buffers = js.Array(Buffer.from("abc"), Buffer.from("def"), Buffer.from("ghijk")) |
101 |
| - assert(Buffer.compare(Buffer.concat(buffers), Buffer.from("abcdefghijk")) == 0) |
102 |
| - assert(Buffer.compare(Buffer.concat(buffers, 5), Buffer.from("abcde")) == 0) |
103 |
| - |
104 |
| - val uints: js.Array[Uint8Array] = js.Array(Buffer.from("abc"), Buffer.from("def"), Buffer.from("ghijk")) |
105 |
| - assert(Buffer.compare(Buffer.concat(uints), Buffer.from("abcdefghijk")) == 0) |
106 |
| - assert(Buffer.compare(Buffer.concat(uints, 5), Buffer.from("abcde")) == 0) |
107 |
| - } |
108 |
| - |
109 |
| - it("should support isBufrer") { |
110 |
| - assert(!Buffer.isBuffer(null)) |
111 |
| - assert(!Buffer.isBuffer(js.Object())) |
112 |
| - assert(!Buffer.isBuffer(js.Array(1, 2, 3))) |
113 |
| - assert(Buffer.isBuffer(Buffer.from("hello"))) |
114 |
| - } |
115 |
| - |
116 |
| - it("should support isEncoding") { |
117 |
| - assert(!Buffer.isEncoding(null)) |
118 |
| - assert(!Buffer.isEncoding("")) |
119 |
| - assert(Buffer.isEncoding("utf8")) |
120 |
| - assert(Buffer.isEncoding("UTF-8")) |
121 |
| - } |
122 |
| - |
123 |
| - it("should support writeBigInt64BE, writeBigInt64LE, writeBigInt64BE and writeBigInt64BE") { |
124 |
| - val buf = Buffer.allocUnsafe(8) |
125 |
| - val v = js.Dynamic.global.BigInt("0x0102030405060708") |
126 |
| - buf.writeBigInt64BE(v, 0); |
127 |
| - assert(Buffer.compare(buf, Buffer.from(js.Array(1, 2, 3, 4, 5, 6, 7, 8))) === 0) |
128 |
| - } |
129 |
| - } |
130 |
| - |
131 |
| - describe("module members") { |
132 |
| - it("should support transcode") { |
133 |
| - // package object method |
134 |
| - assert(buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") == "hello") |
135 |
| - assert(buffer.transcode(Buffer.from("€"), "utf8", "ascii").toString("ascii") == "?") |
136 |
| - |
137 |
| - // extension method |
138 |
| - assert(Buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") == "hello") |
139 |
| - assert(Buffer.transcode(Buffer.from("€"), "utf8", "ascii").toString("ascii") == "?") |
140 |
| - } |
141 |
| - |
142 |
| - it("should support fields") { |
143 |
| - // package object method |
144 |
| - assert(buffer.INSPECT_MAX_BYTES > 0) |
145 |
| - assert(buffer.kMaxLength > 0) |
146 |
| - |
147 |
| - // extension method |
148 |
| - assert(Buffer.INSPECT_MAX_BYTES > 0) |
149 |
| - assert(Buffer.kMaxLength > 0) |
150 |
| - } |
151 |
| - |
152 |
| - it("should support constants") { |
153 |
| - // package object method |
154 |
| - assert(buffer.constants.MAX_LENGTH > 0) |
155 |
| - assert(buffer.constants.MAX_STRING_LENGTH > 0) |
156 |
| - |
157 |
| - // extension method |
158 |
| - assert(Buffer.constants.MAX_LENGTH > 0) |
159 |
| - assert(Buffer.constants.MAX_STRING_LENGTH > 0) |
160 |
| - } |
161 |
| - } |
| 12 | + it("should support writeBigInt64BE, writeBigInt64LE, writeBigInt64BE and writeBigInt64BE") { |
| 13 | + val buf = Buffer.allocUnsafe(8) |
| 14 | + val v = js.Dynamic.global.BigInt("0x0102030405060708") |
| 15 | + buf.writeBigInt64BE(v, 0); |
| 16 | + assert(Buffer.compare(buf, Buffer.from(js.Array(1, 2, 3, 4, 5, 6, 7, 8))) === 0) |
162 | 17 | }
|
163 |
| - |
164 | 18 | }
|
0 commit comments