Skip to content

Commit

Permalink
fix: refactor test-case
Browse files Browse the repository at this point in the history
  • Loading branch information
saddit committed Nov 9, 2022
1 parent 0258d9d commit cae814f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
23 changes: 23 additions & 0 deletions cleanenv_test.go
Expand Up @@ -1187,3 +1187,26 @@ func TestTimeLocation(t *testing.T) {
t.Errorf("wrong location pointers: got %p, want %p", S.Location, want)
}
}

func TestSkipUnexportedField(t *testing.T) {
conf := struct {
Database struct {
Host string `yaml:"host" env:"DB_HOST" env-description:"Database host"`
Port string `yaml:"port" env:"DB_PORT" env-description:"Database port"`
} `yaml:"database"`
server struct {
Host string `yaml:"host" env:"SRV_HOST,HOST" env-description:"Server host" env-default:"localhost"`
Port string `yaml:"port" env:"SRV_PORT,PORT" env-description:"Server port" env-default:"8080"`
} `yaml:"server"`
}{}

if err := ReadConfig("example/simple_config/config.yml", &conf); err != nil {
t.Fatal(err)
}
if conf.server.Host != "" || conf.server.Port != "" {
t.Fatal("unexpect value on unexported fields")
}
if conf.Database.Host == "" || conf.Database.Port == "" {
t.Fatal("expect value on exported fields")
}
}
19 changes: 0 additions & 19 deletions example_test.go
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/url"
"os"
"testing"

"github.com/ilyakaznacheev/cleanenv"
)
Expand Down Expand Up @@ -272,21 +271,3 @@ func ExampleUsage() {
// THREE string
// third parameter
}

func ExampleUnexportedField(t *testing.T) {
conf := struct {
Database struct {
Host string `yaml:"host" env:"DB_HOST" env-description:"Database host"`
Port string `yaml:"port" env:"DB_PORT" env-description:"Database port"`
} `yaml:"database"`
server struct {
Host string `yaml:"host" env:"SRV_HOST,HOST" env-description:"Server host" env-default:"localhost"`
Port string `yaml:"port" env:"SRV_PORT,PORT" env-description:"Server port" env-default:"8080"`
} `yaml:"server"`
}{}

if err := cleanenv.ReadConfig("example/simple_config/config.yml", &conf); err != nil {
t.Fatal(err)
}
t.Logf("%+v", conf)
}

0 comments on commit cae814f

Please sign in to comment.