Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions packages/ash/assembly/__tests__/nl.spec.ts
Original file line number Diff line number Diff line change
@@ -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<u32>(Console.stdout.tell()).toBe(str.lengthUTF8 - 1, "FD offset should be length of string");
expect<string>(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<string>(stdout.readString().result).toStrictEqual(str);
expect<u32>(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<usize>(Console.stderr.tell()).toBeGreaterThan(0);
expect<string>(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<string>(stdout.readString().result).toStrictEqual(str);
expect<u32>(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<string>(stdout.readString().result).toStrictEqual(str);
expect<u32>(Console.stdout.tell()).toBe(str.lengthUTF8 - 1, "FD offset should be length of string");
})

})
3 changes: 2 additions & 1 deletion packages/ash/assembly/__tests__/simple_fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"\
}\
`
36 changes: 36 additions & 0 deletions packages/ash/assembly/bin/nl.ts
Original file line number Diff line number Diff line change
@@ -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<string>(`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++;
}
}
5 changes: 2 additions & 3 deletions packages/assemblyscript/assembly/wasa/mock/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}