yaml_loader_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 TestLoadYAMLFile(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. }