1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 10:20:56 +00:00
Beego/core/config/base_config_test.go

73 lines
2.1 KiB
Go
Raw Normal View History

2020-08-19 14:08:29 +00:00
// Copyright 2020
//
// Licensed 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 config
import (
2020-08-26 03:46:22 +00:00
"context"
2020-08-19 14:08:29 +00:00
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBaseConfiger_DefaultBool(t *testing.T) {
bc := newBaseConfier("true")
2020-10-13 14:32:41 +00:00
assert.True(t, bc.DefaultBool("key1", false))
assert.True(t, bc.DefaultBool("key2", true))
2020-08-19 14:08:29 +00:00
}
func TestBaseConfiger_DefaultFloat(t *testing.T) {
bc := newBaseConfier("12.3")
2020-10-13 14:32:41 +00:00
assert.Equal(t, 12.3, bc.DefaultFloat("key1", 0.1))
assert.Equal(t, 0.1, bc.DefaultFloat("key2", 0.1))
2020-08-19 14:08:29 +00:00
}
func TestBaseConfiger_DefaultInt(t *testing.T) {
bc := newBaseConfier("10")
2020-10-13 14:32:41 +00:00
assert.Equal(t, 10, bc.DefaultInt("key1", 8))
assert.Equal(t, 8, bc.DefaultInt("key2", 8))
2020-08-19 14:08:29 +00:00
}
func TestBaseConfiger_DefaultInt64(t *testing.T) {
bc := newBaseConfier("64")
2020-10-13 14:32:41 +00:00
assert.Equal(t, int64(64), bc.DefaultInt64("key1", int64(8)))
assert.Equal(t, int64(8), bc.DefaultInt64("key2", int64(8)))
2020-08-19 14:08:29 +00:00
}
func TestBaseConfiger_DefaultString(t *testing.T) {
bc := newBaseConfier("Hello")
2020-10-13 14:32:41 +00:00
assert.Equal(t, "Hello", bc.DefaultString("key1", "world"))
assert.Equal(t, "world", bc.DefaultString("key2", "world"))
2020-08-19 14:08:29 +00:00
}
func TestBaseConfiger_DefaultStrings(t *testing.T) {
bc := newBaseConfier("Hello;world")
2020-10-13 14:32:41 +00:00
assert.Equal(t, []string{"Hello", "world"}, bc.DefaultStrings("key1", []string{"world"}))
assert.Equal(t, []string{"world"}, bc.DefaultStrings("key2", []string{"world"}))
2020-08-19 14:08:29 +00:00
}
func newBaseConfier(str1 string) *BaseConfiger {
return &BaseConfiger{
2020-08-26 03:46:22 +00:00
reader: func(ctx context.Context, key string) (string, error) {
2020-08-19 14:08:29 +00:00
if key == "key1" {
return str1, nil
} else {
return "", errors.New("mock error")
}
},
}
}