Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_dirlist.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_dirlist.sas macro
4
5 <h4> SAS Macros </h4>
6 @li mf_nobs.sas
7 @li mf_mkdir.sas
8 @li mp_assert.sas
9 @li mp_assertscope.sas
10 @li mp_dirlist.sas
11
12**/
13
14/**
15 * make a directory structure
16 */
17
18%let root=%sysfunc(pathname(work))/top;
19%mf_mkdir(&root)
20%mf_mkdir(&root/a)
21%mf_mkdir(&root/b)
22%mf_mkdir(&root/a/d)
23%mf_mkdir(&root/a/e)
24%mf_mkdir(&root/a/e/f)
25libname test "&root/a/e/f";
26data test.ds1;
27 x=1;
28run;
29
30%mp_assertscope(SNAPSHOT)
31%mp_dirlist(path=&root, outds=myTable, maxdepth=MAX)
32%mp_assertscope(COMPARE,
33 desc=Test 1: mp_dirlist does not leak scope,
34 outds=work.test_results
35)
36
37%mp_assert(
38 iftrue=(%mf_nobs(work.mytable)=6),
39 desc=All levels returned,
40 outds=work.test_results
41)
42
43%mp_dirlist(path=&root, outds=myTable2, maxdepth=2)
44
45%mp_assert(
46 iftrue=(%mf_nobs(work.mytable2)=5),
47 desc=Top two levels returned,
48 outds=work.test_results
49)
50
51%mp_dirlist(path=&root, outds=work.myTable3, maxdepth=0)
52
53%mp_assert(
54 iftrue=(%mf_nobs(work.mytable3)=2),
55 desc=Top level returned,
56 outds=work.test_results
57)
58
59%mp_dirlist(path=&root/b, outds=work.myTable4)
60%mp_assert(
61 iftrue=(%mf_nobs(work.mytable4)=0),
62 desc=Empty table for empty directory,
63 outds=work.test_results
64)
65
66%mp_dirlist(path=&root/notexisting, outds=work.myTable5)
67%mp_assert(
68 iftrue=(%mf_nobs(work.mytable5)=0),
69 desc=Empty table for non-existing directory,
70 outds=work.test_results
71)
72
73/**
74 * maxobs caps the rows returned, and the scan stops at the cap
75 */
76%mp_dirlist(path=&root, outds=work.myTable6, maxdepth=MAX, maxobs=3)
77%mp_assert(
78 iftrue=(%mf_nobs(work.mytable6)=3),
79 desc=Rows are capped by maxobs,
80 outds=work.test_results
81)
82
83%mp_dirlist(path=&root, outds=work.myTable7, maxdepth=MAX, maxobs=100)
84%mp_assert(
85 iftrue=(%mf_nobs(work.mytable7)=6),
86 desc=A maxobs above the tree size returns everything,
87 outds=work.test_results
88)
89
90/**
91 * showparent adds the starting folder itself
92 */
93%mp_dirlist(path=&root, outds=work.myTable8, maxdepth=0, showparent=YES)
94%mp_assert(
95 iftrue=(%mf_nobs(work.mytable8)=3),
96 desc=Showparent adds one row for the parent,
97 outds=work.test_results
98)
99
100/**
101 * the level column reports depth below the parent
102 */
103data _null_;
104 set work.mytable end=eof;
105 retain maxlevel 0;
106 if level>maxlevel then maxlevel=level;
107 if eof then call symputx('maxlevel',maxlevel,'l');
108run;
109%mp_assert(
110 iftrue=(&maxlevel=3),
111 desc=Deepest entry is three levels below the parent,
112 outds=work.test_results
113)
114
115/**
116 * a file is reported as a file and a folder as a folder
117 */
118data _null_;
119 set work.mytable;
120 if filename='a' then call symputx('type_a',file_or_folder,'l');
121 if filename='ds1.sas7bdat' then call symputx('type_ds1',file_or_folder,'l');
122run;
123%mp_assert(
124 iftrue=("%upcase(&type_a)"="FOLDER"),
125 desc=A directory entry is classified as a folder,
126 outds=work.test_results
127)
128%mp_assert(
129 iftrue=("%upcase(&type_ds1)"="FILE"),
130 desc=A dataset file is classified as a file,
131 outds=work.test_results
132)
133
134
135/**
136 * a blank path is the platform roots - drives on Windows, / on Unix
137 */
138%mp_dirlist(path=, outds=work.myTable9)
139%mp_assert(
140 iftrue=(%mf_nobs(work.mytable9)>=1),
141 desc=A blank path returns the platform roots,
142 outds=work.test_results
143)
144data _null_;
145 set work.mytable9(obs=1);
146 call symputx('rootpath',filepath,'l');
147run;
148%mp_assert(
149 iftrue=(%length(&rootpath)>=1),
150 desc=The root entry carries a path,
151 outds=work.test_results
152)
153
154/**
155 * dump results to the log for offline inspection
156 */
157data _null_;
158 set work.test_results;
159 putlog 'TEST_RESULT_LINE: ' test_result ' - ' test_description;
160run;