|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import createXcodeStringsLoader from "./xcode-strings"; |
| 3 | + |
| 4 | +describe("xcode-strings loader", () => { |
| 5 | + it("should parse single-line entries", async () => { |
| 6 | + const loader = createXcodeStringsLoader(); |
| 7 | + loader.setDefaultLocale("en"); |
| 8 | + const input = `"hello" = "Hello"; |
| 9 | +"world" = "World";`; |
| 10 | + |
| 11 | + const result = await loader.pull("en", input); |
| 12 | + expect(result).toEqual({ |
| 13 | + hello: "Hello", |
| 14 | + world: "World", |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should parse multi-line string values", async () => { |
| 19 | + const loader = createXcodeStringsLoader(); |
| 20 | + loader.setDefaultLocale("en"); |
| 21 | + const input = `"greeting" = "Hello"; |
| 22 | +"multiline" = "This is line one |
| 23 | +This is line two |
| 24 | +This is line three"; |
| 25 | +"another" = "Another value";`; |
| 26 | + |
| 27 | + const result = await loader.pull("en", input); |
| 28 | + expect(result).toEqual({ |
| 29 | + greeting: "Hello", |
| 30 | + multiline: "This is line one\nThis is line two\nThis is line three", |
| 31 | + another: "Another value", |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should handle multi-line string with placeholders", async () => { |
| 36 | + const loader = createXcodeStringsLoader(); |
| 37 | + loader.setDefaultLocale("en"); |
| 38 | + const input = `"add_new_reference_share_text" = "Hi! |
| 39 | +Could you stop by quickly and tell us what you thought of our experience together? Your words will be super important to boost my profile on Worldpackers! |
| 40 | +[url] |
| 41 | +";`; |
| 42 | + |
| 43 | + const result = await loader.pull("en", input); |
| 44 | + expect(result).toEqual({ |
| 45 | + add_new_reference_share_text: |
| 46 | + "Hi!\nCould you stop by quickly and tell us what you thought of our experience together? Your words will be super important to boost my profile on Worldpackers!\n[url]\n", |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should skip comments", async () => { |
| 51 | + const loader = createXcodeStringsLoader(); |
| 52 | + loader.setDefaultLocale("en"); |
| 53 | + const input = `// This is a comment |
| 54 | +"hello" = "Hello"; |
| 55 | +// Another comment |
| 56 | +"world" = "World";`; |
| 57 | + |
| 58 | + const result = await loader.pull("en", input); |
| 59 | + expect(result).toEqual({ |
| 60 | + hello: "Hello", |
| 61 | + world: "World", |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should skip empty lines", async () => { |
| 66 | + const loader = createXcodeStringsLoader(); |
| 67 | + loader.setDefaultLocale("en"); |
| 68 | + const input = `"hello" = "Hello"; |
| 69 | +
|
| 70 | +"world" = "World";`; |
| 71 | + |
| 72 | + const result = await loader.pull("en", input); |
| 73 | + expect(result).toEqual({ |
| 74 | + hello: "Hello", |
| 75 | + world: "World", |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should handle escaped characters in single-line values", async () => { |
| 80 | + const loader = createXcodeStringsLoader(); |
| 81 | + loader.setDefaultLocale("en"); |
| 82 | + const input = `"escaped_quote" = "He said \\"Hello\\""; |
| 83 | +"escaped_newline" = "Line 1\\nLine 2"; |
| 84 | +"escaped_backslash" = "Path: C:\\\\Users";`; |
| 85 | + |
| 86 | + const result = await loader.pull("en", input); |
| 87 | + expect(result).toEqual({ |
| 88 | + escaped_quote: 'He said "Hello"', |
| 89 | + escaped_newline: "Line 1\nLine 2", |
| 90 | + escaped_backslash: "Path: C:\\Users", |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should handle empty values", async () => { |
| 95 | + const loader = createXcodeStringsLoader(); |
| 96 | + loader.setDefaultLocale("en"); |
| 97 | + const input = `"empty" = "";`; |
| 98 | + |
| 99 | + const result = await loader.pull("en", input); |
| 100 | + expect(result).toEqual({ |
| 101 | + empty: "", |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("push should convert object to .strings format", async () => { |
| 106 | + const loader = createXcodeStringsLoader(); |
| 107 | + loader.setDefaultLocale("en"); |
| 108 | + // Need to call pull first to initialize the loader state |
| 109 | + await loader.pull("en", ""); |
| 110 | + |
| 111 | + const payload = { |
| 112 | + hello: "Hello", |
| 113 | + world: "World", |
| 114 | + }; |
| 115 | + |
| 116 | + const result = await loader.push("en", payload); |
| 117 | + expect(result).toBe(`"hello" = "Hello"; |
| 118 | +"world" = "World";`); |
| 119 | + }); |
| 120 | + |
| 121 | + it("push should escape special characters", async () => { |
| 122 | + const loader = createXcodeStringsLoader(); |
| 123 | + loader.setDefaultLocale("en"); |
| 124 | + // Need to call pull first to initialize the loader state |
| 125 | + await loader.pull("en", ""); |
| 126 | + |
| 127 | + const payload = { |
| 128 | + escaped_quote: 'He said "Hello"', |
| 129 | + escaped_newline: "Line 1\nLine 2", |
| 130 | + escaped_backslash: "Path: C:\\Users", |
| 131 | + }; |
| 132 | + |
| 133 | + const result = await loader.push("en", payload); |
| 134 | + expect(result).toBe( |
| 135 | + `"escaped_quote" = "He said \\"Hello\\""; |
| 136 | +"escaped_newline" = "Line 1\\nLine 2"; |
| 137 | +"escaped_backslash" = "Path: C:\\\\Users";`, |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it("push should handle multi-line values by escaping newlines", async () => { |
| 142 | + const loader = createXcodeStringsLoader(); |
| 143 | + loader.setDefaultLocale("en"); |
| 144 | + // Need to call pull first to initialize the loader state |
| 145 | + await loader.pull("en", ""); |
| 146 | + |
| 147 | + const payload = { |
| 148 | + multiline: "This is line one\nThis is line two\nThis is line three", |
| 149 | + }; |
| 150 | + |
| 151 | + const result = await loader.push("en", payload); |
| 152 | + expect(result).toBe( |
| 153 | + `"multiline" = "This is line one\\nThis is line two\\nThis is line three";`, |
| 154 | + ); |
| 155 | + }); |
| 156 | + |
| 157 | + it("should handle mixed single-line and multi-line entries", async () => { |
| 158 | + const loader = createXcodeStringsLoader(); |
| 159 | + loader.setDefaultLocale("en"); |
| 160 | + const input = `"single1" = "Value 1"; |
| 161 | +"multi" = "Line 1 |
| 162 | +Line 2"; |
| 163 | +"single2" = "Value 2";`; |
| 164 | + |
| 165 | + const result = await loader.pull("en", input); |
| 166 | + expect(result).toEqual({ |
| 167 | + single1: "Value 1", |
| 168 | + multi: "Line 1\nLine 2", |
| 169 | + single2: "Value 2", |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments