properties_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. val := props.ToString()
  22. assert.Contains(t, val, "app.name")
  23. assert.Contains(t, val, "app.program")
  24. assert.Contains(t, val, "app.threads")
  25. }
  26. func TestLoadProperties_badContent(t *testing.T) {
  27. filename, err := fs.TempFilenameWithText("hello")
  28. assert.Nil(t, err)
  29. defer os.Remove(filename)
  30. _, err = LoadProperties(filename)
  31. assert.NotNil(t, err)
  32. assert.True(t, len(err.Error()) > 0)
  33. }
  34. func TestSetString(t *testing.T) {
  35. key := "a"
  36. value := "the value of a"
  37. props := NewProperties()
  38. props.SetString(key, value)
  39. assert.Equal(t, value, props.GetString(key))
  40. }
  41. func TestSetInt(t *testing.T) {
  42. key := "a"
  43. value := 101
  44. props := NewProperties()
  45. props.SetInt(key, value)
  46. assert.Equal(t, value, props.GetInt(key))
  47. }
  48. func TestLoadBadFile(t *testing.T) {
  49. _, err := LoadProperties("nosuchfile")
  50. assert.NotNil(t, err)
  51. }