function test($name, $data, $datatype, $plottype, $expect_fail, $errmatch = '') { global $test_verbose, $php_warning, $n_tests, $n_pass, $n_fail; global $test_debug_images; $n_tests++; $php_warning = ''; // Initialize empty. Error handler will append. $details = ''; // Why it passed or failed. if ($test_verbose) { echo "Test case {$n_tests}: {$name}\n"; } $plot = new PHPlot_noerr(); if (empty($test_debug_images)) { $plot->SetPrintImage(False); // Don't produce an image if OK } else { $plot->SetTitle("Case {$n_tests}: {$name}"); $plot->SetOutputFile($test_debug_images . sprintf("%03d", $n_tests) . ".png"); $plot->SetIsInline(True); } $plot->SetDataType($datatype); $plot->SetPlotType($plottype); $failed = !$plot->SetDataValues($data) || !$plot->DrawGraph() || !empty($plot->test_error_text); $fail_message = $plot->test_error_text; if ($test_verbose) { if ($failed) { echo "Result: PHPlot error: '{$fail_message}'\n"; } else { echo "Result: PHPlot graph, no error message\n"; } } # Evaluate pass/fail status of the test case: if ($expect_fail) { if ($failed) { if (preg_match("{$errmatch}i", $fail_message)) { $details = "(produced error as expected)\n"; $test_passed = True; } else { $details = " Expecting an error, got a different error\n" . " Actual message: {$fail_message}\n" . " Expected to match: {$errmatch}\n"; $test_passed = False; } } else { $details = " Expecting an error, but produced a graph.\n"; $test_passed = False; } } else { // Not expected to fail if ($failed) { $details = " Expected a graph, but got this error instead:\n" . " {$fail_message}\n"; $test_passed = False; } else { $details = "(produced graph as expected)\n"; $test_passed = True; } } // Fail the test if any notice or warnings were produced: if (!empty($php_warning)) { if ($test_passed) { // Replace the "test passed" message. $details = "Test would have passed except for PHP warnings or notices:\n" . $php_warning . "\n"; $test_passed = False; } else { $details .= "Test also failed due to PHP warnings or notices:\n" . $php_warning . "\n"; } } if ($test_passed) { $n_pass++; if ($test_verbose) { echo "Pass {$details}"; } } else { $n_fail++; echo "Failed test case {$n_tests}: {$name}\n{$details}"; } if ($test_verbose) { echo "\n"; } }
function test($name, $plot_type, $data_type, $expected) { global $test_verbose, $n_tests, $n_pass, $n_fail; $n_tests++; $title = "Test case {$n_tests}: {$name}"; if ($test_verbose) { echo "{$title}\n"; } $data = make_data_array($plot_type, $data_type); # See note above on error handler returning - need to check each method # here and stop if any returns FALSE, until the last. $plot = new PHPlot_noerr(); if (!$plot->SetPlotType($plot_type) || !$plot->SetDataType($data_type) || !$plot->SetPrintImage(False) || !$plot->SetDataValues($data)) { $n_fail++; echo "Failed test case {$n_tests}: {$name} - Unexpected early error:\n" . " {$plot->test_error_text}\n"; return; } $plot->DrawGraph(); # Regular expression match for error: unsupported data type for plot type. $errmatch = "Data type '[^']*' is not valid for '[^']*' plots"; # Examine result - if there was an error message or not - based on # the expected outcome. $err = $plot->test_error_text; if ($expected) { # Plot type was expected to accept this data type. if (empty($err)) { $test_passed = 1; } else { $test_passed = 0; $details = "Plot type did not accept this data type." . " Error is:\n {$err}"; } } else { # Plot type was expected to NOT accept this data type. if (empty($err)) { $test_passed = 0; $details = "Plot type did not report an error for this data type."; } elseif (preg_match("/{$errmatch}/", $err)) { $test_passed = 1; } else { $test_passed = 0; $details = "Plot type reported an error, but not the expected" . " error:\n {$err}"; } } if ($test_passed) { $n_pass++; } else { $n_fail++; echo "Failed test case {$n_tests}: {$name}\n{$details}\n"; } }