toml_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. 1 = 0
  75. true = true
  76. str = "hello"
  77. [null]
  78. nodeName = "node"
  79. `
  80. expected := `
  81. {
  82. "boolean": [true, false, true, false],
  83. "float": [3.14, 685230.15],
  84. "int": [123, 685230],
  85. "null": {
  86. "nodeName": "node"
  87. },
  88. "string": ["哈哈", "Hello world", "newline newline2"],
  89. "date": ["2018-02-17"],
  90. "datetime": ["2018-02-17T15:02:31+08:00"],
  91. "1": 0,
  92. "true": true,
  93. "str": "hello"
  94. }
  95. `
  96. bs, err := FromTOML([]byte(input))
  97. if err != nil {
  98. t.Error(err)
  99. }
  100. m := make(map[string]interface{})
  101. json.Unmarshal(bs, &m)
  102. assertResult(t, m, expected)
  103. }