xml_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package exprhelpers
  2. import (
  3. "log"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestXMLGetAttributeValue(t *testing.T) {
  8. if err := Init(nil); err != nil {
  9. log.Fatal(err)
  10. }
  11. tests := []struct {
  12. name string
  13. xmlString string
  14. path string
  15. attribute string
  16. expectResult string
  17. }{
  18. {
  19. name: "XMLGetAttributeValue",
  20. xmlString: `<root><child attr="value"/></root>`,
  21. path: "/root/child",
  22. attribute: "attr",
  23. expectResult: "value",
  24. },
  25. {
  26. name: "Non existing attribute for XMLGetAttributeValue",
  27. xmlString: `<root><child attr="value"/></root>`,
  28. path: "/root/child",
  29. attribute: "asdasd",
  30. expectResult: "",
  31. },
  32. {
  33. name: "Non existing path for XMLGetAttributeValue",
  34. xmlString: `<root><child attr="value"/></root>`,
  35. path: "/foo/bar",
  36. attribute: "asdasd",
  37. expectResult: "",
  38. },
  39. {
  40. name: "Invalid XML for XMLGetAttributeValue",
  41. xmlString: `<root><`,
  42. path: "/foo/bar",
  43. attribute: "asdasd",
  44. expectResult: "",
  45. },
  46. {
  47. name: "Invalid path for XMLGetAttributeValue",
  48. xmlString: `<root><child attr="value"/></root>`,
  49. path: "/foo/bar[@",
  50. attribute: "asdasd",
  51. expectResult: "",
  52. },
  53. }
  54. for _, test := range tests {
  55. result := XMLGetAttributeValue(test.xmlString, test.path, test.attribute)
  56. isOk := assert.Equal(t, test.expectResult, result)
  57. if !isOk {
  58. t.Fatalf("test '%s' failed", test.name)
  59. }
  60. log.Printf("test '%s' : OK", test.name)
  61. }
  62. }
  63. func TestXMLGetNodeValue(t *testing.T) {
  64. if err := Init(nil); err != nil {
  65. log.Fatal(err)
  66. }
  67. tests := []struct {
  68. name string
  69. xmlString string
  70. path string
  71. expectResult string
  72. }{
  73. {
  74. name: "XMLGetNodeValue",
  75. xmlString: `<root><child>foobar</child></root>`,
  76. path: "/root/child",
  77. expectResult: "foobar",
  78. },
  79. {
  80. name: "Non existing path for XMLGetNodeValue",
  81. xmlString: `<root><child>foobar</child></root>`,
  82. path: "/foo/bar",
  83. expectResult: "",
  84. },
  85. {
  86. name: "Invalid XML for XMLGetNodeValue",
  87. xmlString: `<root><`,
  88. path: "/foo/bar",
  89. expectResult: "",
  90. },
  91. {
  92. name: "Invalid path for XMLGetNodeValue",
  93. xmlString: `<root><child>foobar</child></root>`,
  94. path: "/foo/bar[@",
  95. expectResult: "",
  96. },
  97. }
  98. for _, test := range tests {
  99. result := XMLGetNodeValue(test.xmlString, test.path)
  100. isOk := assert.Equal(t, test.expectResult, result)
  101. if !isOk {
  102. t.Fatalf("test '%s' failed", test.name)
  103. }
  104. log.Printf("test '%s' : OK", test.name)
  105. }
  106. }