Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mx_append2pgm.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mx_append2pgm.sas macro
4
5 Be sure to run <code>%let mcTestAppLoc=/Public/temp/macrocore;</code> when
6 running in Studio
7
8 <h4> SAS Macros </h4>
9 @li mf_getplatform.sas
10 @li mf_uid.sas
11 @li mp_assert.sas
12 @li mp_assertscope.sas
13 @li ms_createfile.sas
14 @li mv_createfile.sas
15 @li mm_createstp.sas
16 @li mx_append2pgm.sas
17 @li mx_getcode.sas
18
19**/
20
21/**
22 * Test 1 - Append content to an existing program and verify combined output
23 * Also checking for scope leakage
24 */
25
26/* create a unique name for the program */
27%let item=test_%mf_uid();
28
29/* create the initial program with some code */
30filename initpgm temp;
31data _null_;
32 file initpgm;
33 put '%put ORIGINAL LINE;';
34run;
35
36%macro setup_pgm();
37 %let platform=%mf_getplatform();
38 %if &platform=SASJS %then %do;
39 %ms_createfile(&mcTestAppLoc/temp/&item..sas, inref=initpgm)
40 %end;
41 %else %if &platform=SASVIYA %then %do;
42 %mv_createfile(path=&mcTestAppLoc/temp, name=&item..sas, inref=initpgm)
43 %end;
44 %else %do;
45 %let work=%sysfunc(pathname(work));
46 data _null_;
47 file "&work/&item..sas";
48 infile initpgm;
49 input;
50 put _infile_;
51 run;
52 %mm_createstp(stpname=&item
53 ,filename=&item..sas
54 ,directory=&work
55 ,tree=&mcTestAppLoc/temp
56 ,stptype=2
57 ,minify=NO
58 )
59 %end;
60%mend setup_pgm;
61%setup_pgm()
62
63/* create the content to append */
64filename toappnd temp;
65data _null_;
66 file toappnd;
67 put '%put APPENDED LINE;';
68run;
69
70/* run the macro under test with scope checks */
71%mp_assertscope(SNAPSHOT)
72%mx_append2pgm(&mcTestAppLoc/temp/&item, inref=toappnd)
73%mp_assertscope(COMPARE,
74 desc=Test 1: mx_append2pgm does not leak scope,
75 outds=work.test_results,
76 ignorelist=MC2_JADP1LEN MC2_JADP2LEN MC2_JADPNUM MC2_JADVLEN MC2_JADP3LEN
77)
78
79%mp_assert(
80 iftrue=(&syscc=0),
81 desc=Test 1: No errors after appending content to program,
82 outds=work.test_results
83)
84
85/**
86 * Test 2 - Verify the appended content is present
87 * Fetch the modified program and check both original and appended lines exist
88 */
89
90%let test2_orig=0;
91%let test2_appd=0;
92
93%macro verify_test2();
94 %let platform=%mf_getplatform();
95 %if &platform=SASVIYA %then %do;
96 filename verifrf filesrvc folderpath="&mcTestAppLoc/temp";
97 data _null_;
98 infile verifrf("&item..sas") lrecl=32000;
99 input;
100 if index(_infile_,'ORIGINAL LINE') then call symputx('test2_orig','1');
101 if index(_infile_,'APPENDED LINE') then call symputx('test2_appd','1');
102 run;
103 filename verifrf clear;
104 %end;
105 %else %do;
106 %mx_getcode(&mcTestAppLoc/temp/&item, outref=verifrf)
107 data _null_;
108 infile verifrf lrecl=32000;
109 input;
110 if index(_infile_,'ORIGINAL LINE') then call symputx('test2_orig','1');
111 if index(_infile_,'APPENDED LINE') then call symputx('test2_appd','1');
112 run;
113 %end;
114%mend verify_test2;
115%verify_test2()
116
117%mp_assert(
118 iftrue=(&test2_orig=1),
119 desc=Test 2a: Original content is preserved after append,
120 outds=work.test_results
121)
122
123%mp_assert(
124 iftrue=(&test2_appd=1),
125 desc=Test 2b: Appended content is present in modified program,
126 outds=work.test_results
127)
128
129/**
130 * Test 3 - Append multiple times to ensure repeated appends work
131 */
132filename toappd2 temp;
133data _null_;
134 file toappd2;
135 put '%put SECOND APPEND;';
136run;
137
138%mp_assertscope(SNAPSHOT)
139%mx_append2pgm(&mcTestAppLoc/temp/&item, inref=toappd2)
140%mp_assertscope(COMPARE,
141 desc=Test 3: mx_append2pgm does not leak scope on second call,
142 outds=work.test_results
143)
144
145/* verify all three pieces of content exist */
146%let test3_orig=0;
147%let test3_appd=0;
148%let test3_app2=0;
149
150%macro verify_test3();
151 %let platform=%mf_getplatform();
152 %if &platform=SASVIYA %then %do;
153 filename verifr2 filesrvc folderpath="&mcTestAppLoc/temp";
154 data _null_;
155 infile verifr2("&item..sas") lrecl=32000;
156 input;
157 if index(_infile_,'ORIGINAL LINE') then call symputx('test3_orig','1');
158 if index(_infile_,'APPENDED LINE') then call symputx('test3_appd','1');
159 if index(_infile_,'SECOND APPEND') then call symputx('test3_app2','1');
160 run;
161 filename verifr2 clear;
162 %end;
163 %else %do;
164 %mx_getcode(&mcTestAppLoc/temp/&item, outref=verifr2)
165 data _null_;
166 infile verifr2 lrecl=32000;
167 input;
168 if index(_infile_,'ORIGINAL LINE') then call symputx('test3_orig','1');
169 if index(_infile_,'APPENDED LINE') then call symputx('test3_appd','1');
170 if index(_infile_,'SECOND APPEND') then call symputx('test3_app2','1');
171 run;
172 %end;
173%mend verify_test3;
174%verify_test3()
175
176%mp_assert(
177 iftrue=(&test3_orig=1),
178 desc=Test 3a: Original content still present after second append,
179 outds=work.test_results
180)
181
182%mp_assert(
183 iftrue=(&test3_appd=1),
184 desc=Test 3b: First appended content still present after second append,
185 outds=work.test_results
186)
187
188%mp_assert(
189 iftrue=(&test3_app2=1),
190 desc=Test 3c: Second appended content is present,
191 outds=work.test_results
192)