// Copyright 2022 schukai GmbH // SPDX-License-Identifier: AGPL-3.0-or-later package configuration import ( "bytes" "fmt" "io" "testing" "time" ) func TestReadmeExample(t *testing.T) { config := struct { Host string }{ Host: "localhost", } s := New(config) closeChan := make(chan bool) msg := "" s.OnChange(func(event ChangeEvent) { log := event.Changlog msg = fmt.Sprintf("Change from %s to %s", log[0].From, log[0].To) // for Readme //fmt.Println(msg) closeChan <- true }) c := s.Config() c.Host = "www.example.com" s.SetConfig(c) // Wait for change select { case <-closeChan: case <-time.After(time.Second): t.Fatalf("Timeout") } if msg != "Change from localhost to www.example.com" { t.Error("Expected \"Change from localhost to www.example.com\" got ", msg) } } func TestCangeOnChange(t *testing.T) { defaults := ConfigStruct2{ A: "", B: false, C: nil, D: nil, F: 0, G: "", } s := New(defaults) s.SetMnemonic("test") var buf bytes.Buffer buf.WriteString("A: a\n") r := (io.Reader)(&buf) s.AddReader(r, Yaml) s.Import() if s.HasErrors() { t.Error("Expected not error", s.Errors()) } c := s.Config() if c.A != "a" { t.Error("Expected \"a\" got ", c) } closeChan := make(chan bool) counter := 0 s.OnChange(func(event ChangeEvent) { counter++ closeChan <- true }) c.A = "b" s.SetConfig(c) select { case <-closeChan: case <-time.After(time.Second): t.Fatalf("Timeout") } if counter != 1 { t.Error("Expected 1 got ", counter) } }