Skip to content

Instantly share code, notes, and snippets.

@hazzard993
Last active November 6, 2024 03:26
Show Gist options
  • Save hazzard993/e11a87f8f92f407a6c7ecfbf347ebcf4 to your computer and use it in GitHub Desktop.
Save hazzard993/e11a87f8f92f407a6c7ecfbf347ebcf4 to your computer and use it in GitHub Desktop.
Inline pasteable checks for testing behaviour in GameMaker.
var assert_is_callable = function(actual) {
if is_callable(actual) return;
show_error($"Expected callable. Got {actual}", true);
};
var assert_is_array = function(actual) {
if is_array(actual) return;
show_error($"Expected callable. Got {actual}", true);
};
var assert_is_struct = function(actual) {
if is_struct(actual) return;
show_error($"Expected callable. Got {actual}", true);
};
var assert_to_be = function(actual, expected) {
if actual == expected return;
show_error($"Assertion error! Expected {expected}, got: {actual}", true);
};
var assert_to_equal_array = function(actual, expected) {
if (
not is_array(actual) or
not is_array(expected) or
array_length(actual) <> array_length(expected)
) {
show_error($"Expected {expected}. Got: {actual}", true);
}
for (var i = 0; i < array_length(expected); ++i) {
if expected[i] == actual[i] continue;
show_error($"Expected {expected}. Got {actual}", true);
}
};
var assert_to_equal_object = function(actual, expected) {
if (
not is_struct(actual) or
not is_struct(expected)
) {
show_error($"Expected {expected}. Got: {actual}", true);
}
var expected_names = struct_get_names(expected);
var expected_names_length = array_length(expected_names);
var actual_names = struct_get_names(actual);
var unique_names = array_unique(array_concat(actual_names, expected_names));
var unique_names_length = array_length(unique_names);
if expected_names_length <> unique_names_length {
show_error($"Expected {expected}. Got: {actual}", true);
}
for (var i = 0; i < array_length(expected_names); ++i) {
var name = expected_names[i];
if struct_get(actual, name) == struct_get(expected, name) continue;
show_error($"Expected {expected}. Got: {actual}. Property {name} is mismatched", true);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment