github.com/wangyougui/gf/v2@v2.6.5/encoding/gyaml/gyaml_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  
     7  package gyaml_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/encoding/gjson"
    13  	"github.com/wangyougui/gf/v2/encoding/gyaml"
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/internal/json"
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  )
    18  
    19  var yamlStr string = `
    20  #即表示url属性值;
    21  url: https://goframe.org
    22  
    23  #数组,即表示server为[a,b,c]
    24  server:
    25      - 120.168.117.21
    26      - 120.168.117.22
    27  #常量
    28  pi: 3.14   #定义一个数值3.14
    29  hasChild: true  #定义一个boolean值
    30  name: '你好YAML'   #定义一个字符串
    31  `
    32  
    33  var yamlErr string = `
    34  [redis]
    35  dd = 11
    36  [redis]
    37      disk  = "127.0.0.1:6379,0"
    38      cache = "127.0.0.1:6379,1"
    39  `
    40  
    41  func Test_Encode(t *testing.T) {
    42  	// Map.
    43  	gtest.C(t, func(t *gtest.T) {
    44  		b, err := gyaml.Encode(g.Map{
    45  			"k": "v",
    46  		})
    47  		t.AssertNil(err)
    48  		t.Assert(string(b), `k: v
    49  `)
    50  	})
    51  	// Array.
    52  	gtest.C(t, func(t *gtest.T) {
    53  		b, err := gyaml.Encode([]string{"a", "b", "c"})
    54  		t.AssertNil(err)
    55  		t.Assert(string(b), `- a
    56  - b
    57  - c
    58  `)
    59  	})
    60  }
    61  
    62  func Test_EncodeIndent(t *testing.T) {
    63  	// Array.
    64  	gtest.C(t, func(t *gtest.T) {
    65  		b, err := gyaml.EncodeIndent([]string{"a", "b", "c"}, "####")
    66  		t.AssertNil(err)
    67  		t.Assert(string(b), `####- a
    68  ####- b
    69  ####- c
    70  `)
    71  	})
    72  }
    73  
    74  func Test_Decode(t *testing.T) {
    75  	gtest.C(t, func(t *gtest.T) {
    76  		result, err := gyaml.Decode([]byte(yamlStr))
    77  		t.AssertNil(err)
    78  
    79  		t.Assert(result, map[string]interface{}{
    80  			"url":      "https://goframe.org",
    81  			"server":   g.Slice{"120.168.117.21", "120.168.117.22"},
    82  			"pi":       3.14,
    83  			"hasChild": true,
    84  			"name":     "你好YAML",
    85  		})
    86  	})
    87  }
    88  
    89  func Test_DecodeTo(t *testing.T) {
    90  	gtest.C(t, func(t *gtest.T) {
    91  		result := make(map[string]interface{})
    92  		err := gyaml.DecodeTo([]byte(yamlStr), &result)
    93  		t.AssertNil(err)
    94  		t.Assert(result, map[string]interface{}{
    95  			"url":      "https://goframe.org",
    96  			"server":   g.Slice{"120.168.117.21", "120.168.117.22"},
    97  			"pi":       3.14,
    98  			"hasChild": true,
    99  			"name":     "你好YAML",
   100  		})
   101  	})
   102  }
   103  
   104  func Test_DecodeError(t *testing.T) {
   105  	gtest.C(t, func(t *gtest.T) {
   106  		_, err := gyaml.Decode([]byte(yamlErr))
   107  		t.AssertNE(err, nil)
   108  
   109  		result := make(map[string]interface{})
   110  		err = gyaml.DecodeTo([]byte(yamlErr), &result)
   111  		t.AssertNE(err, nil)
   112  	})
   113  }
   114  
   115  func Test_DecodeMapToJson(t *testing.T) {
   116  	gtest.C(t, func(t *gtest.T) {
   117  		data := []byte(`
   118  m:
   119   k: v
   120      `)
   121  		v, err := gyaml.Decode(data)
   122  		t.AssertNil(err)
   123  		b, err := json.Marshal(v)
   124  		t.AssertNil(err)
   125  		t.Assert(b, `{"m":{"k":"v"}}`)
   126  	})
   127  }
   128  
   129  func Test_ToJson(t *testing.T) {
   130  	gtest.C(t, func(t *gtest.T) {
   131  		m := make(map[string]string)
   132  		m["yaml"] = yamlStr
   133  		res, err := gyaml.Encode(m)
   134  		if err != nil {
   135  			t.Errorf("encode failed. %v", err)
   136  			return
   137  		}
   138  
   139  		jsonyaml, err := gyaml.ToJson(res)
   140  		if err != nil {
   141  			t.Errorf("ToJson failed. %v", err)
   142  			return
   143  		}
   144  
   145  		p := gjson.New(res)
   146  		if err != nil {
   147  			t.Errorf("parser failed. %v", err)
   148  			return
   149  		}
   150  		expectJson, err := p.ToJson()
   151  		if err != nil {
   152  			t.Errorf("parser ToJson failed. %v", err)
   153  			return
   154  		}
   155  		t.Assert(jsonyaml, expectJson)
   156  	})
   157  
   158  	gtest.C(t, func(t *gtest.T) {
   159  		_, err := gyaml.ToJson([]byte(yamlErr))
   160  		if err == nil {
   161  			t.Errorf("ToJson failed. %v", err)
   162  			return
   163  		}
   164  	})
   165  }