|
1 #!/usr/bin/php |
|
2 <?php |
|
3 ## By Peter 'grin' Gervai, 2022 |
|
4 ## GPLv3+ and CC-By-Sa-4.0 |
|
5 ## |
|
6 ## $Id$ |
|
7 ## |
|
8 ## This file is the testing "environment" for the regex matching. |
|
9 ## Tries to test unlink (removal) and replacement rules. |
|
10 ## |
|
11 ## The input files are in _t_*.in, and the unlink and replace |
|
12 ## expected results are in _t_*.unlink and _t_*.replace. |
|
13 ## The tests are listed below in the array, with the original |
|
14 ## image and its expected good matching pattern, which is also |
|
15 ## verified. The replacement image also set there. |
|
16 ## |
|
17 |
|
18 $DEBUG=0; |
|
19 |
|
20 require_once( './matcher.inc' ); |
|
21 require_once( './debug.inc' ); |
|
22 |
|
23 $d = new Debug; |
|
24 #$d->set_level(9); # trace |
|
25 |
|
26 $matcher = new Matcher($d); |
|
27 |
|
28 |
|
29 # files are in |
|
30 # input: _t_{id}.in |
|
31 # test data: _t_{id}.data |
|
32 # good replacement: _t_{id}.replace |
|
33 # good unlink: _t_{id}.unlink |
|
34 |
|
35 $dir_tests = './test'; |
|
36 $testnames = collect_test_names($dir_tests); |
|
37 |
|
38 $data_keys = array( 'img', 'pattern', 'repl_img' ); |
|
39 |
|
40 foreach ($testnames as $t) { |
|
41 $tests[$t] = get_test_data( $dir_tests, $t, $data_keys ); |
|
42 } |
|
43 |
|
44 $test_res = array( 'ok' => 0, 'bad' => 0 ); |
|
45 |
|
46 # run |
|
47 foreach ($tests as $key => $t) { |
|
48 #print( "Running test '$key'..." ); |
|
49 #print("IN=" . $t["in"] . "\n"); |
|
50 #print("Im=" . $t["img"] . "\n"); |
|
51 #print("RE=" . $t["unlink"] . "\n"); |
|
52 |
|
53 $img = $t["img"]; |
|
54 $pattern = test_prepare_pattern( $img ); |
|
55 if( $DEBUG ) {print(" Pat=$pattern\n");} |
|
56 $res = test_result( $pattern, $t["pattern"], "pattern", $img, $key ); |
|
57 if( $res == 1 ) { |
|
58 $test_res['bad']++; |
|
59 } else { |
|
60 $test_res['ok']++; |
|
61 } |
|
62 |
|
63 $text = read_text_from_file( "${dir_tests}/_t_${key}.in" ); |
|
64 $t_unlink = read_text_from_file( "${dir_tests}/_t_${key}.unlink" ); |
|
65 $t_replace = read_text_from_file( "${dir_tests}/_t_${key}.replace" ); |
|
66 |
|
67 $res = test_regex_unlink( $text, $pattern ); |
|
68 $res = test_result( $res, $t_unlink, "unlink", $img, $key ); |
|
69 if( $res == 1 ) { |
|
70 $test_res['bad']++; |
|
71 } else { |
|
72 $test_res['ok']++; |
|
73 } |
|
74 |
|
75 $res = test_regex_replace( $text, $pattern, $t["repl_img"] ); |
|
76 $res = test_result( $res, $t_replace, "replace", $img, $key ); |
|
77 if( $res == 1 ) { |
|
78 $test_res['bad']++; |
|
79 } else { |
|
80 $test_res['ok']++; |
|
81 } |
|
82 } |
|
83 |
|
84 print( "\nResults:\n" ); |
|
85 print( "OK : " . $test_res['ok'] . "\nBAD: " . $test_res['bad'] ."\n" ); |
|
86 |
|
87 |
|
88 ## collect the name of the tests (verbose code :)) |
|
89 function collect_test_names($dir) { |
|
90 $dlist = scandir($dir); |
|
91 |
|
92 foreach ($dlist as $key => $val) { |
|
93 if( !is_dir( $dir . '/' . $val ) && preg_match( '/^_t_(.+)\.data$/', $val, $matches ) ) { |
|
94 $tests[] = $matches[1]; |
|
95 } |
|
96 } |
|
97 return $tests; |
|
98 } |
|
99 |
|
100 |
|
101 function get_test_data($dir, $t, $keys) { |
|
102 $f = fopen( "${dir}/_t_${t}.data", 'r'); |
|
103 if( $f ) { |
|
104 while( ($line = fgets($f)) !== false ) { |
|
105 // process a line |
|
106 if( preg_match( '/^(\S+)\s*:\s*(.+)$/', $line, $matches ) ) { |
|
107 # print( "Test $t ${matches[1]} => ${matches[2]}\n" ); |
|
108 if( !in_array( $matches[1], $keys ) ) { |
|
109 trigger_error( "Unknown key '$matches[1]' in ${t}.data", E_USER_ERROR ); |
|
110 } |
|
111 $test[$matches[1]] = $matches[2]; |
|
112 } |
|
113 } |
|
114 } else { |
|
115 trigger_error( "test $t data file is missing", E_USER_ERROR ); |
|
116 } |
|
117 |
|
118 foreach ($keys as $k) { |
|
119 if( !array_key_exists( $k, $test ) ) { |
|
120 trigger_error( "test $t data is missing $k key", E_USER_ERROR ); |
|
121 } |
|
122 } |
|
123 |
|
124 return $test; |
|
125 } |
|
126 |
|
127 |
|
128 function test_result( $result, $expected, $name, $img, $id ) { |
|
129 global $dir_tests; |
|
130 if( $result <> $expected ) { |
|
131 #print( "${test} BAD $name:$id ($img)! result=\n$result\n\nexpect=\n$expected\n\n" ); |
|
132 if( $name == 'pattern' ) { |
|
133 print( "${id} BAD $name ($img)! expected '$expected', result '$result', fix in ${dir_tests}/_t_${id}.data, expected is in _bad_${id}.${name}\n" ); |
|
134 } else { |
|
135 print( "${id} BAD $name ($img)! diff -u ${dir_tests}/_t_${id}.${name} _bad_${id}.${name}\n" ); |
|
136 } |
|
137 write_text_to_file( "_bad_${id}.${name}", $result ); |
|
138 return 1; |
|
139 } else { |
|
140 print( "${id} OK $name ($img)!\n" ); |
|
141 } |
|
142 return 0; |
|
143 } |
|
144 |
|
145 |
|
146 function read_text_from_file( $fname ) { |
|
147 $f = fopen( $fname, 'r') or die( "Cannot read file $fname" ); |
|
148 $text = fread( $f, 1e8 ); |
|
149 fclose($f); |
|
150 return $text; |
|
151 } |
|
152 |
|
153 |
|
154 function write_text_to_file($fname, $text) { |
|
155 #print("Recording into $fname text '$text'"); |
|
156 $f = fopen( $fname, 'w') or die( "Cannot create file $fname" ); |
|
157 $res = fwrite( $f, $text ); |
|
158 fclose($f); |
|
159 } |
|
160 |
|
161 |
|
162 function test_prepare_pattern( $file ) { |
|
163 global $matcher; |
|
164 return $matcher->matcher_prepare_pattern( $file ); |
|
165 } |
|
166 |
|
167 |
|
168 function test_regex_unlink( $text, $pattern ) { |
|
169 global $matcher; |
|
170 return $matcher->matcher_do_unlink( $text, $pattern ); |
|
171 } |
|
172 |
|
173 |
|
174 function test_regex_replace( $text, $pattern, $newimg ) { |
|
175 global $matcher; |
|
176 return $matcher->matcher_do_replacement( $text, $pattern, $newimg ); |
|
177 } |