<commit_before><?php
namespace nochso\WriteMe\Test\Placeholder;

use nochso\WriteMe\Document;
use nochso\WriteMe\Placeholder\Call;

class CallTest extends \PHPUnit_Framework_TestCase
{
    public function extractFirstCallProvider()
    {
        return [
            ['@identifier@', 'identifier', null, []],
            ['@identifier(5)@', 'identifier', null, [5]],
            ['@@escaped@@ @identifier@', 'identifier', null, []],
            ['@@escaped@@@identifier@', 'identifier', null, []],
            ['@@escaped@@@@more.escaped@@@identifier@', 'identifier', null, []],
            ['@identifier.method@', 'identifier', 'method', []],
            ['@identifier.method("string param")@', 'identifier', 'method', ['string param']],
            ['@identifier.method("@")@', 'identifier', 'method', ['@']],
            ['@identifier.method("@foo@")@', 'identifier', 'method', ['@foo@']],
            ['@identifier.method("@()")@', 'identifier', 'method', ['@()']],
            ["@identifier.method('@()')@", 'identifier', 'method', ['@()']],
            ["@identifier.method('string param')@", 'identifier', 'method', ['string param']],
            ['@identifier.method(1)@', 'identifier', 'method', [1]],
            ['@identifier.method(true)@', 'identifier', 'method', [true]],
            ['@identifier.method([1])@', 'identifier', 'method', [[1]]],
            ['@identifier.method(["key" => "value"])@', 'identifier', 'method', [['key' => 'value']]],
            ['@identifier.method(["key" => ["nested key" => true, 2]])@', 'identifier', 'method', [['key' => ['nested key' => true, 2]]]],
            ['@identifier.method(1, 2, 3)@', 'identifier', 'method', [1, 2, 3]],
            ['@identifier.firstcall@ @identifier.secondcall@', 'identifier', 'firstcall', []],
        ];
    }

    /**
     * @dataProvider extractFirstCallProvider
     * 
     * @param string      $rawDocument
     * @param string      $expectedIdentifier
     * @param string|null $expectedMethod
     * @param array       $expectedParameters
     */
    public function testExtractFirstCall($rawDocument, $expectedIdentifier, $expectedMethod, $expectedParameters)
    {
        $document = new Document($rawDocument);
        $call = Call::extractFirstCall($document);
        $this->assertSame($expectedIdentifier, $call->getIdentifier());
        $this->assertSame($expectedMethod, $call->getMethod());
        $this->assertSame($expectedParameters, $call->getParameters());
        $this->assertSame($document, $call->getDocument());
    }

    public function extractFirstCallNullProvider()
    {
        return [
            ['@invalid'],
            [''],
            ['invalid@'],
            ['some content'],
            ['@@escaped@@'],
            ['@@escaped@@@@anotherescaped@@'],
            ['<EMAIL> <EMAIL>'],
        ];
    }

    /**
     * @dataProvider extractFirstCallNullProvider
     *
     * @param string $rawDocument
     */
    public function testExtractFirstCall_WhenNoneFound_MustReturnNull($rawDocument)
    {
        $document = new Document($rawDocument);
        $this->assertNull(Call::extractFirstCall($document));
    }

    public function rawCallProvider()
    {
        return [
            ['@foo@', '@foo@'],
            ['x@foo@y', '@foo@'],
            ['@foo@ @foo2@', '@foo@'],
        ];
    }

    /**
     * @dataProvider rawCallProvider
     *
     * @param string $rawDocument
     * @param string $expectedRawCall
     */
    public function testExtractFirstCall_RawCall($rawDocument, $expectedRawCall)
    {
        $document = new Document($rawDocument);
        $call = Call::extractFirstCall($document);
        $this->assertSame($expectedRawCall, $call->getRawCall());
    }

    public function replaceProvider()
    {
        return [
            'Basic replacement' => ['@foo@', 'NEW', 'NEW'],
            'Replace after escaped placeholder' => ['@@foo@@@foo@', 'NEW', '@@foo@@NEW'],
            'Replace first placeholder only' => ['@foo@ @second.foo@', 'NEW', 'NEW @second.foo@'],
            'Properly regex-escape replacement' => ['@foo@ @second.foo@', 'NEW[]-()/\\ $0 \0', 'NEW[]-()/\\ $0 \0 @second.foo@'],
        ];
    }

    /**
     * @dataProvider replaceProvider
     *
     * @param string $rawDocument
     * @param string $replacement
     * @param string $expectedContent
     */
    public function testReplace($rawDocument, $replacement, $expectedContent)
    {
        $document = new Document($rawDocument);
        $call = Call::extractFirstCall($document);
        $call->replace($replacement);
        $this->assertSame($expectedContent, $document->getContent());
    }
}<commit_msg>test: Define desired behaviour for Call<commit_after><?php
namespace nochso\WriteMe\Test\Placeholder;

use nochso\WriteMe\Document;
use nochso\WriteMe\Interfaces\Placeholder;
use nochso\WriteMe\Placeholder\Call;

class CallTest extends \PHPUnit_Framework_TestCase
{
    public function extractFirstCallProvider()
    {
        return [
            ['@identifier@', 'identifier', null, []],
            ['@identifier(5)@', 'identifier', null, [5]],
            ['@@escaped@@ @identifier@', 'identifier', null, []],
            ['@@escaped@@@identifier@', 'identifier', null, []],
            ['@@escaped@@@@more.escaped@@@identifier@', 'identifier', null, []],
            ['@identifier.method@', 'identifier', 'method', []],
            ['@identifier.method("string param")@', 'identifier', 'method', ['string param']],
            ['@identifier.method("@")@', 'identifier', 'method', ['@']],
            ['@identifier.method("@foo@")@', 'identifier', 'method', ['@foo@']],
            ['@identifier.method("@()")@', 'identifier', 'method', ['@()']],
            ["@identifier.method('@()')@", 'identifier', 'method', ['@()']],
            ["@identifier.method('string param')@", 'identifier', 'method', ['string param']],
            ['@identifier.method(1)@', 'identifier', 'method', [1]],
            ['@identifier.method(true)@', 'identifier', 'method', [true]],
            ['@identifier.method([1])@', 'identifier', 'method', [[1]]],
            ['@identifier.method(["key" => "value"])@', 'identifier', 'method', [['key' => 'value']]],
            ['@identifier.method(["key" => ["nested key" => true, 2]])@', 'identifier', 'method', [['key' => ['nested key' => true, 2]]]],
            ['@identifier.method(1, 2, 3)@', 'identifier', 'method', [1, 2, 3]],
            ['@identifier.firstcall@ @identifier.secondcall@', 'identifier', 'firstcall', []],
        ];
    }

    /**
     * @dataProvider extractFirstCallProvider
     * 
     * @param string      $rawDocument
     * @param string      $expectedIdentifier
     * @param string|null $expectedMethod
     * @param array       $expectedParameters
     */
    public function testExtractFirstCall($rawDocument, $expectedIdentifier, $expectedMethod, $expectedParameters)
    {
        $document = new Document($rawDocument);
        $call = Call::extractFirstCall($document);
        $this->assertSame($expectedIdentifier, $call->getIdentifier());
        $this->assertSame($expectedMethod, $call->getMethod());
        $this->assertSame($expectedParameters, $call->getParameters());
        $this->assertSame($document, $call->getDocument());
    }

    public function extractFirstCallNullProvider()
    {
        return [
            ['@invalid'],
            [''],
            ['invalid@'],
            ['some content'],
            ['@@escaped@@'],
            ['@@escaped@@@@anotherescaped@@'],
            ['<EMAIL> <EMAIL>'],
        ];
    }

    /**
     * @dataProvider extractFirstCallNullProvider
     *
     * @param string $rawDocument
     */
    public function testExtractFirstCall_WhenNoneFound_MustReturnNull($rawDocument)
    {
        $document = new Document($rawDocument);
        $this->assertNull(Call::extractFirstCall($document));
    }

    public function rawCallProvider()
    {
        return [
            ['@foo@', '@foo@'],
            ['x@foo@y', '@foo@'],
            ['@foo@ @foo2@', '@foo@'],
        ];
    }

    /**
     * @dataProvider rawCallProvider
     *
     * @param string $rawDocument
     * @param string $expectedRawCall
     */
    public function testExtractFirstCall_RawCall($rawDocument, $expectedRawCall)
    {
        $document = new Document($rawDocument);
        $call = Call::extractFirstCall($document);
        $this->assertSame($expectedRawCall, $call->getRawCall());
    }

    public function replaceProvider()
    {
        return [
            'Basic replacement' => ['@foo@', 'NEW', 'NEW'],
            'Replace after escaped placeholder' => ['@@foo@@@foo@', 'NEW', '@@foo@@NEW'],
            'Replace first placeholder only' => ['@foo@ @second.foo@', 'NEW', 'NEW @second.foo@'],
            'Properly regex-escape replacement' => ['@foo@ @second.foo@', 'NEW[]-()/\\ $0 \0', 'NEW[]-()/\\ $0 \0 @second.foo@'],
        ];
    }

    /**
     * @dataProvider replaceProvider
     *
     * @param string $rawDocument
     * @param string $replacement
     * @param string $expectedContent
     */
    public function testReplace($rawDocument, $replacement, $expectedContent)
    {
        $document = new Document($rawDocument);
        $call = Call::extractFirstCall($document);
        $call->replace($replacement);
        $this->assertSame($expectedContent, $document->getContent());
    }

    public function testIsReplaced()
    {
        $document = new Document('@foo@');
        $call = Call::extractFirstCall($document);
        $this->assertFalse($call->isReplaced());
        $call->replace('');
        $this->assertTrue($call->isReplaced());
    }

    public function testReplace_MustObserveStartPositionOfCall()
    {
        $document = new Document('@foo@ @foo@');
        $call = Call::extractFirstCall($document, Placeholder::PRIORITY_FIRST, 5);
        $call->replace('I MUST BE SECOND');
        $this->assertSame('@foo@ I MUST BE SECOND', $document->getContent());
    }

    public function testGetStartPositionOfRawCall()
    {
        $document = new Document(' @test@');
        $call = Call::extractFirstCall($document);
        $this->assertSame(1, $call->getStartPositionOfRawCall());
    }

    public function testGetEndPositionOfRawCall()
    {
        $document = new Document('@test@ @test@');
        $call = Call::extractFirstCall($document, Placeholder::PRIORITY_FIRST);
        $this->assertSame(6, $call->getEndPositionOfRawCall());

        $secondCall = Call::extractFirstCall($document, Placeholder::PRIORITY_FIRST, $call->getEndPositionOfRawCall());
        $this->assertSame(13, $secondCall->getEndPositionOfRawCall(), 'End position must be absolute and not be influenced by an offset');
    }
}
{'fraction_non_alphanumeric': 0.19334389857369255, 'fraction_numerical': 0.004008576489232777, 'mean_word_length': 2.913900036483035, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 70, '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': '2464076', 'n_tokens_mistral': 3065, 'n_tokens_neox': 3017, 'n_words': 580}