cfg/cfg_test.go

215 lines
4.3 KiB
Go
Raw Permalink Normal View History

2019-03-08 12:32:55 +01:00
package cfg_test
2018-03-25 17:18:30 +02:00
import (
"testing"
"time"
2019-03-08 12:32:55 +01:00
"git.hoogi.eu/cfg"
2018-03-25 17:18:30 +02:00
)
func TestStandardConfig(t *testing.T) {
2018-03-29 14:11:41 +02:00
type settings struct {
2018-03-25 17:18:30 +02:00
SessionName string `cfg:"session_name"`
FileLocation string `cfg:"file_location" default:"/dev/null"`
Address string
Port int `cfg:"port" default:"2000"`
Size int `default:"30"`
SSL bool `cfg:"ssl"`
Verbose bool `cfg:"verbose" default:"yes"`
SessionTimeout time.Duration `cfg:"session_timeout"`
}
2018-03-29 14:11:41 +02:00
c := addConfig("./testcfg", "config.conf")
2018-03-25 17:18:30 +02:00
2018-03-29 14:11:41 +02:00
s := new(settings)
2018-03-25 17:18:30 +02:00
2018-05-01 21:33:13 +02:00
def, err := c.MergeConfigsInto(s)
2018-03-25 17:18:30 +02:00
if err != nil {
t.Error(err)
}
2018-03-29 14:11:41 +02:00
if s.SessionName != "the-session-name" {
t.Errorf("invalid value %s", s.SessionName)
2018-03-25 17:18:30 +02:00
}
2018-03-29 14:11:41 +02:00
if s.FileLocation != "/dev/null" {
t.Errorf("FileLocation expected to be /dev/null but was %s", s.FileLocation)
2018-03-25 17:18:30 +02:00
}
2018-03-29 14:11:41 +02:00
if s.Address != "127.0.0.1" {
t.Errorf("Address expected to be 127.0.0.1 but was %s", s.Address)
2018-03-25 17:18:30 +02:00
}
2018-03-29 14:11:41 +02:00
if s.Port != 8080 {
t.Errorf("Port expected to be 8080 but was %d", s.Port)
2018-03-25 17:18:30 +02:00
}
2018-03-29 14:11:41 +02:00
if s.Size != 30 {
t.Errorf("Size expected to be 30 but was %d", s.Size)
2018-03-25 17:18:30 +02:00
}
2018-03-29 14:11:41 +02:00
if !s.SSL {
t.Errorf("SSL expected to be true but was %t", s.SSL)
2018-03-25 17:18:30 +02:00
}
2018-03-29 14:11:41 +02:00
if !s.Verbose {
t.Errorf("Verbose expected to be true but was %t", s.Verbose)
2018-03-25 17:18:30 +02:00
}
expDuration, _ := time.ParseDuration("10m")
2018-03-29 14:11:41 +02:00
if s.SessionTimeout != expDuration {
t.Errorf("SessionTimeout expected to be but was %v", s.SessionTimeout)
2018-03-25 18:12:04 +02:00
}
2018-05-01 21:33:13 +02:00
if len(def) != 3 {
t.Error("expected three entries in applied default values")
}
2019-03-07 17:23:03 +01:00
2018-03-25 18:12:04 +02:00
}
func TestInnerStruct(t *testing.T) {
2018-03-29 14:11:41 +02:00
type settings struct {
2018-03-25 18:12:04 +02:00
Server struct {
Address string `cfg:"server_address"`
Port int `cfg:"server_port"`
}
}
2018-03-29 14:11:41 +02:00
c := addConfig("./testcfg", "config.conf")
2018-03-25 18:12:04 +02:00
2018-03-29 14:11:41 +02:00
s := new(settings)
2018-05-01 21:33:13 +02:00
_, err := c.MergeConfigsInto(s)
2018-03-25 18:12:04 +02:00
if err != nil {
t.Error(err)
}
2018-03-29 14:11:41 +02:00
if s.Server.Address != "localhost" {
t.Errorf("server.Address expected to be localhost but was %s", s.Server.Address)
2018-03-25 18:12:04 +02:00
}
2018-03-29 14:11:41 +02:00
if s.Server.Port != 42 {
t.Errorf("server.Port expected to be 42 but was %d", s.Server.Port)
2018-03-25 17:18:30 +02:00
}
}
2018-03-27 22:11:26 +02:00
2019-03-08 12:32:55 +01:00
func TestHumanizeFilesize(t *testing.T) {
var fileSizes = []struct {
fs cfg.FileSize
expected string
}{
{cfg.FileSize(0), "0"},
{cfg.FileSize(6680), "6.6 KB"},
{cfg.FileSize(4314), "4.3 KB"},
{cfg.FileSize(237797290), "226.8 MB"},
{cfg.FileSize(4096), "4.0 KB"},
}
for _, v := range fileSizes {
actual := v.fs.HumanReadable()
if actual != v.expected {
t.Errorf("HumanReadable(%d): expected %s, actual %s", uint64(v.fs), v.expected, actual)
}
}
}
2018-03-29 14:11:41 +02:00
type loginMethod int
const (
mail = iota
username
)
func (lm *loginMethod) Unmarshal(value string) error {
m := loginMethod(username)
if value == "mail" {
m = loginMethod(mail)
}
*lm = m
2018-03-29 14:11:41 +02:00
return nil
}
func (lm loginMethod) String() string {
if lm == mail {
return "mail"
} else {
return "username"
}
}
func TestCustomType(t *testing.T) {
type settings struct {
Custom loginMethod `cfg:"login_method"`
}
c := addConfig("./testcfg", "config.conf")
s := new(settings)
2018-05-01 21:33:13 +02:00
_, err := c.MergeConfigsInto(s)
2018-03-29 14:11:41 +02:00
if err != nil {
t.Error(err)
}
if s.Custom != username {
t.Errorf("s.Custom expected to be username but was %s", s.Custom)
2018-03-29 14:11:41 +02:00
}
}
func TestFileSizes(t *testing.T) {
type settings struct {
2019-03-08 12:32:55 +01:00
Byte cfg.FileSize `cfg:"file_size_byte"`
Kilobyte cfg.FileSize `cfg:"file_size_kilobyte"`
Megabyte cfg.FileSize `cfg:"file_size_megabyte"`
Gigabyte cfg.FileSize `cfg:"file_size_gigabyte"`
Terabyte cfg.FileSize `cfg:"file_size_terabyte"`
Empty cfg.FileSize `cfg:"file_size_empty"`
}
c := addConfig("./testcfg", "config.conf")
s := new(settings)
2018-05-01 21:33:13 +02:00
_, err := c.MergeConfigsInto(s)
if err != nil {
t.Error(err)
}
2019-03-07 17:24:08 +01:00
if s.Byte != 5 {
2019-03-07 18:37:07 +01:00
t.Errorf("s.Byte expected to be %d bytes but was %d", 5, s.Byte)
}
if s.Kilobyte != 1<<10 {
t.Errorf("s.Kilobyte expected to be %d bytes but was %d", 1<<10, s.Kilobyte)
}
if s.Megabyte != 1<<20 {
t.Errorf("s.Megabyte expected to be %d but was %d", 1<<20, s.Megabyte)
}
if s.Gigabyte != 1<<30 {
t.Errorf("s.Gigabyte expected to be %d but was %d", 1<<30, s.Gigabyte)
}
if s.Terabyte != 1<<40 {
t.Errorf("s.Terabyte expected to be %d but was %d", 1<<40, s.Terabyte)
}
if s.Empty != 0 {
t.Errorf("s.Empty expected to be 0 but was %d", s.Empty)
}
2019-03-07 18:37:07 +01:00
}
2018-04-04 23:27:22 +02:00
2019-03-08 12:32:55 +01:00
func addConfig(path, filename string) cfg.ConfigFiles {
cfg := cfg.ConfigFiles{
Files: make([]cfg.File, 0, 1),
2018-03-29 14:11:41 +02:00
}
2018-06-24 23:01:01 +02:00
cfg.AddConfig(path, filename, true)
2018-03-29 14:11:41 +02:00
return cfg
}