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