Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(fmt): add test cases for printf() #5278

Merged
merged 1 commit into from
Jul 4, 2024
Merged
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
27 changes: 25 additions & 2 deletions fmt/printf_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// https://golang.org/src/fmt/fmt_test.go
// BSD: Copyright (c) 2009 The Go Authors. All rights reserved.

import { sprintf } from "./printf.ts";
import { assertEquals } from "@std/assert";
import { printf, sprintf } from "./printf.ts";
import { assertEquals, assertThrows } from "@std/assert";
import { assertSpyCall, spy } from "@std/testing/mock";

Deno.test("sprintf() handles noVerb", function () {
assertEquals(sprintf("bla"), "bla");
Expand Down Expand Up @@ -108,6 +109,7 @@ Deno.test("sprintf() handles floats", function () {
assertEquals(sprintf("%e", Number.MIN_SAFE_INTEGER), "-9.007199e+15");
assertEquals(sprintf("%.3e", 1.9999), "2.000e+00");
assertEquals(sprintf("%.3e", 29.99999), "3.000e+01");
assertEquals(sprintf("%.3e", 999999), "1.000e+06");
});
Deno.test("sprintf() handles floatE", function () {
assertEquals(sprintf("%E", 4), "4.000000E+00");
Expand Down Expand Up @@ -169,6 +171,13 @@ Deno.test("sprintf() handles string", function () {
Deno.test("sprintf() handles hex", function () {
assertEquals(sprintf("%x", "123"), "313233");
assertEquals(sprintf("%x", "n"), "6e");

// hex throws with non-strings and non-numbers
assertThrows(
() => sprintf("%x", {}),
Error,
"currently only number and string are implemented for hex",
);
});
Deno.test("sprintf() handles heX", function () {
assertEquals(sprintf("%X", "123"), "313233");
Expand Down Expand Up @@ -641,6 +650,7 @@ Deno.test("sprintf() handles formatV", function () {
}`,
);
assertEquals(sprintf("%#.1v", a), `{ a: { a: [Object] } }`);
assertEquals(sprintf("%.10v", a), "[object Ob"); // truncated at 10th char
});

Deno.test("sprintf() handles formatJ", function () {
Expand Down Expand Up @@ -716,3 +726,16 @@ Deno.test("sprintf() handles errors", function () {
assertEquals(sprintf("%.[5]f"), "%!(BAD INDEX)");
assertEquals(sprintf("%.[5]*f"), "%!(BAD INDEX)");
});

Deno.test("sprintf() throws with d with sharp option", () => {
assertThrows(() => sprintf("%#d", 1.1), Error, "cannot handle base: 10");
});

Deno.test("printf() prints the result synchronously", () => {
using writeSpy = spy(Deno.stdout, "writeSync");
printf("Hello %s", "world");

assertSpyCall(writeSpy, 0, {
args: [new TextEncoder().encode("Hello world")],
});
});