toml_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package json_test
  2. import (
  3. "encoding/json"
  4. "testing"
  5. . "github.com/v2fly/v2ray-core/v5/infra/conf/json"
  6. )
  7. func TestTOMLToJSON_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. [inbounds.settings]
  16. udp = true
  17. [[outbounds]]
  18. protocol = 'vmess'
  19. [[outbounds.settings.vnext]]
  20. port = 443
  21. address = 'example.com'
  22. [[outbounds.settings.vnext.users]]
  23. id = '98a15fa6-2eb1-edd5-50ea-cfc428aaab78'
  24. [outbounds.streamSettings]
  25. network = 'tcp'
  26. security = 'tls'
  27. `
  28. expected := `
  29. {
  30. "log": {
  31. "loglevel": "debug"
  32. },
  33. "inbounds": [{
  34. "port": 10800,
  35. "listen": "127.0.0.1",
  36. "protocol": "socks",
  37. "settings": {
  38. "udp": true
  39. }
  40. }],
  41. "outbounds": [{
  42. "protocol": "vmess",
  43. "settings": {
  44. "vnext": [{
  45. "port": 443,
  46. "address": "example.com",
  47. "users": [{
  48. "id": "98a15fa6-2eb1-edd5-50ea-cfc428aaab78"
  49. }]
  50. }]
  51. },
  52. "streamSettings": {
  53. "network": "tcp",
  54. "security": "tls"
  55. }
  56. }]
  57. }
  58. `
  59. bs, err := FromTOML([]byte(input))
  60. if err != nil {
  61. t.Error(err)
  62. }
  63. m := make(map[string]interface{})
  64. json.Unmarshal(bs, &m)
  65. assertResult(t, m, expected)
  66. }
  67. func TestTOMLToJSON_ValueTypes(t *testing.T) {
  68. input := `
  69. boolean = [ true, false, true, false ]
  70. float = [ 3.14, 685_230.15 ]
  71. int = [ 123, 685_230 ]
  72. string = [ "哈哈", "Hello world", "newline newline2" ]
  73. date = [ "2018-02-17" ]
  74. datetime = [ "2018-02-17T15:02:31+08:00" ]
  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. "1": 0,
  93. "true": true,
  94. "str": "hello"
  95. }
  96. `
  97. bs, err := FromTOML([]byte(input))
  98. if err != nil {
  99. t.Error(err)
  100. }
  101. m := make(map[string]interface{})
  102. json.Unmarshal(bs, &m)
  103. assertResult(t, m, expected)
  104. }