<commit_before><?php
use Evenement\EventEmitter;
use Peridot\Core\Test;
use Peridot\Core\TestResult;
use Peridot\Core\Suite;
use Peridot\Core\Scope;
use Peridot\Test\ItWasRun;

describe("Test", function() {

    context("when constructed with default parameters", function() {
        it("it should default to a pending state", function() {
            $test = new Test("it should be pending");
            assert($test->getPending(), "test should be pending if definition is null");
        });

        it("it should default to an unfocused state", function() {
            $test = new Test("it should not be focused");
            assert(!$test->isFocused(), "test should not be focused if focused value is not supplied");
        });
    });

    describe('->run()', function() {
        it("should run", function() {
            $test = new ItWasRun("this should run", function() {
                $this->wasRun = true;
            });
            $test->run(new TestResult(new EventEmitter()));
            assert($test->wasRun(), "spec should have run");
        });

        it("should run setup functions", function() {
            $test = new ItWasRun("this should setup", function() {});
            $test->addSetupFunction(function() {
                $this->log .= "setUp ";
            });
            $test->run(new TestResult(new EventEmitter()));
            assert($test->log() == "setUp ", "spec should have been setup");
        });

        it("should run teardown functions", function() {
            $test = new ItWasRun("this should teardown", function() {});
            $test->addTearDownFunction(function() {
                $this->log .= "tearDown ";
            });
            $test->run(new TestResult(new EventEmitter()));
            assert($test->log() == "tearDown ", "spec should have been torn down");
        });

        it("should modify a passed in result", function () {
            $test = new ItWasRun("this should return a result", function () {});
            $result = new TestResult(new EventEmitter());
            $test->run($result);
            assert("1 run, 0 failed" == $result->getSummary(), "result summary should have shown 1 run");
        });

        it("should add failed results to result", function () {
            $test = new ItWasRun("this should return a failed result", function () {
                throw new Exception('blaaargh');
            });
            $result = new TestResult(new EventEmitter());
            $test->run($result);
            assert("1 run, 1 failed" == $result->getSummary(), "result summary should have shown 1 failed");
        });

        $removeErrorHandlers = function () {
            do {
                if ($handler = set_error_handler(function () {})) {
                    restore_error_handler();
                }

                restore_error_handler();
            } while ($handler);
        };

        it("should add failed results to result on error", function () use ($removeErrorHandlers) {
            $test = new ItWasRun("this should return a failed result", function () {
                trigger_error("This is a user notice", E_USER_NOTICE);
            });
            $result = new TestResult(new EventEmitter());
            $removeErrorHandlers();
            error_reporting(-1);
            $test->run($result);
            assert("1 run, 1 failed" == $result->getSummary(), "result summary should have shown 1 failed");
        });

        it("should ignore errors that are excluded by error reporting", function () use ($removeErrorHandlers) {
            $test = new ItWasRun("this should return a failed result", function () {
                trigger_error("This is a user notice", E_USER_NOTICE);
            });
            $result = new TestResult(new EventEmitter());
            $removeErrorHandlers();
            error_reporting(E_ERROR);
            $test->run($result);
            assert("1 run, 0 failed" == $result->getSummary(), "result summary should have shown 0 failed");
        });

        it("should ignore errors that are excluded by the error control operator", function () use ($removeErrorHandlers) {
            $test = new ItWasRun("this should return a failed result", function () {
                $foo = [];
                @strlen($foo['bar']);
            });
            $result = new TestResult(new EventEmitter());
            $removeErrorHandlers();<commit_msg>added spec for error handling<commit_after><?php
use Evenement\EventEmitter;
use Peridot\Core\Test;
use Peridot\Core\TestResult;
use Peridot\Core\Suite;
use Peridot\Core\Scope;
use Peridot\Test\ItWasRun;

describe("Test", function() {

    context("when constructed with default parameters", function() {
        it("it should default to a pending state", function() {
            $test = new Test("it should be pending");
            assert($test->getPending(), "test should be pending if definition is null");
        });

        it("it should default to an unfocused state", function() {
            $test = new Test("it should not be focused");
            assert(!$test->isFocused(), "test should not be focused if focused value is not supplied");
        });
    });

    describe('->run()', function() {
        it("should run", function() {
            $test = new ItWasRun("this should run", function() {
                $this->wasRun = true;
            });
            $test->run(new TestResult(new EventEmitter()));
            assert($test->wasRun(), "spec should have run");
        });

        it("should run setup functions", function() {
            $test = new ItWasRun("this should setup", function() {});
            $test->addSetupFunction(function() {
                $this->log .= "setUp ";
            });
            $test->run(new TestResult(new EventEmitter()));
            assert($test->log() == "setUp ", "spec should have been setup");
        });

        it("should run teardown functions", function() {
            $test = new ItWasRun("this should teardown", function() {});
            $test->addTearDownFunction(function() {
                $this->log .= "tearDown ";
            });
            $test->run(new TestResult(new EventEmitter()));
            assert($test->log() == "tearDown ", "spec should have been torn down");
        });

        it("should modify a passed in result", function () {
            $test = new ItWasRun("this should return a result", function () {});
            $result = new TestResult(new EventEmitter());
            $test->run($result);
            assert("1 run, 0 failed" == $result->getSummary(), "result summary should have shown 1 run");
        });

        it("should add failed results to result", function () {
            $test = new ItWasRun("this should return a failed result", function () {
                throw new Exception('blaaargh');
            });
            $result = new TestResult(new EventEmitter());
            $test->run($result);
            assert("1 run, 1 failed" == $result->getSummary(), "result summary should have shown 1 failed");
        });

        $removeErrorHandlers = function () {
            do {
                if ($handler = set_error_handler(function () {})) {
                    restore_error_handler();
                }

                restore_error_handler();
            } while ($handler);
        };

        it("should pass 5 arguments to the parent error handler on error", function () use ($removeErrorHandlers) {
            if(PHP_VERSION_ID >= 70000) {
                return;
            }
            $removeErrorHandlers();
            $handlerArguments = [];
            set_error_handler(function() use (&$handlerArguments) {
                $handlerArguments = func_get_args();
            });
            $test = new ItWasRun("this should return a failed result", function () {
                trigger_error("This is a user notice", E_USER_NOTICE);
            });
            $result = new TestResult(new EventEmitter());
            error_reporting(-1);
            $test->run($result);
            assert(count($handlerArguments) == 5, sprintf("should pass 5 arguments, %d passed", count($handlerArguments)));
        });

        it("should add failed results to result on error", function () use ($removeErrorHandlers) {
            $test = new ItWasRun("this should return a failed result", function () {
                trigger_error("This is a user notice", E_USER_NOTICE);
            });
            $result = new TestResult(new EventEmitter());
            $removeErrorHandlers();
            error_reporting(-1);
            $test->run($result);
            assert("1 run, 1 failed" == $result->getSummary(), "result summary should have shown 1 failed");
        });

        it("should ignore errors that are excluded by error reporting", function () use ($removeErrorHandlers) {
            $test = new ItWasRun("this should return a failed result", function () {
                trigger_error("This is a user notice", E_USER_NOTICE);
            });
            $result = new TestResult(new EventEmitter());
            $removeErrorHandlers();
            error_reporting(E_ERROR);
            $test->run($result);
            assert("1 run, 0 failed" == $result->getSummary(), "result summary should have shown 0 failed");
        });

        it("should ignore errors that are excluded by the error control operator", function () use ($removeErrorHandlers) {
            $test = new ItWasRun("this should return a failed result", function () {
                $foo = [];
                @strlen($foo['bar']);
            });
            $result = new TestResult(new EventEmitter());
            $removeErrorHandlers();
{'fraction_non_alphanumeric': 0.13565253190613422, 'fraction_numerical': 0.0036023054755043226, 'mean_word_length': 2.10944, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '5152283', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2389, 'n_words': 737}