-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
927 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { createArbParser } from "../../formats/arb.ts"; | ||
|
||
describe("ARB parser", () => { | ||
const parser = createArbParser(); | ||
|
||
describe("parse", () => { | ||
it("should parse valid ARB file", async () => { | ||
const input = `{ | ||
"@@locale": "en", | ||
"greeting": "Hello", | ||
"@greeting": { | ||
"description": "A greeting message" | ||
}, | ||
"message": "World", | ||
"@message": { | ||
"description": "A message" | ||
} | ||
}`; | ||
|
||
const result = await parser.parse(input); | ||
expect(result).toEqual({ | ||
greeting: "Hello", | ||
message: "World", | ||
}); | ||
}); | ||
|
||
it("should handle empty ARB file", async () => { | ||
const input = "{}"; | ||
const result = await parser.parse(input); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it("should throw on invalid JSON", async () => { | ||
const input = "invalid json content"; | ||
await expect(parser.parse(input)).rejects.toThrow( | ||
"Failed to parse ARB translations", | ||
); | ||
}); | ||
}); | ||
|
||
describe("serialize", () => { | ||
it("should serialize translations to ARB format", async () => { | ||
const input = { | ||
greeting: "Hello", | ||
message: "World", | ||
}; | ||
|
||
const result = await parser.serialize("en", input); | ||
const parsed = JSON.parse(result); | ||
expect(parsed).toEqual({ | ||
"@@locale": "en", | ||
greeting: "Hello", | ||
message: "World", | ||
}); | ||
}); | ||
|
||
it("should handle empty translations", async () => { | ||
const input = {}; | ||
const result = await parser.serialize("fr", input); | ||
const parsed = JSON.parse(result); | ||
expect(parsed).toEqual({ | ||
"@@locale": "fr", | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { describe, expect, test } from "bun:test"; | ||
import { createCsvParser } from "../csv.js"; | ||
|
||
describe("CSV Parser", () => { | ||
test("parses CSV with id and value columns", async () => { | ||
const parser = createCsvParser(); | ||
const input = `id,value | ||
greeting,Hello | ||
farewell,Goodbye`; | ||
|
||
const result = await parser.parse(input); | ||
|
||
expect(result).toEqual({ | ||
greeting: "Hello", | ||
farewell: "Goodbye", | ||
}); | ||
}); | ||
|
||
test("ignores rows without id or value", async () => { | ||
const parser = createCsvParser(); | ||
const input = `id,value | ||
greeting,Hello | ||
,Goodbye | ||
invalid_row, | ||
,`; | ||
|
||
const result = await parser.parse(input); | ||
|
||
expect(result).toEqual({ | ||
greeting: "Hello", | ||
}); | ||
}); | ||
|
||
test("throws error on invalid CSV", async () => { | ||
const parser = createCsvParser(); | ||
const input = "invalid,csv,format"; | ||
|
||
await expect(parser.parse(input)).rejects.toThrow( | ||
"Failed to parse CSV translations", | ||
); | ||
}); | ||
|
||
test("serializes data to CSV format", async () => { | ||
const parser = createCsvParser(); | ||
const data = { | ||
greeting: "Hello", | ||
farewell: "Goodbye", | ||
}; | ||
|
||
const result = await parser.serialize("en", data); | ||
|
||
expect(result).toEqual(`id,value | ||
greeting,Hello | ||
farewell,Goodbye | ||
`); | ||
}); | ||
|
||
test("preserves existing columns when serializing", async () => { | ||
const parser = createCsvParser(); | ||
// First parse a CSV with context data to initialize metadata | ||
const initialInput = `id,value,context | ||
greeting,Hello,Welcome message | ||
farewell,Goodbye,Exit message`; | ||
await parser.parse(initialInput); | ||
|
||
const data = { | ||
greeting: "Hola", | ||
farewell: "Adios", | ||
newKey: "New Value", | ||
}; | ||
|
||
const result = await parser.serialize("en", data); | ||
|
||
expect(result).toEqual(`id,value,context | ||
greeting,Hola,Welcome message | ||
farewell,Adios,Exit message | ||
newKey,New Value, | ||
`); | ||
}); | ||
|
||
test("handles empty input when serializing", async () => { | ||
const parser = createCsvParser(); | ||
const data = { | ||
greeting: "Hello", | ||
}; | ||
|
||
const result = await parser.serialize("en", data); | ||
|
||
expect(result).toEqual(`id,value | ||
greeting,Hello | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.