Adding log.gd

This commit is contained in:
Dan Baker 2024-05-02 09:36:31 +01:00
parent eb32d6614e
commit 4522259397
547 changed files with 46844 additions and 0 deletions

View file

@ -0,0 +1,120 @@
# GdUnit generated TestSuite
class_name CmdArgumentParserTest
extends GdUnitTestSuite
# TestSuite generated from
const __source = 'res://addons/gdUnit4/src/cmd/CmdArgumentParser.gd'
var option_a := CmdOption.new("-a", "some help text a", "some description a")
var option_f := CmdOption.new("-f, --foo", "some help text foo", "some description foo")
var option_b := CmdOption.new("-b, --bar", "-b <value>", "comand with required argument", TYPE_STRING)
var option_c := CmdOption.new("-c, --calc", "-c [value]", "command with optional argument", TYPE_STRING, true)
var option_x := CmdOption.new("-x", "some help text x", "some description x")
var _cmd_options :CmdOptions
func before():
# setup command options
_cmd_options = CmdOptions.new([
option_a,
option_f,
option_b,
option_c,
],
# advnaced options
[
option_x,
])
func test_parse_success():
var parser := CmdArgumentParser.new(_cmd_options, "CmdTool.gd")
assert_result(parser.parse([])).is_empty()
# check with godot cmd argumnents before tool argument
assert_result(parser.parse(["-d", "dir/dir/CmdTool.gd"])).is_empty()
# if valid argument set than don't show the help by default
var result := parser.parse(["-d", "dir/dir/CmdTool.gd", "-a"])
assert_result(result).is_success()
assert_array(result.value()).contains_exactly([
CmdCommand.new("-a"),
])
func test_parse_success_required_arg():
var parser := CmdArgumentParser.new(_cmd_options, "CmdTool.gd")
var result := parser.parse(["-d", "dir/dir/CmdTool.gd", "-a", "-b", "valueA", "-b", "valueB"])
assert_result(result).is_success()
assert_array(result.value()).contains_exactly([
CmdCommand.new("-a"),
CmdCommand.new("-b", ["valueA", "valueB"]),
])
# useing command long term
result = parser.parse(["-d", "dir/dir/CmdTool.gd", "-a", "--bar", "value"])
assert_result(result).is_success()
assert_array(result.value()).contains_exactly([
CmdCommand.new("-a"),
CmdCommand.new("-b", ["value"])
])
func test_parse_success_optional_arg():
var parser := CmdArgumentParser.new(_cmd_options, "CmdTool.gd")
# without argument
var result := parser.parse(["-d", "dir/dir/CmdTool.gd", "-c", "-a"])
assert_result(result).is_success()
assert_array(result.value()).contains_exactly([
CmdCommand.new("-c"),
CmdCommand.new("-a")
])
# without argument at end
result = parser.parse(["-d", "dir/dir/CmdTool.gd", "-a", "-c"])
assert_result(result).is_success()
assert_array(result.value()).contains_exactly([
CmdCommand.new("-a"),
CmdCommand.new("-c")
])
# with argument
result = parser.parse(["-d", "dir/dir/CmdTool.gd", "-c", "argument", "-a"])
assert_result(result).is_success()
assert_array(result.value()).contains_exactly([
CmdCommand.new("-c", ["argument"]),
CmdCommand.new("-a")
])
func test_parse_success_repead_cmd_args():
var parser := CmdArgumentParser.new(_cmd_options, "CmdTool.gd")
# without argument
var result := parser.parse(["-d", "dir/dir/CmdTool.gd", "-c", "argument", "-a"])
assert_result(result).is_success()
assert_array(result.value()).contains_exactly([
CmdCommand.new("-c", ["argument"]),
CmdCommand.new("-a")
])
# with repeading commands argument
result = parser.parse(["-d", "dir/dir/CmdTool.gd", "-c", "argument1", "-a", "-c", "argument2", "-c", "argument3"])
assert_result(result).is_success()
assert_array(result.value()).contains_exactly([
CmdCommand.new("-c", ["argument1", "argument2", "argument3"]),
CmdCommand.new("-a")
])
func test_parse_error():
var parser := CmdArgumentParser.new(_cmd_options, "CmdTool.gd")
assert_result(parser.parse([])).is_empty()
# if invalid arguemens set than return with error and show the help by default
assert_result(parser.parse(["-d", "dir/dir/CmdTool.gd", "-unknown"])).is_error()\
.contains_message("Unknown '-unknown' command!")

View file

@ -0,0 +1,123 @@
# GdUnit generated TestSuite
class_name CmdCommandHandlerTest
extends GdUnitTestSuite
# TestSuite generated from
const __source = 'res://addons/gdUnit4/src/cmd/CmdCommandHandler.gd'
var _cmd_options: CmdOptions
var _cmd_instance: TestCommands
# small example of command class
class TestCommands:
func cmd_a() -> String:
return "cmd_a"
func cmd_foo() -> String:
return "cmd_foo"
func cmd_bar(value :String) -> String:
return value
func cmd_bar2(value_a: String, value_b: String) -> Array:
return [value_a, value_b]
func cmd_x() -> String:
return "cmd_x"
func before() -> void:
# setup command options
_cmd_options = CmdOptions.new([
CmdOption.new("-a", "some help text a", "some description a"),
CmdOption.new("-f, --foo", "some help text foo", "some description foo"),
CmdOption.new("-b, --bar", "some help text bar", "some description bar"),
CmdOption.new("-b2, --bar2", "some help text bar", "some description bar"),
],
# advnaced options
[
CmdOption.new("-x", "some help text x", "some description x"),
])
_cmd_instance = TestCommands.new()
func test__validate_no_registerd_commands() -> void:
var cmd_handler := CmdCommandHandler.new(_cmd_options)
assert_result(cmd_handler._validate()).is_success()
func test__validate_registerd_commands() -> void:
var cmd_handler: = CmdCommandHandler.new(_cmd_options)
cmd_handler.register_cb("-a", Callable(_cmd_instance, "cmd_a"))
cmd_handler.register_cb("-f", Callable(_cmd_instance, "cmd_foo"))
cmd_handler.register_cb("-b", Callable(_cmd_instance, "cmd_bar"))
assert_result(cmd_handler._validate()).is_success()
func test__validate_registerd_unknown_commands() -> void:
var cmd_handler: = CmdCommandHandler.new(_cmd_options)
cmd_handler.register_cb("-a", Callable(_cmd_instance, "cmd_a"))
cmd_handler.register_cb("-d", Callable(_cmd_instance, "cmd_foo"))
cmd_handler.register_cb("-b", Callable(_cmd_instance, "cmd_bar"))
cmd_handler.register_cb("-y", Callable(_cmd_instance, "cmd_x"))
assert_result(cmd_handler._validate())\
.is_error()\
.contains_message("The command '-d' is unknown, verify your CmdOptions!\nThe command '-y' is unknown, verify your CmdOptions!")
func test__validate_registerd_invalid_callbacks() -> void:
var cmd_handler := CmdCommandHandler.new(_cmd_options)
cmd_handler.register_cb("-a", Callable(_cmd_instance, "cmd_a"))
cmd_handler.register_cb("-f")
cmd_handler.register_cb("-b", Callable(_cmd_instance, "cmd_not_exists"))
assert_result(cmd_handler._validate())\
.is_error()\
.contains_message("Invalid function reference for command '-b', Check the function reference!")
func test__validate_registerd_register_same_callback_twice() -> void:
var cmd_handler: = CmdCommandHandler.new(_cmd_options)
cmd_handler.register_cb("-a", Callable(_cmd_instance, "cmd_a"))
cmd_handler.register_cb("-b", Callable(_cmd_instance, "cmd_a"))
if cmd_handler._enhanced_fr_test:
assert_result(cmd_handler._validate())\
.is_error()\
.contains_message("The function reference 'cmd_a' already registerd for command '-a'!")
func test_execute_no_commands() -> void:
var cmd_handler: = CmdCommandHandler.new(_cmd_options)
assert_result(cmd_handler.execute([])).is_success()
func test_execute_commands_no_cb_registered() -> void:
var cmd_handler: = CmdCommandHandler.new(_cmd_options)
assert_result(cmd_handler.execute([CmdCommand.new("-a")])).is_success()
func test_execute_commands_with_cb_registered() -> void:
var cmd_handler: = CmdCommandHandler.new(_cmd_options)
var cmd_spy = spy(_cmd_instance)
cmd_handler.register_cb("-a", Callable(cmd_spy, "cmd_a"))
cmd_handler.register_cb("-b", Callable(cmd_spy, "cmd_bar"))
cmd_handler.register_cbv("-b2", Callable(cmd_spy, "cmd_bar2"))
assert_result(cmd_handler.execute([CmdCommand.new("-a")])).is_success()
verify(cmd_spy).cmd_a()
verify_no_more_interactions(cmd_spy)
reset(cmd_spy)
assert_result(cmd_handler.execute([
CmdCommand.new("-a"),
CmdCommand.new("-b", ["some_value"]),
CmdCommand.new("-b2", ["value1", "value2"])])).is_success()
verify(cmd_spy).cmd_a()
verify(cmd_spy).cmd_bar("some_value")
verify(cmd_spy).cmd_bar2("value1", "value2")
verify_no_more_interactions(cmd_spy)

View file

@ -0,0 +1,32 @@
# GdUnit generated TestSuite
class_name CmdCommandTest
extends GdUnitTestSuite
# TestSuite generated from
const __source = 'res://addons/gdUnit4/src/cmd/CmdCommand.gd'
func test_create():
var cmd_a := CmdCommand.new("cmd_a")
assert_str(cmd_a.name()).is_equal("cmd_a")
assert_array(cmd_a.arguments()).is_empty()
var cmd_b := CmdCommand.new("cmd_b", ["arg1"])
assert_str(cmd_b.name()).is_equal("cmd_b")
assert_array(cmd_b.arguments()).contains_exactly(["arg1"])
assert_object(cmd_a).is_not_equal(cmd_b)
func test_add_argument():
var cmd_a := CmdCommand.new("cmd_a")
cmd_a.add_argument("arg1")
cmd_a.add_argument("arg2")
assert_str(cmd_a.name()).is_equal("cmd_a")
assert_array(cmd_a.arguments()).contains_exactly(["arg1", "arg2"])
var cmd_b := CmdCommand.new("cmd_b", ["arg1"])
cmd_b.add_argument("arg2")
cmd_b.add_argument("arg3")
assert_str(cmd_b.name()).is_equal("cmd_b")
assert_array(cmd_b.arguments()).contains_exactly(["arg1", "arg2", "arg3"])

View file

@ -0,0 +1,85 @@
# GdUnit generated TestSuite
class_name CmdConsoleTest
extends GdUnitTestSuite
# TestSuite generated from
const __source = 'res://addons/gdUnit4/src/cmd/CmdConsole.gd'
func test_print_color_default() -> void:
var console :CmdConsole = spy(CmdConsole.new())
console.print_color("test message", Color.RED)
verify(console).color(Color.RED)
verify(console).end_color()
verify(console).printl("test message")
verify(console).bold(false)
verify(console).italic(false)
verify(console).underline(false)
verify(console, 0).new_line()
reset(console)
console.print_color("test message2", Color.BLUE)
verify(console).color(Color.BLUE)
verify(console).end_color()
verify(console).printl("test message2")
verify(console).bold(false)
verify(console).italic(false)
verify(console).underline(false)
verify(console, 0).new_line()
func test_print_color_with_flags() -> void:
var console :CmdConsole = spy(CmdConsole.new())
# bold
console.print_color("test message", Color.RED, CmdConsole.BOLD)
verify(console).bold(true)
verify(console).italic(false)
verify(console).underline(false)
reset(console)
# italic
console.print_color("test message", Color.RED, CmdConsole.ITALIC)
verify(console).bold(false)
verify(console).italic(true)
verify(console).underline(false)
reset(console)
# underline
console.print_color("test message", Color.RED, CmdConsole.UNDERLINE)
verify(console).bold(false)
verify(console).italic(false)
verify(console).underline(true)
reset(console)
# combile italic & underline
console.print_color("test message", Color.RED, CmdConsole.ITALIC|CmdConsole.UNDERLINE)
verify(console).bold(false)
verify(console).italic(true)
verify(console).underline(true)
reset(console)
# combile bold & italic
console.print_color("test message", Color.RED, CmdConsole.BOLD|CmdConsole.ITALIC)
verify(console).bold(true)
verify(console).italic(true)
verify(console).underline(false)
reset(console)
# combile all
console.print_color("test message", Color.RED, CmdConsole.BOLD|CmdConsole.ITALIC|CmdConsole.UNDERLINE)
verify(console).bold(true)
verify(console).italic(true)
verify(console).underline(true)
reset(console)
func test_prints_color() -> void:
var console :CmdConsole = spy(CmdConsole.new())
console.prints_color("test message", Color.RED, CmdConsole.BOLD|CmdConsole.ITALIC)
# verify prints delegates to print_color
verify(console).print_color("test message", Color.RED, CmdConsole.BOLD|CmdConsole.ITALIC)
# and adds a new line
verify(console).new_line()

View file

@ -0,0 +1,51 @@
# GdUnit generated TestSuite
class_name CmdOptionTest
extends GdUnitTestSuite
# TestSuite generated from
const __source = 'res://addons/gdUnit4/src/cmd/CmdOption.gd'
func test_commands():
assert_array(CmdOption.new("-a", "help a", "describe a").commands())\
.contains_exactly(["-a"])
assert_array(CmdOption.new("-a, --aaa", "help a", "describe a").commands())\
.contains_exactly(["-a", "--aaa"])
# containing space or tabs
assert_array(CmdOption.new("-b , --bb ", "help a", "describe a")\
.commands()).contains_exactly(["-b", "--bb"])
func test_short_command():
assert_str(CmdOption.new("-a, --aaa", "help a", "describe a").short_command()).is_equal("-a")
func test_help():
assert_str(CmdOption.new("-a, --aaa", "help a", "describe a").help()).is_equal("help a")
func test_description():
assert_str(CmdOption.new("-a, --aaa", "help a", "describe a").description()).is_equal("describe a")
func test_type():
assert_int(CmdOption.new("-a", "", "").type()).is_equal(TYPE_NIL)
assert_int(CmdOption.new("-a", "", "", TYPE_STRING).type()).is_equal(TYPE_STRING)
assert_int(CmdOption.new("-a", "", "", TYPE_BOOL).type()).is_equal(TYPE_BOOL)
func test_is_argument_optional():
assert_bool(CmdOption.new("-a", "", "").is_argument_optional()).is_false()
assert_bool(CmdOption.new("-a", "", "", TYPE_BOOL, false).is_argument_optional()).is_false()
assert_bool(CmdOption.new("-a", "", "", TYPE_BOOL, true).is_argument_optional()).is_true()
func test_has_argument():
assert_bool(CmdOption.new("-a", "", "").has_argument()).is_false()
assert_bool(CmdOption.new("-a", "", "", TYPE_NIL).has_argument()).is_false()
assert_bool(CmdOption.new("-a", "", "", TYPE_BOOL).has_argument()).is_true()
func test_describe():
assert_str(CmdOption.new("-a, --aaa", "help a", "describe a").describe())\
.is_equal(' ["-a", "--aaa"] describe a \n help a\n')

View file

@ -0,0 +1,57 @@
# GdUnit generated TestSuite
class_name CmdOptionsTest
extends GdUnitTestSuite
# TestSuite generated from
const __source = 'res://addons/gdUnit4/src/cmd/CmdOptions.gd'
var option_a := CmdOption.new("-a", "some help text a", "some description a")
var option_f := CmdOption.new("-f, --foo", "some help text foo", "some description foo")
var option_b := CmdOption.new("-b, --bar", "some help text bar", "some description bar")
var option_x := CmdOption.new("-x", "some help text x", "some description x")
var _cmd_options :CmdOptions
func before():
# setup command options
_cmd_options = CmdOptions.new([
option_a,
option_f,
option_b,
],
# advnaced options
[
option_x,
])
func test_get_option():
assert_object(_cmd_options.get_option("-a")).is_same(option_a)
assert_object(_cmd_options.get_option("-f")).is_same(option_f)
assert_object(_cmd_options.get_option("--foo")).is_same(option_f)
assert_object(_cmd_options.get_option("-b")).is_same(option_b)
assert_object(_cmd_options.get_option("--bar")).is_same(option_b)
assert_object(_cmd_options.get_option("-x")).is_same(option_x)
# for not existsing command
assert_object(_cmd_options.get_option("-z")).is_null()
func test_default_options():
assert_array(_cmd_options.default_options()).contains_exactly([
option_a,
option_f,
option_b])
func test_advanced_options():
assert_array(_cmd_options.advanced_options()).contains_exactly([option_x])
func test_options():
assert_array(_cmd_options.options()).contains_exactly([
option_a,
option_f,
option_b,
option_x])