diff --git a/packages/ash/assembly/__tests__/nl.spec.ts b/packages/ash/assembly/__tests__/nl.spec.ts new file mode 100644 index 0000000..289bde0 --- /dev/null +++ b/packages/ash/assembly/__tests__/nl.spec.ts @@ -0,0 +1,64 @@ +import { main as nl } from "../bin/nl"; +import { stdout, stderr } from './fixtures'; + + +describe("nl", (): void => { + beforeEach((): void => { + stdout.reset() + Console.stdout.erase() //Erases and resets + stderr.reset(); + Console.stderr.erase() //Erases and resets + CommandLine.reset(); + CommandLine.push("nl"); + }); + + it("with numbers", (): void => { + CommandLine.push("/numbers")//See ./simple_fs.ts + nl(CommandLine.all()) + let str = " 1 0\n 2 1\n 3 2\n 4 3\n 5 4\n 6 5\n" + + " 7 6\n 8 7\n 9 8\n 10 9\n 11 10\n 12 11\n 13 12\n" + + " 14 13\n 15 14\n 16 15\n 17 16\n 18 17\n 19 18\n 20 19\n"; + stdout.reset(); + expect(Console.stdout.tell()).toBe(str.lengthUTF8 - 1, "FD offset should be length of string"); + expect(stdout.readString().result).toStrictEqual(str); + }); + + it("with words", () => { + CommandLine.push("/home/bob/documents/secret.txt"); + let str = " 1 For my eyes only.\n 2 No one else" + nl(CommandLine.all()); + stdout.reset(); + expect(stdout.readString().result).toStrictEqual(str); + expect(Console.stdout.tell()).toBe(str.lengthUTF8 - 1, "FD offset should be length of string"); + }) + + it("should write to stderr if file not found", () => { + CommandLine.push("/doesnotexist"); + nl(CommandLine.all()) + expect(Console.stderr.tell()).toBeGreaterThan(0); + expect(stderr.readString().result).toStrictEqual("nl: /doesnotexist: No such file or directory\n") + }); + + it("options - t", () => { + CommandLine.push("-b") + CommandLine.push("t") + CommandLine.push("/home/bob/documents/secret.txt"); + let str = " 1 For my eyes only.\n 2 No one else" + nl(CommandLine.all()); + stdout.reset(); + expect(stdout.readString().result).toStrictEqual(str); + expect(Console.stdout.tell()).toBe(str.lengthUTF8 - 1, "FD offset should be length of string"); + }) + + it("options - n", () => { + CommandLine.push("-b") + CommandLine.push("n") + CommandLine.push("/home/bob/documents/secret.txt") + nl(CommandLine.all()) + stdout.reset() + let str = " For my eyes only.\n No one else" + expect(stdout.readString().result).toStrictEqual(str); + expect(Console.stdout.tell()).toBe(str.lengthUTF8 - 1, "FD offset should be length of string"); + }) + +}) \ No newline at end of file diff --git a/packages/ash/assembly/__tests__/simple_fs.ts b/packages/ash/assembly/__tests__/simple_fs.ts index 22a9ede..6077f25 100644 --- a/packages/ash/assembly/__tests__/simple_fs.ts +++ b/packages/ash/assembly/__tests__/simple_fs.ts @@ -16,6 +16,7 @@ export const fs_str = `\ }\ }\ },\ - "numbers": "0\\n1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n10\\n11\\n12\\n13\\n14\\n15\\n16\\n17\\n18\\n19\\n"\ + "numbers": "0\\n1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n10\\n11\\n12\\n13\\n14\\n15\\n16\\n17\\n18\\n19\\n",\ + "newLines": "hey\\n\\nhey"\ }\ ` \ No newline at end of file diff --git a/packages/ash/assembly/bin/nl.ts b/packages/ash/assembly/bin/nl.ts new file mode 100644 index 0000000..3dd82dc --- /dev/null +++ b/packages/ash/assembly/bin/nl.ts @@ -0,0 +1,36 @@ +export function main(args: string[]): void { + let option: string = ""; + let argnumber = 1; + if (args[1] == "-b") { + option = args[2] + argnumber = 3; + } + let res = fs.openFile(args[argnumber]) + if (res.error) { + //This is just the log for testing need to use Console.error + Console.error("nl: " + args[argnumber] + ": No such file or directory"); + // log(`head: file: No such file or directory`); + return; + } + let file = res.result; + let line = file.readLine(); + let num = 1; + Console.stdin.reset(); + if (line.failed) { + return; + } + + while (!line.failed) { + let intro = ""; + if (option == "" || option == "t") { + //@ts-ignore Integer does have to string method. + intro = " " + num.toString() + " "; + } + else if (option == "n") { + intro = " " + } + Console.write(intro.concat(line.result)); + line = file.readLine(); + num++; + } +} diff --git a/packages/assemblyscript/assembly/wasa/mock/utils/index.ts b/packages/assemblyscript/assembly/wasa/mock/utils/index.ts index 30cd3aa..6aed5d5 100644 --- a/packages/assemblyscript/assembly/wasa/mock/utils/index.ts +++ b/packages/assemblyscript/assembly/wasa/mock/utils/index.ts @@ -28,14 +28,13 @@ export class StringUtils { static fromCStringTilNewLine(cstring: usize, max: usize): string | null { let size: usize = 0; while (!this.terminates(cstring + size) && size < max - 1) { - if (this.isNewLine(cstring + size)) { + if (this.isNewLine(cstring + size++)) { break; } - size++; } if (size == 0) { return null } - return String.fromUTF8(cstring, size + 1); + return String.fromUTF8(cstring, size); } } \ No newline at end of file