From 7ba47ad790478c9d20ceff82d23d081d3ae50c98 Mon Sep 17 00:00:00 2001 From: Naser Mirzaei Date: Thu, 18 Apr 2024 12:49:17 -0400 Subject: [PATCH] Add examples to docs --- env_test.go | 16 +++++++++++ generic_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/env_test.go b/env_test.go index e3216c3..71b9b34 100644 --- a/env_test.go +++ b/env_test.go @@ -1,6 +1,8 @@ package env_test import ( + "fmt" + "os" "testing" "github.com/nasermirzaei89/env" @@ -16,6 +18,13 @@ func TestEnv_String(t *testing.T) { assert.EqualValues(t, v, res) } +func ExampleEnvironment() { + _ = os.Setenv("ENV", "testing") + + fmt.Println(env.Environment()) + // Output: testing +} + func TestEnvironment(t *testing.T) { assert.Zero(t, env.Environment()) @@ -44,6 +53,13 @@ func TestIs(t *testing.T) { }) } +func ExampleIs() { + _ = os.Setenv("ENV", "testing") + + fmt.Println(env.Is(env.Testing)) + // Output: true +} + func TestIsDevelopment(t *testing.T) { assert.False(t, env.IsDevelopment()) diff --git a/generic_test.go b/generic_test.go index e0e4913..c3f3314 100644 --- a/generic_test.go +++ b/generic_test.go @@ -1,6 +1,8 @@ package env_test import ( + "fmt" + "os" "strings" "testing" @@ -821,6 +823,41 @@ func TestGet(t *testing.T) { } } +func ExampleGet_string() { + _ = os.Setenv("V1", "val") + + fmt.Println(env.Get("V1", "default")) + // Output: val +} + +func ExampleGet_stringSlice() { + _ = os.Setenv("V1", "foo,bar,baz") + + fmt.Println(env.Get("V1", []string{"default"})) + // Output: [foo bar baz] +} + +func ExampleGet_int() { + _ = os.Setenv("V1", "14") + + fmt.Println(env.Get("V1", 12)) + // Output: 14 +} + +func ExampleGet_intSlice() { + _ = os.Setenv("V1", "31,32,33") + + fmt.Println(env.Get("V1", []int{21})) + // Output: [31 32 33] +} + +func ExampleGet_bool() { + _ = os.Setenv("V1", "true") + + fmt.Println(env.Get("V1", false)) + // Output: true +} + func TestMustGet(t *testing.T) { // bool { @@ -1599,3 +1636,38 @@ func TestMustGet(t *testing.T) { }) } } + +func ExampleMustGet_string() { + _ = os.Setenv("V1", "val") + + fmt.Println(env.MustGet[string]("V1")) + // Output: val +} + +func ExampleMustGet_stringSlice() { + _ = os.Setenv("V1", "foo,bar,baz") + + fmt.Println(env.MustGet[[]string]("V1")) + // Output: [foo bar baz] +} + +func ExampleMustGet_int() { + _ = os.Setenv("V1", "14") + + fmt.Println(env.MustGet[int]("V1")) + // Output: 14 +} + +func ExampleMustGet_intSlice() { + _ = os.Setenv("V1", "31,32,33") + + fmt.Println(env.MustGet[[]int]("V1")) + // Output: [31 32 33] +} + +func ExampleMustGet_bool() { + _ = os.Setenv("V1", "true") + + fmt.Println(env.MustGet[bool]("V1")) + // Output: true +}