diff options
| author | Sophia Pearson <codergal89@gmail.com> | 2022-05-23 00:02:58 +0200 |
|---|---|---|
| committer | Sophia Pearson <codergal89@gmail.com> | 2022-05-23 00:02:58 +0200 |
| commit | 6fe4972aa48a29d0aafee0461ccd6c635ca9ee6c (patch) | |
| tree | 30ff503136f1d41ccbb35d3bb9fa6ab92f1e839a /Tests/ComponentTests/ParserTests | |
| parent | f3c50714834d2d5cb204625c63aaeffb50bef236 (diff) | |
| download | texty-6fe4972aa48a29d0aafee0461ccd6c635ca9ee6c.tar.xz texty-6fe4972aa48a29d0aafee0461ccd6c635ca9ee6c.zip | |
commands: add basic command parser infrastructure
Diffstat (limited to 'Tests/ComponentTests/ParserTests')
3 files changed, 93 insertions, 0 deletions
diff --git a/Tests/ComponentTests/ParserTests/CommandParserTestBase.gd b/Tests/ComponentTests/ParserTests/CommandParserTestBase.gd new file mode 100644 index 0000000..6e452cb --- /dev/null +++ b/Tests/ComponentTests/ParserTests/CommandParserTestBase.gd @@ -0,0 +1,29 @@ +extends GutTest + +class_name CommandParserTestBase + +const CommandParser = preload("res://Scripts/CommandParser.cs") + +var _instance: CommandParser + +func _to_bits(number: int, bits: int) -> Array: + var result = [] + for bit in bits: + result.append(number & 1) + number = number >> 1 + return result + +func before_each(): + _instance = autofree(CommandParser.new()) + +func generate_capitalization_permutations(text: String) -> Array: + var result = [] + + for permutation in pow(2, text.length()): + var mask = _to_bits(permutation, text.length()) + var copy = String(text) + for index in len(mask): + if mask[index] == 1: + copy[index] = copy[index].to_upper() + result.append(copy) + return result; diff --git a/Tests/ComponentTests/ParserTests/test_CommandParser.gd b/Tests/ComponentTests/ParserTests/test_CommandParser.gd new file mode 100644 index 0000000..bdb14a6 --- /dev/null +++ b/Tests/ComponentTests/ParserTests/test_CommandParser.gd @@ -0,0 +1,9 @@ +extends CommandParserTestBase + +const Command = preload("res://Scripts/Command.cs") + +func parse(input: String) -> Command: + return autofree(_instance.TryParse(input)) + +func test_parsing_the_empty_string_returns_null(): + assert_null(parse('')) diff --git a/Tests/ComponentTests/ParserTests/test_LookCommand.gd b/Tests/ComponentTests/ParserTests/test_LookCommand.gd new file mode 100644 index 0000000..32268b0 --- /dev/null +++ b/Tests/ComponentTests/ParserTests/test_LookCommand.gd @@ -0,0 +1,55 @@ +extends CommandParserTestBase + +const LookCommand = preload("res://Scripts/Commands/LookCommand.cs") + +var _around_permutations = generate_capitalization_permutations('around') +var _at_permutations = generate_capitalization_permutations('at') +var _look_permutations = generate_capitalization_permutations('look') + +func parse(input: String) -> LookCommand: + return autofree(_instance.TryParse(input)) + +func test_parsing_look_command_without_an_argument_returns_non_null(): + assert_not_null(parse('look')) + +func test_parsing_look_command_is_case_insensitive(permutation=use_parameters(_look_permutations)): + assert_not_null(parse(permutation)) + +func test_parsing_look_command_without_an_argument_returns_look_command_with_an_emtpy_target(): + assert_eq('', parse('look').Target) + +func test_parsing_look_at_command_is_case_insensitive(permutation=use_parameters(_at_permutations)): + assert_not_null(parse('look ' + permutation)) + +func test_parsing_look_at_command_without_further_arguments_returns_look_command_with_an_emtpy_target(): + assert_eq('', parse('look at').Target) + +func test_parsing_look_at_command_without_further_arguments_returns_look_command_with_non_null_modifier(): + assert_not_null(parse('look at').Modifier) + +func test_parsing_look_at_command_without_further_arguments_returns_look_command_with_at_modifier(): + assert_eq('At', parse('look at').ModifierAsString) + +func test_parsing_look_at_command_with_more_arguments_returns_look_command_with_non_empty_target(): + assert_ne('', parse('look at door on the left').Target) + +func test_parsing_look_at_command_with_an_additional_argument_returns_look_command_with_the_additional_argument_as_the_target(): + assert_eq('window', parse('look at window').Target) + +func test_parsing_look_at_command_with_multiple_additional_arguments_returns_look_command_with_the_additional_arguments_as_the_target(): + assert_eq('table near the door', parse('look at table near the door').Target) + +func test_parsing_look_around_command_is_case_insensitive(permutation=use_parameters(_around_permutations)): + assert_not_null(parse('look ' + permutation)) + +func test_parsing_look_around_command_without_further_arguments_returns_look_command_with_an_empty_target(): + assert_eq('', parse('look around').Target) + +func test_parsing_look_around_command_without_further_arguments_returns_look_command_with_non_null_modifier(): + assert_not_null(parse('look around').Modifier) + +func test_parsing_look_around_command_without_further_arguments_returns_look_command_with_around_modifier(): + assert_eq('Around', parse('look around').ModifierAsString) + +func test_parsing_look_around_command_ignores_all_additional_arguments(): + assert_eq('', parse('look around the room').Target) |
