-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #480 from jxs1211/test-pkg-utils-string
test: pkg-utils-string
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |