Debian Codegraph @

langs/language_test.go

raw
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package langs
15
16 import (
17 "sync"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/common/loggers"
22 "golang.org/x/text/collate"
23 "golang.org/x/text/language"
24 )
25
26 func TestCollator(t *testing.T) {
27 c := qt.New(t)
28
29 var wg sync.WaitGroup
30
31 coll := &Collator{c: collate.New(language.English, collate.Loose)}
32
33 for range 10 {
34 wg.Add(1)
35 go func() {
36 coll.Lock()
37 defer coll.Unlock()
38 defer wg.Done()
39 for range 10 {
40 k := coll.CompareStrings("abc", "def")
41 c.Assert(k, qt.Equals, -1)
42 }
43 }()
44 }
45 wg.Wait()
46 }
47
48 func BenchmarkCollator(b *testing.B) {
49 s := []string{"foo", "bar", "éntre", "baz", "qux", "quux", "corge", "grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud"}
50
51 doWork := func(coll *Collator) {
52 for i := range s {
53 for j := i + 1; j < len(s); j++ {
54 _ = coll.CompareStrings(s[i], s[j])
55 }
56 }
57 }
58
59 b.Run("Single", func(b *testing.B) {
60 coll := &Collator{c: collate.New(language.English, collate.Loose)}
61 for b.Loop() {
62 doWork(coll)
63 }
64 })
65
66 b.Run("Para", func(b *testing.B) {
67 b.RunParallel(func(pb *testing.PB) {
68 coll := &Collator{c: collate.New(language.English, collate.Loose)}
69
70 for pb.Next() {
71 coll.Lock()
72 doWork(coll)
73 coll.Unlock()
74 }
75 })
76 })
77 }
78
79 // TestLanguageLegacyFieldFallbacks verifies that Locale(), Direction(), and
80 // Label() fall back to legacy LanguageConfig fields when the canonical fields
81 // are not set. This matters for programmatic construction that bypasses the
82 // allconfig migration (which normally copies legacy→canonical and clears them).
83 func TestLanguageLegacyFieldFallbacks(t *testing.T) {
84 c := qt.New(t)
85
86 l, err := NewLanguage("en", "en", "UTC", LanguageConfig{
87 LanguageCode: "en-US",
88 LanguageName: "English",
89 LanguageDirection: "ltr",
90 }, loggers.NewDefault())
91 c.Assert(err, qt.IsNil)
92 c.Assert(l.Locale(), qt.Equals, "en-US")
93 c.Assert(l.Label(), qt.Equals, "English")
94 c.Assert(l.Direction(), qt.Equals, "ltr")
95
96 // Deprecated methods must not panic when logger is nil (no logger provided
97 // at construction). They delegate through Logger() which has a nil guard.
98 lNoLogger, err := NewLanguage("en", "en", "UTC", LanguageConfig{}, nil)
99 c.Assert(err, qt.IsNil)
100 _ = lNoLogger.LanguageCode()
101 _ = lNoLogger.LanguageName()
102 _ = lNoLogger.LanguageDirection()
103 }