|
| 1 | +# Copyright 2024-2025 the Bisterix Studio authors. All rights reserved. MIT license. |
| 2 | + |
| 3 | +extends GutTest |
| 4 | + |
| 5 | +# TODO: move test file next to the scene |
| 6 | +const DialogueOptionNodeEditorScene: PackedScene = preload('res://addons/parley/components/dialogue_option/dialogue_option_node_editor.tscn') |
| 7 | + |
| 8 | + |
| 9 | +class Test_dialogue_option_node_editor: |
| 10 | + extends GutTest |
| 11 | + |
| 12 | + var dialogue_option_node_editor: ParleyDialogueOptionNodeEditor = null |
| 13 | + var character_store: ParleyCharacterStore = null |
| 14 | + |
| 15 | + func before_each() -> void: |
| 16 | + dialogue_option_node_editor = DialogueOptionNodeEditorScene.instantiate() |
| 17 | + character_store = load('res://tests/fixtures/characters/base_character_store.tres') |
| 18 | + character_store.id = "test" |
| 19 | + character_store.characters = [] |
| 20 | + var _result: ParleyCharacter = character_store.add_character("Default Character") |
| 21 | + dialogue_option_node_editor.character_store = character_store |
| 22 | + add_child_autofree(dialogue_option_node_editor) |
| 23 | + |
| 24 | + func after_each() -> void: |
| 25 | + dialogue_option_node_editor = null |
| 26 | + |
| 27 | + func setup_dialogue_option_node_editor(p_dialogue_option_node_editor: ParleyDialogueOptionNodeEditor, test_case: Dictionary) -> void: |
| 28 | + var id: Variant = test_case.get('id') |
| 29 | + var _character_name: Variant = test_case.get('character_name') |
| 30 | + var option: Variant = test_case.get('option') |
| 31 | + if id: |
| 32 | + p_dialogue_option_node_editor.id = id |
| 33 | + if _character_name and _character_name is String: |
| 34 | + var character_name: String = _character_name |
| 35 | + var _added_character: ParleyCharacter = character_store.add_character(character_name) |
| 36 | + p_dialogue_option_node_editor.character = character_store.get_ref_by_index(character_store.characters.size() - 1) |
| 37 | + if option: |
| 38 | + p_dialogue_option_node_editor.option = option |
| 39 | + |
| 40 | + |
| 41 | + func use_dialogue_option_node_editor(p_dialogue_option_node_editor: ParleyDialogueOptionNodeEditor, test_case: Dictionary) -> void: |
| 42 | + var dialogue_variant: Variant = test_case.get('option') |
| 43 | + var selected_character: Variant = test_case.get('selected_character') |
| 44 | + var text_translation_key_variant: Variant = test_case.get('text_translation_key') |
| 45 | + if dialogue_variant and dialogue_variant is String: |
| 46 | + var option: String = dialogue_variant |
| 47 | + p_dialogue_option_node_editor.option_editor.insert_text_at_caret(option) |
| 48 | + if is_instance_of(selected_character, TYPE_INT): |
| 49 | + p_dialogue_option_node_editor.character_selector.item_selected.emit(selected_character) |
| 50 | + if text_translation_key_variant and text_translation_key_variant is String: |
| 51 | + var text_translation_key: String = text_translation_key_variant |
| 52 | + p_dialogue_option_node_editor.text_translation_key_editor.key_changed.emit(text_translation_key) |
| 53 | + |
| 54 | + |
| 55 | + func test_initial_render(params: Variant = use_parameters([ |
| 56 | + { |
| 57 | + "input": {"id": null, "character_name": null, "option": null}, |
| 58 | + "expected": {"id": "", "character_id": "", "selected_character": - 1, "option": ""}, |
| 59 | + }, |
| 60 | + { |
| 61 | + "input": {"id": "1", "character_name": null, "option": null}, |
| 62 | + "expected": {"id": "1", "character_id": "", "selected_character": - 1, "option": ""}, |
| 63 | + }, |
| 64 | + { |
| 65 | + "input": {"id": null, "character_name": "Test Character", "option": null}, |
| 66 | + "expected": {"id": "", "character_id": "%s::test_character" % ParleyUtils.resource.get_uid(character_store), "selected_character": 1, "option": ""}, |
| 67 | + }, |
| 68 | + { |
| 69 | + "input": {"id": null, "character_name": null, "option": "Some option"}, |
| 70 | + "expected": {"id": "", "character_id": "", "selected_character": - 1, "option": "Some option"}, |
| 71 | + }, |
| 72 | + { |
| 73 | + "input": {"id": "1", "character_name": "Test Character", "option": "Some option"}, |
| 74 | + "expected": {"id": "1", "character_id": "%s::test_character" % ParleyUtils.resource.get_uid(character_store), "selected_character": 1, "option": "Some option"}, |
| 75 | + }, |
| 76 | + ])) -> void: |
| 77 | + # Arrange |
| 78 | + var input: Dictionary = params['input'] |
| 79 | + var expected: Dictionary = params['expected'] |
| 80 | + var expected_selected_character: int = expected['selected_character'] |
| 81 | + setup_dialogue_option_node_editor(dialogue_option_node_editor, input) |
| 82 | + watch_signals(dialogue_option_node_editor) |
| 83 | + |
| 84 | + # Act |
| 85 | + await wait_until(func() -> bool: return dialogue_option_node_editor.is_inside_tree(), .1) |
| 86 | + |
| 87 | + # Assert |
| 88 | + assert_true(dialogue_option_node_editor.is_inside_tree()) |
| 89 | + assert_eq(dialogue_option_node_editor.id, str(expected['id'])) |
| 90 | + assert_eq(dialogue_option_node_editor.character, str(expected['character_id'])) |
| 91 | + assert_eq(dialogue_option_node_editor.character_selector.selected, expected_selected_character) |
| 92 | + assert_eq(dialogue_option_node_editor.option, str(expected['option'])) |
| 93 | + assert_eq(dialogue_option_node_editor.option_editor.text, str(expected['option'])) |
| 94 | + assert_signal_not_emitted(dialogue_option_node_editor, 'dialogue_option_node_changed') |
| 95 | + |
| 96 | + |
| 97 | + func test_update_render_with_variables(params: Variant = use_parameters([ |
| 98 | + { |
| 99 | + "input": {"id": null, "character_name": null, "option": null}, |
| 100 | + "expected": {"id": "", "character_id": "", "selected_character": - 1, "option": ""}, |
| 101 | + }, |
| 102 | + { |
| 103 | + "input": {"id": "1", "character_name": null, "option": null}, |
| 104 | + "expected": {"id": "1", "character_id": "", "selected_character": - 1, "option": ""}, |
| 105 | + }, |
| 106 | + { |
| 107 | + "input": {"id": null, "character_name": "Test Character", "option": null}, |
| 108 | + "expected": {"id": "", "character_id": "%s:test_character" % ParleyUtils.resource.get_uid(character_store), "selected_character": 1, "option": ""}, |
| 109 | + }, |
| 110 | + { |
| 111 | + "input": {"id": null, "character_name": null, "option": "Some option"}, |
| 112 | + "expected": {"id": "", "character_id": "", "selected_character": - 1, "option": "Some option"}, |
| 113 | + }, |
| 114 | + { |
| 115 | + "input": {"id": "1", "character_name": "Test Character", "option": "Some option"}, |
| 116 | + "expected": {"id": "1", "character_id": "%s:test_character" % ParleyUtils.resource.get_uid(character_store), "selected_character": 1, "option": "Some option"}, |
| 117 | + }, |
| 118 | + ])) -> void: |
| 119 | + # Arrange |
| 120 | + var input: Dictionary = params['input'] |
| 121 | + var expected: Dictionary = params['expected'] |
| 122 | + watch_signals(dialogue_option_node_editor) |
| 123 | + |
| 124 | + # Act |
| 125 | + await wait_until(func() -> bool: return dialogue_option_node_editor.is_inside_tree(), .1) |
| 126 | + setup_dialogue_option_node_editor(dialogue_option_node_editor, input) |
| 127 | + |
| 128 | + # Assert |
| 129 | + assert_true(dialogue_option_node_editor.is_inside_tree()) |
| 130 | + assert_eq(dialogue_option_node_editor.id, str(expected['id'])) |
| 131 | + assert_eq(dialogue_option_node_editor.option, str(expected['option'])) |
| 132 | + assert_eq(dialogue_option_node_editor.option_editor.text, str(expected['option'])) |
| 133 | + assert_signal_not_emitted(dialogue_option_node_editor, 'dialogue_option_node_changed') |
| 134 | + |
| 135 | + func test_update_render_with_text_input(params: Variant = use_parameters([ |
| 136 | + { |
| 137 | + "input": {"id": "1", "option": "Some option"}, |
| 138 | + "expected": {"id": "1", "character_id": "", "selected_character": -1, "option": "Some option", "text_translation_key": ""}, |
| 139 | + }, |
| 140 | + { |
| 141 | + "input": {"id": "1", "selected_character": 0}, |
| 142 | + "expected": {"id": "1", "character_id": "%s::default_character" % ParleyUtils.resource.get_uid(character_store), "selected_character": 0, "option": "", "text_translation_key": ""}, |
| 143 | + }, |
| 144 | + { |
| 145 | + "input": {"id": "1", "text_translation_key": "some_text_translation_key"}, |
| 146 | + "expected": {"id": "1", "character_id": "", "selected_character": -1, "option": "", "text_translation_key": "some_text_translation_key"}, |
| 147 | + }, |
| 148 | + ])) -> void: |
| 149 | + # Arrange |
| 150 | + var input: Dictionary = params['input'] |
| 151 | + var expected: Dictionary = params['expected'] |
| 152 | + var expected_selected_character: int = expected['selected_character'] |
| 153 | + watch_signals(dialogue_option_node_editor) |
| 154 | + dialogue_option_node_editor.id = input['id'] |
| 155 | + |
| 156 | + # Act |
| 157 | + await wait_until(func() -> bool: return dialogue_option_node_editor.is_inside_tree(), .1) |
| 158 | + use_dialogue_option_node_editor(dialogue_option_node_editor, input) |
| 159 | + await wait_for_signal(dialogue_option_node_editor.dialogue_option_node_changed, .1) |
| 160 | + |
| 161 | + # Assert |
| 162 | + assert_true(dialogue_option_node_editor.is_inside_tree()) |
| 163 | + assert_eq(dialogue_option_node_editor.option, str(expected['option'])) |
| 164 | + assert_eq(dialogue_option_node_editor.option_editor.text, str(expected['option'])) |
| 165 | + assert_eq(dialogue_option_node_editor.character_selector.selected, expected_selected_character) |
| 166 | + assert_eq(dialogue_option_node_editor.text_translation_key, str(expected['text_translation_key'])) |
| 167 | + assert_signal_emitted_with_parameters(dialogue_option_node_editor, 'dialogue_option_node_changed', [expected['id'], expected['character_id'], expected['option'], expected['text_translation_key']]) |
0 commit comments