Unit tests for the mp_replace macro

ATTENTION : Ce contenu est DÉSACTIVÉ. Il est invisible pour les visiteurs.
Difficulty Level
Beginner
Published on :
This program executes a series of unit tests to validate the functionality of the `%mp_replace` macro. It dynamically creates text files with specific content (simple strings, trailing spaces, dots), invokes the macro to perform replacements, then reads the modified files to compare the expected results with the obtained results using the `%mp_assert` and `%mp_assertscope` macros.
Data Analysis

Type : CREATION_INTERNE


Data (text files) are generated on the fly by DATA _NULL_ steps using the FILE instruction.

1 Code Block
DATA STEP Data
Explanation :
Initialization of the first test case: defining strings and creating a text file containing the target.
Copied!
1%let test1="&sasjswork/file.txt";
2%let str=replace/me;
3%let rep=with/this;
4DATA _null_;
5 file &test1;
6 put 'blahblah';
7 put "blahblah&str.blah";
8 put 'blahblahblah';
9RUN;
2 Code Block
MACRO CALL
Explanation :
Execution of the replacement macro `%mp_replace`. `%mp_assertscope` is used to ensure that the macro does not create parasitic variables in the global environment.
Copied!
1%mp_assertscope(SNAPSHOT)
2%mp_replace(&test1, findvar=str, replacevar=rep)
3%mp_assertscope(COMPARE)
3 Code Block
MACRO CALL
Explanation :
Reading the file result and validating the replacement via the `%mp_assert` assertion macro.
Copied!
1DATA _null_;
2 INFILE &test1;
3 INPUT;
4 IF _n_=2 THEN call symputx('test1result',_infile_);
5RUN;
6 
7%mp_assert(
8 iftrue=("&test1result" = "blahblah&rep.blah"),
9 desc=Checking first replace,
10 outds=work.test_results
11)
4 Code Block
DATA STEP Data
Explanation :
Creation of the second test case: handling strings containing trailing spaces.
Copied!
1%let test2="&sasjswork/file2.txt";
2%let str=%str(replacewith trailing spaces );
3%let rep=%str( with more spaces );
4DATA _null_;
5 file &test2 lrecl=500;
6 put 'blahblah';
7 put "blahblah&str.blah&str. replace &str.X";
8 put "blahbreplacewith&str.spacesahblah";
9RUN;
5 Code Block
MACRO CALL
Explanation :
Performing the replacement and verifying that spaces have been handled correctly.
Copied!
1%mp_replace(&test2, findvar=str, replacevar=rep)
2 
3DATA _null_;
4 INFILE &test2 lrecl=500;
5 INPUT;
6 IF _n_=2 THEN call symputx('test2resulta',_infile_);
7 IF _n_=3 THEN call symputx('test2resultb',_infile_);
8RUN;
9 
10%mp_assert(
11 iftrue=("&test2resulta" = "blahblah&rep.blah&rep. replace &rep.X"),
12 desc=Checking second replace 2nd row,
13 outds=work.test_results
14)
6 Code Block
DATA STEP Data
Explanation :
Creation of the third test case: handling strings containing dots.
Copied!
1%let test3="&sasjswork/file3.txt";
2%let str=%str(replace.string.with.dots );
3%let rep=%str( more.dots);
4DATA _null_;
5 file &test3 lrecl=500;
6 put 'blahblah';
7 put "blahblah&str.blah&str. replace &str.X";
8 put "blahbreplacewith&str.spacesahblah";
9RUN;
7 Code Block
MACRO CALL
Explanation :
Final execution and validation for the scenario with dots in character strings.
Copied!
1%mp_replace(&test3, findvar=str, replacevar=rep)
2 
3DATA _null_;
4 INFILE &test3 lrecl=500;
5 INPUT;
6 IF _n_=2 THEN call symputx('test3resulta',_infile_);
7 IF _n_=3 THEN call symputx('test3resultb',_infile_);
8RUN;
9 
10%mp_assert(
11 iftrue=("&test3resulta" = "blahblah&rep.blah&rep. replace &rep.X"),
12 desc=Checking third replace 2nd row (dots),
13 outds=work.test_results
14)
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.

Related Documentation

Aucune documentation spécifique pour cette catégorie.