2020-08-21 18:36:08 +00:00
|
|
|
package flags
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2022-02-22 16:27:32 +00:00
|
|
|
"io"
|
2020-08-21 18:36:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-12-01 08:40:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-08-21 18:36:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-07-09 18:24:39 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-08-21 18:36:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFixed8_String(t *testing.T) {
|
2020-12-01 08:40:58 +00:00
|
|
|
value := fixedn.Fixed8(123)
|
2020-08-21 18:36:08 +00:00
|
|
|
f := Fixed8{
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, "0.00000123", f.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8_Set(t *testing.T) {
|
2020-12-01 08:40:58 +00:00
|
|
|
value := fixedn.Fixed8(123)
|
2020-08-21 18:36:08 +00:00
|
|
|
f := Fixed8{}
|
|
|
|
|
|
|
|
require.Error(t, f.Set("not-a-fixed8"))
|
|
|
|
|
|
|
|
require.NoError(t, f.Set("0.00000123"))
|
|
|
|
require.Equal(t, value, f.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8_Fixed8(t *testing.T) {
|
|
|
|
f := Fixed8{
|
2020-12-01 08:40:58 +00:00
|
|
|
Value: fixedn.Fixed8(123),
|
2020-08-21 18:36:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 08:40:58 +00:00
|
|
|
require.Equal(t, fixedn.Fixed8(123), f.Fixed8())
|
2020-08-21 18:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8Flag_String(t *testing.T) {
|
|
|
|
flag := Fixed8Flag{
|
|
|
|
Name: "myFlag",
|
|
|
|
Usage: "Gas amount",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, "--myFlag value\tGas amount", flag.String())
|
|
|
|
}
|
|
|
|
|
2024-07-09 18:24:39 +00:00
|
|
|
func TestFixed8Flag_Names(t *testing.T) {
|
2020-08-21 18:36:08 +00:00
|
|
|
flag := Fixed8Flag{
|
|
|
|
Name: "myFlag",
|
|
|
|
}
|
|
|
|
|
2024-07-09 18:24:39 +00:00
|
|
|
require.Equal(t, []string{"myFlag"}, flag.Names())
|
2020-08-21 18:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8(t *testing.T) {
|
|
|
|
f := flag.NewFlagSet("", flag.ContinueOnError)
|
2022-02-22 16:27:32 +00:00
|
|
|
f.SetOutput(io.Discard) // don't pollute test output
|
2024-07-09 18:24:39 +00:00
|
|
|
gas := Fixed8Flag{Name: "gas", Aliases: []string{"g"}, Usage: "Gas amount", Value: Fixed8{Value: 0}, Required: true, Hidden: false, Action: nil}
|
|
|
|
err := gas.Apply(f)
|
|
|
|
require.NoError(t, err)
|
2020-08-21 18:36:08 +00:00
|
|
|
require.NoError(t, f.Parse([]string{"--gas", "0.123"}))
|
|
|
|
require.Equal(t, "0.123", f.Lookup("g").Value.String())
|
|
|
|
require.NoError(t, f.Parse([]string{"-g", "0.456"}))
|
|
|
|
require.Equal(t, "0.456", f.Lookup("g").Value.String())
|
|
|
|
require.Error(t, f.Parse([]string{"--gas", "kek"}))
|
|
|
|
}
|
2024-07-09 18:24:39 +00:00
|
|
|
|
|
|
|
func TestFixed8Flag_Get(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", flag.ContinueOnError)
|
|
|
|
ctx := cli.NewContext(app, set, nil)
|
|
|
|
flag := Fixed8Flag{
|
|
|
|
Name: "testFlag",
|
|
|
|
}
|
|
|
|
fixedFlag := Fixed8{Value: fixedn.Fixed8(123)}
|
|
|
|
set.Var(&fixedFlag, "testFlag", "test usage")
|
|
|
|
require.NoError(t, set.Set("testFlag", "0.00000321"))
|
|
|
|
expected := flag.Get(ctx)
|
|
|
|
require.Equal(t, fixedn.Fixed8(321), expected.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8Flag_GetValue(t *testing.T) {
|
|
|
|
f := Fixed8Flag{Value: Fixed8{Value: fixedn.Fixed8(123)}}
|
|
|
|
require.Equal(t, "0.00000123", f.GetValue())
|
|
|
|
require.True(t, f.TakesValue())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8Flag_RunAction(t *testing.T) {
|
|
|
|
called := false
|
|
|
|
action := func(ctx *cli.Context, s string) error {
|
|
|
|
called = true
|
|
|
|
require.Equal(t, "0.00000123", s)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", flag.ContinueOnError)
|
|
|
|
ctx := cli.NewContext(app, set, nil)
|
|
|
|
f := Fixed8Flag{
|
|
|
|
Action: action,
|
|
|
|
Value: Fixed8{Value: fixedn.Fixed8(123)},
|
|
|
|
}
|
|
|
|
err := f.RunAction(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, called)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8Flag_GetUsage(t *testing.T) {
|
|
|
|
f := Fixed8Flag{Usage: "Use this flag to specify gas amount"}
|
|
|
|
require.Equal(t, "Use this flag to specify gas amount", f.GetUsage())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8Flag_IsVisible(t *testing.T) {
|
|
|
|
f := Fixed8Flag{Hidden: false}
|
|
|
|
require.True(t, f.IsVisible())
|
|
|
|
|
|
|
|
f.Hidden = true
|
|
|
|
require.False(t, f.IsVisible())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixed8Flag_IsRequired(t *testing.T) {
|
|
|
|
f := Fixed8Flag{Required: false}
|
|
|
|
require.False(t, f.IsRequired())
|
|
|
|
|
|
|
|
f.Required = true
|
|
|
|
require.True(t, f.IsRequired())
|
|
|
|
}
|