Exemple #1
0
func TestNotOK(t *testing.T) {
	t.Run("should not end testing if the given error is not nil", func(t *testing.T) {
		tester := new(mockTester)

		assert.NotOK(tester, errors.New("Testing"))

		if tester.errorfCalls != 0 {
			t.Errorf(
				"Expected Errorf not to have been called, it had been called %d times.",
				tester.errorfCalls,
			)
		}
	})

	t.Run("should end testing if the given error is nil", func(t *testing.T) {
		tester := new(mockTester)

		assert.NotOK(tester, nil)

		if tester.errorfCalls != 1 {
			t.Errorf(
				"Expected Errorf to have been called once, it had been called %d times.",
				tester.errorfCalls,
			)
		}
	})
}
func TestParseArgumentSpecification(t *testing.T) {
	t.Run("should set the name", func(t *testing.T) {
		argument, err := specification.ParseArgumentSpecification("GALAXY_QUEST")
		assert.OK(t, err)
		assert.Equal(t, "GALAXY_QUEST", argument.Name)
	})

	t.Run("should set whether or not the argument is required", func(t *testing.T) {
		argument, err := specification.ParseArgumentSpecification("GALAXY_QUEST")
		assert.OK(t, err)
		assert.Equal(t, true, argument.Required)

		argument, err = specification.ParseArgumentSpecification("[MEMENTO]")
		assert.OK(t, err)
		assert.Equal(t, false, argument.Required)
	})

	t.Run("should expect a close bracket if an opening one is given", func(t *testing.T) {
		_, err := specification.ParseArgumentSpecification("[MEMENTO")
		assert.NotOK(t, err)
	})

	t.Run("should not expect any whitespace", func(t *testing.T) {
		_, err := specification.ParseArgumentSpecification("GALAXY QUEST")
		assert.NotOK(t, err)
	})

	t.Run("should always expect an identifier", func(t *testing.T) {
		_, err := specification.ParseArgumentSpecification("")
		assert.NotOK(t, err)

		_, err = specification.ParseArgumentSpecification("[]")
		assert.NotOK(t, err)
	})
}
func TestParseOptionSpecification(t *testing.T) {
	t.Run("should set the name(s)", func(t *testing.T) {
		option, err := specification.ParseOptionSpecification("--galaxy-quest")
		assert.OK(t, err)
		assert.True(t, len(option.Names) == 1, "Expected an option name")

		name := option.Names[0]
		assert.Equal(t, "galaxy-quest", name)

		option, err = specification.ParseOptionSpecification("-g, --galaxy-quest")
		assert.OK(t, err)
		assert.True(t, len(option.Names) == 2, "Expected 2 option names")

		shortName := option.Names[0]
		longName := option.Names[1]
		assert.Equal(t, "g", shortName)
		assert.Equal(t, "galaxy-quest", longName)

		option, err = specification.ParseOptionSpecification("-a, -b, -c, --dee, --ee, --eff")
		assert.OK(t, err)
		assert.True(t, len(option.Names) == 6, "Expected 6 option names")
	})

	t.Run("should allow no value mode to be set", func(t *testing.T) {
		option, err := specification.ParseOptionSpecification("--galaxy-quest")
		assert.OK(t, err)
		assert.Equal(t, parameters.OptionValueNone, option.ValueMode)
	})

	t.Run("should allow option values", func(t *testing.T) {
		option, err := specification.ParseOptionSpecification("--galaxy-quest-2[=ALAN_RICKMAN]")
		assert.OK(t, err)
		assert.Equal(t, parameters.OptionValueOptional, option.ValueMode)
	})

	t.Run("should allow required values", func(t *testing.T) {
		option, err := specification.ParseOptionSpecification("--galaxy-quest=ALAN_RICKMAN")
		assert.OK(t, err)
		assert.Equal(t, parameters.OptionValueRequired, option.ValueMode)
	})

	t.Run("should error when given an invalid long option name", func(t *testing.T) {
		_, err := specification.ParseOptionSpecification("--$$$")
		assert.NotOK(t, err)
	})

	t.Run("should error when given an invalid short option name", func(t *testing.T) {
		_, err := specification.ParseOptionSpecification("-$")
		assert.NotOK(t, err)
	})

	t.Run("should error when given an short option name that's too long", func(t *testing.T) {
		_, err := specification.ParseOptionSpecification("-abc")
		assert.NotOK(t, err)
	})

	t.Run("should error when given an invalid specification", func(t *testing.T) {
		_, err := specification.ParseOptionSpecification("abc")
		assert.NotOK(t, err)
	})

	t.Run("should error if no identifier is given for the value name", func(t *testing.T) {
		_, err := specification.ParseOptionSpecification("--galaxy-quest=")
		assert.NotOK(t, err)

		_, err = specification.ParseOptionSpecification("--galaxy-quest[=]")
		assert.NotOK(t, err)
	})

	t.Run("should expect EQUALS if an LBRACK is found", func(t *testing.T) {
		_, err := specification.ParseOptionSpecification("--galaxy-quest[]")
		assert.NotOK(t, err)

		_, err = specification.ParseOptionSpecification("--galaxy-quest[")
		assert.NotOK(t, err)
	})

	t.Run("should expect an RBRACK if an LBRACK is given", func(t *testing.T) {
		_, err := specification.ParseOptionSpecification("--galaxy-quest-2[=ALAN_RICKMAN")
		assert.NotOK(t, err)
	})

	t.Run("should expect EOF after value identifier is no LBRACK is given", func(t *testing.T) {
		_, err := specification.ParseOptionSpecification("--galaxy-quest-2=ALAN_RICKMAN]")
		assert.NotOK(t, err)
	})
}
func TestMapInput(t *testing.T) {
	createInput := func(params []string) *console.Input {
		return console.ParseInput(params)
	}

	t.Run("should map arguments to their reference values", func(t *testing.T) {
		var s1 string
		var s2 string

		assert.Equal(t, "", s1)
		assert.Equal(t, "", s2)

		input := createInput([]string{"hello", "world"})

		definition := console.NewDefinition()
		definition.AddArgument(parameters.NewStringValue(&s1), "S1", "")
		definition.AddArgument(parameters.NewStringValue(&s2), "S2", "")

		err := console.MapInput(definition, input)
		assert.OK(t, err)

		assert.Equal(t, "hello", s1)
		assert.Equal(t, "world", s2)
	})

	t.Run("should error parsing arguments with invalid values", func(t *testing.T) {
		var i1 int

		assert.Equal(t, 0, i1)

		input := createInput([]string{"foo"})

		definition := console.NewDefinition()
		definition.AddArgument(parameters.NewIntValue(&i1), "I1", "")

		err := console.MapInput(definition, input)
		assert.NotOK(t, err)
	})

	t.Run("should error when required arguments are missing from input", func(t *testing.T) {
		var s1 string
		var s2 string

		assert.Equal(t, "", s1)
		assert.Equal(t, "", s2)

		input := createInput([]string{"foo"})

		definition := console.NewDefinition()
		definition.AddArgument(parameters.NewStringValue(&s1), "S1", "")
		definition.AddArgument(parameters.NewStringValue(&s1), "S2", "")

		err := console.MapInput(definition, input)
		assert.NotOK(t, err)
	})

	t.Run("should not error when optional arguments are missing from input", func(t *testing.T) {
		var s1 string
		var s2 string

		assert.Equal(t, "", s1)
		assert.Equal(t, "", s2)

		input := createInput([]string{"foo"})

		definition := console.NewDefinition()
		definition.AddArgument(parameters.NewStringValue(&s1), "S1", "")
		definition.AddArgument(parameters.NewStringValue(&s1), "[S2]", "")

		err := console.MapInput(definition, input)
		assert.OK(t, err)

		assert.Equal(t, "foo", s1)
		assert.Equal(t, "", s2)
	})

	t.Run("should map short options to their reference values", func(t *testing.T) {
		var s1 string
		var s2 string

		assert.Equal(t, "", s1)
		assert.Equal(t, "", s2)

		input := createInput([]string{"-a=foo", "-b=bar"})

		definition := console.NewDefinition()
		definition.AddOption(parameters.NewStringValue(&s1), "-a=S1", "")
		definition.AddOption(parameters.NewStringValue(&s2), "-b=S2", "")

		err := console.MapInput(definition, input)
		assert.OK(t, err)

		assert.Equal(t, "foo", s1)
		assert.Equal(t, "bar", s2)
	})

	t.Run("should map long options to their reference values", func(t *testing.T) {
		var s1 string
		var s2 string

		assert.Equal(t, "", s1)
		assert.Equal(t, "", s2)

		input := createInput([]string{"--foo=bar", "--baz=qux"})

		definition := console.NewDefinition()
		definition.AddOption(parameters.NewStringValue(&s1), "--foo=S1", "")
		definition.AddOption(parameters.NewStringValue(&s2), "--baz=S2", "")

		err := console.MapInput(definition, input)
		assert.OK(t, err)

		assert.Equal(t, "bar", s1)
		assert.Equal(t, "qux", s2)
	})

	t.Run("should ignore options that don't exist in the definition", func(t *testing.T) {
		var s2 string

		input := createInput([]string{"--foo=bar"})

		definition := console.NewDefinition()
		definition.AddOption(parameters.NewStringValue(&s2), "--baz=S2", "")

		err := console.MapInput(definition, input)
		assert.OK(t, err)
	})

	t.Run("should error parsing an option that requires a value with no value", func(t *testing.T) {
		var s1 string

		input := createInput([]string{"--foo"})

		definition := console.NewDefinition()
		definition.AddOption(parameters.NewStringValue(&s1), "--foo=s1", "")

		err := console.MapInput(definition, input)
		assert.NotOK(t, err)
	})

	t.Run("should not error parsing an option that doesn't require a value", func(t *testing.T) {
		var s1 string

		input := createInput([]string{"--foo"})

		definition := console.NewDefinition()
		definition.AddOption(parameters.NewStringValue(&s1), "--foo=s1", "")

		err := console.MapInput(definition, input)
		assert.NotOK(t, err)
	})

	t.Run("should set flag option values where applicable", func(t *testing.T) {
		var b1 bool

		assert.Equal(t, false, b1)

		input := createInput([]string{"--foo"})

		definition := console.NewDefinition()
		definition.AddOption(parameters.NewBoolValue(&b1), "--foo", "")

		err := console.MapInput(definition, input)
		assert.OK(t, err)

		assert.Equal(t, true, b1)
	})

	t.Run("should error parsing options with invalid values", func(t *testing.T) {
		var i1 int

		assert.Equal(t, 0, i1)

		input := createInput([]string{"--foo=hello"})

		definition := console.NewDefinition()
		definition.AddOption(parameters.NewIntValue(&i1), "--foo=I1", "")

		err := console.MapInput(definition, input)
		assert.NotOK(t, err)
	})
}
Exemple #5
0
func TestIPValue(t *testing.T) {
	t.Run("NewIPValue()", func(t *testing.T) {
		ipRef := net.ParseIP("127.0.0.1")
		ipValue := parameters.NewIPValue(&ipRef)

		assert.Equal(t, "127.0.0.1", ipValue.String())
	})

	t.Run("Set()", func(t *testing.T) {
		t.Run("should not error for valid values", func(t *testing.T) {
			var value parameters.IPValue

			valid := []string{
				"127.0.0.1",
				"192.168.0.1",
				"10.0.0.1",
				"255.255.255.0",
				"8.8.8.8",
				"8.8.4.4",
			}

			for _, item := range valid {
				err := value.Set(item)
				assert.OK(t, err)
			}
		})

		t.Run("should error for invalid values", func(t *testing.T) {
			var value parameters.IPValue

			invalid := []string{
				"",
				"Not an IP adddress",
				"Hello, World!",
				"123 Fake Street",
				"127 0 0 1",
			}

			for _, item := range invalid {
				err := value.Set(item)
				assert.NotOK(t, err)
			}
		})

		t.Run("should modify the IP that it references", func(t *testing.T) {
			ref := net.ParseIP("127.0.0.1")
			value := parameters.NewIPValue(&ref)

			assert.Equal(t, value.String(), ref.String())

			value.Set("192.168.0.1")
			assert.Equal(t, value.String(), ref.String())

			value.Set("10.0.0.1")
			assert.Equal(t, value.String(), ref.String())
		})
	})

	t.Run("String()", func(t *testing.T) {
		inOut := map[string]string{
			"127.0.0.1":   "127.0.0.1",
			"192.168.0.1": "192.168.0.1",
			"10.0.0.1":    "10.0.0.1",
		}

		for in, expected := range inOut {
			value := new(parameters.IPValue)
			value.Set(in)

			actual := value.String()
			assert.Equal(t, expected, actual)
		}
	})
}
Exemple #6
0
func TestIntValue(t *testing.T) {
	t.Run("NewIntValue()", func(t *testing.T) {
		intRef := 3
		intValue := parameters.NewIntValue(&intRef)

		assert.Equal(t, "3", intValue.String())
	})

	t.Run("Set()", func(t *testing.T) {
		t.Run("should not error for valid values", func(t *testing.T) {
			var value parameters.Float64Value

			valid := []string{
				"3",
				"10",
				"25",
				"100000",
			}

			for _, item := range valid {
				err := value.Set(item)
				assert.OK(t, err)
			}
		})

		t.Run("should error for invalid values", func(t *testing.T) {
			var value parameters.IntValue

			invalid := []string{
				"",
				"Hello, World!",
				"Three point one four",
				"92233720368547758070",
			}

			for _, item := range invalid {
				err := value.Set(item)
				assert.NotOK(t, err)
			}
		})

		t.Run("should modify the int that it references", func(t *testing.T) {
			ref := 5
			value := parameters.NewIntValue(&ref)

			assert.Equal(t, 5, ref)

			value.Set("10")
			assert.Equal(t, 10, ref)

			value.Set("25")
			assert.Equal(t, 25, ref)
		})
	})

	t.Run("String()", func(t *testing.T) {
		inOut := map[string]string{
			"3":    "3",
			"10":   "10",
			"1000": "1000",
		}

		for in, expected := range inOut {
			value := new(parameters.IntValue)
			value.Set(in)

			actual := value.String()
			assert.Equal(t, expected, actual)
		}
	})
}
Exemple #7
0
func TestFloat64Value(t *testing.T) {
	t.Run("NewFloat64Value()", func(t *testing.T) {
		float := float64(3.14)
		floatValue := parameters.NewFloat64Value(&float)

		assert.Equal(t, "3.14", floatValue.String())
	})

	t.Run("Set()", func(t *testing.T) {
		t.Run("should not error for valid values", func(t *testing.T) {
			var value parameters.Float64Value

			valid := []string{
				"3",
				"3.1",
				"3.14",
				"314.159e-2",
			}

			for _, item := range valid {
				err := value.Set(item)
				assert.OK(t, err)
			}
		})

		t.Run("should error for invalid values", func(t *testing.T) {
			var value parameters.Float64Value

			invalid := []string{
				"",
				"Hello, World!",
				"Three point one four",
			}

			for _, item := range invalid {
				err := value.Set(item)
				assert.NotOK(t, err)
			}
		})

		t.Run("should modify the float64 that it references", func(t *testing.T) {
			ref := float64(3.14)
			value := parameters.NewFloat64Value(&ref)

			assert.Equal(t, float64(3.14), ref)

			value.Set("3.14159")
			assert.Equal(t, float64(3.14159), ref)

			value.Set("10")
			assert.Equal(t, float64(10), ref)
		})
	})

	t.Run("String()", func(t *testing.T) {
		inOut := map[string]string{
			"3":          "3",
			"3.14":       "3.14",
			"3.14159":    "3.14159",
			"314.159e-2": "3.14159",
		}

		for in, expected := range inOut {
			value := new(parameters.Float64Value)
			value.Set(in)

			actual := value.String()
			assert.Equal(t, expected, actual)
		}
	})
}
Exemple #8
0
func TestBoolValue(t *testing.T) {
	t.Run("NewBoolValue()", func(t *testing.T) {
		truthy := true
		falsey := false

		truthyValue := parameters.NewBoolValue(&truthy)
		falseyValue := parameters.NewBoolValue(&falsey)

		assert.Equal(t, truthyValue.String(), "true")
		assert.Equal(t, falseyValue.String(), "false")
	})

	t.Run("Set()", func(t *testing.T) {
		t.Run("should not error for valid values", func(t *testing.T) {
			var value parameters.BoolValue

			valid := []string{
				"1",
				"t",
				"T",
				"TRUE",
				"true",
				"True",
				"0",
				"f",
				"F",
				"FALSE",
				"false",
				"False",
			}

			for _, item := range valid {
				err := value.Set(item)
				assert.OK(t, err)
			}
		})

		t.Run("should error for invalid values", func(t *testing.T) {
			var value parameters.BoolValue

			invalid := []string{
				"",
				"yes",
				"no",
				"Y",
				"N",
			}

			for _, item := range invalid {
				err := value.Set(item)
				assert.NotOK(t, err)
			}
		})

		t.Run("should modify the bool that it references", func(t *testing.T) {
			ref := true
			value := parameters.NewBoolValue(&ref)

			assert.Equal(t, true, ref)

			value.Set("false")
			assert.Equal(t, false, ref)

			value.Set("true")
			assert.Equal(t, true, ref)
		})
	})

	t.Run("String()", func(t *testing.T) {
		inOut := map[string]string{
			"1":     "true",
			"t":     "true",
			"T":     "true",
			"TRUE":  "true",
			"true":  "true",
			"True":  "true",
			"0":     "false",
			"f":     "false",
			"F":     "false",
			"FALSE": "false",
			"false": "false",
			"False": "false",
		}

		for in, expected := range inOut {
			value := new(parameters.BoolValue)
			value.Set(in)

			actual := value.String()
			assert.Equal(t, expected, actual)
		}
	})

	t.Run("FlagValue()", func(t *testing.T) {
		var value parameters.BoolValue

		assert.Equal(t, "true", value.FlagValue())
	})
}
Exemple #9
0
func TestDurationValue(t *testing.T) {
	t.Run("NewDurationValue()", func(t *testing.T) {
		duration := time.Second
		durationValue := parameters.NewDurationValue(&duration)

		assert.Equal(t, "1s", durationValue.String())
	})

	t.Run("Set()", func(t *testing.T) {
		t.Run("should not error for valid values", func(t *testing.T) {
			var value parameters.DurationValue

			valid := []string{
				"5us",
				"999ns",
				"1s",
				"3m",
				"1h33m2s",
				"5h",
				"365h",
			}

			for _, item := range valid {
				err := value.Set(item)
				assert.OK(t, err)
			}
		})

		t.Run("should error for invalid values", func(t *testing.T) {
			var value parameters.DurationValue

			invalid := []string{
				"",
				"1d",
				"20y",
				"20 decades",
			}

			for _, item := range invalid {
				err := value.Set(item)
				assert.NotOK(t, err)
			}
		})

		t.Run("should modify the bool that it references", func(t *testing.T) {
			ref := time.Second
			value := parameters.NewDurationValue(&ref)

			assert.Equal(t, time.Second, ref)

			value.Set("1m")
			assert.Equal(t, time.Minute, ref)

			value.Set("1h")
			assert.Equal(t, ref, time.Hour)
		})
	})

	t.Run("String()", func(t *testing.T) {
		inOut := map[string]string{
			"1us": "1µs",
			"1ns": "1ns",
			"1s":  "1s",
			"1m":  "1m0s",
			"1h":  "1h0m0s",
			"5s":  "5s",
			"10h": "10h0m0s",
		}

		for in, expected := range inOut {
			value := new(parameters.DurationValue)
			value.Set(in)

			actual := value.String()
			assert.Equal(t, expected, actual)
		}
	})
}