<commit_before>'use strict';

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;

            function initStorage(initialValues) {

                $window = {}
                $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(function($provide) {
                    $provide.value('$window', $window);
                });

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

            }

            it('should 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
                expect($storage).to.deep.equal({
                    string: 'a string',
                    number: 123,
                    bool: true,
                    object: {string:'a string', number: 123, bool: true}
                });
            })

            it('should add a key to window.' + storageType + ' when a key is added to $storage', function(done) {
                initStorage({});
                $storage.newKey = 'some value';
                $rootScope.$digest();
                setTimeout(function() {
                    expect($window[storageType].data).to.deep.equal({'ngStorage-newKey': '"some value"'});
                    done();
                }, 125);
            });

            it('should update the key in window.' + storageType + ' when the associated key in $' + storageType + ' is updated', function(done) {
                initStorage({'ngStorage-existing': '"update me"'});
                $storage.existing = 'updated';
                $rootScope.$digest();
                setTimeout(function() {
                    expect($window[storageType].data).to.deep.equal({'ngStorage-existing': '"updated"'});
                    done();
                }, 125);
            });

            it('should delete the key from window.' + storageType + ' when the associated key in $' + storageType + ' is deleted', function(done) {
                initStorage({'ngStorage-existing': '"delete me"'});
                delete $storage.existing;
                $rootScope.$digest();
                setTimeout(function() {
                    expect($window[storageType].data).to.deep.equal({});
                    done();
                }, 125);
            });
        });
    }
});

<commit_msg>Adds test for $reset and $default behaviors.<commit_after>'use strict';

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;

            function initStorage(initialValues) {

                $window = {}
                $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(function($provide) {
                    $provide.value('$window', $window);
                });

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

            }

            it('should 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
                expect($storage).to.deep.equal({
                    string: 'a string',
                    number: 123,
                    bool: true,
                    object: {string:'a string', number: 123, bool: true}
                });
            })

            it('should add a key to window.' + storageType + ' when a key is added to $storage', function(done) {
                initStorage({});
                $storage.newKey = 'some value';
                $rootScope.$digest();
                setTimeout(function() {
                    expect($window[storageType].data).to.deep.equal({'ngStorage-newKey': '"some value"'});
                    done();
                }, 125);
            });

            it('should update the key in window.' + storageType + ' when the associated key in $' + storageType + ' is updated', function(done) {
                initStorage({'ngStorage-existing': '"update me"'});
                $storage.existing = 'updated';
                $rootScope.$digest();
                setTimeout(function() {
                    expect($window[storageType].data).to.deep.equal({'ngStorage-existing': '"updated"'});
                    done();
                }, 125);
            });

            it('should delete the key from window.' + storageType + ' when the associated key in $' + storageType + ' is deleted', function(done) {
                initStorage({'ngStorage-existing': '"delete me"'});
                delete $storage.existing;
                $rootScope.$digest();
                setTimeout(function() {
                    expect($window[storageType].data).to.deep.equal({});
                    done();
                }, 125);
            });

            describe('when $reset is called with no arguments', function(){

                beforeEach(function (done) {
                    initStorage({
                        nonNgStorage: 'this should be ingored',
                        'ngStorage-delete': '"this should be deleted"'
                    });
                    $storage.$reset();
                    $rootScope.$digest();
                    setTimeout(done, 125);
                });

                it('should delete all ngStorage- keys from window.' + storageType, function() {
                    expect($window[storageType].data).to.deep.equal({nonNgStorage: 'this should be ingored'});
                });

                it('should delete all keys from $' + storageType, function() {
                    delete $storage.$default
                    delete $storage.$reset
                    expect($storage).to.deep.equal({});
                });

            });

            describe('when $reset is called with an object', function(){

                beforeEach(function (done) {
                    initStorage({
                        nonNgStorage: 'this should be ingored',
                        'ngStorage-delete': '"this should be deleted"'
                    });
                    $storage.$reset({some: 'value'});
                    $rootScope.$digest();
                    setTimeout(done, 125);
                });

                it('should reset the ngStorage- keys on window.' + storageType + ' to match the object', function() {
                    expect($window[storageType].data).to.deep.equal({
                        nonNgStorage: 'this should be ingored',
                        'ngStorage-some': '"value"'
                    });
                });

                it('should reset $' + storageType + ' to match the object', function() {
                    delete $storage.$default
                    delete $storage.$reset
                    expect($storage).to.deep.equal({some: 'value'});
                });

            });

            describe('when $default is called', function(){

                beforeEach(function (done) {
                    initStorage({
                        nonNgStorage: 'this should be ingored',
                        'ngStorage-existing': '"this should not be replaced"'
                    });
                    $storage.$default({
                        existing: 'oops! replaced!',
                        'new': 'new value'
                    });
                    $rootScope.$digest();
                    setTimeout(done, 125);
                });

                it('should should add any missing ngStorage- keys on window.' + storageType, function() {
                    expect($window[storageType].data['ngStorage-new']).to.equal('"new value"');
                });

                it('should should add any missing values to $' + storageType, function() {
                    expect($storage['new']).to.equal('new value');
                });

                it('should should not modify any existing ngStorage- keys on window.' + storageType, function() {
                    expect($window[storageType].data['ngStorage-existing']).to.equal('"this should not be replaced"');
                });

                it('should should not modify any existing values on $' + storageType, function() {
                    expect($storage['existing']).to.equal('this should not be replaced');
                });
            });
        });
    }
});


{'fraction_non_alphanumeric': 0.12816388724286804, 'fraction_numerical': 0.004317277575552358, 'mean_word_length': 1.3842583249243188, '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': '6665416', 'n_tokens_mistral': 2915, 'n_tokens_neox': 2623, 'n_words': 613}