aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/bun.js')
-rw-r--r--test/bun.js/console-iterator-run.js3
-rw-r--r--test/bun.js/console-iterator.test.js64
-rw-r--r--test/bun.js/index-of-line.test.ts37
3 files changed, 104 insertions, 0 deletions
diff --git a/test/bun.js/console-iterator-run.js b/test/bun.js/console-iterator-run.js
new file mode 100644
index 000000000..7664c85a1
--- /dev/null
+++ b/test/bun.js/console-iterator-run.js
@@ -0,0 +1,3 @@
+for await (const line of console) {
+ console.write(line);
+}
diff --git a/test/bun.js/console-iterator.test.js b/test/bun.js/console-iterator.test.js
new file mode 100644
index 000000000..c211fe8e6
--- /dev/null
+++ b/test/bun.js/console-iterator.test.js
@@ -0,0 +1,64 @@
+import { spawnSync, spawn } from "bun";
+import { describe, expect, it } from "bun:test";
+import { bunExe } from "bunExe";
+
+describe("should work for static input", () => {
+ const inputs = [
+ "hello world",
+ "hello world\n",
+ "hello world\n\n",
+ "hello world\n\n\n",
+ "Hello\nWorld\n",
+ "1",
+ "❤️ Red Heart ✨ Sparkles 🔥 Fire\n❤️ Red Heart ✨ Sparkles\n❤️ Red Heart\n❤️\n\nnormal",
+ ];
+
+ for (let input of inputs) {
+ it(input.replaceAll("\n", "\\n"), () => {
+ const { stdout } = spawnSync({
+ cmd: [bunExe(), import.meta.dir + "/" + "console-iterator-run.js"],
+ stdin: Buffer.from(input),
+ env: {
+ BUN_DEBUG_QUIET_LOGS: "1",
+ },
+ });
+ expect(stdout.toString()).toBe(input.replaceAll("\n", ""));
+ });
+ }
+});
+
+describe("should work for streaming input", () => {
+ const inputs = [
+ "hello world",
+ "hello world\n",
+ "hello world\n\n",
+ "hello world\n\n\n",
+ "Hello\nWorld\n",
+ "1",
+ "❤️ Red Heart ✨ Sparkles 🔥 Fire\n❤️ Red Heart ✨ Sparkles\n❤️ Red Heart\n❤️\n\nnormal",
+ ];
+
+ for (let input of inputs) {
+ it(input.replaceAll("\n", "\\n"), async () => {
+ const { stdout, stdin } = spawn({
+ cmd: [bunExe(), import.meta.dir + "/" + "console-iterator-run.js"],
+ stdin: "pipe",
+ stdout: "pipe",
+ env: {
+ BUN_DEBUG_QUIET_LOGS: "1",
+ },
+ });
+ stdin.write(input.slice(0, 4));
+ await new Promise((resolve) => setTimeout(resolve, 1));
+ stdin.write(input.slice(4, 5));
+ await new Promise((resolve) => setTimeout(resolve, 1));
+ stdin.write(input.slice(5));
+ await new Promise((resolve) => setTimeout(resolve, 1));
+ stdin.end();
+
+ expect(await new Response(stdout).text()).toBe(
+ input.replaceAll("\n", ""),
+ );
+ });
+ }
+});
diff --git a/test/bun.js/index-of-line.test.ts b/test/bun.js/index-of-line.test.ts
new file mode 100644
index 000000000..1c6cbaea2
--- /dev/null
+++ b/test/bun.js/index-of-line.test.ts
@@ -0,0 +1,37 @@
+import { expect, test } from "bun:test";
+import { indexOfLine } from "bun";
+
+test("indexOfLine", () => {
+ const source = `
+ const a = 1;
+
+ const b = 2;
+
+ 😋const c = 3; // handles unicode
+
+ 😋 Get Emoji — All Emojis to ✂️
+
+ const b = 2;
+
+ const c = 3;
+`;
+ var i = 0;
+ var j = 0;
+ const buffer = Buffer.from(source);
+ var nonEmptyLineCount = 0;
+ while (i < buffer.length) {
+ const prev = j;
+ j = source.indexOf("\n", j);
+ i = indexOfLine(buffer, i);
+
+ const delta = Buffer.byteLength(source.slice(0, j), "utf8") - j;
+ console.log(source.slice(prev + 1, j));
+ if (i === -1) {
+ expect(j).toBe(-1);
+ expect(nonEmptyLineCount).toBe(6);
+ break;
+ }
+ expect(i++ - delta).toBe(j++);
+ nonEmptyLineCount++;
+ }
+});