Published on :
Macro Test CREATION_INTERNE

Test of the mf_getuniquelibref macro

This code is also available in: Deutsch Español Français
Awaiting validation
This SAS© script implements a series of unit tests for the `mf_getuniquelibref` macro, which is designed to generate unique libref names. It uses the assertion macros from the SASUnit framework (`%mp_assertscope` and `%mp_assert`). The script begins by saving the state of the macro environment (`%mp_assertscope(SNAPSHOT)`), then it tests the creation of valid librefs with standard prefixes ('lib', 'libref7'). For each valid case, it assigns the libref to the `work` library and uses `%mp_assert` to confirm that the operation completed without a system error (`%syscc=0`). A check of the macro environment (`%mp_assertscope(COMPARE)`) is performed after the first valid libref test. In the second part, the script tests invalid libref scenarios: prefixes exceeding 8 characters ('lib8char', 'invalidlib') or prefixes starting with a digit ('8adlib'). For these cases, `%mp_assert` is used to ensure that the `mf_getuniquelibref` macro returns 0, indicating a failure to generate the libref. All assertion results are logged in the `work.test_results` table.
Data Analysis

Type : CREATION_INTERNE


The script does not manipulate external data or data from standard SAS libraries like SASHELP. It operates on the creation and assignment of temporary librefs to the `work` library. Test results are stored in the `work.test_results` table, dynamically generated during script execution.

1 Code Block
SASUnit Macro Calls Data
Explanation :
This block performs tests for valid librefs. It initializes a snapshot of the environment, then uses `mf_getuniquelibref` to create two unique librefs ('libshort' and 'lib7') with valid prefixes. Each libref is then assigned to the `work` library. Assertions via `%mp_assert` are used to verify that the assignment (`LIBNAME`) completed without error (`%syscc=0`), and the results are stored in `work.test_results`.
Copied!
1/* check valid libs */
2%mp_assertscope(SNAPSHOT)
3%let libshort=%mf_getuniquelibref(prefix=lib);
4%mp_assertscope(COMPARE,ignorelist=LIBSHORT)
5LIBNAME &libshort (work);
6%mp_assert(
7 iftrue=(&syscc=0),
8 desc=Checking for valid libref &libshort,
9 outds=work.test_results
10)
11 
12%let lib7=%mf_getuniquelibref(prefix=libref7);
13LIBNAME &lib7 (work);
14%mp_assert(
15 iftrue=(&syscc=0),
16 desc=Checking for valid libref &lib7,
17 outds=work.test_results
18)
2 Code Block
SASUnit Macro Calls Data
Explanation :
This block tests cases where `mf_getuniquelibref` should fail. It attempts to generate librefs with invalid prefixes: 'lib8char' and 'invalidlib' (more than 8 characters), and '8adlib' (starting with a digit). For each test, `%mp_assert` verifies that the macro returns a value of 0, indicating that no valid libref could be created, and records these expected failures in `work.test_results`.
Copied!
1/* check for invalid libs */
2 
3%let lib8=%mf_getuniquelibref(prefix=lib8char);
4%mp_assert(
5 iftrue=(&lib8=0),
6 desc=Invalid prefix (8 chars),
7 outds=work.test_results
8)
9 
10%let liblong=%mf_getuniquelibref(prefix=invalidlib);
11%mp_assert(
12 iftrue=(&liblong=0),
13 desc=Checking for invalid libref (long),
14 outds=work.test_results
15)
16 
17%let badlib=%mf_getuniquelibref(prefix=8adlib);
18%mp_assert(
19 iftrue=(&badlib=0),
20 desc=Checking for invalid libref (8adlib),
21 outds=work.test_results
22)
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.
Copyright Info : Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de This file is part of SASUnit, the Unit testing framework for SAS(R) programs. For copyright information and terms of usage under the GNU Lesser General Public License see included file README.md or https://github.com/HMS-Analytical-Software/SASUnit/wiki/readme/.