Skip to content

Commit

Permalink
Merge pull request #480 from jxs1211/test-pkg-utils-string
Browse files Browse the repository at this point in the history
test: pkg-utils-string
  • Loading branch information
qmhu authored Aug 16, 2022
2 parents e3af62b + 262a935 commit 9c12d3f
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions pkg/utils/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package utils

import "testing"

func TestParseFloat(t *testing.T) {
tests := []struct {
name string
str string
defaultValue float64
want float64
wantErr bool
}{
// TODO: Add test cases.
{
name: "empty string",
str: "",
defaultValue: 0.16,
want: 0.16,
wantErr: false,
},
{
name: "non empty string",
str: "0.64",
defaultValue: 0.16,
want: 0.64,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseFloat(tt.str, tt.defaultValue)
if (err != nil) != tt.wantErr {
t.Errorf("ParseFloat() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseFloat() = %v, want %v", got, tt.want)
}
})
}
}

func TestParsePercentage(t *testing.T) {
tests := []struct {
name string
input string
want float64
wantErr bool
}{
{
name: "empty string",
input: "",
want: 0.00,
wantErr: false,
},
{
name: "string parse error",
input: "1a",
want: 0.00,
wantErr: true,
},
{
name: "string parse ok",
input: "10%",
want: 0.10,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParsePercentage(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParsePercentage() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParsePercentage() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 9c12d3f

Please sign in to comment.