|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { BashEnv } from "../../BashEnv.js"; |
| 3 | + |
| 4 | +describe("printf", () => { |
| 5 | + describe("basic format specifiers", () => { |
| 6 | + it("should format string with %s", async () => { |
| 7 | + const env = new BashEnv(); |
| 8 | + const result = await env.exec('printf "Hello %s" world'); |
| 9 | + expect(result.stdout).toBe("Hello world"); |
| 10 | + expect(result.exitCode).toBe(0); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should format integer with %d", async () => { |
| 14 | + const env = new BashEnv(); |
| 15 | + const result = await env.exec('printf "Number: %d" 42'); |
| 16 | + expect(result.stdout).toBe("Number: 42"); |
| 17 | + expect(result.exitCode).toBe(0); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should format float with %f", async () => { |
| 21 | + const env = new BashEnv(); |
| 22 | + const result = await env.exec('printf "Value: %f" 3.14'); |
| 23 | + expect(result.stdout).toBe("Value: 3.14"); |
| 24 | + expect(result.exitCode).toBe(0); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should format hex with %x", async () => { |
| 28 | + const env = new BashEnv(); |
| 29 | + const result = await env.exec('printf "Hex: %x" 255'); |
| 30 | + expect(result.stdout).toBe("Hex: ff"); |
| 31 | + expect(result.exitCode).toBe(0); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should format octal with %o", async () => { |
| 35 | + const env = new BashEnv(); |
| 36 | + const result = await env.exec('printf "Octal: %o" 8'); |
| 37 | + expect(result.stdout).toBe("Octal: 10"); |
| 38 | + expect(result.exitCode).toBe(0); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should handle literal %% ", async () => { |
| 42 | + const env = new BashEnv(); |
| 43 | + const result = await env.exec('printf "100%%"'); |
| 44 | + expect(result.stdout).toBe("100%"); |
| 45 | + expect(result.exitCode).toBe(0); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should handle multiple arguments", async () => { |
| 49 | + const env = new BashEnv(); |
| 50 | + const result = await env.exec('printf "%s is %d years old" Alice 30'); |
| 51 | + expect(result.stdout).toBe("Alice is 30 years old"); |
| 52 | + expect(result.exitCode).toBe(0); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe("escape sequences", () => { |
| 57 | + it("should handle newline \\n", async () => { |
| 58 | + const env = new BashEnv(); |
| 59 | + const result = await env.exec('printf "line1\\nline2"'); |
| 60 | + expect(result.stdout).toBe("line1\nline2"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should handle tab \\t", async () => { |
| 64 | + const env = new BashEnv(); |
| 65 | + const result = await env.exec('printf "col1\\tcol2"'); |
| 66 | + expect(result.stdout).toBe("col1\tcol2"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should handle backslash \\\\", async () => { |
| 70 | + const env = new BashEnv(); |
| 71 | + // 8 backslashes in source -> 4 in bash string -> 2 for printf -> 1 literal |
| 72 | + const result = await env.exec('printf "x\\\\\\\\y"'); |
| 73 | + expect(result.stdout).toBe("x\\y"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should handle carriage return \\r", async () => { |
| 77 | + const env = new BashEnv(); |
| 78 | + const result = await env.exec('printf "hello\\rworld"'); |
| 79 | + expect(result.stdout).toBe("hello\rworld"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should handle octal escape sequences", async () => { |
| 83 | + const env = new BashEnv(); |
| 84 | + const result = await env.exec('printf "\\101\\102\\103"'); |
| 85 | + expect(result.stdout).toBe("ABC"); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe("width and precision", () => { |
| 90 | + it("should handle width specifier", async () => { |
| 91 | + const env = new BashEnv(); |
| 92 | + const result = await env.exec('printf "%10s" "hi"'); |
| 93 | + expect(result.stdout).toBe(" hi"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should handle precision for floats", async () => { |
| 97 | + const env = new BashEnv(); |
| 98 | + const result = await env.exec('printf "%.2f" 3.14159'); |
| 99 | + expect(result.stdout).toBe("3.14"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should handle zero-padding", async () => { |
| 103 | + const env = new BashEnv(); |
| 104 | + const result = await env.exec('printf "%05d" 42'); |
| 105 | + expect(result.stdout).toBe("00042"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should handle left-justify with -", async () => { |
| 109 | + const env = new BashEnv(); |
| 110 | + const result = await env.exec('printf "%-10s|" "hi"'); |
| 111 | + expect(result.stdout).toBe("hi |"); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe("error handling", () => { |
| 116 | + it("should error with no arguments", async () => { |
| 117 | + const env = new BashEnv(); |
| 118 | + const result = await env.exec("printf"); |
| 119 | + expect(result.stderr).toContain("usage"); |
| 120 | + expect(result.exitCode).toBe(1); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should handle missing arguments gracefully", async () => { |
| 124 | + const env = new BashEnv(); |
| 125 | + const result = await env.exec('printf "%s %s" only'); |
| 126 | + expect(result.stdout).toBe("only "); |
| 127 | + expect(result.exitCode).toBe(0); |
| 128 | + }); |
| 129 | + |
| 130 | + it("should handle non-numeric for %d", async () => { |
| 131 | + const env = new BashEnv(); |
| 132 | + const result = await env.exec('printf "%d" notanumber'); |
| 133 | + expect(result.stdout).toBe("0"); |
| 134 | + expect(result.exitCode).toBe(0); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe("--help", () => { |
| 139 | + it("should display help", async () => { |
| 140 | + const env = new BashEnv(); |
| 141 | + const result = await env.exec("printf --help"); |
| 142 | + expect(result.stdout).toContain("printf"); |
| 143 | + expect(result.stdout).toContain("FORMAT"); |
| 144 | + expect(result.exitCode).toBe(0); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments