<commit_before><?php

/**
 * @author <NAME> <<EMAIL>>
 */


namespace Oliva\Test\Node;

require_once __DIR__ . '/bootstrap.php';

use Tester,
	Tester\Assert;
use Oliva\Test\DataWrapper;
use Oliva\Utils\Tree\Node\Node;


/**
 * @see \Oliva\Utils\Tree\Node\NodeBase
 */
class NodeTest extends Tester\TestCase
{


	protected function setUp()
	{
		parent::setUp();
	}


	protected function tearDown()
	{
		parent::tearDown();
	}


	public function testScalar()
	{
		$node = new Node(100);
		Assert::same(100, $node->getObject());

		// cannot call a function on a scalar
		Assert::exception(function() use ($node) {
			$node->foo();
		}, 'BadMethodCallException');

		// cannot get/set a member of a scalar
		Assert::exception(function() use ($node) {
			$node->foo;
		}, 'RuntimeException');
	}


	public function testScalarOperations()
	{
		// operations are not supported - conversion of Node to scalar types is not possible
		$i = new Node(1);
		$s = new Node('string');

		Assert::error(function()use($i) {
			(int) $i;
		}, E_NOTICE);
		Assert::error(function()use($s) {
			(string) $s;
		}, E_RECOVERABLE_ERROR);
	}


	public function testArray()
	{
		$array = [1, 2, 3];
		$node = new Node($array);
		Assert::same($array, $node->getObject());

		// cannot call a function on an array
		Assert::exception(function() use ($node) {
			$node->foo();
		}, 'BadMethodCallException');

		// requesting $array['foo'] should raise E_NOTICE
		Assert::error(function() use ($node) {
			$node->foo;
		}, E_NOTICE);

		foreach (array_keys($array) as $key) {
			Assert::same($array[$key], $node->{$key});
		}

		$clone = $array;
		$node[] = 5;
		$clone[] = 5;
		Assert::same($clone, $node->getObject());

		$node[100] = 'foo';
		$clone[100] = 'foo';
		Assert::same($clone, $node->getObject());

		$node[''] = 6;
		$clone[''] = 6;
		Assert::same($clone, $node->getObject());

		// this case is a known bug!
		// cannot be solved without breaking the ability to add by calling $node[] = foo; (PHP 5.6)
		$node[NULL] = 7;
		$clone[NULL] = 7;
		$buggy = [1, 2, 3, 5, 100 => 'foo', '' => 6, 7];
		Assert::notSame($clone, $node->getObject()); // Assert::same will fail - see below
		Assert::same($buggy, $node->getObject()); // $clone !== $buggy
	}
<commit_msg>dev note<commit_after><?php

/**
 * @author <NAME> <<EMAIL>>
 */


namespace Oliva\Test\Node;

require_once __DIR__ . '/bootstrap.php';

use Tester,
	Tester\Assert;
use Oliva\Test\DataWrapper;
use Oliva\Utils\Tree\Node\Node;


/**
 * @see \Oliva\Utils\Tree\Node\NodeBase
 */
class NodeTest extends Tester\TestCase
{


	protected function setUp()
	{
		parent::setUp();
	}


	protected function tearDown()
	{
		parent::tearDown();
	}


	public function testScalar()
	{
		$node = new Node(100);
		Assert::same(100, $node->getObject());

		// cannot call a function on a scalar
		Assert::exception(function() use ($node) {
			$node->foo();
		}, 'BadMethodCallException');

		// cannot get/set a member of a scalar
		Assert::exception(function() use ($node) {
			$node->foo;
		}, 'RuntimeException');
	}


	public function testScalarOperations()
	{
		// operations are not supported - conversion of Node to scalar types is not possible
		$i = new Node(1);
		$s = new Node('string');

		Assert::error(function()use($i) {
			(int) $i;
		}, E_NOTICE);
		Assert::error(function()use($s) {
			(string) $s;
		}, E_RECOVERABLE_ERROR);
	}


	public function testArray()
	{
		$array = [1, 2, 3];
		$node = new Node($array);
		Assert::same($array, $node->getObject());

		// cannot call a function on an array
		Assert::exception(function() use ($node) {
			$node->foo();
		}, 'BadMethodCallException');

		// requesting $array['foo'] should raise E_NOTICE
		// Note: this behaviour should NOT be changed using property_exists and array_key_exists methods,
		// it would break usability with objects with overloaded properties
		Assert::error(function() use ($node) {
			$node->foo;
		}, E_NOTICE);

		foreach (array_keys($array) as $key) {
			Assert::same($array[$key], $node->{$key});
		}

		$clone = $array;
		$node[] = 5;
		$clone[] = 5;
		Assert::same($clone, $node->getObject());

		$node[100] = 'foo';
		$clone[100] = 'foo';
		Assert::same($clone, $node->getObject());

		$node[''] = 6;
		$clone[''] = 6;
		Assert::same($clone, $node->getObject());

		// this case is a known bug!
		// cannot be solved without breaking the ability to add by calling $node[] = foo; (PHP 5.6)
		$node[NULL] = 7;
		$clone[NULL] = 7;
		$buggy = [1, 2, 3, 5, 100 => 'foo', '' => 6, 7];
		Assert::notSame($clone, $node->getObject()); // Assert::same will fail - see below
		Assert::same($buggy, $node->getObject()); // $clone !== $buggy
	}

{'fraction_non_alphanumeric': 0.1920515574650913, 'fraction_numerical': 0.01417830290010741, 'mean_word_length': 3.746177370030581, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, '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': '7208669', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1611, 'n_words': 424}