From 5776e72e5d6215a84c2fb30c05dd01d13b0788c6 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 10 Sep 2024 15:11:57 -0300 Subject: [PATCH 1/8] Cover context.int64 --- message/context.go | 20 ++++-- message/context_test.go | 154 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 167 insertions(+), 7 deletions(-) diff --git a/message/context.go b/message/context.go index 1c9d083..f38ccde 100644 --- a/message/context.go +++ b/message/context.go @@ -31,19 +31,25 @@ func (c Context) Int64(key string) (int64, error) { switch i := v.(type) { case int: return int64(i), nil + case int8: + return int64(i), nil + case int16: + return int64(i), nil + case int32: + return int64(i), nil + case int64: + return int64(i), nil case uint: if i > math.MaxInt64 { return 0, fmt.Errorf("unable to convert uint %v to int64 from arg %s", v, key) } return int64(i), nil //nolint:gosec - case float64: - return int64(i), nil - case float32: + case uint8: return int64(i), nil - case int64: + case uint16: return int64(i), nil - case int32: + case uint32: return int64(i), nil case uint64: if i > math.MaxInt64 { @@ -51,7 +57,9 @@ func (c Context) Int64(key string) (int64, error) { } return int64(i), nil //nolint:gosec - case uint32: + case float32: + return int64(i), nil + case float64: return int64(i), nil case string: fl, err := strconv.ParseFloat(i, 64) diff --git a/message/context_test.go b/message/context_test.go index e3322b5..04a5a99 100644 --- a/message/context_test.go +++ b/message/context_test.go @@ -1,6 +1,9 @@ package message -import "testing" +import ( + "math" + "testing" +) func Test_context_AsString(t *testing.T) { tests := []struct { @@ -69,3 +72,152 @@ func Test_context_AsString(t *testing.T) { }) } } + +func TestContext_Int64(t *testing.T) { + tests := []struct { + name string + c Context + key string + want int64 + wantErr bool + }{ + { + "int", + Context{"foo": int(42)}, + "foo", + 42, + false, + }, + { + "int8", + Context{"foo": int8(42)}, + "foo", + 42, + false, + }, + { + "int16", + Context{"foo": int16(42)}, + "foo", + 42, + false, + }, + { + "int32", + Context{"foo": int32(42)}, + "foo", + 42, + false, + }, + { + "int64", + Context{"foo": int64(42)}, + "foo", + 42, + false, + }, + { + "uint", + Context{"foo": uint(42)}, + "foo", + 42, + false, + }, + { + "uint8", + Context{"foo": uint8(42)}, + "foo", + 42, + false, + }, + { + "uint16", + Context{"foo": uint16(42)}, + "foo", + 42, + false, + }, + { + "uint32", + Context{"foo": uint32(42)}, + "foo", + 42, + false, + }, + { + "max uint32", + Context{"foo": uint32(math.MaxUint32)}, + "foo", + math.MaxUint32, + false, + }, + { + "uint64", + Context{"foo": uint64(42)}, + "foo", + 42, + false, + }, + { + "max uint64", + Context{"foo": uint64(math.MaxUint64)}, + "foo", + 0, + true, + }, + { + "float32", + Context{"foo": float32(42.42)}, + "foo", + 42, + false, + }, + { + "float64", + Context{"foo": float64(42.42)}, + "foo", + 42, + false, + }, + { + "string float", + Context{"foo": "42.42"}, + "foo", + 42, + false, + }, + { + "string int", + Context{"foo": "42"}, + "foo", + 42, + false, + }, + { + "invalid string", + Context{"foo": "bar"}, + "foo", + 0, + true, + }, + { + "unknown type", + Context{"foo": []byte("bar")}, + "foo", + 0, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.c.Int64(tt.key) + if (err != nil) != tt.wantErr { + t.Errorf("Context.Int64() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Context.Int64() = %v, want %v", got, tt.want) + } + }) + } +} From d3c27182f0a66c2312e2abc047aee4383fd2ece1 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 10 Sep 2024 15:21:43 -0300 Subject: [PATCH 2/8] Cover context float64 --- message/context.go | 18 ++++-- message/context_test.go | 135 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 5 deletions(-) diff --git a/message/context.go b/message/context.go index f38ccde..449ed53 100644 --- a/message/context.go +++ b/message/context.go @@ -82,20 +82,28 @@ func (c Context) Float64(key string) (float64, error) { switch i := v.(type) { case int: return float64(i), nil - case uint: + case int8: return float64(i), nil - case float64: + case int16: return float64(i), nil - case float32: + case int32: return float64(i), nil case int64: return float64(i), nil - case int32: + case uint: return float64(i), nil - case uint64: + case uint8: + return float64(i), nil + case uint16: return float64(i), nil case uint32: return float64(i), nil + case uint64: + return float64(i), nil + case float64: + return float64(i), nil + case float32: + return float64(i), nil case string: fl, err := strconv.ParseFloat(i, 64) if err != nil { diff --git a/message/context_test.go b/message/context_test.go index 04a5a99..d9b933a 100644 --- a/message/context_test.go +++ b/message/context_test.go @@ -221,3 +221,138 @@ func TestContext_Int64(t *testing.T) { }) } } + +func TestContext_Float64(t *testing.T) { + tests := []struct { + name string + c Context + key string + want float64 + wantErr bool + }{ + { + "int", + Context{"foo": int(42)}, + "foo", + 42, + false, + }, + { + "int8", + Context{"foo": int8(42)}, + "foo", + 42, + false, + }, + { + "int16", + Context{"foo": int16(42)}, + "foo", + 42, + false, + }, + { + "int32", + Context{"foo": int32(42)}, + "foo", + 42, + false, + }, + { + "int64", + Context{"foo": int64(42)}, + "foo", + 42, + false, + }, + { + "uint", + Context{"foo": uint(42)}, + "foo", + 42, + false, + }, + { + "uint8", + Context{"foo": uint8(42)}, + "foo", + 42, + false, + }, + { + "uint16", + Context{"foo": uint16(42)}, + "foo", + 42, + false, + }, + { + "uint32", + Context{"foo": uint32(42)}, + "foo", + 42, + false, + }, + { + "uint64", + Context{"foo": uint64(42)}, + "foo", + 42, + false, + }, + { + "float32", + Context{"foo": float32(42.42)}, + "foo", + float64(float32(42.42)), + false, + }, + { + "float64", + Context{"foo": float64(42.42)}, + "foo", + 42.42, + false, + }, + { + "string float", + Context{"foo": "42.42"}, + "foo", + 42.42, + false, + }, + { + "string int", + Context{"foo": "42"}, + "foo", + 42, + false, + }, + { + "invalid string", + Context{"foo": "bar"}, + "foo", + 0, + true, + }, + { + "unknown type", + Context{"foo": []byte("bar")}, + "foo", + 0, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.c.Float64(tt.key) + if (err != nil) != tt.wantErr { + t.Errorf("Context.Float64() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Context.Float64() = %v, want %v", got, tt.want) + } + }) + } +} From 1e0b2d7fe7fb18ed3fc1a6db94e3831d28f69dc9 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 10 Sep 2024 15:39:20 -0300 Subject: [PATCH 3/8] Cover number --- message/number_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/message/number_test.go b/message/number_test.go index ae1fc8f..5db7bea 100644 --- a/message/number_test.go +++ b/message/number_test.go @@ -71,6 +71,55 @@ func TestNumber_Eval(t *testing.T) { "123,456,789", false, }, + + { + "to decimal if no format", + "n", + NoneNumberFormat, + Context{"n": 3.14}, + "3.14", + false, + }, + { + "to decimal if invalid format", + "n", + NumberFormat(42), + Context{"n": 3.14}, + "3.14", + false, + }, + { + "error on invalid arg", + "n", + NoneNumberFormat, + Context{"n": "foo"}, + "", + true, + }, + { + "error on invalid arg", + "n", + IntegerNumberFormat, + Context{"n": "foo"}, + "", + true, + }, + { + "error on invalid arg", + "n", + PercentNumberFormat, + Context{"n": "foo"}, + "", + true, + }, + { + "error on invalid arg", + "n", + NumberFormat(42), + Context{"n": "foo"}, + "", + true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 9522cb86166a706a0a3975686aade0f1d85a8db4 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 10 Sep 2024 15:39:25 -0300 Subject: [PATCH 4/8] Cover plural --- message/plural_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/message/plural_test.go b/message/plural_test.go index 129f168..13300fd 100644 --- a/message/plural_test.go +++ b/message/plural_test.go @@ -118,6 +118,39 @@ func TestPlural_Eval(t *testing.T) { "other", false, }, + { + "other case if no other case", + fields{ + "count", + language.English, + 0, + nil, + map[plural.Form]Evalable{ + plural.Few: Content("few"), + plural.Other: Content("other"), + }, + }, + Context{"count": 1}, + "other", + false, + }, + + { + "offset", + fields{ + "count", + language.English, + 2, + nil, + map[plural.Form]Evalable{ + plural.One: Content("one"), + plural.Other: PlainArg("#"), + }, + }, + Context{"count": 4}, + "2", + false, + }, } for _, tt := range tests { From 8ae270201e05e5ad9add24a45894f1861b7dfe2f Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 10 Sep 2024 15:39:30 -0300 Subject: [PATCH 5/8] Update context_test.go --- message/context_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/message/context_test.go b/message/context_test.go index d9b933a..287c6b7 100644 --- a/message/context_test.go +++ b/message/context_test.go @@ -207,6 +207,13 @@ func TestContext_Int64(t *testing.T) { 0, true, }, + { + "unknown name", + Context{"foo": 42}, + "bar", + 0, + true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -342,6 +349,13 @@ func TestContext_Float64(t *testing.T) { 0, true, }, + { + "unknown name", + Context{"foo": 42}, + "bar", + 0, + true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 24f64e1af2746f1fda1e8ce936754611a6e43627 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 10 Sep 2024 15:45:56 -0300 Subject: [PATCH 6/8] Update codecov layout --- .github/workflows/test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 433f934..725a9ab 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,3 +23,6 @@ jobs: uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} + comment: + layout: "condensed_header, condensed_files, condensed_footer" + hide_project_coverage: TRUE From 60c2b4133ea2462aa723133fdb1eb919338460f1 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 10 Sep 2024 15:54:31 -0300 Subject: [PATCH 7/8] Add codecov.yml --- .github/workflows/test.yml | 3 --- codecov.yml | 5 +++++ 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 codecov.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 725a9ab..433f934 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,6 +23,3 @@ jobs: uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} - comment: - layout: "condensed_header, condensed_files, condensed_footer" - hide_project_coverage: TRUE diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..36eed4b --- /dev/null +++ b/codecov.yml @@ -0,0 +1,5 @@ +comment: false +coverage: + range: 50..75 + round: down + precision: 2 From 29920c8ed7d6a33878f523bb7d7ed88dde5cb58f Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 10 Sep 2024 16:07:46 -0300 Subject: [PATCH 8/8] Cover toPluralForm --- message/plural_test.go | 131 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/message/plural_test.go b/message/plural_test.go index 13300fd..d1fcbd3 100644 --- a/message/plural_test.go +++ b/message/plural_test.go @@ -1,6 +1,7 @@ package message import ( + "reflect" "testing" "golang.org/x/text/feature/plural" @@ -171,3 +172,133 @@ func TestPlural_Eval(t *testing.T) { }) } } + +func Test_toPluralForm(t *testing.T) { + tests := []struct { + name string + num any + want pm + wantErr bool + }{ + { + "int", + int(42), + pm{i: 42}, + false, + }, + { + "-int", + int(-42), + pm{i: 42}, + false, + }, + { + "int8", + int8(42), + pm{i: 42}, + false, + }, + { + "-int8", + int8(-42), + pm{i: 42}, + false, + }, + { + "int16", + int16(42), + pm{i: 42}, + false, + }, + { + "-int16", + int16(-42), + pm{i: 42}, + false, + }, + { + "int32", + int32(42), + pm{i: 42}, + false, + }, + { + "-int32", + int32(-42), + pm{i: 42}, + false, + }, + { + "int64", + int64(42), + pm{i: 42}, + false, + }, + { + "-int64", + int64(-42), + pm{i: 42}, + false, + }, + { + "uint", + uint(42), + pm{i: 42}, + false, + }, + { + "uint8", + uint8(42), + pm{i: 42}, + false, + }, + { + "uint16", + uint16(42), + pm{i: 42}, + false, + }, + { + "uint32", + uint32(42), + pm{i: 42}, + false, + }, + { + "uint64", + uint64(42), + pm{i: 42}, + false, + }, + { + "error on unknown type", + []byte("foo"), + pm{}, + true, + }, + { + "float string", + "42.4200", + pm{i: 42, v: 4, w: 2, f: 4200, t: 42}, + false, + }, + { + "float string", + "1200.50", + pm{i: 1200, v: 2, w: 1, f: 50, t: 5}, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := toPluralForm(tt.num) + if (err != nil) != tt.wantErr { + t.Errorf("toPluralForm() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("toPluralForm() = %v, want %v", got, tt.want) + } + }) + } +}