-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_format_test.go
More file actions
100 lines (89 loc) · 1.98 KB
/
Copy pathdocument_format_test.go
File metadata and controls
100 lines (89 loc) · 1.98 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 WoozyMasta
// Source: github.com/woozymasta/pofile
package pofile
import (
"strings"
"testing"
)
func TestFormatDocumentPreserve(t *testing.T) {
t.Parallel()
document := &Document{
Headers: []Header{
{Key: "X-Generator", Value: "test"},
{Key: "Language", Value: "ru"},
},
Entries: []*Entry{
{
Context: "b",
ID: "b",
Translations: map[int]string{
0: "Б",
},
},
{
Context: "a",
ID: "a",
Translations: map[int]string{
0: "А",
},
},
},
}
data, err := FormatDocument(document, &WriteOptions{
Mode: WriteModePreserve,
})
if err != nil {
t.Fatalf("FormatDocument error: %v", err)
}
text := string(data)
firstB := strings.Index(text, `msgctxt "b"`)
firstA := strings.Index(text, `msgctxt "a"`)
if !(firstB >= 0 && firstA > firstB) {
t.Fatalf("entry order is not preserved:\n%s", text)
}
}
func TestFormatDocumentCanonicalSort(t *testing.T) {
t.Parallel()
document := &Document{
Headers: []Header{
{Key: "X-Generator", Value: "test"},
{Key: "Language", Value: "ru"},
},
Entries: []*Entry{
{
Context: "b",
ID: "b",
Translations: map[int]string{
0: "Б",
},
},
{
Context: "a",
ID: "a",
Translations: map[int]string{
0: "А",
},
},
},
}
data, err := FormatDocument(document, &WriteOptions{
Mode: WriteModeCanonical,
SortEntries: true,
SortHeaders: true,
})
if err != nil {
t.Fatalf("FormatDocument error: %v", err)
}
text := string(data)
languagePos := strings.Index(text, `"Language: ru\n"`)
generatorPos := strings.Index(text, `"X-Generator: test\n"`)
if !(languagePos >= 0 && generatorPos > languagePos) {
t.Fatalf("headers are not sorted canonically:\n%s", text)
}
firstA := strings.Index(text, `msgctxt "a"`)
firstB := strings.Index(text, `msgctxt "b"`)
if !(firstA >= 0 && firstB > firstA) {
t.Fatalf("entries are not sorted canonically:\n%s", text)
}
}