language_test.go (2664B)
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/config"
22 "golang.org/x/text/collate"
23 "golang.org/x/text/language"
24 )
25
26 func TestGetGlobalOnlySetting(t *testing.T) {
27 c := qt.New(t)
28 v := config.NewWithTestDefaults()
29 v.Set("defaultContentLanguageInSubdir", true)
30 v.Set("contentDir", "content")
31 v.Set("paginatePath", "page")
32 lang := NewDefaultLanguage(v)
33 lang.Set("defaultContentLanguageInSubdir", false)
34 lang.Set("paginatePath", "side")
35
36 c.Assert(lang.GetBool("defaultContentLanguageInSubdir"), qt.Equals, true)
37 c.Assert(lang.GetString("paginatePath"), qt.Equals, "side")
38 }
39
40 func TestLanguageParams(t *testing.T) {
41 c := qt.New(t)
42
43 v := config.NewWithTestDefaults()
44 v.Set("p1", "p1cfg")
45 v.Set("contentDir", "content")
46
47 lang := NewDefaultLanguage(v)
48 lang.SetParam("p1", "p1p")
49
50 c.Assert(lang.Params()["p1"], qt.Equals, "p1p")
51 c.Assert(lang.Get("p1"), qt.Equals, "p1cfg")
52 }
53
54 func TestCollator(t *testing.T) {
55
56 c := qt.New(t)
57
58 var wg sync.WaitGroup
59
60 coll := &Collator{c: collate.New(language.English, collate.Loose)}
61
62 for i := 0; i < 10; i++ {
63 wg.Add(1)
64 go func() {
65 coll.Lock()
66 defer coll.Unlock()
67 defer wg.Done()
68 for j := 0; j < 10; j++ {
69 k := coll.CompareStrings("abc", "def")
70 c.Assert(k, qt.Equals, -1)
71 }
72 }()
73 }
74 wg.Wait()
75
76 }
77
78 func BenchmarkCollator(b *testing.B) {
79 s := []string{"foo", "bar", "éntre", "baz", "qux", "quux", "corge", "grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud"}
80
81 doWork := func(coll *Collator) {
82 for i := 0; i < len(s); i++ {
83 for j := i + 1; j < len(s); j++ {
84 _ = coll.CompareStrings(s[i], s[j])
85 }
86 }
87 }
88
89 b.Run("Single", func(b *testing.B) {
90 coll := &Collator{c: collate.New(language.English, collate.Loose)}
91 for i := 0; i < b.N; i++ {
92 doWork(coll)
93 }
94 })
95
96 b.Run("Para", func(b *testing.B) {
97 b.RunParallel(func(pb *testing.PB) {
98 coll := &Collator{c: collate.New(language.English, collate.Loose)}
99
100 for pb.Next() {
101 coll.Lock()
102 doWork(coll)
103 coll.Unlock()
104 }
105 })
106 })
107
108 }