golang.org/x/text@v0.14.0/number/format_test.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package number 6 7 import ( 8 "fmt" 9 "testing" 10 11 "golang.org/x/text/feature/plural" 12 "golang.org/x/text/language" 13 "golang.org/x/text/message" 14 ) 15 16 func TestWrongVerb(t *testing.T) { 17 testCases := []struct { 18 f Formatter 19 fmt string 20 want string 21 }{{ 22 f: Decimal(12), 23 fmt: "%e", 24 want: "%!e(int=12)", 25 }, { 26 f: Scientific(12), 27 fmt: "%f", 28 want: "%!f(int=12)", 29 }, { 30 f: Engineering(12), 31 fmt: "%f", 32 want: "%!f(int=12)", 33 }, { 34 f: Percent(12), 35 fmt: "%e", 36 want: "%!e(int=12)", 37 }} 38 for _, tc := range testCases { 39 t.Run("", func(t *testing.T) { 40 tag := language.Und 41 got := message.NewPrinter(tag).Sprintf(tc.fmt, tc.f) 42 if got != tc.want { 43 t.Errorf("got %q; want %q", got, tc.want) 44 } 45 }) 46 } 47 } 48 49 func TestDigits(t *testing.T) { 50 testCases := []struct { 51 f Formatter 52 scale int 53 want string 54 }{{ 55 f: Decimal(3), 56 scale: 0, 57 want: "digits:[3] exp:1 comma:0 end:1", 58 }, { 59 f: Decimal(3.1), 60 scale: 0, 61 want: "digits:[3] exp:1 comma:0 end:1", 62 }, { 63 f: Scientific(3.1), 64 scale: 0, 65 want: "digits:[3] exp:1 comma:1 end:1", 66 }, { 67 f: Scientific(3.1), 68 scale: 3, 69 want: "digits:[3 1] exp:1 comma:1 end:4", 70 }} 71 for _, tc := range testCases { 72 t.Run("", func(t *testing.T) { 73 d := tc.f.Digits(nil, language.Croatian, tc.scale) 74 got := fmt.Sprintf("digits:%d exp:%d comma:%d end:%d", d.Digits, d.Exp, d.Comma, d.End) 75 if got != tc.want { 76 t.Errorf("got %v; want %v", got, tc.want) 77 } 78 }) 79 } 80 } 81 82 func TestPluralIntegration(t *testing.T) { 83 testCases := []struct { 84 f Formatter 85 want string 86 }{{ 87 f: Decimal(1), 88 want: "one: 1", 89 }, { 90 f: Decimal(5), 91 want: "other: 5", 92 }} 93 for _, tc := range testCases { 94 t.Run("", func(t *testing.T) { 95 message.Set(language.English, "num %f", plural.Selectf(1, "%f", 96 "one", "one: %f", 97 "other", "other: %f")) 98 99 p := message.NewPrinter(language.English) 100 101 // Indirect the call to p.Sprintf through the variable f 102 // to avoid Go tip failing a vet check. 103 // TODO: remove once vet check has been fixed. See Issue #22936. 104 f := p.Sprintf 105 got := f("num %f", tc.f) 106 107 if got != tc.want { 108 t.Errorf("got %q; want %q", got, tc.want) 109 } 110 }) 111 } 112 }