metadata_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package metadata_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "cdop.pt/go/free/platepipe/metadata"
  6. . "cdop.pt/go/open/assertive"
  7. )
  8. func TestHasMetadata(t *testing.T) {
  9. cases := []struct {
  10. buf []byte
  11. ret bool
  12. pos int
  13. }{
  14. {[]byte(""), false, 0},
  15. {[]byte("test"), false, 0},
  16. {[]byte("test\ntest"), false, 0},
  17. {[]byte(" barekey = 'value'\n\n"), false, 0},
  18. {[]byte("barekey = 'value'\n\n"), true, 19},
  19. {[]byte("1barekey = 'value'\n\n"), true, 20},
  20. {[]byte("'key' = 'value'\n\n"), true, 17},
  21. {[]byte(`"key" = 'value'` + "\n\n"), true, 17},
  22. {[]byte("barekey = 'value'\n\nother text"), true, 19},
  23. {[]byte("barekey = 'value'\n\r\nother text"), true, 20},
  24. }
  25. for _, c := range cases {
  26. ret, pos := metadata.IsPresent(c.buf)
  27. if ret != c.ret || pos != c.pos {
  28. t.Errorf("IsPresent(%s) returned %v, %v",
  29. string(c.buf), ret, pos)
  30. }
  31. }
  32. }
  33. func TestFromTomlBuffer(t *testing.T) {
  34. t.Run("valid metadata", func(t *testing.T) {
  35. ret, err := metadata.FromTomlBuffer([]byte("strkey='value'\nintkey = 10"))
  36. Want(t, err == nil)
  37. Want(t, fmt.Sprint(ret) == fmt.Sprint(map[string]any{
  38. "strkey": "value",
  39. "intkey": 10,
  40. }))
  41. })
  42. t.Run("invalid metadata", func(t *testing.T) {
  43. ret, err := metadata.FromTomlBuffer([]byte("strkey: 'value'\nintkey = 10"))
  44. Need(t, err != nil)
  45. Want(t, err.Error() ==
  46. "toml: line 1: expected '.' or '=', but got ':' instead")
  47. Want(t, fmt.Sprint(ret) == fmt.Sprint(map[string]any{}))
  48. })
  49. }