예제 #1
4
func TestFalse(t *testing.T) {
	t.Run("should not end testing if the condition is falsey", func(t *testing.T) {
		tester := new(mockTester)

		assert.False(tester, false, "")

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

		assert.False(tester, true, "")

		if tester.errorfCalls != 1 {
			t.Errorf(
				"Expected Errorf to have been called once, it had been called %d times.",
				tester.errorfCalls,
			)
		}
	})
}
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.")
	})
}
예제 #3
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()))
		})
	})
}