Ejemplo n.º 1
0
func TestTrue(t *testing.T) {
	t.Run("should not end testing if the condition is truthy", func(t *testing.T) {
		tester := new(mockTester)

		assert.True(tester, true, "")

		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 condition is falsey", func(t *testing.T) {
		tester := new(mockTester)

		assert.True(tester, false, "")

		if tester.errorfCalls != 1 {
			t.Errorf(
				"Expected Errorf to have been called once, it had been called %d times.",
				tester.errorfCalls,
			)
		}
	})
}
Ejemplo n.º 2
0
func TestDescribeArguments(t *testing.T) {
	t.Run("should include a title", func(t *testing.T) {
		result := parameters.DescribeArguments([]parameters.Argument{})

		assert.True(t, strings.Contains(result, "ARGUMENTS:"), "Expected a title.")
	})

	t.Run("should include argument names", func(t *testing.T) {
		result := parameters.DescribeArguments([]parameters.Argument{
			{
				Name: "TEST_ARG",
			},
		})

		assert.True(t, strings.Contains(result, "TEST_ARG"), "Expected argument name in result.")
	})

	t.Run("should handle multiple arguments", func(t *testing.T) {
		result := parameters.DescribeArguments([]parameters.Argument{
			{
				Name: "TEST_ARG1",
			},
			{
				Name: "TEST_ARG2",
			},
		})

		assert.True(t, strings.Contains(result, "TEST_ARG1"), "Expected argument name in result.")
		assert.True(t, strings.Contains(result, "TEST_ARG2"), "Expected argument name in result.")
	})

	t.Run("should sort arguments into alphabetical order", func(t *testing.T) {
		result := parameters.DescribeArguments([]parameters.Argument{
			{
				Name: "FOO",
			},
			{
				Name: "BAR",
			},
		})

		fooIdx := strings.Index(result, "FOO")
		barIdx := strings.Index(result, "BAR")

		assert.True(t, fooIdx > barIdx, "Expected FOO to come after BAR.")
	})
}
Ejemplo n.º 3
0
func TestDescribeCommands(t *testing.T) {
	t.Run("should include a title", func(t *testing.T) {
		result := console.DescribeCommands([]console.Command{})

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

	t.Run("should show all command names", func(t *testing.T) {
		result := console.DescribeCommands([]console.Command{
			{
				Name: "foo-cmd",
			},
			{
				Name: "bar-cmd",
			},
		})

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

	t.Run("should show all command descriptions", func(t *testing.T) {
		fooCmdDesc := "The foo-cmd description is this."
		barCmdDesc := "An alternative command description for bar-cmd."

		result := console.DescribeCommands([]console.Command{
			{
				Name:        "foo-cmd",
				Description: fooCmdDesc,
			},
			{
				Name:        "bar-cmd",
				Description: barCmdDesc,
			},
		})

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

	t.Run("should show the commands in alphabetical order", func(t *testing.T) {
		result := console.DescribeCommands([]console.Command{
			{
				Name: "foo-cmd",
			},
			{
				Name: "bar-cmd",
			},
		})

		fooIdx := strings.Index(result, "foo-cmd")
		barIdx := strings.Index(result, "bar-cmd")

		assert.True(t, fooIdx > barIdx, "Expected foo-cmd to come after bar-cmd.")
	})
}
Ejemplo n.º 4
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.")
	})
}
Ejemplo n.º 5
0
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.")
	})
}
Ejemplo n.º 6
0
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)
	})
}
Ejemplo n.º 7
0
func TestNewOutput(t *testing.T) {
	output := console.NewOutput(&bytes.Buffer{})

	assert.True(t, output != nil, "Output should not be nil")
}
Ejemplo n.º 8
0
func TestNewScanner(t *testing.T) {
	scanner := specification.NewScanner(strings.NewReader(""))
	assert.True(t, scanner != nil, "Scanner should not be nil")
}
Ejemplo n.º 9
0
func TestDescribeOptions(t *testing.T) {
	t.Run("should include a title", func(t *testing.T) {
		result := parameters.DescribeOptions([]parameters.Option{})

		assert.True(t, strings.Contains(result, "OPTIONS:"), "Expected a title.")
	})

	t.Run("should include option names", func(t *testing.T) {
		result := parameters.DescribeOptions([]parameters.Option{
			{
				Names: []string{
					"t",
					"test",
				},
			},
		})

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

	t.Run("should handle multiple arguments", func(t *testing.T) {
		result := parameters.DescribeOptions([]parameters.Option{
			{
				Names: []string{
					"f",
					"foo",
				},
			},
			{
				Names: []string{
					"b",
					"bar",
				},
			},
		})

		assert.True(t, strings.Contains(result, "-f"), "Expected option name in result.")
		assert.True(t, strings.Contains(result, "--foo"), "Expected option name in result.")
		assert.True(t, strings.Contains(result, "-b"), "Expected option name in result.")
		assert.True(t, strings.Contains(result, "--bar"), "Expected option name in result.")
	})

	t.Run("should sort arguments into alphabetical order", func(t *testing.T) {
		result := parameters.DescribeOptions([]parameters.Option{
			{
				Names: []string{
					"f",
					"foo",
				},
			},
			{
				Names: []string{
					"b",
					"bar",
				},
			},
		})

		fooIdx := strings.Index(result, "--foo")
		barIdx := strings.Index(result, "--bar")

		assert.True(t, fooIdx > barIdx, "Expected FOO to come after BAR.")
	})

	t.Run("should show value names", func(t *testing.T) {
		result := parameters.DescribeOptions([]parameters.Option{
			{
				Names:     []string{"foo"},
				ValueMode: parameters.OptionValueRequired,
				ValueName: "FOO_NAME",
			},
		})

		assert.True(t, strings.Contains(result, "FOO_NAME"), "Expected value name in output.")
	})

	t.Run("should show value names as optional if they are", func(t *testing.T) {
		result := parameters.DescribeOptions([]parameters.Option{
			{
				Names:     []string{"foo"},
				ValueMode: parameters.OptionValueOptional,
				ValueName: "FOO_NAME",
			},
		})

		assert.True(t, strings.Contains(result, "[=FOO_NAME]"), "Expected value name in output.")
	})

	t.Run("should sort short options before long options names", func(t *testing.T) {
		result := parameters.DescribeOptions([]parameters.Option{
			{
				Names: []string{
					"foo",
					"f",
				},
			},
		})

		fooIdx := strings.Index(result, "--foo")
		fIdx := strings.Index(result, "-f")

		assert.True(t, fooIdx > fIdx, "Expected --foo to come after -f.")
	})

	t.Run("should sort short options into alphabetical order", func(t *testing.T) {
		result := parameters.DescribeOptions([]parameters.Option{
			{
				Names: []string{
					"f",
					"b",
				},
			},
		})

		bIdx := strings.Index(result, "-b")
		fIdx := strings.Index(result, "-f")

		assert.True(t, fIdx > bIdx, "Expected -f to come after -b.")
	})
}
Ejemplo n.º 10
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))
	})
}
Ejemplo n.º 11
0
func TestNewApplication(t *testing.T) {
	application := console.NewApplication("eidolon/console", "1.2.3+testing")
	assert.True(t, application != nil, "Application should not be nil")
}
Ejemplo n.º 12
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()))
		})
	})
}
Ejemplo n.º 13
0
func TestNewDefinition(t *testing.T) {
	definition := console.NewDefinition()
	assert.True(t, definition != nil, "Definition should not be nil")
}