Skip to content

Commit

Permalink
Merge pull request #36 from bfrymire/create_assert_raises
Browse files Browse the repository at this point in the history
Create assertRaises and assertRaiseErrorValue assertion methods
  • Loading branch information
bfrymire authored Aug 26, 2022
2 parents 5f45f39 + dbd4ff5 commit 4495a81
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Crispy

Version 1.5.2
Version 1.6.0

Unit testing framework built in GML for GameMaker Studio 2.3.6+

Expand Down
6 changes: 2 additions & 4 deletions objects/obj_test/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ results_max = 255;
results_box = new gui_box(info_box.x1, info_box.y2 + 3, info_box.x2, room_height - 2);


// Defining Dracula Theme colors
// https://github.com/dracula/dracula-theme#color-palette
dracula_theme = {
// Defining colors
colors = {
background: make_color_rgb(40, 42, 54),
current_line: make_color_rgb(68, 71, 90),
selection: make_color_rgb(68, 71, 90),
Expand All @@ -80,4 +79,3 @@ dracula_theme = {
red: make_color_rgb(255, 85, 85),
yellow: make_color_rgb(241, 250, 140),
}
colors = dracula_theme;
85 changes: 82 additions & 3 deletions scripts/TestCase/TestCase.gml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function TestCase(_name, _func) : BaseTestClass(_name) constructor {
addLog(new CrispyLog(self, {
pass: false,
msg: _message,
helper_text: "first and second are not equal: " + string(_first) + ", " + string(_second)
helper_text: "first and second are not equal: " + string(_first) + ", " + string(_second),
}));
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ function TestCase(_name, _func) : BaseTestClass(_name) constructor {
addLog(new CrispyLog(self, {
pass: false,
msg: _message,
helper_text: "Expression is not true."
helper_text: "Expression is not true.",
}));
}
}
Expand Down Expand Up @@ -282,7 +282,7 @@ function TestCase(_name, _func) : BaseTestClass(_name) constructor {
addLog(new CrispyLog(self, {
pass: false,
msg: _message,
helper_text: "Expression is not undefined."
helper_text: "Expression is not undefined.",
}));
}
}
Expand Down Expand Up @@ -316,6 +316,85 @@ function TestCase(_name, _func) : BaseTestClass(_name) constructor {
}
}

/**
* Test whether the provided function will throw an error message
* @function assertRaises
* @param {method} function - Function to check whether it throws an error message
* @param [string|undefined] message - Custom message to output on failure
*/
static assertRaises = function(_func, _message) {
// Check supplied arguments
if argument_count < 1 {
show_error(instanceof(self) + ".assertRaises() expected 1 argument, recieved " + string(argument_count) + ".", true);
}
if !is_method(_func) {
throw(instanceof(self) + ".assertRaises() \"func\" expected a method, received " + typeof(_func) + ".");
}
if !is_string(_message) && !is_undefined(_message) {
throw(instanceof(self) + ".assertRaises() \"message\" expected either a string or undefined, received " + typeof(_message) + ".");
}
try {
_func();
addLog(new CrispyLog(self, {
pass: false,
msg: _message,
helper_text: "Error message was not thrown.",
}));
}
catch(err) {
addLog(new CrispyLog(self, {
pass: true,
}));
}
}

/**
* Test the value of the error message thrown in the provided function
* @function assertRaiseErrorValue
* @param {method} function - Function ran to throw an error message
* @param {string} value - Value of error message to check
* @param [string|undefined] message - Custom message to output on failure
*/
static assertRaiseErrorValue = function(_func, _value, _message) {
// Check supplied arguments
if argument_count < 2 {
show_error(instanceof(self) + ".assertRaiseErrorValue() expected 2 arguments, recieved " + string(argument_count) + ".", true);
}
if !is_method(_func) {
throw(instanceof(self) + ".assertRaiseErrorValue() \"func\" expected a method, received " + typeof(_func) + ".");
}
if !is_string(_value) {
throw(instanceof(self) + ".assertRaiseErrorValue() \"value\" expected a string, received " + typeof(_value) + ".");
}
if !is_string(_message) && !is_undefined(_message) {
throw(instanceof(self) + ".assertRaiseErrorValue() \"message\" expected either a string or undefined, received " + typeof(_message) + ".");
}
try {
_func();
addLog(new CrispyLog(self, {
pass: false,
helper_text: "Error message was not thrown.",
}));
}
catch(err) {
// If the error message was thrown using show_error, use the
// message value from the exception struct for the assertion
if is_struct(err) && variable_struct_exists(err, "message") && is_string(err.message) {
err = err.message;
}
if err == _value {
addLog(new CrispyLog(self), {
pass: true,
});
} else {
addLog(new CrispyLog(self, {
pass: false,
msg: _message,
helper_text: "Error message is not equal to value: \"" + err + "\" != \"" + _value + "\"",
}));
}
}
}

/**
* Function ran before test, used to set up test
Expand Down
5 changes: 3 additions & 2 deletions scripts/__crispy_config_macros/__crispy_config_macros.gml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#macro CRISPY_NAME "Crispy"
#macro CRISPY_AUTHOR "Brent Frymire"
#macro CRISPY_VERSION "1.5.2"
#macro CRISPY_DATE "2022-7-21"
#macro CRISPY_REPO "https://github.com/bfrymire/crispy"
#macro CRISPY_VERSION "1.6.0"
#macro CRISPY_DATE "2022-8-26"

#macro CRISPY_RUN true // Boolean flag that can be used to automatically run tests
#macro CRISPY_DEBUG false // Enables outputting extra context on some silent functions
Expand Down

0 comments on commit 4495a81

Please sign in to comment.