| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  | ! Copyright (C) 2011 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2011-08-27 13:52:01 -04:00
										 |  |  | USING: accessors assocs fry io kernel math prettyprint | 
					
						
							|  |  |  | quotations sequences sequences.deep splitting strings | 
					
						
							| 
									
										
										
										
											2011-08-27 17:12:41 -04:00
										 |  |  | tools.annotations vocabs words arrays words.symbol | 
					
						
							| 
									
										
										
										
											2011-11-29 00:26:06 -05:00
										 |  |  | combinators.short-circuit namespaces tools.test | 
					
						
							| 
									
										
										
										
											2011-11-01 19:46:02 -04:00
										 |  |  | combinators continuations classes ;
 | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  | IN: tools.coverage | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: coverage < identity-tuple executed? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <coverage> coverage | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-29 00:26:06 -05:00
										 |  |  | SYMBOL: covered | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | : flag-covered ( coverage -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-29 00:26:06 -05:00
										 |  |  |     covered get-global [ t >>executed? ] when drop ;
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-29 00:26:06 -05:00
										 |  |  | : coverage-on ( -- ) t covered set-global ;
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-29 00:26:06 -05:00
										 |  |  | : coverage-off ( -- ) f covered set-global ;
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: add-coverage ( object -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: remove-coverage ( object -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: reset-coverage ( object -- )
 | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-27 02:04:02 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : private-vocab-name ( string -- string' )
 | 
					
						
							|  |  |  |     ".private" ?tail drop ".private" append ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-27 17:12:41 -04:00
										 |  |  | : coverage-words ( string -- words )
 | 
					
						
							| 
									
										
										
										
											2011-11-01 19:46:02 -04:00
										 |  |  |     words [ { [ primitive? not ] [ symbol? not ] [ predicate? not ] } 1&& ] filter ;
 | 
					
						
							| 
									
										
										
										
											2011-08-27 17:12:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:52:01 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:34:05 -04:00
										 |  |  | : each-word ( string quot -- )
 | 
					
						
							| 
									
										
										
										
											2011-08-27 01:56:02 -04:00
										 |  |  |     over ".private" tail? [ | 
					
						
							| 
									
										
										
										
											2011-08-27 17:12:41 -04:00
										 |  |  |         [ coverage-words ] dip each
 | 
					
						
							| 
									
										
										
										
											2011-08-27 01:56:02 -04:00
										 |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2011-08-27 17:12:41 -04:00
										 |  |  |         [ [ private-vocab-name coverage-words ] dip each ] | 
					
						
							|  |  |  |         [ [ coverage-words ] dip each ] 2bi
 | 
					
						
							| 
									
										
										
										
											2011-08-27 01:56:02 -04:00
										 |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-27 16:54:25 -04:00
										 |  |  | : map-words ( string quot -- sequence )
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:34:05 -04:00
										 |  |  |     over ".private" tail? [ | 
					
						
							| 
									
										
										
										
											2011-08-27 17:12:41 -04:00
										 |  |  |         [ coverage-words ] dip map
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:34:05 -04:00
										 |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2011-08-27 17:12:41 -04:00
										 |  |  |         [ [ private-vocab-name coverage-words ] dip map ] | 
					
						
							|  |  |  |         [ [ coverage-words ] dip map ] 2bi append
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:34:05 -04:00
										 |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | M: string add-coverage | 
					
						
							|  |  |  |     [ add-coverage ] each-word ;
 | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | M: string remove-coverage | 
					
						
							|  |  |  |     [ remove-coverage ] each-word ;
 | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | M: word add-coverage  | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  |     H{ } clone [ "coverage" set-word-prop ] 2keep
 | 
					
						
							|  |  |  |     '[ | 
					
						
							|  |  |  |         \ coverage new [ _ set-at ] 2keep
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  |         '[ _ flag-covered ] prepend
 | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  |     ] deep-annotate ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | M: word remove-coverage | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  |     [ reset ] [ f "coverage" set-word-prop ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | M: string reset-coverage | 
					
						
							|  |  |  |     [ reset-coverage ] each-word ;
 | 
					
						
							| 
									
										
										
										
											2011-08-27 01:56:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | M: word reset-coverage | 
					
						
							|  |  |  |     [ dup coverage? [ f >>executed? ] when drop ] each-word ;
 | 
					
						
							| 
									
										
										
										
											2011-08-27 01:56:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  | GENERIC: coverage ( object -- seq )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: string coverage | 
					
						
							| 
									
										
										
										
											2011-08-27 16:54:25 -04:00
										 |  |  |     [ dup coverage 2array ] map-words ;
 | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: word coverage ( word -- seq )
 | 
					
						
							|  |  |  |     "coverage" word-prop >alist
 | 
					
						
							|  |  |  |     [ drop executed?>> not ] assoc-filter values ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: coverage. ( object -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: string coverage. | 
					
						
							| 
									
										
										
										
											2011-08-27 17:12:41 -04:00
										 |  |  |     [ coverage. ] each-word ;
 | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | : pair-coverage. ( word quots -- )
 | 
					
						
							|  |  |  |     dup empty? [ | 
					
						
							|  |  |  |         2drop
 | 
					
						
							| 
									
										
										
										
											2011-08-26 15:32:41 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         [ name>> ":" append print ] | 
					
						
							| 
									
										
										
										
											2011-08-27 16:54:25 -04:00
										 |  |  |         [ [ "    " write . ] each ] bi*
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: word coverage. | 
					
						
							|  |  |  |     dup coverage pair-coverage. ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sequence coverage. | 
					
						
							|  |  |  |     [ first2 pair-coverage. ] each ;
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:34:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:52:01 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:34:05 -04:00
										 |  |  | GENERIC: count-callables ( object -- n )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: string count-callables | 
					
						
							| 
									
										
										
										
											2011-08-27 16:54:25 -04:00
										 |  |  |     [ count-callables ] map-words sum ;
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:34:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: word count-callables | 
					
						
							| 
									
										
										
										
											2011-11-01 19:46:02 -04:00
										 |  |  |     def>> [ callable? ] deep-filter length ;
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:34:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-27 13:52:01 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 22:12:05 -04:00
										 |  |  | : test-coverage ( vocab -- coverage )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         add-coverage | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         dup '[ | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 _ | 
					
						
							|  |  |  |                 [ coverage-on test coverage-off ] | 
					
						
							|  |  |  |                 [ coverage ] bi
 | 
					
						
							|  |  |  |             ] [ _ remove-coverage ] [ ] cleanup
 | 
					
						
							|  |  |  |         ] call
 | 
					
						
							|  |  |  |     ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-27 16:54:25 -04:00
										 |  |  | : %coverage ( string -- x )
 | 
					
						
							| 
									
										
										
										
											2011-11-01 19:46:02 -04:00
										 |  |  |     [ test-coverage values concat length ] | 
					
						
							| 
									
										
										
										
											2011-08-27 16:54:25 -04:00
										 |  |  |     [ count-callables ] bi [ swap - ] keep /f ; inline
 |