/Library/SimpleTest/extensions/coverage/test/coverage_calculator_test.php

https://github.com/GunioRobot/eglooframework · PHP · 65 lines · 55 code · 10 blank · 0 comment · 0 complexity · 6f21fb2c8c66aafb2f17b477e730c150 MD5 · raw file

  1. <?php
  2. require_once(dirname(__FILE__) . '/../../../autorun.php');
  3. class CoverageCalculatorTest extends UnitTestCase {
  4. function skip() {
  5. $this->skipIf(
  6. !file_exists('DB/sqlite.php'),
  7. 'The Coverage extension needs to have PEAR installed');
  8. }
  9. function setUp() {
  10. require_once dirname(__FILE__) .'/../coverage_calculator.php';
  11. $this->calc = new CoverageCalculator();
  12. }
  13. function testVariables() {
  14. $coverage = array('file' => array(1,1,1,1));
  15. $untouched = array('missed-file');
  16. $variables = $this->calc->variables($coverage, $untouched);
  17. $this->assertEqual(4, $variables['totalLoc']);
  18. $this->assertEqual(100, $variables['totalPercentCoverage']);
  19. $this->assertEqual(4, $variables['totalLinesOfCoverage']);
  20. $expected = array('file' => array('byFileReport' => 'file.html', 'percentage' => 100));
  21. $this->assertEqual($expected, $variables['coverageByFile']);
  22. $this->assertEqual(50, $variables['filesTouchedPercentage']);
  23. $this->assertEqual($untouched, $variables['untouched']);
  24. }
  25. function testPercentageCoverageByFile() {
  26. $coverage = array(0,0,0,1,1,1);
  27. $results = array();
  28. $this->calc->percentCoverageByFile($coverage, 'file', $results);
  29. $pct = $results[0];
  30. $this->assertEqual(50, $pct['file']['percentage']);
  31. $this->assertEqual('file.html', $pct['file']['byFileReport']);
  32. }
  33. function testTotalLoc() {
  34. $this->assertEqual(13, $this->calc->totalLoc(10, array(1,2,3)));
  35. }
  36. function testLineCoverage() {
  37. $this->assertEqual(10, $this->calc->lineCoverage(10, -1));
  38. $this->assertEqual(10, $this->calc->lineCoverage(10, 0));
  39. $this->assertEqual(11, $this->calc->lineCoverage(10, 1));
  40. }
  41. function testTotalCoverage() {
  42. $this->assertEqual(11, $this->calc->totalCoverage(10, array(-1,1)));
  43. }
  44. static function getAttribute($element, $attribute) {
  45. $a = $element->attributes();
  46. return $a[$attribute];
  47. }
  48. static function dom($stream) {
  49. rewind($stream);
  50. $actual = stream_get_contents($stream);
  51. $html = DOMDocument::loadHTML($actual);
  52. return simplexml_import_dom($html);
  53. }
  54. }
  55. ?>