From ce4e4838d0f46be968f2865c2e3c7688cdec8108 Mon Sep 17 00:00:00 2001 From: Patrick <dreamlike.sky@foxmail.com> Date: Thu, 12 Mar 2020 12:55:24 +0800 Subject: [PATCH] add yaml unit test --- common/yaml/testdata/config.yml | 7 ++++++ common/yaml/yaml.go | 17 +++++++++++++++ common/yaml/yaml_test.go | 38 +++++++++++++++++++++++++++++++++ config/base_config_test.go | 12 ----------- 4 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 common/yaml/testdata/config.yml create mode 100644 common/yaml/yaml_test.go diff --git a/common/yaml/testdata/config.yml b/common/yaml/testdata/config.yml new file mode 100644 index 000000000..b5c2ca8ad --- /dev/null +++ b/common/yaml/testdata/config.yml @@ -0,0 +1,7 @@ + +intTest: 11 +booleanTest: false +strTest: "strTest" + +child: + strTest: "childStrTest" \ No newline at end of file diff --git a/common/yaml/yaml.go b/common/yaml/yaml.go index 6c0829bdb..1d8ac65e9 100644 --- a/common/yaml/yaml.go +++ b/common/yaml/yaml.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package yaml import ( diff --git a/common/yaml/yaml_test.go b/common/yaml/yaml_test.go new file mode 100644 index 000000000..8d0eefddc --- /dev/null +++ b/common/yaml/yaml_test.go @@ -0,0 +1,38 @@ +package yaml + +import ( + "path/filepath" + "testing" +) + +import ( + "github.com/stretchr/testify/assert" +) + +func TestUnmarshalYMLConfig(t *testing.T) { + conPath, err := filepath.Abs("./testdata/config.yml") + assert.NoError(t, err) + c := &Config{} + assert.NoError(t, UnmarshalYMLConfig(conPath, c)) + assert.Equal(t, "strTest", c.StrTest) + assert.Equal(t, 11, c.IntTest) + assert.Equal(t, false, c.BooleanTest) + assert.Equal(t, "childStrTest", c.ChildConfig.StrTest) +} + +func TestUnmarshalYMLConfig_Error(t *testing.T) { + c := &Config{} + assert.Error(t, UnmarshalYMLConfig("./testdata/config", c)) + assert.Error(t, UnmarshalYMLConfig("", c)) +} + +type Config struct { + StrTest string `yaml:"strTest" default:"default" json:"strTest,omitempty" property:"strTest"` + IntTest int `default:"109" yaml:"intTest" json:"intTest,omitempty" property:"intTest"` + BooleanTest bool `yaml:"booleanTest" default:"true" json:"booleanTest,omitempty"` + ChildConfig ChildConfig `yaml:"child" json:"child,omitempty"` +} + +type ChildConfig struct { + StrTest string `default:"strTest" default:"default" yaml:"strTest" json:"strTest,omitempty"` +} diff --git a/config/base_config_test.go b/config/base_config_test.go index 34a885241..d16b24209 100644 --- a/config/base_config_test.go +++ b/config/base_config_test.go @@ -18,7 +18,6 @@ package config import ( "fmt" - "path/filepath" "reflect" "testing" ) @@ -28,7 +27,6 @@ import ( import ( "github.com/apache/dubbo-go/common/config" "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/common/yaml" "github.com/apache/dubbo-go/config_center" _ "github.com/apache/dubbo-go/config_center/apollo" ) @@ -519,13 +517,3 @@ func Test_initializeStruct(t *testing.T) { return consumerConfig.References != nil }) } - -func TestUnmarshalYMLConfig(t *testing.T) { - conPath, err := filepath.Abs("./testdata/consumer_config_with_configcenter.yml") - assert.NoError(t, err) - c := &ConsumerConfig{} - assert.NoError(t, yaml.UnmarshalYMLConfig(conPath, c)) - assert.Equal(t, "default", c.ProxyFactory) - assert.Equal(t, "dubbo.properties", c.ConfigCenterConfig.ConfigFile) - assert.Equal(t, "100ms", c.Connect_Timeout) -} -- GitLab