github.com/Anderson-Lu/gobox@v0.0.0-20191127065433-3e6c4c2da420/config/config_helper_test.go (about)

     1  package config
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type MyConfig struct {
     9  	Name   string  `yaml:"name"`
    10  	Value  string  `yaml:"value"`
    11  	Int    int     `yaml:"int"`
    12  	Double float64 `yaml:"double"`
    13  	T      T       `yaml:"t"`
    14  }
    15  
    16  type T struct {
    17  	Name string `yaml:"tname"`
    18  }
    19  
    20  func TestInitConfigFile(t *testing.T) {
    21  	var myconf = MyConfig{}
    22  
    23  	var expect = MyConfig{
    24  		Name:   "helloword",
    25  		Value:  "i am value",
    26  		Int:    1,
    27  		Double: 1.11,
    28  		T: T{
    29  			Name: "t",
    30  		},
    31  	}
    32  
    33  	type args struct {
    34  		path   string
    35  		format int
    36  		value  MyConfig
    37  	}
    38  	tests := []struct {
    39  		name    string
    40  		args    args
    41  		wantErr bool
    42  	}{
    43  		{
    44  			name: "测试加载json配置文件",
    45  			args: args{
    46  				path:   "./config_test.json",
    47  				format: CONFIG_JSON_FORMAT,
    48  				value:  myconf,
    49  			},
    50  			wantErr: false,
    51  		},
    52  		{
    53  			name: "测试加载yaml配置文件",
    54  			args: args{
    55  				path:   "./config_test.yaml",
    56  				format: CONFIG_YAML_FORMAT,
    57  				value:  myconf,
    58  			},
    59  		},
    60  	}
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			if err := InitConfigFile(tt.args.path, tt.args.format, &tt.args.value); (err != nil) != tt.wantErr {
    64  				t.Errorf("InitConfigFile() error = %v, wantErr %v", err, tt.wantErr)
    65  			}
    66  			if !reflect.DeepEqual(tt.args.value, expect) {
    67  				t.Errorf("InitConfigFile() error =%v, want = %v", tt.args.value, expect)
    68  			}
    69  		})
    70  	}
    71  }