<commit_before>'use strict';

/**
 * Dependencies.
 */

var directionality,
    visit,
    content,
    Retext,
    assert;

directionality = require('./');
visit = require('retext-visit');
content = require('retext-content');
Retext = require('retext');
assert = require('assert');

/**
 * Fixtures.
 */

var sentence,
    leftToRightWords,
    rightToLeftWords;

/**
 * Base English sentence.
 */

sentence = 'A simple, english, sentence.';

/**
 * French, baguette!
 */

leftToRightWords = 'Un simple anglais phrase'.split(' ');

/**
 * Arabic.
 *
 * I'm no good at Aabic---but Google says this is the
 * translation of the example. For our purpose validity
 * however does not matter---we just need some
 * right-to-left words.
 */

rightToLeftWords = 'أ الجملة الانجليزية بسيطة'.split(' ');

/**
 * Retext
 */

var retext;

retext = new Retext()
    .use(visit)
    .use(content)
    .use(directionality);

/**
 * Tests
 */

describe('directionality()', function () {
    it('should be a `function`', function () {
        assert(typeof directionality === 'function');
    });

    it('should expose an `attach` method', function () {
        assert(typeof directionality.attach === 'function');
    });

    retext.parse(sentence, function (err, tree) {
        it('should not throw', function (done) {
            done(err);
        });

        it('should process each `WordNode`', function () {
            var index;

            tree.visit(tree.WORD_NODE, function (node) {
                assert(node.data.direction === 'ltr');
            });

            /**
             * Cover some optimizations: when a node's
             * direction does not change, there is no
             * need to re-process parents.
             */

            index = -1;

            tree.visit(tree.WORD_NODE, function (node) {
                node[0].fromString(leftToRightWords[++index]);

                assert(node.data.direction === 'ltr');
            });
        });

        it('should process each `Parent`', function () {
            tree.visit(tree.WORD_NODE, function (node) {
                var parent;

                parent = node.parent;

                while (parent) {
                    assert(parent.data.direction === 'ltr');

                    parent = parent.parent;
                }
            });
        });

        it('should set each `direction` to `null` when a `WordNode` (no ' +
            'longer?) has a value',
            function () {
                tree.visit(tree.WORD_NODE, function (node) {
                    /**
                     * We use the less secure `fromString`
                     * on a TextNode here to test for
                     * `changetext` events.
                     */

                    node[0].fromString('');

                    assert(node.data.direction === 'neutral');
                });
            }
        );

        it('should re-process each `WordNode` and `Parent`', function () {
            var index;

            /**
             * First set some left-to-rigth words, so we
             * can check for `neutral` changes.
             */

            index = -1;

            tree.visit(tree.WORD_NODE, function (node) {
                node.replaceContent(leftToRightWords[++index]);

                assert(node.data.direction === 'ltr');
            });

            index = -1;

            tree.visit(tree.WORD_NODE, function (node) {
                var parent,
                    shouldBeDirection;

                node.replaceContent(rightToLeftWords[++index]);

                assert(node.data.direction === 'rtl');

                /**
                 * When all words are `rtl`, the parent
                 * should reflext this instead of showing
                 * `neutral`.
                 */

                shouldBeDirection = index === 3 ? 'rtl' : 'neutral';

                parent = node.parent;

                while (parent) {
                    assert(parent.data.direction === shouldBeDirection);

                    parent = parent.parent;
                }
            });
        });

        it('should set `Parent`s `direction` to `neutral` when all its ' +
            '`WordNode`s are removed',
            function () {
                var parent;

                parent = tree.head.head;

                tree.visit(tree.TEXT_NODE, function (node) {
                    if (node.parent.type === node.WORD_NODE) {
                        node.remove();
                    }
                });

                while (parent) {
                    assert(parent.data.direction === 'neutral');

                    parent = parent.parent;
                }
            }
        );
    });
});<commit_msg>Refactor test for changes in retext<commit_after>'use strict';

/**
 * Dependencies.
 */

var directionality,
    visit,
    content,
    Retext,
    assert;

directionality = require('./');
visit = require('retext-visit');
content = require('retext-content');
Retext = require('retext');
assert = require('assert');

/**
 * Fixtures.
 */

var sentence,
    leftToRightWords,
    rightToLeftWords;

/**
 * Base English sentence.
 */

sentence = 'A simple, english, sentence.';

/**
 * French, baguette!
 */

leftToRightWords = 'Un simple anglais phrase'.split(' ');

/**
 * Arabic.
 *
 * I'm no good at Aabic---but Google says this is the
 * translation of the example. For our purpose validity
 * however does not matter---we just need some
 * right-to-left words.
 */

rightToLeftWords = 'أ الجملة الانجليزية بسيطة'.split(' ');

/**
 * Retext
 */

var retext;

retext = new Retext()
    .use(visit)
    .use(content)
    .use(directionality);

/**
 * Tests
 */

describe('directionality()', function () {
    it('should be a `function`', function () {
        assert(typeof directionality === 'function');
    });

    it('should expose an `attach` method', function () {
        assert(typeof directionality.attach === 'function');
    });

    retext.parse(sentence, function (err, tree) {
        it('should not throw', function (done) {
            done(err);
        });

        it('should process each `WordNode`', function () {
            var index;

            tree.visit(tree.WORD_NODE, function (node) {
                assert(node.data.direction === 'ltr');
            });

            /**
             * Cover some optimizations: when a node's
             * direction does not change, there is no
             * need to re-process parents.
             */

            index = -1;

            tree.visit(tree.WORD_NODE, function (node) {
                node[0].fromString(leftToRightWords[++index]);

                assert(node.data.direction === 'ltr');
            });
        });

        it('should process each `Parent`', function () {
            tree.visit(tree.WORD_NODE, function (node) {
                var parent;

                parent = node.parent;

                while (parent) {
                    assert(parent.data.direction === 'ltr');

                    parent = parent.parent;
                }
            });
        });

        it('should set each `direction` to `null` when a `WordNode` (no ' +
            'longer?) has a value',
            function () {
                tree.visit(tree.WORD_NODE, function (node) {
                    /**
                     * We use the less secure `fromString`
                     * on a TextNode here to test for
                     * `changetext` events.
                     */

                    node[0].fromString('');

                    assert(node.data.direction === 'neutral');
                });
            }
        );

        it('should re-process each `WordNode` and `Parent`', function () {
            var index;

            /**
             * First set some left-to-rigth words, so we
             * can check for `neutral` changes.
             */

            index = -1;

            tree.visit(tree.WORD_NODE, function (node) {
                node.replaceContent(leftToRightWords[++index]);

                assert(node.data.direction === 'ltr');
            });

            index = -1;

            tree.visit(tree.WORD_NODE, function (node) {
                var parent,
                    shouldBeDirection;

                node.replaceContent(rightToLeftWords[++index]);

                assert(node.data.direction === 'rtl');

                /**
                 * When all words are `rtl`, the parent
                 * should reflext this instead of showing
                 * `neutral`.
                 */

                shouldBeDirection = index === 3 ? 'rtl' : 'neutral';

                parent = node.parent;

                while (parent) {
                    assert(parent.data.direction === shouldBeDirection);

                    parent = parent.parent;
                }
            });
        });

        it('should set `Parent`s `direction` to `neutral` when all its ' +
            '`WordNode`s are removed',
            function () {
                var parent;

                parent = tree.head.head;

                tree.visit(tree.TEXT_NODE, function (node) {
                    node.remove();
                });

                while (parent) {
                    assert(parent.data.direction === 'neutral');

                    parent = parent.parent;
                }
            }
        );
    });
});
{'fraction_non_alphanumeric': 0.1294979301560344, 'fraction_numerical': 0.0012737501326823054, 'mean_word_length': 1.4152781338118432, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6373283', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2399, 'n_words': 600}