flagger/flagger_test.go

277 lines
6.9 KiB
Go
Raw Normal View History

2017-08-20 14:59:51 +05:00
// Flagger - arbitrary CLI flags parser.
//
// Copyright (c) 2017-2019, Stanislav N. aka pztrn.
2017-08-20 14:59:51 +05:00
//
2018-03-23 08:14:25 +05:00
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
2017-08-20 14:59:51 +05:00
//
2018-03-23 08:14:25 +05:00
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
2017-08-20 14:59:51 +05:00
//
2018-03-23 08:14:25 +05:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2017-08-20 14:59:51 +05:00
package flagger
import (
2018-03-23 08:14:25 +05:00
// stdlib
"log"
"os"
"testing"
2017-08-20 14:59:51 +05:00
// other
"github.com/stretchr/testify/require"
2017-08-20 14:59:51 +05:00
)
func TestFlaggerInitialization(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
2018-03-23 08:14:25 +05:00
f.Initialize()
2017-08-20 14:59:51 +05:00
}
func TestFlaggerInitializationWithNilLogger(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", nil)
require.NotNil(t, f)
f.Initialize()
}
2017-08-20 14:59:51 +05:00
func TestFlaggerAddBoolFlag(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestBool := Flag{
Name: "testboolflag",
Description: "Testing boolean flag",
Type: "bool",
DefaultValue: true,
}
err := f.AddFlag(&flagTestBool)
require.Nil(t, err)
}
func TestFlaggerAddSameBoolVar(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestBool := Flag{
2018-03-23 08:14:25 +05:00
Name: "testboolflag",
Description: "Testing boolean flag",
Type: "bool",
DefaultValue: true,
}
err := f.AddFlag(&flagTestBool)
require.Nil(t, err)
flagTestBool1 := Flag{
Name: "testboolflag",
Description: "Testing boolean flag",
Type: "bool",
DefaultValue: true,
2018-03-23 08:14:25 +05:00
}
err1 := f.AddFlag(&flagTestBool1)
require.NotNil(t, err1)
2017-08-20 14:59:51 +05:00
}
func TestFlaggerAddIntFlag(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestInt := Flag{
2018-03-23 08:14:25 +05:00
Name: "testintflag",
Description: "Testing integer flag",
Type: "int",
DefaultValue: 1,
}
err := f.AddFlag(&flagTestInt)
require.Nil(t, err)
2017-08-20 14:59:51 +05:00
}
func TestFlaggerAddStringFlag(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestString := Flag{
2018-03-23 08:14:25 +05:00
Name: "teststringflag",
Description: "Testing string flag",
Type: "string",
DefaultValue: "superstring",
}
err := f.AddFlag(&flagTestString)
require.Nil(t, err)
}
func TestFlaggerParse(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestString := Flag{
Name: "teststringflag",
Description: "Testing string flag",
Type: "string",
DefaultValue: "superstring",
2018-03-23 08:14:25 +05:00
}
err := f.AddFlag(&flagTestString)
require.Nil(t, err)
f.Parse()
2017-08-20 14:59:51 +05:00
}
func TestFlaggerParseAndReparse(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestString := Flag{
Name: "teststringflag",
Description: "Testing string flag",
Type: "string",
DefaultValue: "superstring",
}
err := f.AddFlag(&flagTestString)
require.Nil(t, err)
f.Parse()
2018-03-23 08:14:25 +05:00
f.Parse()
2017-08-20 14:59:51 +05:00
}
func TestFlaggerGetBoolFlag(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestBool := Flag{
Name: "testboolflag",
Description: "Testing boolean flag",
Type: "bool",
DefaultValue: true,
2018-03-23 08:14:25 +05:00
}
err := f.AddFlag(&flagTestBool)
require.Nil(t, err)
f.Parse()
val, err := f.GetBoolValue("testboolflag")
require.Nil(t, err)
require.True(t, val)
}
func TestFlaggerGetUnknownBoolFlag(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
2017-08-20 14:59:51 +05:00
flagTestBool := Flag{
Name: "testboolflag",
Description: "Testing boolean flag",
Type: "bool",
DefaultValue: true,
2018-03-23 08:14:25 +05:00
}
err := f.AddFlag(&flagTestBool)
require.Nil(t, err)
f.Parse()
val, err := f.GetBoolValue("unknownboolflag")
require.NotNil(t, err)
require.False(t, val)
2017-08-20 14:59:51 +05:00
}
func TestFlaggerGetIntFlag(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestInt := Flag{
Name: "testintflag",
Description: "Testing integer flag",
Type: "int",
DefaultValue: 1,
2018-03-23 08:14:25 +05:00
}
err := f.AddFlag(&flagTestInt)
require.Nil(t, err)
f.Parse()
2017-08-20 14:59:51 +05:00
val, err := f.GetIntValue("testintflag")
require.Nil(t, err)
require.NotEqual(t, 0, val)
}
func TestFlaggerGetUnknownIntFlag(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestInt := Flag{
Name: "testintflag",
Description: "Testing integer flag",
Type: "int",
DefaultValue: 1,
2018-03-23 08:14:25 +05:00
}
err := f.AddFlag(&flagTestInt)
require.Nil(t, err)
f.Parse()
val, err := f.GetIntValue("unknownintflag")
require.NotNil(t, err)
require.Equal(t, 0, val)
2017-08-20 14:59:51 +05:00
}
func TestFlaggerGetStringFlag(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestString := Flag{
Name: "teststringflag",
Description: "Testing string flag",
Type: "string",
DefaultValue: "superstring",
2018-03-23 08:14:25 +05:00
}
err := f.AddFlag(&flagTestString)
require.Nil(t, err)
2017-08-20 14:59:51 +05:00
f.Parse()
val, err := f.GetStringValue("teststringflag")
require.Nil(t, err)
require.NotEqual(t, "", val)
2018-12-04 15:50:01 +05:00
require.Equal(t, "superstring", val)
}
func TestFlaggerGetUnknownStringFlag(t *testing.T) {
2018-12-18 04:04:57 +05:00
f := New("tests", LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
require.NotNil(t, f)
f.Initialize()
flagTestString := Flag{
Name: "teststringflag",
Description: "Testing string flag",
Type: "string",
DefaultValue: "superstring",
2018-03-23 08:14:25 +05:00
}
err := f.AddFlag(&flagTestString)
require.Nil(t, err)
f.Parse()
val, err := f.GetStringValue("unknownstringflag")
require.NotNil(t, err)
require.Equal(t, "", val)
2017-08-20 14:59:51 +05:00
}