github.com/msales/pkg/v3@v3.24.0/mocks/mocks_test.go (about)

     1  package mocks_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/msales/pkg/v3/clix"
     8  	"github.com/msales/pkg/v3/mocks"
     9  	"github.com/stretchr/testify/assert"
    10  	"gopkg.in/urfave/cli.v1"
    11  )
    12  
    13  func TestInitContext(t *testing.T) {
    14  	args := map[string]string{
    15  		"foo": "bar",
    16  		"faz": "3",
    17  	}
    18  
    19  	flags := clix.Flags{
    20  		cli.StringFlag{
    21  			Name:   "foo",
    22  			EnvVar: "ENV_FOO",
    23  		},
    24  		cli.IntFlag{
    25  			Name:   "faz",
    26  			EnvVar: "ENV_FAZ",
    27  		},
    28  	}
    29  	ctx := mocks.InitContext(args, flags)
    30  	assert.Implements(t, (*context.Context)(nil), ctx)
    31  	assert.IsType(t, &clix.Context{}, ctx)
    32  }
    33  
    34  func TestInitContext_PanicsBadFlag(t *testing.T) {
    35  	args := map[string]string{
    36  		"foo": "bar",
    37  		"faz": "asdf",
    38  	}
    39  
    40  	flags := clix.Flags{
    41  		cli.StringFlag{
    42  			Name:   "foo",
    43  			EnvVar: "ENV_FOO",
    44  		},
    45  		cli.IntFlag{
    46  			Name:   "faz",
    47  			EnvVar: "ENV_FAZ",
    48  		},
    49  	}
    50  	assert.Panics(t, func() {
    51  		_ = mocks.InitContext(args, flags)
    52  	})
    53  }
    54  
    55  func TestInitContext_PanicsWrongLogLevel(t *testing.T) {
    56  	args := map[string]string{
    57  		"foo":             "bar",
    58  		"faz":             "4",
    59  		clix.FlagLogLevel: "not existing",
    60  	}
    61  
    62  	flags := clix.Flags{
    63  		cli.StringFlag{
    64  			Name:   "foo",
    65  			EnvVar: "ENV_FOO",
    66  		},
    67  		cli.IntFlag{
    68  			Name:   "faz",
    69  			EnvVar: "ENV_FAZ",
    70  		},
    71  	}.Merge(clix.CommonFlags)
    72  	assert.Panics(t, func() {
    73  		_ = mocks.InitContext(args, flags)
    74  	})
    75  }
    76  
    77  func TestLogger_Debug(t *testing.T) {
    78  	assert.NotPanics(t, func() {
    79  		l := new(mocks.Logger)
    80  		l.On("Debug", "test msg")
    81  		l.Debug("test msg")
    82  	})
    83  }
    84  
    85  func TestLogger_Info(t *testing.T) {
    86  	assert.NotPanics(t, func() {
    87  		l := new(mocks.Logger)
    88  		l.On("Info", "test msg")
    89  		l.Info("test msg")
    90  	})
    91  }
    92  
    93  func TestLogger_Error(t *testing.T) {
    94  	assert.NotPanics(t, func() {
    95  		l := new(mocks.Logger)
    96  		l.On("Error", "test msg")
    97  		l.Error("test msg")
    98  	})
    99  }