yaml_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package json
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestYMLToJSON_V2Style(t *testing.T) {
  8. input := `
  9. log:
  10. loglevel: debug
  11. inbounds:
  12. - port: 10800
  13. listen: 127.0.0.1
  14. protocol: socks
  15. settings:
  16. udp: true
  17. outbounds:
  18. - protocol: vmess
  19. settings:
  20. vnext:
  21. - address: example.com
  22. port: 443
  23. users:
  24. - id: '98a15fa6-2eb1-edd5-50ea-cfc428aaab78'
  25. streamSettings:
  26. network: tcp
  27. security: tls
  28. `
  29. expected := `
  30. {
  31. "log": {
  32. "loglevel": "debug"
  33. },
  34. "inbounds": [{
  35. "port": 10800,
  36. "listen": "127.0.0.1",
  37. "protocol": "socks",
  38. "settings": {
  39. "udp": true
  40. }
  41. }],
  42. "outbounds": [{
  43. "protocol": "vmess",
  44. "settings": {
  45. "vnext": [{
  46. "port": 443,
  47. "address": "example.com",
  48. "users": [{
  49. "id": "98a15fa6-2eb1-edd5-50ea-cfc428aaab78"
  50. }]
  51. }]
  52. },
  53. "streamSettings": {
  54. "network": "tcp",
  55. "security": "tls"
  56. }
  57. }]
  58. }
  59. `
  60. bs, err := FromYAML([]byte(input))
  61. if err != nil {
  62. t.Error(err)
  63. }
  64. m := make(map[string]interface{})
  65. json.Unmarshal(bs, &m)
  66. assertResult(t, m, expected)
  67. }
  68. func TestYMLToJSON_ValueTypes(t *testing.T) {
  69. input := `
  70. boolean:
  71. - TRUE
  72. - FALSE
  73. - true
  74. - false
  75. float:
  76. - 3.14
  77. - 6.8523015e+5
  78. int:
  79. - 123
  80. - 0b1010_0111_0100_1010_1110
  81. null:
  82. nodeName: 'node'
  83. parent: ~ # ~ for null
  84. string:
  85. - 哈哈
  86. - 'Hello world'
  87. - newline
  88. newline2 # multi-line string
  89. date:
  90. - 2018-02-17 # yyyy-MM-dd
  91. datetime:
  92. - 2018-02-17T15:02:31+08:00 # ISO 8601 time
  93. mixed:
  94. - true
  95. - false
  96. - 1
  97. - 0
  98. - null
  99. - hello
  100. # arbitrary keys
  101. 1: 0
  102. true: false
  103. TRUE: TRUE
  104. "str": "hello"
  105. `
  106. expected := `
  107. {
  108. "boolean": [true, false, true, false],
  109. "float": [3.14, 685230.15],
  110. "int": [123, 685230],
  111. "null": {
  112. "nodeName": "node",
  113. "parent": null
  114. },
  115. "string": ["哈哈", "Hello world", "newline newline2"],
  116. "date": ["2018-02-17"],
  117. "datetime": ["2018-02-17T15:02:31+08:00"],
  118. "mixed": [true,false,1,0,null,"hello"],
  119. "1": 0,
  120. "true": true,
  121. "str": "hello"
  122. }
  123. `
  124. bs, err := FromYAML([]byte(input))
  125. if err != nil {
  126. t.Error(err)
  127. }
  128. m := make(map[string]interface{})
  129. json.Unmarshal(bs, &m)
  130. assertResult(t, m, expected)
  131. }
  132. func assertResult(t *testing.T, value map[string]interface{}, expected string) {
  133. e := make(map[string]interface{})
  134. err := json.Unmarshal([]byte(expected), &e)
  135. if err != nil {
  136. t.Error(err)
  137. }
  138. if !reflect.DeepEqual(value, e) {
  139. bs, _ := json.Marshal(value)
  140. t.Fatalf("expected:\n%s\n\nactual:\n%s", expected, string(bs))
  141. }
  142. }