Skip to content

Commit

Permalink
Add test case for FloatFromEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpt1114 committed Aug 21, 2023
1 parent 43e87b6 commit 4256a87
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/util/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,24 @@ func TestFloatFromEnv(t *testing.T) {
// mock
_ = os.Setenv("test_float_from_env", "0.75")
_ = os.Setenv("test_float_from_env_empty", "")
_ = os.Setenv("test_float_from_env_trim", "0.75 ")
_ = os.Setenv("test_float_from_env_string", "zeropointfive")
_ = os.Setenv("test_float_from_env_trim", " 0.75 ")
_ = os.Setenv("test_float_from_env_0", "+0.75")
_ = os.Setenv("test_float_from_env_1", "-0.75")
_ = os.Setenv("test_float_from_env_2", ".75")
_ = os.Setenv("test_float_from_env_3", "01.75")

// Test default value
assert.Equal(t, 0.75, FloatFromEnv("not_exists", 0.75))
assert.Equal(t, 0.75, FloatFromEnv("test_float_from_env_empty", 0.75))
assert.Equal(t, 0.75, FloatFromEnv("test_float_from_env_string", 0.75))

// Test not default value
assert.Equal(t, 0.75, FloatFromEnv("test_float_from_env", 0.5))
assert.Equal(t, 0.75, FloatFromEnv("test_float_from_env_0", 0.5))
assert.Equal(t, -0.75, FloatFromEnv("test_float_from_env_1", 0.5))
assert.Equal(t, 0.75, FloatFromEnv("test_float_from_env_2", 0.5))
assert.Equal(t, 1.75, FloatFromEnv("test_float_from_env_3", 0.5))

// Test trim space
assert.Equal(t, 0.75, FloatFromEnv("test_float_from_env_trim", 0.5))
Expand Down

0 comments on commit 4256a87

Please sign in to comment.