-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
72 lines (57 loc) · 1.75 KB
/
error_test.go
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
// SPDX-FileCopyrightText: 2019-2024 caixw
//
// SPDX-License-Identifier: MIT
package config
import (
"testing"
"github.com/issue9/assert/v4"
"github.com/issue9/localeutil"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/message/catalog"
)
var (
_ error = &FieldError{}
_ localeutil.Stringer = &FieldError{}
)
func TestNewFieldError(t *testing.T) {
a := assert.New(t, false)
err1 := NewFieldError("f1", "err1")
a.NotNil(err1)
err2 := NewFieldError("f2", err1)
a.NotNil(err2).
Equal(err2.Field, "f2.f1").
Equal(err1.Field, "f2.f1")
}
func TestFieldError_LocaleString(t *testing.T) {
a := assert.New(t, false)
hans := language.MustParse("cmn-hans")
hant := language.MustParse("cmn-hant")
b := catalog.NewBuilder()
b.SetString(hans, "%s at %s:%s", "位于 %[2]s:%[3]s 发生了 %[1]s")
b.SetString(hant, "%s at %s:%s", "位于 %[2]s:%[3]s 发生了 %[1]s")
a.NotError(b.SetString(hans, "k1", "cn1"))
a.NotError(b.SetString(hant, "k1", "tw1"))
cnp := message.NewPrinter(hans, message.Catalog(b))
twp := message.NewPrinter(hant, message.Catalog(b))
ferr := NewFieldError("", localeutil.Phrase("k1"))
ferr.Path = "path"
a.Equal("位于 path: 发生了 cn1", ferr.LocaleString(cnp))
a.Equal("位于 path: 发生了 tw1", ferr.LocaleString(twp))
a.Equal("k1 at path:", ferr.Error())
}
func TestFieldError_SetFieldParent(t *testing.T) {
a := assert.New(t, false)
err := NewFieldError("f1", "error")
err.AddFieldParent("f2")
a.Equal(err.Field, "f2.f1")
err.AddFieldParent("f3")
a.Equal(err.Field, "f3.f2.f1")
err.AddFieldParent("")
a.Equal(err.Field, "f3.f2.f1")
err = NewFieldError("", "error")
err.AddFieldParent("f2")
a.Equal(err.Field, "f2")
err.AddFieldParent("f3")
a.Equal(err.Field, "f3.f2")
}