toml_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package json
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. func TestTOMLToJSON_V2Style(t *testing.T) {
  7. input := `
  8. [log]
  9. loglevel = 'debug'
  10. [[inbounds]]
  11. port = 10800
  12. listen = '127.0.0.1'
  13. protocol = 'socks'
  14. [inbounds.settings]
  15. udp = true
  16. [[outbounds]]
  17. protocol = 'vmess'
  18. [[outbounds.settings.vnext]]
  19. port = 443
  20. address = 'example.com'
  21. [[outbounds.settings.vnext.users]]
  22. id = '98a15fa6-2eb1-edd5-50ea-cfc428aaab78'
  23. [outbounds.streamSettings]
  24. network = 'tcp'
  25. security = 'tls'
  26. `
  27. expected := `
  28. {
  29. "log": {
  30. "loglevel": "debug"
  31. },
  32. "inbounds": [{
  33. "port": 10800,
  34. "listen": "127.0.0.1",
  35. "protocol": "socks",
  36. "settings": {
  37. "udp": true
  38. }
  39. }],
  40. "outbounds": [{
  41. "protocol": "vmess",
  42. "settings": {
  43. "vnext": [{
  44. "port": 443,
  45. "address": "example.com",
  46. "users": [{
  47. "id": "98a15fa6-2eb1-edd5-50ea-cfc428aaab78"
  48. }]
  49. }]
  50. },
  51. "streamSettings": {
  52. "network": "tcp",
  53. "security": "tls"
  54. }
  55. }]
  56. }
  57. `
  58. bs, err := FromTOML([]byte(input))
  59. if err != nil {
  60. t.Error(err)
  61. }
  62. m := make(map[string]interface{})
  63. json.Unmarshal(bs, &m)
  64. assertResult(t, m, expected)
  65. }
  66. func TestTOMLToJSON_ValueTypes(t *testing.T) {
  67. input := `
  68. boolean = [ true, false, true, false ]
  69. float = [ 3.14, 685_230.15 ]
  70. int = [ 123, 685_230 ]
  71. string = [ "哈哈", "Hello world", "newline newline2" ]
  72. date = [ "2018-02-17" ]
  73. datetime = [ "2018-02-17T15:02:31+08:00" ]
  74. mixed = [ true, false, 1, 0, "hello" ]
  75. 1 = 0
  76. true = true
  77. str = "hello"
  78. [null]
  79. nodeName = "node"
  80. `
  81. expected := `
  82. {
  83. "boolean": [true, false, true, false],
  84. "float": [3.14, 685230.15],
  85. "int": [123, 685230],
  86. "null": {
  87. "nodeName": "node"
  88. },
  89. "string": ["哈哈", "Hello world", "newline newline2"],
  90. "date": ["2018-02-17"],
  91. "datetime": ["2018-02-17T15:02:31+08:00"],
  92. "mixed": [true,false,1,0,"hello"],
  93. "1": 0,
  94. "true": true,
  95. "str": "hello"
  96. }
  97. `
  98. bs, err := FromTOML([]byte(input))
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. m := make(map[string]interface{})
  103. json.Unmarshal(bs, &m)
  104. assertResult(t, m, expected)
  105. }