<commit_before><?php namespace net\xp_framework\unittest\util;

use unittest\TestCase;
use util\Observable;


/**
 * Test Observable class
 *
 * @see  xp://util.Observable
 */
class ObservableTest extends TestCase {
  protected static $observable;

  /**
   * Creates observable
   */
  #[@beforeClass]
  public static function defineObservable() {
    self::$observable= \lang\ClassLoader::defineClass('net.xp_framework.unittest.util.ObservableFixture', 'util.Observable', array(), '{
      private $value= 0;

      public function setValue($value) {
        $this->value= $value;
        $this->setChanged();
        $this->notifyObservers();
      }

      public function getValue() {
        return $this->value;
      }
    }');
  }

  /**
   * Tests hasChanged() method
   */
  #[@test]
  public function originally_unchanged() {
    $o= self::$observable->newInstance();
    $this->assertFalse($o->hasChanged());
  }

  /**
   * Tests setChanged() method
   */
  #[@test]
  public function changed() {
    $o= self::$observable->newInstance();
    $o->setChanged();
    $this->assertTrue($o->hasChanged());
  }

  /**
   * Tests clearChanged() method
   */
  #[@test]
  public function change_cleared() {
    $o= self::$observable->newInstance();
    $o->setChanged();
    $o->clearChanged();
    $this->assertFalse($o->hasChanged());
  }

  /**
   * Tests addObserver() method
   */
  #[@test]
  public function add_observer_returns_added_observer() {
    $observer= newinstance('util.Observer', array(), '{
      public function update($obs, $arg= NULL) { /* Intentionally empty */ }
    }');
    $o= self::$observable->newInstance();
    $this->assertEquals($observer, $o->addObserver($observer));
  }

  /**
   * Tests notifyObservers() method
   */
  #[@test]
  public function observer_gets_called_with_observable() {
    $observer= newinstance('util.Observer', array(), '{
      public $calls= array();
      public function update($obs, $arg= NULL) {
        $this->calls[]= array($obs, $arg);
      }
    }');
    $o= self::$observable->newInstance();
    $o->addObserver($observer);
    $o->setValue(5);
    $this->assertEquals(array(array($o, null)), $observer->calls);
  }
}<commit_msg>Convert to newinstance with closure definitions<commit_after><?php namespace net\xp_framework\unittest\util;

use unittest\TestCase;
use util\Observable;


/**
 * Test Observable class
 *
 * @see  xp://util.Observable
 */
class ObservableTest extends TestCase {
  protected static $observable;

  /**
   * Creates observable
   */
  #[@beforeClass]
  public static function defineObservable() {
    self::$observable= \lang\ClassLoader::defineClass('net.xp_framework.unittest.util.ObservableFixture', 'util.Observable', array(), '{
      private $value= 0;

      public function setValue($value) {
        $this->value= $value;
        $this->setChanged();
        $this->notifyObservers();
      }

      public function getValue() {
        return $this->value;
      }
    }');
  }

  /**
   * Tests hasChanged() method
   */
  #[@test]
  public function originally_unchanged() {
    $o= self::$observable->newInstance();
    $this->assertFalse($o->hasChanged());
  }

  /**
   * Tests setChanged() method
   */
  #[@test]
  public function changed() {
    $o= self::$observable->newInstance();
    $o->setChanged();
    $this->assertTrue($o->hasChanged());
  }

  /**
   * Tests clearChanged() method
   */
  #[@test]
  public function change_cleared() {
    $o= self::$observable->newInstance();
    $o->setChanged();
    $o->clearChanged();
    $this->assertFalse($o->hasChanged());
  }

  /**
   * Tests addObserver() method
   */
  #[@test]
  public function add_observer_returns_added_observer() {
    $observer= newinstance('util.Observer', array(), array(
      'update' => function($self, $obs, $arg= null) {
        /* Intentionally empty */
      }
    ));
    $o= self::$observable->newInstance();
    $this->assertEquals($observer, $o->addObserver($observer));
  }

  /**
   * Tests notifyObservers() method
   */
  #[@test]
  public function observer_gets_called_with_observable() {
    $observer= newinstance('util.Observer', array(), array(
      'calls' => array(),
      'update' => function($self, $obs, $arg= null) {
        $self->calls[]= array($obs, $arg);
      }
    ));
    $o= self::$observable->newInstance();
    $o->addObserver($observer);
    $o->setValue(5);
    $this->assertEquals(array(array($o, null)), $observer->calls);
  }
}
{'fraction_non_alphanumeric': 0.18104604380867234, 'fraction_numerical': 0.000894054537326777, 'mean_word_length': 3.237689393939394, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6440826', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1399, 'n_words': 260}