You can use the Assert function to place assertions in the MAXScript code. It was made possible by the introduction of line number support in the MAXScript evaluation tree. It is available in 3ds Max 2010 and higher.
Additionally, an advanced set of assert functions and an AssertReporter message log system have been added to 3ds Max 2012 and are being used for test suites by Autodesk during the development of 3ds Max.
An assertion is a predicate (a true|false statement) placed in the code to indicate that the developer thinks the predicate always evaluates to true at that place.
Assertions are used to help reason about program correctness.
If an assertion evaluates to false at run-time, an "Assertion fault" is generated which typically causes execution to abort. This draws attention to the location at which the logical inconsistency is detected and can be preferable to the behavior that would otherwise result.
assert <expression> [message:<string>] [options:#( #dialog | #listener | #debug_output_window | #debugBreak | #c_break | #all)] [showstack:<bool>]
Asserts that the result of the expression passed as the first argument to the function must be true at the given place in the code.
The expression must resolve to a Boolean value, otherwise a MAXScript exception will be thrown.
If the expression returns true , the code continues normally.
If the expression returns false , the execution will be interrupted depending on the following options:
message: - If this optional keyword is supplied, the string will be displayed as the assert's message.
options: - An array of the following MAXScript name options. If the array is empty, it defaults to #listener .
#dialog - brings up a modal dialog that halts execution of the application.
#listener - prints out the failure info to theMAXScript Listener
#debug_output_window - prints out to the Visual Studio output debugger window, (if present)
#debugBreak - Halts execution in the MAXScript Debugger. If specified, the #dialog option is ignored. This option is only valid if quiet mode is false. Otherwise nothing happens.
#c_break - Breaks execution in Visual Studio under the Debugger. This only works if running a debug build of 3ds Max under the Debugger. If specified, it first turns on the #debug_output_window flag internally to aid in debugging. The #debugBreak and #dialog option will be ignored and it always occurs last to allow the other options to output their data.
showstack: -defines whether to include the MAXScript stack traceback in the printouts. This only applies for the #listener and #debug_output_window options. For all other options the stack information is ignored. Default is false.
A number of assert functions and an AssertReporter struct to manage their messages have been implemented in 3ds Max 2012 and are now being used for internal testing at Autodesk.
These functions are available to all MAXScript users and can be employed in custom test frameworks to ensure the correctness of the program flow.
These Test Framework functions have the following advantages:
All asserts are reported automatically.
This new system should significantly reduce the visual clutter of the asserts as developers have the option of NOT having to construct massive error strings for the message of the asserts (although they still can provide custom messages where needed). Also, these asserts run quietly and gather their messages in a dedicated AssertReporter to be queried and analyzed after running the code without interrupting the actual test run.
Returns false and logs an assert failure message with the AssertReporter if the return value of the expression is not true.
Will log a default message in the form of "Assert - Expected true, got false - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
Returns false and logs an assert failure message with the AssertReporter if the return value of the expression is not false.
Will log a default message in the form of "Assert - Expected false, got true - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
Returns false and logs an assert failure message with the AssertReporter if the return value of the expression passed as second argument is not equal to the expected value passed as first argument.
Will log a default message in the form of "Assert - Expected 1, got 2 - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
Returns false and logs an assert failure message with the AssertReporter if the return value of the expression passed as second argument is equal to the expected value passed as first argument.
Will log a default message in the form of "Assert - Expected 1, got 1 - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
Returns false and logs an assert failure message with the AssertReporter if the returns value of the expression is undefined.
Will log a default message in the form of "Assert - Expected "defined", got undefined - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
Returns false and logs an assert failure message with the AssertReporter if the returns value of the expression is not undefined.
Will log a default message in the form of "Assert - Expected undefined, got 1 - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
<boolean>assert_float <float>expected <expression>actual [tolerance:<float>delta] [message:<string>]
Returns false and logs an assert failure message with the AssertReporter if the returns value of the expression passed as second argument is not the expected float value.
If the tolerance: optional keyword argument is provided, the assert will return false if the actual return value is different from the expected one beyond the tolerance delta value.
Will log a default message in the form of "Assert - Expected 1.0, got 2 - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
<boolean>assert_string_equal <string>expected <exression>actual [ignorecase:<boolean>val] [message:]
Returns false if the return value of the expression passed as second argument is not equal to the expected string.
The default for the optional ignorecase: keyword argument is true. It should be supplied as false to perform case sensitive comparison.
Will log a default message in the form of "Assert - Expected "A", got "b" - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
<boolean>assert_point3_equal <point3>expected <expression>actual [tolerance:<float>delta] [message:]
Returns false if the return value of the expression passed as second argument is not equal to the expected Point3 value.
If the tolerance: optional keyword argument is provided, the assert will return false if any of the actual return value's components is different from the expected ones' corresponding component beyond the tolerance delta value.
Will log a default message in the form of "Assert - Expected [1,2,3], got [1.1,2.1,3.1] - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
Returns false if the return value of the expression passed as second argument is not equal to the expected matrix value.
If the tolerance: optional keyword argument is provided, the assert will return false if any of the components of the actual return value is different from the expected ones' corresponding component beyond the tolerance delta value.
Will log a default message in the form of "Assert - Expected (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0]), got (matrix3 [1,0,0] [0,1,0] [0,0,1] [42.1234,43.8329,0]) - (No File) in frame <No Stack frame>, at line 1" unless the optional message: keyword argument is provided.
Note that due to the limited precision of numbers representation in computers, values at the exact edge of the tolerance delta might or might not cause an assert to be logged when using the assert_float() and assert_point3_equal() methods.
The explanation for the above results is that the first method checks whether the difference of the expected and actual values is less than the tolerance value and in this case, it is not. The two values are converted to doubles internally and the first 7 decimal places are identical (all zeros), so the difference is NOT less than the tolerance delta.
In the second case though, the Point3 value [1.0,2.0,3.1] is actually represented internally as [1.0,2.0,3.0999999]. Thus the difference between 3.0 and 3.0999999 is just below the tolerance and due to this precision error, the assert passes as true.
The global AssertReporter C++ struct collects the results of the asserts and provides methods for managing and accessing the results after a test run.
Removes all assertion entries from the AssertReporter and resets all failures and exceptions results to zero.
Returns an array of strings containing the assert failures stored in the AssertReporter.
Returns the count of the assert failures stored in the AssertReporter. This is faster than calling (AssertReporter.GetAssertFailuresCount()).count
Returns an array of strings containing any exceptions logged with the AssertReporter.
Returns the count of the exception failures stored in the AssertReporter. This is faster than calling (AsserReporter.GetExceptionFailuresCount()).count
Returns an array of strings containing any messages that were logged through the AssertReporter, regardless of their type.
Logging Methods:Saves a message with the AssertReporter. The messages can be retrieved later using AssertReporter.GetMessages()
Saves a message as an exception with the AssertReporter. The exception messages can be retrieved later using AssertReporter.GetExceptionFailures()
Saves a message as an assert failure with the AssertReporter. The failure messages can be retrieved later using AssertReporter.GetAssertFailures()
The following two methods can be used for saving some sort of state during test runs, and is only provided for future flexibility.
Saves a custom user defined string in the AssertReporter.