source: ccr/trunk/p/GPLUNIT.m@ 109

Last change on this file since 109 was 86, checked in by George Lilly, 16 years ago

the Variable "I" was being stepped on in the Meds processing.. newed it.

File size: 6.1 KB
Line 
1GPLUNIT ; CCDCCR/GPL - Unit Testing Library; 5/07/08
2 ;;0.1;CCDCCR;nopatch;noreleasedate
3 ;Copyright 2008 WorldVistA. Licensed under the terms of the GNU
4 ;General Public License See attached copy of the License.
5 ;
6 ;This program is free software; you can redistribute it and/or modify
7 ;it under the terms of the GNU General Public License as published by
8 ;the Free Software Foundation; either version 2 of the License, or
9 ;(at your option) any later version.
10 ;
11 ;This program is distributed in the hope that it will be useful,
12 ;but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;GNU General Public License for more details.
15 ;
16 ;You should have received a copy of the GNU General Public License along
17 ;with this program; if not, write to the Free Software Foundation, Inc.,
18 ;51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ;
20 W "This is a unit testing library",!
21 W !
22 Q
23 ;
24ZT(ZARY,BAT,TST) ; private routine to add a test case to the ZARY array
25 ; ZARY IS PASSED BY REFERENCE
26 ; BAT is a string identifying the test battery
27 ; TST is a test which will evaluate to true or false
28 ; I '$G(ZARY) D
29 ; . S ZARY(0)=0 ; initially there are no elements
30 ; W "GOT HERE LOADING "_TST,!
31 N CNT ; count of array elements
32 S CNT=ZARY(0) ; contains array count
33 S CNT=CNT+1 ; increment count
34 S ZARY(CNT)=TST ; put the test in the array
35 I $D(ZARY(BAT)) D ; NOT THE FIRST TEST IN BATTERY
36 . N II,TN ; TEMP FOR ENDING TEST IN BATTERY
37 . S II=$P(ZARY(BAT),"^",2)
38 . S $P(ZARY(BAT),"^",2)=II+1
39 I '$D(ZARY(BAT)) D ; FIRST TEST IN THIS BATTERY
40 . S ZARY(BAT)=CNT_"^"_CNT ; FIRST AND LAST TESTS IN BATTERY
41 . S ZARY("TESTS",BAT)="" ; PUT THE BATTERY IN THE TESTS INDEX
42 . ; S TN=$NA(ZARY("TESTS"))
43 . ; D PUSH^GPLXPATH(TN,BAT)
44 S ZARY(0)=CNT ; update the array counter
45 Q
46 ;
47ZLOAD(ZARY,ROUTINE) ; load tests into ZARY which is passed by reference
48 ; ZARY IS PASSED BY NAME
49 ; ZARY = name of the root, closed array format (e.g., "^TMP($J)")
50 ; ROUTINE = NAME OF THE ROUTINE - PASSED BY VALUE
51 K @ZARY
52 S @ZARY@(0)=0 ; initialize array count
53 N LINE,LABEL,BODY
54 N INTEST S INTEST=0 ; switch for in the test case section
55 N SECTION S SECTION="[anonymous]" ; test case section
56 ;
57 N NUM F NUM=1:1 S LINE=$T(+NUM^@ROUTINE) Q:LINE="" D
58 . I LINE?." "1";;><TEST>".E S INTEST=1 ; entering test section
59 . I LINE?." "1";;><TEMPLATE>".E S INTEST=1 ; entering TEMPLATE section
60 . I LINE?." "1";;></TEST>".E S INTEST=0 ; leaving test section
61 . I LINE?." "1";;></TEMPLATE>".E S INTEST=0 ; leaving TEMPLATE section
62 . I INTEST D ; within the testing section
63 . . I LINE?." "1";;><".E D ; section name found
64 . . . S SECTION=$P($P(LINE,";;><",2),">",1) ; pull out name
65 . . I LINE?." "1";;>>".E D ; test case found
66 . . . D ZT(.@ZARY,SECTION,$P(LINE,";;>>",2)) ; put the test in the array
67 S @ZARY@("ALL")="1"_"^"_@ZARY@(0) ; MAKE A BATTERY FOR ALL
68 Q
69 ;
70ZTEST(ZARY,WHICH) ; try out the tests using a passed array ZTEST
71 N ZI,ZX,ZR,ZP
72 S DEBUG=0
73 ; I WHICH="ALL" D Q ; RUN ALL THE TESTS
74 ; . W "DOING ALL",!
75 ; . N J,NT
76 ; . S NT=$NA(ZARY("TESTS"))
77 ; . W NT,@NT@(0),!
78 ; . F J=1:1:@NT@(0) D ;
79 ; . . W @NT@(J),!
80 ; . . D ZTEST^GPLUNIT(@ZARY,@NT@(J))
81 I '$D(ZARY(WHICH)) D ; TEST SECTION DOESN'T EXIST
82 . W "ERROR -- TEST SECTION DOESN'T EXIST -> ",WHICH,!
83 . Q ; EXIT
84 N FIRST,LAST
85 S FIRST=$P(ZARY(WHICH),"^",1)
86 S LAST=$P(ZARY(WHICH),"^",2)
87 F ZI=FIRST:1:LAST D
88 . I ZARY(ZI)?1">"1.E D ; NOT A TEST, JUST RUN THE STATEMENT
89 . . S ZP=$E(ZARY(ZI),2,$L(ZARY(ZI)))
90 . . ; W ZP,!
91 . . S ZX=ZP
92 . . W "RUNNING: "_ZP
93 . . X ZX
94 . . W "..SUCCESS: ",WHICH,!
95 . I ZARY(ZI)?1"?"1.E D ; THIS IS A TEST
96 . . S ZP=$E(ZARY(ZI),2,$L(ZARY(ZI)))
97 . . S ZX="S ZR="_ZP
98 . . W "TRYING: "_ZP
99 . . X ZX
100 . . W $S(ZR=1:"..PASSED ",1:"..FAILED "),!
101 . . I '$D(TPASSED) D ; NOT INITIALIZED YET
102 . . . S TPASSED=0 S TFAILED=0
103 . . I ZR S TPASSED=TPASSED+1
104 . . I 'ZR S TFAILED=TFAILED+1
105 Q
106 ;
107TEST ; RUN ALL THE TEST CASES
108 N ZTMP
109 D ZLOAD(.ZTMP)
110 D ZTEST(.ZTMP,"ALL")
111 W "PASSED: ",TPASSED,!
112 W "FAILED: ",TFAILED,!
113 W !
114 W "THE TESTS!",!
115 ZWR ZTMP
116 Q
117 ;
118GTSTS(GTZARY,RTN) ; return an array of test names
119 N I,J S I="" S I=$O(GTZARY("TESTS",I))
120 F J=0:0 Q:I="" D
121 . D PUSH^GPLXPATH(RTN,I)
122 . S I=$O(GTZARY("TESTS",I))
123 Q
124 ;
125TESTALL(RNM) ; RUN ALL THE TESTS
126 N ZI,J,TZTMP,TSTS,TOTP,TOTF
127 S TOTP=0 S TOTF=0
128 D ZLOAD^GPLUNIT("TZTMP",RNM)
129 D GTSTS(.TZTMP,"TSTS")
130 F ZI=1:1:TSTS(0) D ;
131 . S TPASSED=0 S TFAILED=0
132 . D ZTEST^GPLUNIT(.TZTMP,TSTS(ZI))
133 . S TOTP=TOTP+TPASSED
134 . S TOTF=TOTF+TFAILED
135 . S $P(TSTS(ZI),"^",2)=TPASSED
136 . S $P(TSTS(ZI),"^",3)=TFAILED
137 F I=1:1:TSTS(0) D ;
138 . W "TEST=> ",$P(TSTS(ZI),"^",1)
139 . W " PASSED=>",$P(TSTS(ZI),"^",2)
140 . W " FAILED=>",$P(TSTS(ZI),"^",3),!
141 W "TOTAL=> PASSED:",TOTP," FAILED:",TOTF,!
142 Q
143 ;
144TLIST(ZARY) ; LIST ALL THE TESTS
145 ; THEY ARE MARKED AS ;;><TESTNAME> IN THE TEST CASES
146 ; ZARY IS PASSED BY REFERENCE
147 N I,J,K S I="" S I=$O(ZARY("TESTS",I))
148 S K=1
149 F J=0:0 Q:I="" D
150 . ; W "I IS NOW=",I,!
151 . W I," "
152 . S I=$O(ZARY("TESTS",I))
153 . S K=K+1 I K=6 D
154 . . W !
155 . . S K=1
156 Q
157 ;
Note: See TracBrowser for help on using the repository browser.