Last active
October 9, 2021 19:48
-
-
Save Razzlegames/24a875bf3a3af8c5663f0997418c0d7e to your computer and use it in GitHub Desktop.
Run all integration tests using Yield to wait for test completion before running next test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extends Node | |
| onready var gem = $caveTestBonusLevel/collectables/Gem | |
| onready var monkey = $HoarderMonkey/HoarderMonkeyRigidBody2D | |
| onready var assertTool = Assert.new(get_script().get_path()) | |
| var collectedPath | |
| var errors = [] | |
| func _ready(): | |
| Signals.connect(Signals.SPECIAL_COLLECTION_EVENT_NAME, self, "_specialCollection") | |
| var expectedGemPath = gem.get_path() | |
| Engine.time_scale = 4.0 | |
| yield(get_tree().create_timer(5), "timeout") | |
| assertTool.assert_false(is_instance_valid(gem), "Gem wasn't deleted after collection") | |
| assertTool.assert_true(expectedGemPath == collectedPath, | |
| "Expected Gem path not found. Expected: " + expectedGemPath + | |
| "; collectedPath: " + collectedPath) | |
| _assertGemInMenuAndResetState() | |
| Signals.emit_signal(Signals.TEST_COMPLETE, errors) | |
| func _assertGemInMenuAndResetState(): | |
| var specialHoardablesContainer = DefaultLevelMenus.get_node("PlayMenu").specialHoardablesContainer | |
| var gemFound = specialHoardablesContainer.get_node("gem") | |
| assertTool.assert_true(is_instance_valid(gemFound), "Menu Gem not found after collecting") | |
| specialHoardablesContainer.remove_child(gemFound) | |
| assertTool.assert_false(is_instance_valid(specialHoardablesContainer.get_node("gem")), | |
| "PlayMenu not reset: Gem NOT removed from menu") | |
| func _specialCollection(objectPath): | |
| collectedPath = objectPath |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extends Node | |
| export(String, DIR) var testsPath | |
| var sceneList = [] | |
| var allErrors = [] | |
| onready var assertTool = Assert.new(get_script().get_path()) | |
| func _ready(): | |
| _connectSignals() | |
| getAllScenes(testsPath) | |
| for filePath in sceneList: | |
| _printTestStarted(filePath) | |
| yield(_runScene(filePath), "completed") | |
| _printTestComplete(filePath) | |
| _resetEngineState() | |
| _printAllTestsComplete() | |
| func _printAllTestsComplete(): | |
| if allErrors.size() == 0: | |
| print("=======================================================") | |
| print("==== Tests Complete: No logged failures =====") | |
| print("=======================================================") | |
| get_tree().quit() | |
| else: | |
| print("=======================================================") | |
| print("==== Tests Failed [" + str(allErrors.size()) + "] " + | |
| " (look for failed asserts) =====") | |
| print("=======================================================") | |
| _printAllFailedTests() | |
| get_tree().quit(1) | |
| func _printAllFailedTests(): | |
| for i in len(allErrors): | |
| var error = allErrors[i] | |
| print("Error [" + str(i) + "] " + error.toString()) | |
| func _printTestStarted(testPath): | |
| print("-----------------------------------------------------") | |
| print("--- Test Started: " + testPath) | |
| print("-----------------------------------------------------") | |
| func _printTestComplete(testPath): | |
| print("-----------------------------------------------------") | |
| print("--- Test Finished: " + testPath) | |
| print("-----------------------------------------------------") | |
| func _connectSignals(): | |
| Signals.connect(Signals.TEST_FAILURE, self, "_handleError") | |
| func _runScene(filePath): | |
| print("Loading scene: " + filePath) | |
| var scene = load(filePath).instance() | |
| assertTool.assert_true(scene != null, "Scene Null") | |
| print("Scene loaded: " + filePath) | |
| print("adding scene to parent: "+ filePath) | |
| add_child(scene) | |
| yield(Signals, Signals.TEST_COMPLETE) | |
| removeScene(scene) | |
| func getAllScenes(dirPath): | |
| var dir = openDir(dirPath) | |
| dir.list_dir_begin(true, true) | |
| var fileName = dir.get_next() | |
| while fileName != "" : | |
| var filePath = dirPath + "/" + fileName | |
| if dir.current_is_dir(): | |
| print("Dir found decending " + filePath) | |
| getAllScenes(filePath) | |
| else: | |
| if filePath.ends_with(".tscn") && fileName.begins_with("test"): | |
| print("**** Test Path found **** : " + filePath) | |
| sceneList.push_back(filePath) | |
| fileName = dir.get_next() | |
| print("Directory walking done: " + dirPath) | |
| dir.list_dir_end() | |
| func openDir(dirPath): | |
| var dir = Directory.new() | |
| var openResult = dir.open(dirPath) | |
| assertTool.assert_true(openResult == OK, | |
| "Error opening directory: " + dirPath) | |
| if openResult != OK: | |
| return null | |
| return dir | |
| func _resetEngineState(): | |
| Engine.time_scale = 1 | |
| func _handleError(error): | |
| allErrors.push_back(error) | |
| func removeScene(scene): | |
| print("removing scene " + scene.get_path()) | |
| scene.queue_free() | |
| remove_child(scene) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment