github.com/wangyougui/gf/v2@v2.6.5/encoding/gtoml/gtoml_z_unit_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  package gtoml_test
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/wangyougui/gf/v2/encoding/gjson"
    12  	"github.com/wangyougui/gf/v2/encoding/gtoml"
    13  	"github.com/wangyougui/gf/v2/test/gtest"
    14  )
    15  
    16  var tomlStr string = `
    17  # 模板引擎目录
    18  viewpath = "/home/www/templates/"
    19  # MySQL数据库配置
    20  [redis]
    21      disk  = "127.0.0.1:6379,0"
    22      cache = "127.0.0.1:6379,1"
    23  `
    24  
    25  var tomlErr string = `
    26  # 模板引擎目录
    27  viewpath = "/home/www/templates/"
    28  # MySQL数据库配置
    29  [redis]
    30  dd = 11
    31  [redis]
    32      disk  = "127.0.0.1:6379,0"
    33      cache = "127.0.0.1:6379,1"
    34  `
    35  
    36  func TestEncode(t *testing.T) {
    37  	gtest.C(t, func(t *gtest.T) {
    38  		m := make(map[string]string)
    39  		m["toml"] = tomlStr
    40  		res, err := gtoml.Encode(m)
    41  		if err != nil {
    42  			t.Errorf("encode failed. %v", err)
    43  			return
    44  		}
    45  
    46  		t.Assert(gjson.New(res).Get("toml").String(), tomlStr)
    47  	})
    48  
    49  	gtest.C(t, func(t *gtest.T) {
    50  		_, err := gtoml.Encode(tomlErr)
    51  		if err == nil {
    52  			t.Errorf("encode should be failed. %v", err)
    53  			return
    54  		}
    55  	})
    56  }
    57  
    58  func TestDecode(t *testing.T) {
    59  	gtest.C(t, func(t *gtest.T) {
    60  		m := make(map[string]string)
    61  		m["toml"] = tomlStr
    62  		res, err := gtoml.Encode(m)
    63  		if err != nil {
    64  			t.Errorf("encode failed. %v", err)
    65  			return
    66  		}
    67  
    68  		decodeStr, err := gtoml.Decode(res)
    69  		if err != nil {
    70  			t.Errorf("decode failed. %v", err)
    71  			return
    72  		}
    73  
    74  		t.Assert(decodeStr.(map[string]interface{})["toml"], tomlStr)
    75  
    76  		decodeStr1 := make(map[string]interface{})
    77  		err = gtoml.DecodeTo(res, &decodeStr1)
    78  		if err != nil {
    79  			t.Errorf("decodeTo failed. %v", err)
    80  			return
    81  		}
    82  		t.Assert(decodeStr1["toml"], tomlStr)
    83  	})
    84  
    85  	gtest.C(t, func(t *gtest.T) {
    86  		_, err := gtoml.Decode([]byte(tomlErr))
    87  		if err == nil {
    88  			t.Errorf("decode failed. %v", err)
    89  			return
    90  		}
    91  
    92  		decodeStr1 := make(map[string]interface{})
    93  		err = gtoml.DecodeTo([]byte(tomlErr), &decodeStr1)
    94  		if err == nil {
    95  			t.Errorf("decodeTo failed. %v", err)
    96  			return
    97  		}
    98  	})
    99  }
   100  
   101  func TestToJson(t *testing.T) {
   102  	gtest.C(t, func(t *gtest.T) {
   103  		m := make(map[string]string)
   104  		m["toml"] = tomlStr
   105  		res, err := gtoml.Encode(m)
   106  		if err != nil {
   107  			t.Errorf("encode failed. %v", err)
   108  			return
   109  		}
   110  
   111  		jsonToml, err := gtoml.ToJson(res)
   112  		if err != nil {
   113  			t.Errorf("ToJson failed. %v", err)
   114  			return
   115  		}
   116  
   117  		p := gjson.New(res)
   118  		expectJson, err := p.ToJson()
   119  		if err != nil {
   120  			t.Errorf("parser ToJson failed. %v", err)
   121  			return
   122  		}
   123  		t.Assert(jsonToml, expectJson)
   124  	})
   125  
   126  	gtest.C(t, func(t *gtest.T) {
   127  		_, err := gtoml.ToJson([]byte(tomlErr))
   128  		if err == nil {
   129  			t.Errorf("ToJson failed. %v", err)
   130  			return
   131  		}
   132  	})
   133  }