github.com/gogf/gf/v2@v2.7.4/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/gogf/gf.
     6  package gtoml_test
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/gogf/gf/v2/encoding/gjson"
    12  	"github.com/gogf/gf/v2/encoding/gtoml"
    13  	"github.com/gogf/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  		t.Assert(err, nil)
    52  	})
    53  }
    54  
    55  func TestDecode(t *testing.T) {
    56  	gtest.C(t, func(t *gtest.T) {
    57  		m := make(map[string]string)
    58  		m["toml"] = tomlStr
    59  		res, err := gtoml.Encode(m)
    60  		if err != nil {
    61  			t.Errorf("encode failed. %v", err)
    62  			return
    63  		}
    64  
    65  		decodeStr, err := gtoml.Decode(res)
    66  		if err != nil {
    67  			t.Errorf("decode failed. %v", err)
    68  			return
    69  		}
    70  
    71  		t.Assert(decodeStr.(map[string]interface{})["toml"], tomlStr)
    72  
    73  		decodeStr1 := make(map[string]interface{})
    74  		err = gtoml.DecodeTo(res, &decodeStr1)
    75  		if err != nil {
    76  			t.Errorf("decodeTo failed. %v", err)
    77  			return
    78  		}
    79  		t.Assert(decodeStr1["toml"], tomlStr)
    80  	})
    81  
    82  	gtest.C(t, func(t *gtest.T) {
    83  		_, err := gtoml.Decode([]byte(tomlErr))
    84  		if err == nil {
    85  			t.Errorf("decode failed. %v", err)
    86  			return
    87  		}
    88  
    89  		decodeStr1 := make(map[string]interface{})
    90  		err = gtoml.DecodeTo([]byte(tomlErr), &decodeStr1)
    91  		if err == nil {
    92  			t.Errorf("decodeTo failed. %v", err)
    93  			return
    94  		}
    95  	})
    96  }
    97  
    98  func TestToJson(t *testing.T) {
    99  	gtest.C(t, func(t *gtest.T) {
   100  		m := make(map[string]string)
   101  		m["toml"] = tomlStr
   102  		res, err := gtoml.Encode(m)
   103  		if err != nil {
   104  			t.Errorf("encode failed. %v", err)
   105  			return
   106  		}
   107  
   108  		jsonToml, err := gtoml.ToJson(res)
   109  		if err != nil {
   110  			t.Errorf("ToJson failed. %v", err)
   111  			return
   112  		}
   113  
   114  		p := gjson.New(res)
   115  		expectJson, err := p.ToJson()
   116  		if err != nil {
   117  			t.Errorf("parser ToJson failed. %v", err)
   118  			return
   119  		}
   120  		t.Assert(jsonToml, expectJson)
   121  	})
   122  
   123  	gtest.C(t, func(t *gtest.T) {
   124  		_, err := gtoml.ToJson([]byte(tomlErr))
   125  		if err == nil {
   126  			t.Errorf("ToJson failed. %v", err)
   127  			return
   128  		}
   129  	})
   130  }