Skip to content

Commit

Permalink
Add examples to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nasermirzaei89 committed Apr 18, 2024
1 parent bd3359d commit 7ba47ad
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
16 changes: 16 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package env_test

import (
"fmt"
"os"
"testing"

"github.com/nasermirzaei89/env"
Expand All @@ -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())

Expand Down Expand Up @@ -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())

Expand Down
72 changes: 72 additions & 0 deletions generic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package env_test

import (
"fmt"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
}

0 comments on commit 7ba47ad

Please sign in to comment.