Ejemplo n.º 1
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)
		}
	})
}
Ejemplo n.º 2
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)
		}
	})
}