nixfig/nixfig_test.go

72 lines
1.0 KiB
Go

package nixfig
import (
"os"
"testing"
"github.com/alecthomas/assert/v2"
)
type Config struct {
User string
Log struct {
Level string
File string
}
HTTP struct {
Host string
Port int
Admins []string
}
}
var testCfg = Config{
User: "jolheiser",
Log: struct {
Level string
File string
}{
Level: "warn",
File: "jolheiser.log",
},
HTTP: struct {
Host string
Port int
Admins []string
}{
Host: "0.0.0.0",
Port: 1234,
Admins: []string{"jolheiser", "admin"},
},
}
func TestNixNotFound(t *testing.T) {
oldNix := Nix
Nix = ""
_, err := Marshal(nil)
assert.IsError(t, err, ErrNixNotFound)
Nix = oldNix
}
func TestUnmarshal(t *testing.T) {
data, err := os.ReadFile("testdata/config.nix")
assert.NoError(t, err)
var cfg Config
err = Unmarshal(data, &cfg)
assert.NoError(t, err)
assert.Equal(t, testCfg, cfg)
}
func TestMarshal(t *testing.T) {
data, err := Marshal(testCfg)
assert.NoError(t, err)
var cfg Config
err = Unmarshal(data, &cfg)
assert.NoError(t, err)
assert.Equal(t, testCfg, cfg)
}