|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { pseudoLocalize, pseudoLocalizeObject } from "./pseudo-localize"; |
| 3 | + |
| 4 | +describe("pseudoLocalize", () => { |
| 5 | + it("should replace characters with accented versions", () => { |
| 6 | + const result = pseudoLocalize("hello", { addMarker: false }); |
| 7 | + expect(result).toBe("ĥèļļø"); |
| 8 | + }); |
| 9 | + |
| 10 | + it("should add marker by default", () => { |
| 11 | + const result = pseudoLocalize("hello"); |
| 12 | + expect(result).toBe("ĥèļļø⚡"); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should not add marker when disabled", () => { |
| 16 | + const result = pseudoLocalize("hello", { addMarker: false }); |
| 17 | + expect(result).not.toContain("⚡"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should handle uppercase letters", () => { |
| 21 | + const result = pseudoLocalize("HELLO", { addMarker: false }); |
| 22 | + expect(result).toBe("ĤÈĻĻØ"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should preserve non-alphabetic characters", () => { |
| 26 | + const result = pseudoLocalize("Hello123!", { addMarker: false }); |
| 27 | + expect(result).toBe("Ĥèļļø123!"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should handle empty strings", () => { |
| 31 | + const result = pseudoLocalize(""); |
| 32 | + expect(result).toBe(""); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should handle strings with spaces", () => { |
| 36 | + const result = pseudoLocalize("Hello World", { addMarker: false }); |
| 37 | + expect(result).toBe("Ĥèļļø Ŵøŕļð"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should add length expansion when enabled", () => { |
| 41 | + const original = "hello"; |
| 42 | + const result = pseudoLocalize(original, { |
| 43 | + addMarker: false, |
| 44 | + addLengthMarker: true, |
| 45 | + lengthExpansion: 30, |
| 46 | + }); |
| 47 | + // 30% expansion of 5 chars = 2 extra chars (rounded up) |
| 48 | + expect(result.length).toBeGreaterThan("ĥèļļø".length); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should handle example from feature proposal", () => { |
| 52 | + const result = pseudoLocalize("Submit"); |
| 53 | + expect(result).toContain("⚡"); |
| 54 | + expect(result.startsWith("Š")).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should handle longer text", () => { |
| 58 | + const result = pseudoLocalize("Welcome back!"); |
| 59 | + expect(result).toBe("Ŵèļçømè ƀãçķ!⚡"); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("pseudoLocalizeObject", () => { |
| 64 | + it("should pseudo-localize string values", () => { |
| 65 | + const obj = { greeting: "hello" }; |
| 66 | + const result = pseudoLocalizeObject(obj, { addMarker: false }); |
| 67 | + expect(result.greeting).toBe("ĥèļļø"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should handle nested objects", () => { |
| 71 | + const obj = { |
| 72 | + en: { |
| 73 | + greeting: "hello", |
| 74 | + farewell: "goodbye", |
| 75 | + }, |
| 76 | + }; |
| 77 | + const result = pseudoLocalizeObject(obj, { addMarker: false }); |
| 78 | + expect(result.en.greeting).toBe("ĥèļļø"); |
| 79 | + expect(result.en.farewell).toContain("ĝ"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should handle arrays", () => { |
| 83 | + const obj = { |
| 84 | + messages: ["hello", "world"], |
| 85 | + }; |
| 86 | + const result = pseudoLocalizeObject(obj, { addMarker: false }); |
| 87 | + expect(Array.isArray(result.messages)).toBe(true); |
| 88 | + expect(result.messages[0]).toBe("ĥèļļø"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should preserve non-string values", () => { |
| 92 | + const obj = { |
| 93 | + greeting: "hello", |
| 94 | + count: 42, |
| 95 | + active: true, |
| 96 | + nothing: null, |
| 97 | + }; |
| 98 | + const result = pseudoLocalizeObject(obj, { addMarker: false }); |
| 99 | + expect(result.greeting).toBe("ĥèļļø"); |
| 100 | + expect(result.count).toBe(42); |
| 101 | + expect(result.active).toBe(true); |
| 102 | + expect(result.nothing).toBe(null); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should handle complex nested structures", () => { |
| 106 | + const obj = { |
| 107 | + ui: { |
| 108 | + buttons: { |
| 109 | + submit: "Submit", |
| 110 | + cancel: "Cancel", |
| 111 | + }, |
| 112 | + messages: ["error", "warning"], |
| 113 | + }, |
| 114 | + }; |
| 115 | + const result = pseudoLocalizeObject(obj, { addMarker: false }); |
| 116 | + expect(result.ui.buttons.submit).toContain("Š"); |
| 117 | + expect(result.ui.messages[0]).toContain("è"); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should handle empty objects", () => { |
| 121 | + const result = pseudoLocalizeObject({}, { addMarker: false }); |
| 122 | + expect(result).toEqual({}); |
| 123 | + }); |
| 124 | +}); |
0 commit comments