<commit_before>import chai from 'chai';
import { createSelector, createSelectorCreator, defaultMemoize } from '../src/index';
import { default as lodashMemoize } from 'lodash.memoize';

let assert = chai.assert;

suite('selector', () => {
    test('basic selector', () => {
        const selector = createSelector(state => state.a, a => a);
        assert.equal(selector({a: 1}), 1);
    });
    test('basic selector multiple keys', () => {
        const selector = createSelector(
            state => state.a,
            state => state.b,
            (a, b) => a + b
        );
        assert.equal(selector({a: 1, b: 2}), 3);
    });
    test('first argument can be an array', () => {
        const selector = createSelector(
            [state => state.a], a => a);
        assert.equal(selector({a: 1}), 1);
    });
    test('can accept props', () => {
        const selector = createSelector(
            state => state.a,
            state => state.b,
            (state, props) => props.val,
            (a, b, c) => a + b + c
        );
        assert.equal(selector({a: 1, b: 2}, {val: 100}), 103);
    });
    test('chained selector', () => {
        const selector1 = createSelector(
            state => state.sub, sub => sub);
        const selector2 = createSelector(
            selector1, sub => sub.value);
        assert.equal(selector2({sub: { value: 1}}), 1);
    });
    test('chained selector with props', () => {
        const selector1 = createSelector(
            state => state.sub,
            (state, props) => props.x,
            (sub, x) => ({sub, x})
        );
        const selector2 = createSelector(
            selector1,
            (state, props) => props.y,
            (param, y) => param.sub.value + param.x + y
        );
        assert.equal(selector2({sub: { value: 1}}, {x: 100, y: 200}), 301);
    });
    test('memoized selector', () => {
        let called = 0;
        const selector = createSelector(state => state.a, a => {
            called++;
            return a;
        });
        assert.equal(selector({a: 1}), 1);
        assert.equal(selector({a: 1}), 1);
        assert.equal(called, 1);
        assert.equal(selector({a: 2}), 2);
        assert.equal(called, 2);
    });
    test('memoized composite arguments', () => {
        let called = 0;
        const selector = createSelector(state => state.sub, sub => {
            called++;
            return sub;
        });
        const state = { sub: { a: 1 } };
        assert.deepEqual(selector(state), { a: 1 });
        assert.deepEqual(selector(state), { a: 1 });
        assert.equal(called, 1);
    });
    test('override valueEquals', () => {
        // a rather absurd equals operation we can verify in tests
        const createOverridenSelector = createSelectorCreator(
          defaultMemoize,
          (a, b) => typeof a === typeof b
        );
        let called = 0;
        const selector = createOverridenSelector(state => state.a, a => {
            called++;
            return a;
        });
        assert.equal(selector({a: 1}), 1);
        assert.equal(selector({a: 2}), 1); // yes, really true
        assert.equal(called, 1);
        assert.equal(selector({a: 'A'}), 'A');
        assert.equal(called, 2);
    });
    test('custom memoize', () => {
        let called = 0;
        const customSelectorCreator = createSelectorCreator(lodashMemoize, JSON.stringify);
        const selector = customSelectorCreator(
            state => state.a,
            state => state.b,
            (a, b) => {
                called++;
                return a + b;
            }
        );
        assert.equal(selector({a: 1, b: 2}), 3);
        assert.equal(selector({a: 1, b: 2}), 3);<commit_msg>check memoization in each test<commit_after>import chai from 'chai';
import { createSelector, createSelectorCreator, defaultMemoize } from '../src/index';
import { default as lodashMemoize } from 'lodash.memoize';

let assert = chai.assert;

suite('selector', () => {
    test('basic selector', () => {
        let called = 0;
        const selector = createSelector(state => state.a, a => {
            called++;
            return a;
        });
        assert.equal(selector({a: 1}), 1);
        assert.equal(selector({a: 1}), 1);
        assert.equal(called, 1);
        assert.equal(selector({a: 2}), 2);
        assert.equal(called, 2);
    });
    test('basic selector multiple keys', () => {
        let called = 0;
        const selector = createSelector(
            state => state.a,
            state => state.b,
            (a, b) => {
                called++;
                return a + b;
            }
        );
        const state1 = {a: 1, b: 2};
        assert.equal(selector(state1), 3);
        assert.equal(selector(state1), 3);
        assert.equal(called, 1);
        const state2 = {a: 3, b: 2};
        assert.equal(selector(state2), 5);
        assert.equal(selector(state2), 5);
        assert.equal(called, 2);
    });
    test('memoized composite arguments', () => {
        let called = 0;
        const selector = createSelector(
            state => state.sub,
            sub => {
                called++;
                return sub;
            }
        );
        const state1 = { sub: { a: 1 } };
        assert.deepEqual(selector(state1), { a: 1 });
        assert.deepEqual(selector(state1), { a: 1 });
        assert.equal(called, 1);
        const state2 = { sub: { a: 2 } };
        assert.deepEqual(selector(state2), { a: 2 });
        assert.equal(called, 2);
    });
    test('first argument can be an array', () => {
        let called = 0;
        const selector = createSelector(
            [state => state.a, state => state.b],
            (a, b) => {
                called++;
                return a + b;
            }
        );
        assert.equal(selector({a: 1, b: 2}), 3);
        assert.equal(selector({a: 1, b: 2}), 3);
        assert.equal(called, 1);
        assert.equal(selector({a: 3, b: 2}), 5);
        assert.equal(called, 2);
    });
    test('can accept props', () => {
        let called = 0;
        const selector = createSelector(
            state => state.a,
            state => state.b,
            (state, props) => props.c,
            (a, b, c) => {
                called++;
                return a + b + c;
            }
        );
        assert.equal(selector({a: 1, b: 2}, {c: 100}), 103);
    });
    test('chained selector', () => {
        let called = 0;
        const selector1 = createSelector(
            state => state.sub,
            sub => sub
        );
        const selector2 = createSelector(
            selector1,
            sub => {
                called++;
                return sub.value;
            }
        );
        const state1 = {sub: { value: 1}};
        assert.equal(selector2(state1), 1);
        assert.equal(selector2(state1), 1);
        assert.equal(called, 1);
        const state2 = {sub: { value: 2}};
        assert.equal(selector2(state2), 2);
        assert.equal(called, 2);
    });
    test('chained selector with props', () => {
        let called = 0;
        const selector1 = createSelector(
            state => state.sub,
            (state, props) => props.x,
            (sub, x) => ({sub, x})
        );
        const selector2 = createSelector(
            selector1,
            (state, props) => props.y,
            (param, y) => {
                called++;
                return param.sub.value + param.x + y;
            }
        );
        const state1 = {sub: { value: 1}};
        assert.equal(selector2(state1, {x: 100, y: 200}), 301);
        assert.equal(selector2(state1, {x: 100, y: 200}), 301);
        assert.equal(called, 1);
        const state2 = {sub: { value: 2}};
        assert.equal(selector2(state2, {x: 100, y: 201}), 303);
        assert.equal(called, 2);
    });
    test('override valueEquals', () => {
        // a rather absurd equals operation we can verify in tests
        const createOverridenSelector = createSelectorCreator(
          defaultMemoize,
          (a, b) => typeof a === typeof b
        );
        let called = 0;
        const selector = createOverridenSelector(state => state.a, a => {
            called++;
            return a;
        });
        assert.equal(selector({a: 1}), 1);
        assert.equal(selector({a: 2}), 1); // yes, really true
        assert.equal(called, 1);
        assert.equal(selector({a: 'A'}), 'A');
        assert.equal(called, 2);
    });
    test('custom memoize', () => {
        const customSelectorCreator = createSelectorCreator(lodashMemoize, JSON.stringify);
        let called = 0;
        const selector = customSelectorCreator(
            state => state.a,
            state => state.b,
            (a, b) => {
                called++;
                return a + b;
            }
        );
        assert.equal(selector({a: 1, b: 2}), 3);
        assert.equal(selector({a: 1, b: 2}), 3);
{'fraction_non_alphanumeric': 0.1576305220883534, 'fraction_numerical': 0.02219991075412762, 'mean_word_length': 1.7084592145015105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 81, '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': '4700948', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2576, 'n_words': 671}