github.com/wangyougui/gf/v2@v2.6.5/os/gcfg/gcfg_z_unit_adapter_content_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  // go test *.go -bench=".*" -benchmem
     8  
     9  package gcfg_test
    10  
    11  import (
    12  	"testing"
    13  
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/os/gcfg"
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  )
    18  
    19  func TestAdapterContent_Available_Get_Data(t *testing.T) {
    20  	gtest.C(t, func(t *gtest.T) {
    21  		adapter, err := gcfg.NewAdapterContent()
    22  		t.AssertNil(err)
    23  		t.Assert(adapter.Available(ctx), false)
    24  	})
    25  	gtest.C(t, func(t *gtest.T) {
    26  		content := `{"a": 1, "b": 2, "c": {"d": 3}}`
    27  		adapter, err := gcfg.NewAdapterContent(content)
    28  		t.AssertNil(err)
    29  
    30  		c := gcfg.NewWithAdapter(adapter)
    31  		t.Assert(c.Available(ctx), true)
    32  		t.Assert(c.MustGet(ctx, "a"), 1)
    33  		t.Assert(c.MustGet(ctx, "b"), 2)
    34  		t.Assert(c.MustGet(ctx, "c.d"), 3)
    35  		t.Assert(c.MustGet(ctx, "d"), nil)
    36  		t.Assert(c.MustData(ctx), g.Map{
    37  			"a": 1,
    38  			"b": 2,
    39  			"c": g.Map{
    40  				"d": 3,
    41  			},
    42  		})
    43  	})
    44  }
    45  
    46  func TestAdapterContent_SetContent(t *testing.T) {
    47  	gtest.C(t, func(t *gtest.T) {
    48  		adapter, err := gcfg.NewAdapterContent()
    49  		t.AssertNil(err)
    50  		t.Assert(adapter.Available(ctx), false)
    51  
    52  		content := `{"a": 1, "b": 2, "c": {"d": 3}}`
    53  		err = adapter.SetContent(content)
    54  		t.AssertNil(err)
    55  		c := gcfg.NewWithAdapter(adapter)
    56  		t.Assert(c.Available(ctx), true)
    57  		t.Assert(c.MustGet(ctx, "a"), 1)
    58  		t.Assert(c.MustGet(ctx, "b"), 2)
    59  		t.Assert(c.MustGet(ctx, "c.d"), 3)
    60  		t.Assert(c.MustGet(ctx, "d"), nil)
    61  		t.Assert(c.MustData(ctx), g.Map{
    62  			"a": 1,
    63  			"b": 2,
    64  			"c": g.Map{
    65  				"d": 3,
    66  			},
    67  		})
    68  	})
    69  
    70  }