Skip to content

Commit

Permalink
refactor: provider timezone and env (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 authored Sep 8, 2023
1 parent 1fbf43b commit 8f156e9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
11 changes: 11 additions & 0 deletions env/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package env

import "context"

func Provider(env Env) func(ctx context.Context) error {
return func(ctx context.Context) error {
SetEnv(env)

return nil
}
}
18 changes: 18 additions & 0 deletions env/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package env

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestProvider(t *testing.T) {
assert.True(t, Is(Prod))
assert.False(t, Is(Dev))

assert.NoError(t, Provider(Dev)(context.Background()))

assert.False(t, Is(Prod))
assert.True(t, Is(Dev))
}
2 changes: 1 addition & 1 deletion timezone/timezone.go β†’ timezone/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Local(name string) Option {
}
}

func Timezone(opts ...Option) func(ctx context.Context) error {
func Provider(opts ...Option) func(ctx context.Context) error {
op := options{
local: "UTC",
}
Expand Down
6 changes: 3 additions & 3 deletions timezone/timezone_test.go β†’ timezone/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
func TestTimezone(t *testing.T) {
// before
assert.Equal(t, "Local", time.Local.String())
assert.NoError(t, Timezone()(context.Background()))
assert.NoError(t, Provider()(context.Background()))
assert.Equal(t, "UTC", time.Local.String())

// after
assert.NoError(t, Timezone(Local("Asia/Shanghai"))(context.Background()))
assert.NoError(t, Provider(Local("Asia/Shanghai"))(context.Background()))
assert.Equal(t, "Asia/Shanghai", time.Local.String())

// err
assert.Error(t, Timezone(Local("Asia/Beijing"))(context.Background()))
assert.Error(t, Provider(Local("Asia/Beijing"))(context.Background()))
}

0 comments on commit 8f156e9

Please sign in to comment.