github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/json/toml_test.go (about)

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