<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id: XmlTestListener.class.php 13822 2009-11-12 14:55:15Z friebe $
 */

  uses(
    'unittest.TestListener', 
    'io.streams.OutputStreamWriter', 
    'xml.Tree',
    'util.collections.HashTable'
  );

  /**
   * Creates an XML file suitable for importing into Netbeans
   *
   * @test     None yet
   * @purpose  TestListener
   */
  class JunitXmlTestListener extends Object implements TestListener {
    public $out= NULL;
    protected $tree= NULL;
    protected $root= NULL;
    protected $classes= NULL;
    protected $overalltime= 0;


    /**
     * Constructor
     *
     * @param   io.streams.OutputStreamWriter out
     */
    public function __construct(OutputStreamWriter $out) {
      $this->out= $out;
      $this->tree= create(new Tree('testsuites'))->withRoot($this->root= new Node('testsuites'));
      
      $this->classes= create('new util.collections.HashTable<lang.XPClass, xml.Node>()');
    }

    /**
     * Tries to get class uri via reflection
     *
     * @param lang.XPClass class The class to return the URI for
     * @return string
     */
    private function getFileUri(XPClass $class) {
      try {
        $loader= $class->getClassLoader();
        $Urimethod= $loader->getClass()->getMethod('classURI');
        $Urimethod->setAccessible(TRUE);
        return $Urimethod->invoke($loader, $class->getName());
      } catch (Exception $ignored) {
        return '';
      }
    }

    /**
     * Tries to get method start line
     *
     * @param lang.XPClass $class
     * @param string $methodname
     * @return int
     */
    private function getStartLine(XPClass $class, $methodname) {
      try {
        return $class->_reflect->getMethod($methodname)->getStartLine();
      } catch (Exception $ignored) {
        return 0;
      }
    }
    
    /**
     * Called when a test case starts.
     *
     * @param   unittest.TestCase failure
     */
    public function testStarted(TestCase $case) {
      // NOOP
    }

    /**
     * Add test result node, if it does not yet exist.
     *
     * @param   unittest.TestCase case
     * @return  xml.Node
     */
    protected function testNode(TestCase $case) {
      $class= $case->getClass();
      if (!$this->classes->containsKey($class)) {
        $this->classes[$class]= $this->root->addChild(new Node('testsuite', NULL, array(
          'name'       => $class->getName(),
          'file'       => $this->getFileUri($class),
          'fullPackage'=> $class->getPackage()->getName(),
          'package'    => $class->getPackage()->getSimpleName(),
          'tests'      => 0,
          'assertions' => 0,
          'failures'   => 0,
          'errors'     => 0,
          'skipped'    => 0,
          'time'       => 0.0
        )));
      }

      return $this->classes[$class];
    }
    
    /**
     * Add test case information node and update suite information.
     *
     * @param   unittest.TestOutcome outcome
     * @param   string inc
     * @return  xml.Node
     */
    protected function addTestCase(TestOutcome $outcome, $inc= NULL) {
      $testClass= $outcome->test->getClass();

      // Update test count
      $n= $this->testNode($outcome->test);
      $n->setAttribute('tests', $n->getAttribute('tests')+ 1);
      $n->setAttribute('assertions', $n->getAttribute('assertions')+ $outcome->test->getAssertions());
      $inc && $n->setAttribute($inc, $n->getAttribute($inc)+ 1);

      //Update test counter
      $this->root->setAttribute('tests', $this->root->getAttribute('tests')+ 1);
      $this->root->setAttribute(
        'assertions',
        $this->root->getAttribute('assertions')+ $outcome->test->getAssertions()
      );
      $inc && $this->root->setAttribute($inc, $this->root->getAttribute($inc)+ 1);
      $this->overalltime+= $outcome->elapsed();
      $this->root->setAttribute('time', sprintf('%.6f', $this->overalltime));
      
      // Add testcase information
      return $n->addChild(new Node('testcase', NULL, array(
        'name'       => $outcome->test->getName(),
        'class'      => $testClass->getName(),
        'file'       => $this->getFileUri($testClass),
        'line'       => $this->getStartLine($testClass, $outcome->test->getName()),
        'assertions' => $outcome->test->getAssertions(),
        'time'       => sprintf('%.6f', $outcome->elapsed())
      )));
    }<commit_msg>- put elapsed time to testsuite node and not to root node # get rid of overall time member<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id: XmlTestListener.class.php 13822 2009-11-12 14:55:15Z friebe $
 */

  uses(
    'unittest.TestListener', 
    'io.streams.OutputStreamWriter', 
    'xml.Tree',
    'util.collections.HashTable'
  );

  /**
   * Creates an XML file suitable for importing into Netbeans
   *
   * @test     None yet
   * @purpose  TestListener
   */
  class JunitXmlTestListener extends Object implements TestListener {
    public $out= NULL;
    protected $tree= NULL;
    protected $root= NULL;
    protected $classes= NULL;


    /**
     * Constructor
     *
     * @param   io.streams.OutputStreamWriter out
     */
    public function __construct(OutputStreamWriter $out) {
      $this->out= $out;
      $this->tree= create(new Tree('testsuites'))->withRoot($this->root= new Node('testsuites'));
      
      $this->classes= create('new util.collections.HashTable<lang.XPClass, xml.Node>()');
    }

    /**
     * Tries to get class uri via reflection
     *
     * @param lang.XPClass class The class to return the URI for
     * @return string
     */
    private function getFileUri(XPClass $class) {
      try {
        $loader= $class->getClassLoader();
        $Urimethod= $loader->getClass()->getMethod('classURI');
        $Urimethod->setAccessible(TRUE);
        return $Urimethod->invoke($loader, $class->getName());
      } catch (Exception $ignored) {
        return '';
      }
    }

    /**
     * Tries to get method start line
     *
     * @param lang.XPClass $class
     * @param string $methodname
     * @return int
     */
    private function getStartLine(XPClass $class, $methodname) {
      try {
        return $class->_reflect->getMethod($methodname)->getStartLine();
      } catch (Exception $ignored) {
        return 0;
      }
    }
    
    /**
     * Called when a test case starts.
     *
     * @param   unittest.TestCase failure
     */
    public function testStarted(TestCase $case) {
      // NOOP
    }

    /**
     * Add test result node, if it does not yet exist.
     *
     * @param   unittest.TestCase case
     * @return  xml.Node
     */
    protected function testNode(TestCase $case) {
      $class= $case->getClass();
      if (!$this->classes->containsKey($class)) {
        $this->classes[$class]= $this->root->addChild(new Node('testsuite', NULL, array(
          'name'       => $class->getName(),
          'file'       => $this->getFileUri($class),
          'fullPackage'=> $class->getPackage()->getName(),
          'package'    => $class->getPackage()->getSimpleName(),
          'tests'      => 0,
          'assertions' => 0,
          'failures'   => 0,
          'errors'     => 0,
          'skipped'    => 0,
          'time'       => 0.0
        )));
      }

      return $this->classes[$class];
    }
    
    /**
     * Add test case information node and update suite information.
     *
     * @param   unittest.TestOutcome outcome
     * @param   string inc
     * @return  xml.Node
     */
    protected function addTestCase(TestOutcome $outcome, $inc= NULL) {
      $testClass= $outcome->test->getClass();

      // Update test count
      $n= $this->testNode($outcome->test);
      $n->setAttribute('tests', $n->getAttribute('tests')+ 1);
      $n->setAttribute('assertions', $n->getAttribute('assertions')+ $outcome->test->getAssertions());
      $n->setAttribute('time', $n->getAttribute('time')+ $outcome->elapsed());
      $inc && $n->setAttribute($inc, $n->getAttribute($inc)+ 1);

      //Update test counter
      $this->root->setAttribute('tests', $this->root->getAttribute('tests')+ 1);
      $this->root->setAttribute(
        'assertions',
        $this->root->getAttribute('assertions')+ $outcome->test->getAssertions()
      );
      $inc && $this->root->setAttribute($inc, $this->root->getAttribute($inc)+ 1);
      
      // Add testcase information
      return $n->addChild(new Node('testcase', NULL, array(
        'name'       => $outcome->test->getName(),
        'class'      => $testClass->getName(),
        'file'       => $this->getFileUri($testClass),
        'line'       => $this->getStartLine($testClass, $outcome->test->getName()),
        'assertions' => $outcome->test->getAssertions(),
        'time'       => sprintf('%.6f', $outcome->elapsed())
      )));
    }
{'fraction_non_alphanumeric': 0.1609546431399163, 'fraction_numerical': 0.007465218866644045, 'mean_word_length': 2.3594224924012157, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 173, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1886605', 'n_tokens_mistral': 2698, 'n_tokens_neox': 2641, 'n_words': 576}