<commit_before><?php

declare (strict_types = 1); // @codeCoverageIgnore

namespace Recoil;

use Eloquent\Phony\Phony;

context('can be invoked by yielding a thenable', function () {
    rit('that does fulfill', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $fulfill, callable $reject) {
                    $fulfill('<value>');
                },
            ]
        );

        expect(yield $promise->get())->to->equal('<value>');
    });

    rit('that does reject with throwable', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $fulfill, callable $reject) {
                    $reject(new \Exception('<rejected>'));
                },
            ]
        );

        try {
            yield $promise->get();
            expect(false)->to->equal('Expected exception was not thrown.');
        } catch (\Exception $e) {
            expect($e->getMessage())->to->equal('<rejected>');
        }
    });

    rit('that does reject with non throwable', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $fulfill, callable $reject) {
                    $reject('<rejected>');
                },
            ]
        );

        try {
            yield $promise->get();
            expect(false)->to->equal('Expected exception was not thrown.');
        } catch (\Exception $e) {
            expect($e->getMessage())->to->equal('<rejected>');
        }
    });

    rit('that does cancel', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $fulfill, callable $reject) {},
                'cancel' => function () {},
            ]
        );

        $strand = yield Recoil::execute(function () use ($promise) {
            yield $promise->get();
        });

        yield;

        $strand->terminate();

        $promise->cancel->called();
    });
});

context('can be invoked by yielding a thenable that is also a doneable', function () {
    rit('that does fulfill', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $fulfill, callable $reject) {},
                'done' => function (callable $fulfill, callable $reject) {
                    $fulfill('<value>');
                },
            ]
        );

        expect(yield $promise->get())->to->equal('<value>');
    });

    rit('that does reject with throwable', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $fulfill, callable $reject) {},
                'done' => function (callable $fulfill, callable $reject) {
                    $reject(new \Exception('<rejected>'));
                },
            ]
        );

        try {
            yield $promise->get();
            expect(false)->to->equal('Expected exception was not thrown.');
        } catch (\Exception $e) {
            expect($e->getMessage())->to->equal('<rejected>');
        }
    });

    rit('that does reject with non throwable', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $fulfill, callable $reject) {},
                'done' => function (callable $fulfill, callable $reject) {
                    $reject('<rejected>');
                },
            ]
        );

        try {
            yield $promise->get();
            expect(false)->to->equal('Expected exception was not thrown.');
        } catch (\Exception $e) {
            expect($e->getMessage())->to->equal('<rejected>');
        }
    });

    rit('that does cancel', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $fulfill, callable $reject) {},
                'done' => function (callable $fulfill, callable $reject) {},
                'cancel' => function () {},<commit_msg>Reword functional test names.<commit_after><?php

declare (strict_types = 1); // @codeCoverageIgnore

namespace Recoil;

use Eloquent\Phony\Phony;

context('when it has a then method', function () {
    rit('resumes the strand when resolved', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $resolve, callable $reject) {
                    $resolve('<value>');
                },
            ]
        );

        expect(yield $promise->get())->to->equal('<value>');
    });

    rit('resumes the strand with an exception when rejected', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $resolve, callable $reject) {
                    $reject(new \Exception('<rejected>'));
                },
            ]
        );

        try {
            yield $promise->get();
            expect(false)->to->equal('Expected exception was not thrown.');
        } catch (\Exception $e) {
            expect($e->getMessage())->to->equal('<rejected>');
        }
    });

    rit('resumes the strand with a value when rejected', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $resolve, callable $reject) {
                    $reject('<rejected>');
                },
            ]
        );

        try {
            yield $promise->get();
            expect(false)->to->equal('Expected exception was not thrown.');
        } catch (\Exception $e) {
            expect($e->getMessage())->to->equal('<rejected>');
        }
    });

    rit('terminates the strand when cancelled', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $resolve, callable $reject) {},
                'cancel' => function () {},
            ]
        );

        $strand = yield Recoil::execute(function () use ($promise) {
            yield $promise->get();
        });

        yield;

        $strand->terminate();

        $promise->cancel->called();
    });
});

context('when it has both then and done methods', function () {
    rit('resumes the strand when resolved', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $resolve, callable $reject) {},
                'done' => function (callable $resolve, callable $reject) {
                    $resolve('<value>');
                },
            ]
        );

        expect(yield $promise->get())->to->equal('<value>');
    });

    rit('resumes the strand with an exception when rejected', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $resolve, callable $reject) {},
                'done' => function (callable $resolve, callable $reject) {
                    $reject(new \Exception('<rejected>'));
                },
            ]
        );

        try {
            yield $promise->get();
            expect(false)->to->equal('Expected exception was not thrown.');
        } catch (\Exception $e) {
            expect($e->getMessage())->to->equal('<rejected>');
        }
    });

    rit('resumes the strand with a value when rejected', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $resolve, callable $reject) {},
                'done' => function (callable $resolve, callable $reject) {
                    $reject('<rejected>');
                },
            ]
        );

        try {
            yield $promise->get();
            expect(false)->to->equal('Expected exception was not thrown.');
        } catch (\Exception $e) {
            expect($e->getMessage())->to->equal('<rejected>');
        }
    });

    rit('terminates the strand when cancelled', function () {
        $promise = Phony::partialMock(
            [
                'then' => function (callable $resolve, callable $reject) {},
                'done' => function (callable $resolve, callable $reject) {},
                'cancel' => function () {},
{'fraction_non_alphanumeric': 0.1580760241112068, 'fraction_numerical': 0.00024603272235207283, 'mean_word_length': 1.6482084690553747, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 123, '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': '369174', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2090, 'n_words': 484}