summaryrefslogtreecommitdiff
path: root/tests/Agent.Core.Tests/LocaleCatalogTests.cs
blob: e5b608557fe22cb3caa56fe8e891fbfdd03eaef0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Agent.Core.Localization;

namespace Agent.Core.Tests;

public sealed class LocaleCatalogTests
{
    [Fact]
    public void MissingSelectedKeyFallsBackToEnglish()
    {
        using var directory = new TestDirectory();
        directory.File("en.json", """{"hello":"Hello {name}","only_en":"fallback"}""");
        directory.File("ja.json", """{"hello":"こんにちは {name}"}""");
        LocaleCatalog catalog = LocaleCatalog.Load(directory.Path, "ja");
        Assert.Equal("fallback", catalog.Get("only_en"));
        Assert.Equal("こんにちは Codex", catalog.Format("hello", new Dictionary<string, object?> { ["name"] = "Codex" }));
    }

    [Fact]
    public void AdditionalLocaleLoadsWithoutCodeChange()
    {
        using var directory = new TestDirectory();
        directory.File("en.json", """{"hello":"Hello"}""");
        directory.File("zh-CN.json", """{"hello":"你好"}""");
        Assert.Equal("你好", LocaleCatalog.Load(directory.Path, "zh-CN").Get("hello"));
    }

    [Theory]
    [InlineData("{broken")]
    [InlineData("[]")]
    public void MalformedLocaleIsFatal(string json)
    {
        using var directory = new TestDirectory();
        directory.File("en.json", json);
        Assert.Throws<LocaleException>(() => LocaleCatalog.Load(directory.Path, "en"));
    }

    [Fact]
    public void UnknownKeyIsFatal()
    {
        using var directory = new TestDirectory();
        directory.File("en.json", """{"known":"value"}""");
        directory.File("ja.json", """{"known":"値","unknown":"x"}""");
        Assert.Throws<LocaleException>(() => LocaleCatalog.Load(directory.Path, "ja"));
    }

    [Fact]
    public void PlaceholderMismatchIsFatal()
    {
        using var directory = new TestDirectory();
        directory.File("en.json", """{"hello":"Hello {name}"}""");
        directory.File("ja.json", """{"hello":"こんにちは {user}"}""");
        Assert.Throws<LocaleException>(() => LocaleCatalog.Load(directory.Path, "ja"));
    }
}