yaml_loader_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package yaml_loader
  2. import (
  3. "os"
  4. "testing"
  5. )
  6. type TestYamlModel1 struct {
  7. Test `yaml:"test1"`
  8. }
  9. type TestYamlModel2 struct {
  10. Test `yaml:"test2"`
  11. }
  12. type Test struct {
  13. Name string `yaml:"name"`
  14. }
  15. func TestLoadYAML(t *testing.T) {
  16. testModel1 := new(TestYamlModel1)
  17. testModel2 := new(TestYamlModel2)
  18. retObjects := []any{testModel1, testModel2}
  19. err := LoadYamlFile("test.yaml", &retObjects)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if retObjects[0].(*TestYamlModel1).Name != "foo1" {
  24. t.Fatal("model1名称错误")
  25. }
  26. if retObjects[1].(*TestYamlModel2).Name != "foo2" {
  27. t.Fatal("model2名称错误")
  28. }
  29. testModel1 = new(TestYamlModel1)
  30. testModel2 = new(TestYamlModel2)
  31. retObjects = []any{testModel1, testModel2}
  32. yamlFileBytes, err := os.ReadFile("test.yaml")
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. err = LoadYamlBytes(yamlFileBytes, &retObjects)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if retObjects[0].(*TestYamlModel1).Name != "foo1" {
  41. t.Fatal("model1名称错误")
  42. }
  43. if retObjects[1].(*TestYamlModel2).Name != "foo2" {
  44. t.Fatal("model2名称错误")
  45. }
  46. retObjects = []any{make(map[string]any), make(map[string]any)}
  47. err = LoadYamlFile("test.yaml", &retObjects)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. if retObjects[0].(map[string]any)["test1"].(map[string]any)["name"] != "foo1" {
  52. t.Fatal("model1名称错误")
  53. }
  54. if retObjects[1].(map[string]any)["test2"].(map[string]any)["name"] != "foo2" {
  55. t.Fatal("model2名称错误")
  56. }
  57. }
  58. func TestLoadYAMLMap(t *testing.T) {
  59. retObjectMaps := make([]map[string]any, 0)
  60. err := LoadYamlMapFile("test.yaml", func(objectMap map[string]any) error {
  61. retObjectMaps = append(retObjectMaps, objectMap)
  62. return nil
  63. })
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. if retObjectMaps[0]["test1"].(map[string]any)["name"] != "foo1" {
  68. t.Fatal("model1名称错误")
  69. }
  70. if retObjectMaps[1]["test2"].(map[string]any)["name"] != "foo2" {
  71. t.Fatal("model2名称错误")
  72. }
  73. retObjectMaps = make([]map[string]any, 0)
  74. yamlFileBytes, err := os.ReadFile("test.yaml")
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. err = LoadYamlMapBytes(yamlFileBytes, func(objectMap map[string]any) error {
  79. retObjectMaps = append(retObjectMaps, objectMap)
  80. return nil
  81. })
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if retObjectMaps[0]["test1"].(map[string]any)["name"] != "foo1" {
  86. t.Fatal("model1名称错误")
  87. }
  88. if retObjectMaps[1]["test2"].(map[string]any)["name"] != "foo2" {
  89. t.Fatal("model2名称错误")
  90. }
  91. }