github.com/zhongdalu/gf@v1.0.0/g/encoding/gtoml/gtoml_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  package gtoml_test
     7  
     8  import (
     9  	"github.com/zhongdalu/gf/g/encoding/gparser"
    10  	"github.com/zhongdalu/gf/g/encoding/gtoml"
    11  	"github.com/zhongdalu/gf/g/test/gtest"
    12  	"testing"
    13  )
    14  
    15  var tomlStr string = `
    16  # 模板引擎目录
    17  viewpath = "/home/www/templates/"
    18  # MySQL数据库配置
    19  [redis]
    20      disk  = "127.0.0.1:6379,0"
    21      cache = "127.0.0.1:6379,1"
    22  `
    23  
    24  var tomlErr string = `
    25  # 模板引擎目录
    26  viewpath = "/home/www/templates/"
    27  # MySQL数据库配置
    28  [redis]
    29  dd = 11
    30  [redis]
    31      disk  = "127.0.0.1:6379,0"
    32      cache = "127.0.0.1:6379,1"
    33  `
    34  
    35  func TestEncode(t *testing.T) {
    36  	gtest.Case(t, func() {
    37  		m := make(map[string]string)
    38  		m["toml"] = tomlStr
    39  		res, err := gtoml.Encode(m)
    40  		if err != nil {
    41  			t.Errorf("encode failed. %v", err)
    42  			return
    43  		}
    44  
    45  		p, err := gparser.LoadContent(res)
    46  		if err != nil {
    47  			t.Errorf("parser failed. %v", err)
    48  			return
    49  		}
    50  
    51  		gtest.Assert(p.GetString("toml"), tomlStr)
    52  	})
    53  
    54  	gtest.Case(t, func() {
    55  		_, err := gtoml.Encode(tomlErr)
    56  		if err == nil {
    57  			t.Errorf("encode should be failed. %v", err)
    58  			return
    59  		}
    60  	})
    61  }
    62  
    63  func TestDecode(t *testing.T) {
    64  	gtest.Case(t, func() {
    65  		m := make(map[string]string)
    66  		m["toml"] = tomlStr
    67  		res, err := gtoml.Encode(m)
    68  		if err != nil {
    69  			t.Errorf("encode failed. %v", err)
    70  			return
    71  		}
    72  
    73  		decodeStr, err := gtoml.Decode(res)
    74  		if err != nil {
    75  			t.Errorf("decode failed. %v", err)
    76  			return
    77  		}
    78  
    79  		gtest.Assert(decodeStr.(map[string]interface{})["toml"], tomlStr)
    80  
    81  		decodeStr1 := make(map[string]interface{})
    82  		err = gtoml.DecodeTo(res, &decodeStr1)
    83  		if err != nil {
    84  			t.Errorf("decodeTo failed. %v", err)
    85  			return
    86  		}
    87  		gtest.Assert(decodeStr1["toml"], tomlStr)
    88  	})
    89  
    90  	gtest.Case(t, func() {
    91  		_, err := gtoml.Decode([]byte(tomlErr))
    92  		if err == nil {
    93  			t.Errorf("decode failed. %v", err)
    94  			return
    95  		}
    96  
    97  		decodeStr1 := make(map[string]interface{})
    98  		err = gtoml.DecodeTo([]byte(tomlErr), &decodeStr1)
    99  		if err == nil {
   100  			t.Errorf("decodeTo failed. %v", err)
   101  			return
   102  		}
   103  	})
   104  }
   105  
   106  func TestToJson(t *testing.T) {
   107  	gtest.Case(t, func() {
   108  		m := make(map[string]string)
   109  		m["toml"] = tomlStr
   110  		res, err := gtoml.Encode(m)
   111  		if err != nil {
   112  			t.Errorf("encode failed. %v", err)
   113  			return
   114  		}
   115  
   116  		jsonToml, err := gtoml.ToJson(res)
   117  		if err != nil {
   118  			t.Errorf("ToJson failed. %v", err)
   119  			return
   120  		}
   121  
   122  		p, err := gparser.LoadContent(res)
   123  		if err != nil {
   124  			t.Errorf("parser failed. %v", err)
   125  			return
   126  		}
   127  		expectJson, err := p.ToJson()
   128  		if err != nil {
   129  			t.Errorf("parser ToJson failed. %v", err)
   130  			return
   131  		}
   132  		gtest.Assert(jsonToml, expectJson)
   133  	})
   134  
   135  	gtest.Case(t, func() {
   136  		_, err := gtoml.ToJson([]byte(tomlErr))
   137  		if err == nil {
   138  			t.Errorf("ToJson failed. %v", err)
   139  			return
   140  		}
   141  	})
   142  }