github.com/mailgun/holster/v4@v4.20.0/setter/setter_test.go (about)

     1  /*
     2  Copyright 2017 Mailgun Technologies Inc
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package setter_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/mailgun/holster/v4/setter"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestIfEmpty(t *testing.T) {
    26  	var conf struct {
    27  		Foo string
    28  		Bar int
    29  	}
    30  	assert.Equal(t, "", conf.Foo)
    31  	assert.Equal(t, 0, conf.Bar)
    32  
    33  	// Should apply the default values
    34  	setter.SetDefault(&conf.Foo, "default")
    35  	setter.SetDefault(&conf.Bar, 200)
    36  
    37  	assert.Equal(t, "default", conf.Foo)
    38  	assert.Equal(t, 200, conf.Bar)
    39  
    40  	conf.Foo = "thrawn"
    41  	conf.Bar = 500
    42  
    43  	// Should NOT apply the default values
    44  	setter.SetDefault(&conf.Foo, "default")
    45  	setter.SetDefault(&conf.Bar, 200)
    46  
    47  	assert.Equal(t, "thrawn", conf.Foo)
    48  	assert.Equal(t, 500, conf.Bar)
    49  }
    50  
    51  func TestIfDefaultPrecedence(t *testing.T) {
    52  	var conf struct {
    53  		Foo string
    54  		Bar string
    55  	}
    56  	assert.Equal(t, "", conf.Foo)
    57  	assert.Equal(t, "", conf.Bar)
    58  
    59  	// Should use the final default value
    60  	envValue := ""
    61  	setter.SetDefault(&conf.Foo, envValue, "default")
    62  	assert.Equal(t, "default", conf.Foo)
    63  
    64  	// Should use envValue
    65  	envValue = "bar"
    66  	setter.SetDefault(&conf.Bar, envValue, "default")
    67  	assert.Equal(t, "bar", conf.Bar)
    68  }
    69  
    70  func TestIsEmpty(t *testing.T) {
    71  	var count64 int64
    72  	var thing string
    73  
    74  	// Should return true
    75  	assert.Equal(t, true, setter.IsZero(count64))
    76  	assert.Equal(t, true, setter.IsZero(thing))
    77  
    78  	thing = "thrawn"
    79  	count64 = int64(1)
    80  	assert.Equal(t, false, setter.IsZero(count64))
    81  	assert.Equal(t, false, setter.IsZero(thing))
    82  }
    83  
    84  func TestIfEmptyTypePanic(t *testing.T) {
    85  	defer func() {
    86  		if r := recover(); r != nil {
    87  			assert.Equal(t, "reflect.Set: value of type int is not assignable to type string", r)
    88  		}
    89  	}()
    90  
    91  	var thing string
    92  	// Should panic
    93  	setter.SetDefault(&thing, 1)
    94  	assert.Fail(t, "Should have caught panic")
    95  }
    96  
    97  func TestIfEmptyNonPtrPanic(t *testing.T) {
    98  	defer func() {
    99  		if r := recover(); r != nil {
   100  			assert.Equal(t, "setter.SetDefault: Expected first argument to be of type reflect.Ptr", r)
   101  		}
   102  	}()
   103  
   104  	var thing string
   105  	// Should panic
   106  	setter.SetDefault(thing, "thrawn")
   107  	assert.Fail(t, "Should have caught panic")
   108  }
   109  
   110  type MyInterface interface {
   111  	Thing() string
   112  }
   113  
   114  type MyImplementation struct{}
   115  
   116  func (s *MyImplementation) Thing() string {
   117  	return "thing"
   118  }
   119  
   120  func NewImplementation() MyInterface {
   121  	// Type and Value are not nil
   122  	var p *MyImplementation = nil
   123  	return p
   124  }
   125  
   126  type MyStruct struct {
   127  	T MyInterface
   128  }
   129  
   130  func NewMyStruct(t MyInterface) *MyStruct {
   131  	return &MyStruct{T: t}
   132  }
   133  
   134  func TestIsNil(t *testing.T) {
   135  	m := MyStruct{T: &MyImplementation{}}
   136  	assert.True(t, m.T != nil)
   137  	m.T = nil
   138  	assert.True(t, m.T == nil)
   139  
   140  	o := NewMyStruct(nil)
   141  	assert.True(t, o.T == nil)
   142  
   143  	thing := NewImplementation()
   144  	assert.False(t, thing == nil)
   145  	assert.True(t, setter.IsNil(thing))
   146  	assert.False(t, setter.IsNil(&MyImplementation{}))
   147  }