properties_test.go 999 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package conf
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/core/fs"
  7. )
  8. func TestProperties(t *testing.T) {
  9. text := `app.name = test
  10. app.program=app
  11. # this is comment
  12. app.threads = 5`
  13. tmpfile, err := fs.TempFilenameWithText(text)
  14. assert.Nil(t, err)
  15. defer os.Remove(tmpfile)
  16. props, err := LoadProperties(tmpfile)
  17. assert.Nil(t, err)
  18. assert.Equal(t, "test", props.GetString("app.name"))
  19. assert.Equal(t, "app", props.GetString("app.program"))
  20. assert.Equal(t, 5, props.GetInt("app.threads"))
  21. }
  22. func TestSetString(t *testing.T) {
  23. key := "a"
  24. value := "the value of a"
  25. props := NewProperties()
  26. props.SetString(key, value)
  27. assert.Equal(t, value, props.GetString(key))
  28. }
  29. func TestSetInt(t *testing.T) {
  30. key := "a"
  31. value := 101
  32. props := NewProperties()
  33. props.SetInt(key, value)
  34. assert.Equal(t, value, props.GetInt(key))
  35. }
  36. func TestLoadBadFile(t *testing.T) {
  37. _, err := LoadProperties("nosuchfile")
  38. assert.NotNil(t, err)
  39. }