Published on :
Macro CREATION_INTERNE

Macro assertTrue - Check if a condition is true

This code is also available in: Deutsch Español Français
This macro, 'assertTrue', is part of the SASUNIT framework and is designed to check if a given condition evaluates to 'true'. For numeric types, a value of 0 is considered false, while any other value is true. For character types, an empty string or one composed only of spaces, as well as the word 'false' (case-insensitive), are considered false. All other strings are true. The macro initializes local variables, checks the call sequence via %_checkCallingSequence, then proceeds to evaluate the i_cond parameter according to its type (numeric or character). Finally, it calls the internal macro %_asserts to record the assertion result.
Data Analysis

Type : CREATION_INTERNE


The code evaluates the input variable 'i_cond', which is a condition passed as a parameter. It does not directly read data from external files or databases, but processes a value or an expression internal to the script.

1 Code Block
Macro SAS
Explanation :
This block defines the 'assertTrue' macro with two parameters: 'i_cond' (the condition to evaluate) and 'i_desc' (a description of the assertion). It declares the global variable 'g_inTestCase' and calls '%endTestCall' for message handling. The macro then performs a call sequence check via '%_checkCallingSequence' to ensure it is used correctly in an assertion context. Local variables are declared to store intermediate evaluation results.
Copied!
1%MACRO assertTrue (
2 i_cond =
3 ,i_desc =
4);
5 
6%GLOBAL g_inTestCase;
7%endTestCall(i_messageStyle=NOTE);
8%IF %_checkCallingSequence(i_callerType=assert) NE 0 %THEN %DO;
9 %RETURN;
10%END;
11 
12%LOCAL l_result l_expected l_actual;
2 Code Block
Macro SAS (Conditional Logic)
Explanation :
This block evaluates the 'i_cond' condition if it is of character type, determined by the '%datatyp' macro. It sets 'l_expected' to 'true'. If the string is empty, 'l_result' is set to 2 (false). Otherwise, the string is converted to lowercase. If it is 'true', 'l_result' is 0 (true); if it is 'false', 'l_result' is 2 (false). If the string is neither 'true' nor 'false', it is trimmed of spaces with '%sysfunc(strip())', and 'l_result' is set based on its length after trimming: 0 if not empty (true), 2 if empty (false).
Copied!
1/*-- evaluate character type ------------------------------------------------*/
2%IF (%datatyp(&i_cond.) eq CHAR) %THEN %DO;
3 %LET l_expected=true;
4 %IF (%LENGTH(&i_cond.) eq 0) %THEN %DO;
5 %LET l_result = 2;
6 %END;
7 %ELSE %DO;
8 %LET l_actual = %lowcase (&i_cond.);
9 %IF (&l_actual. eq true) %THEN %DO;
10 %LET l_result = 0;
11 %END;
12 %ELSE %IF (&l_actual. eq false) %THEN %DO;
13 %LET l_result = 2;
14 %END;
15 %ELSE %DO;
16 %LET l_actual = %sysfunc(strip(&i_cond.));
17 %LET l_result = %eval((%LENGTH(&l_actual.) eq 0)*2);
18 %END;
19 %END;
20%END;
3 Code Block
Macro SAS (Conditional Logic)
Explanation :
This block evaluates the 'i_cond' condition if it is of numeric type. It sets 'l_expected' to 1. The value of 'i_cond' is stored in 'l_actual'. If 'i_cond' is equal to 0, 'l_result' is set to 2 (false). In all other cases (non-zero numeric value), 'l_result' is set to 0 (true).
Copied!
1/*-- evaluate numeric type ------------------------------------------------*/
2%ELSE %IF (%datatyp(&i_cond.) eq NUMERIC) %THEN %DO;
3 %LET l_expected=1;
4 %LET l_actual = &i_cond.;
5 %IF (&i_cond eq 0) %THEN %DO;
6 %let l_result = 2;
7 %END;
8 %ELSE %DO;
9 %let l_result = 0;
10 %END;
11%END;
4 Code Block
Macro SAS
Explanation :
This final block calls the internal macro '%_asserts'. This is the core of assertion logging. It passes the assertion type ('assertTrue'), the expected value ('l_expected'), the actual value ('l_actual'), the assertion description ('i_desc'), and the evaluation result ('l_result') to it. This '%_asserts' macro is responsible for managing test results and reporting them.
Copied!
1%_asserts(
2 i_type = assertTrue
3 ,i_expected = &l_expected.
4 ,i_actual = &l_actual.
5 ,i_desc = &i_desc.
6 ,i_result = &l_result.
7)
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.