flagger/flagger_test.go

140 lines
3.5 KiB
Go
Raw Normal View History

2017-08-20 14:59:51 +05:00
// Flagger - arbitrary CLI flags parser.
//
2018-03-23 08:14:25 +05:00
// Copyright (c) 2017-2018, 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
)
var (
2018-03-23 08:14:25 +05:00
f *Flagger
2017-08-20 14:59:51 +05:00
)
func TestFlaggerInitialization(t *testing.T) {
2018-03-23 08:14:25 +05:00
f = New(LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
if f == nil {
t.Fatal("Logger initialization failed!")
t.FailNow()
}
f.Initialize()
2017-08-20 14:59:51 +05:00
}
func TestFlaggerInitializationWithNilLogger(t *testing.T) {
fl := New(nil)
if f == nil {
t.Fatal("Logger initialization failed!")
t.FailNow()
}
fl.Initialize()
}
2017-08-20 14:59:51 +05:00
func TestFlaggerAddBoolFlag(t *testing.T) {
flagTestBool := Flag{
2018-03-23 08:14:25 +05:00
Name: "testboolflag",
Description: "Testing boolean flag",
Type: "bool",
DefaultValue: true,
}
err := f.AddFlag(&flagTestBool)
2018-03-23 08:14:25 +05:00
if err != nil {
t.Fatal("Failed to add boolean flag!")
t.FailNow()
}
2017-08-20 14:59:51 +05:00
}
func TestFlaggerAddIntFlag(t *testing.T) {
flagTestInt := Flag{
2018-03-23 08:14:25 +05:00
Name: "testintflag",
Description: "Testing integer flag",
Type: "int",
DefaultValue: 1,
}
err := f.AddFlag(&flagTestInt)
2018-03-23 08:14:25 +05:00
if err != nil {
t.Fatal("Failed to add integer flag!")
t.FailNow()
}
2017-08-20 14:59:51 +05:00
}
func TestFlaggerAddStringFlag(t *testing.T) {
flagTestString := Flag{
2018-03-23 08:14:25 +05:00
Name: "teststringflag",
Description: "Testing string flag",
Type: "string",
DefaultValue: "superstring",
}
err := f.AddFlag(&flagTestString)
2018-03-23 08:14:25 +05:00
if err != nil {
t.Fatal("Failed to add string flag!")
t.FailNow()
}
2017-08-20 14:59:51 +05:00
}
// This test doing nothing more but launching flags parsing.
func TestFlaggerParse(t *testing.T) {
2018-03-23 08:14:25 +05:00
f.Parse()
2017-08-20 14:59:51 +05:00
}
func TestFlaggerGetBoolFlag(t *testing.T) {
2018-03-23 08:14:25 +05:00
val, err := f.GetBoolValue("testboolflag")
if err != nil {
t.Fatal("Failed to get boolean flag: " + err.Error())
t.FailNow()
}
2017-08-20 14:59:51 +05:00
2018-03-23 08:14:25 +05:00
if !val {
t.Fatal("Failed to get boolean flag - should be true, but false received")
t.FailNow()
}
2017-08-20 14:59:51 +05:00
}
func TestFlaggerGetIntFlag(t *testing.T) {
2018-03-23 08:14:25 +05:00
val, err := f.GetIntValue("testintflag")
if err != nil {
t.Fatal("Failed to get integer flag: " + err.Error())
t.FailNow()
}
2017-08-20 14:59:51 +05:00
2018-03-23 08:14:25 +05:00
if val == 0 {
t.Fatal("Failed to get integer flag - should be 1, but 0 received")
t.FailNow()
}
2017-08-20 14:59:51 +05:00
}
func TestFlaggerGetStringFlag(t *testing.T) {
2018-03-23 08:14:25 +05:00
val, err := f.GetStringValue("teststringflag")
if err != nil {
t.Fatal("Failed to get string flag: " + err.Error())
t.FailNow()
}
2017-08-20 14:59:51 +05:00
2018-03-23 08:14:25 +05:00
if val == "" {
t.Fatal("Failed to get string flag - should be 'superstring', but nothing received")
t.FailNow()
}
2017-08-20 14:59:51 +05:00
}