Пример #1
0
func TestStringValue(t *testing.T) {
	t.Run("NewStringValue()", func(t *testing.T) {
		expected := "Hello, World!"
		actual := parameters.NewStringValue(&expected)

		assert.Equal(t, expected, actual.String())
	})

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

			valid := []string{
				"Hello",
				"World",
				"Hello, World!",
				"3.14",
				"http://www.google.co.uk/",
			}

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

		t.Run("should modify the string that it references", func(t *testing.T) {
			ref := "Hello"

			value := parameters.NewStringValue(&ref)
			assert.Equal(t, "Hello", ref)

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

	t.Run("String()", func(t *testing.T) {
		inOut := map[string]string{
			"Hello": "Hello",
			"World": "World",
			"hello": "hello",
			"world": "world",
		}

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

			actual := value.String()
			assert.Equal(t, expected, actual)
		}
	})
}
Пример #2
0
func main() {
	application := console.NewApplication("eidolon/console", "0.1.0")
	application.Logo = `
                                             #
                              ###            ##
######## ### ####### #######  ###   #######  ###  ##
         ###       ##      ## ###         ## #### ##
 ####### ###  ###  ## ##   ## ###    ##   ## #######
 ###     ###  ###  ## ##   ## ###    ##   ## ### ###
 ####### ###  ######   #####  ####### #####  ###  ##
                                                   #
`

	application.AddCommand(console.Command{
		Name:        "greet:example",
		Description: "Greet's the given user, or the world.",
		Help:        "You don't have to specify a name.",
		Configure: func(definition *console.Definition) {
			definition.AddOption(
				parameters.NewStringValue(&name),
				"-n, --name=NAME",
				"Provide a name for the greeting.",
			)

			definition.AddArgument(
				parameters.NewIntValue(&favNum),
				"FAVOURITE_NUMBER",
				"Provide your favourite number.",
			)
		},
		Execute: func(input *console.Input, output *console.Output) error {
			output.Printf("Hello, %s!\n", name)
			output.Printf("Your favourite number is %d.\n", favNum)
			return nil
		},
	})

	code := application.Run(os.Args[1:])

	os.Exit(code)
}
Пример #3
0
func TestDescribeCommand(t *testing.T) {
	t.Run("should return command usage information", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test",
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "USAGE:"), "Expected usage information.")
	})

	t.Run("should include the command name", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test-command-name",
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "test-command-name"), "Expected command name.")
	})

	t.Run("should show the command description", func(t *testing.T) {
		description := "This is the test-command-name description."

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name:        "test-command-name",
			Description: description,
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, description), "Expected command description.")
	})

	t.Run("should return command help if there is some", func(t *testing.T) {
		help := "This is some help for the test-command-name command."

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test-command-name",
			Help: help,
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "HELP:"), "Expected command help header.")
		assert.True(t, strings.Contains(result, help), "Expected command help.")
	})

	t.Run("should show arguments if there are any", func(t *testing.T) {
		var s1 string
		var s2 string

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test-command-name",
			Configure: func(definition *console.Definition) {
				definition.AddArgument(parameters.NewStringValue(&s1), "STRING_ARG_S1", "")
				definition.AddArgument(parameters.NewStringValue(&s2), "STRING_ARG_S2", "")
			},
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "STRING_ARG_S1"), "Expected argument name.")
		assert.True(t, strings.Contains(result, "STRING_ARG_S2"), "Expected argument name.")
	})

	t.Run("should show optional arguments wrapped in brackets", func(t *testing.T) {
		var s1 string
		var s2 string

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		command := console.Command{
			Name: "test-command-name",
			Configure: func(definition *console.Definition) {
				definition.AddArgument(parameters.NewStringValue(&s1), "[STRING_ARG_S1]", "")
				definition.AddArgument(parameters.NewStringValue(&s2), "[STRING_ARG_S2]", "")
			},
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "[STRING_ARG_S1]"), "Expected argument name.")
		assert.True(t, strings.Contains(result, "[STRING_ARG_S2]"), "Expected argument name.")
	})

	t.Run("should show that there are options if there are any", func(t *testing.T) {
		var s1 string
		var s2 string

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Configure = func(definition *console.Definition) {
			definition.AddOption(parameters.NewStringValue(&s1), "--s1=VALUE", "")
		}

		command := console.Command{
			Name: "test-command-name",
			Configure: func(definition *console.Definition) {
				definition.AddOption(parameters.NewStringValue(&s2), "--s2=VALUE", "")
			},
		}

		result := console.DescribeCommand(application, &command)

		assert.True(t, strings.Contains(result, "[OPTIONS...]"), "Expected options.")
	})
}
func TestDescribeApplication(t *testing.T) {
	t.Run("should show the application logo if there is one", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Logo = "Eidolon Console\n"

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, application.Logo), "Expected application logo.")
	})

	t.Run("should show the application name", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, application.Name), "Expected application name.")
	})

	t.Run("should show the application version", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, application.Version), "Expected application version.")
	})

	t.Run("should show the application usage", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.UsageName = "eidolon_console_binary"

		usage := application.UsageName + " COMMAND [OPTIONS...] [ARGUMENTS...]"

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, "USAGE:"), "Expected application usage title.")
		assert.True(t, strings.Contains(result, usage), "Expected application usage.")
	})

	t.Run("should show the application options if there are any", func(t *testing.T) {
		var s1 string

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Configure = func(definition *console.Definition) {
			definition.AddOption(parameters.NewStringValue(&s1), "--s1", "S1 option for testing.")
		}

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, "OPTIONS:"), "Expected application options title.")
		assert.True(t, strings.Contains(result, "-h"), "Expected application options.")
		assert.True(t, strings.Contains(result, "--help"), "Expected application options.")
		assert.True(t, strings.Contains(result, "--s1"), "Expected application options.")
	})

	t.Run("should show the application commands if there are any", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.AddCommands([]console.Command{
			{
				Name: "foo-cmd",
			},
			{
				Name: "bar-cmd",
			},
		})

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, "COMMANDS:"), "Expected application commands title.")
		assert.True(t, strings.Contains(result, "foo-cmd"), "Expected application commands.")
		assert.True(t, strings.Contains(result, "bar-cmd"), "Expected application commands.")

	})

	t.Run("should not show the commands title if there are no commands", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Logo = "Eidolon Console\n"

		result := console.DescribeApplication(application)

		assert.False(t, strings.Contains(result, "COMMANDS:"), "Expected no commands title.")
	})

	t.Run("should show the application help if there is any", func(t *testing.T) {
		help := "This is some application help right here. Lorem ipsum dolor sit amet."

		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Help = help

		result := console.DescribeApplication(application)

		assert.True(t, strings.Contains(result, "HELP:"), "Expected help title.")
		assert.True(t, strings.Contains(result, help), "Expected help.")
	})

	t.Run("should not show the help title if there is no application help", func(t *testing.T) {
		application := console.NewApplication("eidolon/console", "1.2.3+testing")
		application.Logo = "Eidolon Console\n"

		result := console.DescribeApplication(application)

		assert.False(t, strings.Contains(result, "HELP:"), "Expected no help title.")
	})
}
Пример #5
0
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)
	})
}
Пример #6
0
func TestApplication(t *testing.T) {
	createApplication := func(writer io.Writer) *console.Application {
		application := console.NewApplication("eidolon/console", "1.2.3.+testing")
		application.Writer = writer

		return application
	}

	createTestCommand := func(a *string, b *int) console.Command {
		return console.Command{
			Name: "test",
			Configure: func(definition *console.Definition) {
				definition.AddArgument(parameters.NewStringValue(a), "STRINGARG", "")
				definition.AddOption(parameters.NewIntValue(b), "--int-opt=VALUE", "")
			},
			Execute: func(input *console.Input, output *console.Output) error {
				output.Printf("STRINGARG = %s", *a)
				output.Printf("--int-opt = %v", *b)
				return nil
			},
		}
	}

	t.Run("Run()", func(t *testing.T) {
		t.Run("should return exit code 2 if no command was asked for", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			code := application.Run([]string{})

			assert.Equal(t, 100, code)
		})

		t.Run("should return exit code 2 if no command was found", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			code := application.Run([]string{"foo"})

			assert.Equal(t, 100, code)
		})

		t.Run("should return exit code 100 if the help flag is set", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			code := application.Run([]string{"--help"})

			assert.Equal(t, 100, code)
		})

		t.Run("should show application help if the help flag is set", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.Run([]string{"--help"})

			output := writer.String()
			containsUsage := strings.Contains(output, "USAGE:")
			containsArguments := strings.Contains(output, "ARGUMENTS:")
			containsOptions := strings.Contains(output, "OPTIONS:")
			containsHelp := containsUsage && containsOptions && !containsArguments

			assert.True(t, containsHelp, "Expected help output.")
		})

		t.Run("should show command help if the help flag is set when running a command", func(t *testing.T) {
			var a string
			var b int

			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.AddCommand(createTestCommand(&a, &b))
			application.Run([]string{"test", "--help"})

			output := writer.String()
			containsUsage := strings.Contains(output, "USAGE:")
			containsArguments := strings.Contains(output, "ARGUMENTS:")
			containsOptions := strings.Contains(output, "OPTIONS:")
			containsHelp := containsUsage && containsOptions && containsArguments

			assert.True(t, containsHelp, "Expected help output.")
		})

		t.Run("should return exit code 0 if a command was found, and ran OK", func(t *testing.T) {
			var a string
			var b int

			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.AddCommand(createTestCommand(&a, &b))

			code := application.Run([]string{"test", "aval", "--int-opt=384"})

			assert.Equal(t, 0, code)
		})

		t.Run("should return exit code 101 if mapping input fails", func(t *testing.T) {
			var a string
			var b int

			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.AddCommand(createTestCommand(&a, &b))

			code := application.Run([]string{"test", "aval", "--int-opt=hello"})

			assert.Equal(t, 101, code)
		})

		t.Run("should return exit code 102 if the command execution fails", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.AddCommand(console.Command{
				Name: "test",
				Execute: func(input *console.Input, output *console.Output) error {
					return errors.New("Testing errors")
				},
			})

			code := application.Run([]string{"test", "aval", "--int-opt=hello"})

			assert.Equal(t, 102, code)
		})

		t.Run("should configure the application definition", func(t *testing.T) {
			var a string
			var b int
			var foo string

			writer := bytes.Buffer{}
			application := createApplication(&writer)
			application.Configure = func(definition *console.Definition) {
				definition.AddOption(parameters.NewStringValue(&foo), "--foo=FOO", "")
			}

			application.AddCommand(createTestCommand(&a, &b))
			application.Run([]string{"test", "aval", "--foo=bar"})

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

	t.Run("AddCommands()", func(t *testing.T) {
		t.Run("should work when adding 1 command", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)

			assert.Equal(t, 0, len(application.Commands))

			application.AddCommands([]console.Command{
				{
					Name: "test1",
				},
			})

			assert.Equal(t, 1, len(application.Commands))
		})

		t.Run("should work when adding no commands", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)

			assert.Equal(t, 0, len(application.Commands))

			application.AddCommands([]console.Command{})

			assert.Equal(t, 0, len(application.Commands))
		})

		t.Run("should work when adding more than 1 command", func(t *testing.T) {
			writer := bytes.Buffer{}
			application := createApplication(&writer)

			assert.Equal(t, 0, len(application.Commands))

			application.AddCommands([]console.Command{
				{
					Name: "test1",
				},
				{
					Name: "test2",
				},
				{
					Name: "test3",
				},
			})

			assert.Equal(t, 3, len(application.Commands))
		})
	})

	t.Run("AddCommand()", func(t *testing.T) {
		writer := bytes.Buffer{}
		application := createApplication(&writer)

		assert.Equal(t, 0, len(application.Commands))

		application.AddCommand(console.Command{
			Name: "test1",
		})

		assert.Equal(t, 1, len(application.Commands))
	})
}
Пример #7
0
func TestDefinition(t *testing.T) {
	t.Run("Arguments()", func(t *testing.T) {
		t.Run("should return an empty slice if no arguments have been added", func(t *testing.T) {
			definition := console.NewDefinition()
			assert.True(t, len(definition.Arguments()) == 0, "Arguments() length should be 0")
		})

		t.Run("should return an ordered slice of arguments", func(t *testing.T) {
			var s1 string
			var s2 string
			var s3 string

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

			arguments := definition.Arguments()

			assert.True(t, len(arguments) == 3, "Arguments() length should be 3")
			assert.Equal(t, "S1", arguments[0].Name)
			assert.Equal(t, "S2", arguments[1].Name)
			assert.Equal(t, "S3", arguments[2].Name)
		})
	})

	t.Run("Options()", func(t *testing.T) {
		t.Run("should return an empty slice if no options have been added", func(t *testing.T) {
			definition := console.NewDefinition()
			assert.True(t, len(definition.Options()) == 0, "Options() length should be 0")
		})

		t.Run("should return a slice of options", func(t *testing.T) {
			var s1 string
			var s2 string
			var s3 string

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

			options := definition.Options()

			assert.Equal(t, 3, len(options))
		})
	})

	t.Run("AddArgument()", func(t *testing.T) {
		t.Run("should error if an invalid option specification is given", func(t *testing.T) {
			defer func() {
				r := recover()
				assert.False(t, r == nil, "We should be recovering from a panic.")
			}()

			var s1 string

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

		t.Run("should error if an argument with the same name exists", func(t *testing.T) {
			defer func() {
				r := recover()
				assert.False(t, r == nil, "We should be recovering from a panic.")
			}()

			var s1 string
			var s2 string

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

		t.Run("should add an argument", func(t *testing.T) {
			var s1 string

			definition := console.NewDefinition()
			assert.Equal(t, 0, len(definition.Arguments()))

			definition.AddArgument(parameters.NewStringValue(&s1), "S1", "")
			assert.Equal(t, 1, len(definition.Arguments()))
		})
	})

	t.Run("AddOption()", func(t *testing.T) {
		t.Run("should error if an invalid option specification is given", func(t *testing.T) {
			defer func() {
				r := recover()
				assert.False(t, r == nil, "We should be recovering from a panic.")
			}()

			var s1 string

			definition := console.NewDefinition()
			definition.AddOption(parameters.NewStringValue(&s1), "!!! S1", "")
		})

		t.Run("should error if an option with the same name exists", func(t *testing.T) {
			defer func() {
				r := recover()
				assert.False(t, r == nil, "We should be recovering from a panic.")
			}()

			var s1 string
			var s2 string

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

		t.Run("should add an option", func(t *testing.T) {
			var s1 string

			definition := console.NewDefinition()
			assert.Equal(t, 0, len(definition.Options()))

			definition.AddOption(parameters.NewStringValue(&s1), "--s1", "")
			assert.Equal(t, 1, len(definition.Options()))
		})
	})
}