<commit_before>'use strict';

/* global describe, beforeEach, it, module, inject, expect, chai */

describe('ngStorage', function() {
    var expect = chai.expect;

    beforeEach(module('ngStorage'));

    it('should contain a $localStorage service', inject(function(
        $localStorage
    ) {
        expect($localStorage).not.to.equal(null);
    }));

    it('should contain a $sessionStorage service', inject(function(
        $sessionStorage
    ) {
        expect($sessionStorage).not.to.equal(null);
    }));

    describeStorageBehaviorFor('localStorage');
    describeStorageBehaviorFor('sessionStorage');

    function describeStorageBehaviorFor(storageType) {

        describe('$' + storageType, function() {

            var $window, $rootScope, $storage, $storageProvider, $timeout;

            function initStorage(initialValues) {

                $window = {
                    eventHandlers: {},
                    addEventListener: function(event, handler) {
                        this.eventHandlers[event] = handler;
                    }
                };

                $window[storageType] = {
                    length: Object.keys(initialValues).length,
                    data: initialValues,
                    getItem: function(key) { return this.data[key]; },
                    setItem: function(key, value) {
                        this.data[key] = value;
                        this.length = Object.keys(this.data).length;
                    },
                    removeItem: function(key) {
                        delete this.data[key];
                        this.length = Object.keys(this.data).length;
                    },
                    key: function(i) { return Object.keys(this.data)[i]; }
                };

                module(['$provide', '$' + storageType + 'Provider', function($provide, _$storageProvider_) {
                    $provide.value('$window', $window);
                    $storageProvider = _$storageProvider_;
                }]);

                inject(['$rootScope', '$' + storageType, '$timeout',
                    function(_$rootScope_, _$storage_, _$timeout) {
                        $rootScope = _$rootScope_;
                        $storage = _$storage_;
                        $timeout = _$timeout;
                    }
                ]);

            }

            it('should, upon loading, contain a value for each ngStorage- key in window.' +
                storageType, function() {

                initStorage({
                    nonNgStorage: 'this should be ingored',
                    'ngStorage-string': '"a string"',
                    'ngStorage-number': '123',
                    'ngStorage-bool': 'true',
                    'ngStorage-object': '{"string":"a string", "number": 123, "bool": true}'
                });

                delete $storage.$default;
                delete $storage.$reset;
                delete $storage.$sync;
                delete $storage.$apply;

                expect($storage).to.deep.equal({
                    string: 'a string',<commit_msg>Provide $document for tests.<commit_after>'use strict';

/* global describe, beforeEach, it, module, inject, expect, chai */

describe('ngStorage', function() {
    var expect = chai.expect;

    beforeEach(module('ngStorage'));

    it('should contain a $localStorage service', inject(function(
        $localStorage
    ) {
        expect($localStorage).not.to.equal(null);
    }));

    it('should contain a $sessionStorage service', inject(function(
        $sessionStorage
    ) {
        expect($sessionStorage).not.to.equal(null);
    }));

    describeStorageBehaviorFor('localStorage');
    describeStorageBehaviorFor('sessionStorage');

    function describeStorageBehaviorFor(storageType) {

        describe('$' + storageType, function() {

            var $window, $rootScope, $storage, $storageProvider, $timeout;

            function initStorage(initialValues) {

                $window = {
                    eventHandlers: {},
                    addEventListener: function(event, handler) {
                        this.eventHandlers[event] = handler;
                    }
                };

                $window[storageType] = {
                    length: Object.keys(initialValues).length,
                    data: initialValues,
                    getItem: function(key) { return this.data[key]; },
                    setItem: function(key, value) {
                        this.data[key] = value;
                        this.length = Object.keys(this.data).length;
                    },
                    removeItem: function(key) {
                        delete this.data[key];
                        this.length = Object.keys(this.data).length;
                    },
                    key: function(i) { return Object.keys(this.data)[i]; }
                };

                module(['$provide', '$' + storageType + 'Provider', function($provide, _$storageProvider_) {
                    $provide.value('$window', $window);
                    $provide.value('$document', angular.element(document));
                    $storageProvider = _$storageProvider_;
                }]);

                inject(['$rootScope', '$' + storageType, '$timeout',
                    function(_$rootScope_, _$storage_, _$timeout) {
                        $rootScope = _$rootScope_;
                        $storage = _$storage_;
                        $timeout = _$timeout;
                    }
                ]);

            }

            it('should, upon loading, contain a value for each ngStorage- key in window.' +
                storageType, function() {

                initStorage({
                    nonNgStorage: 'this should be ingored',
                    'ngStorage-string': '"a string"',
                    'ngStorage-number': '123',
                    'ngStorage-bool': 'true',
                    'ngStorage-object': '{"string":"a string", "number": 123, "bool": true}'
                });

                delete $storage.$default;
                delete $storage.$reset;
                delete $storage.$sync;
                delete $storage.$apply;

                expect($storage).to.deep.equal({
                    string: 'a string',
{'fraction_non_alphanumeric': 0.12260415016632346, 'fraction_numerical': 0.0019008395374623792, 'mean_word_length': 1.482894219425875, 'pattern_counts': {'":': 6, '<': 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': '51129', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1439, 'n_words': 298}