<commit_before>var Nested = require( '../../../dist/index' ),
    expect = require( 'chai' ).expect,
    sinon = require( 'sinon' );

var Model = Nested.Model, Collection = Nested.Collection;

describe( 'Memory management', function(){
    var M = Model.extend();
    
    M.define({
        attributes : {
            x : Number.integer,
            agg : M.value( null ),
            ref : M.shared,
            col : M.Collection,
            refs : M.Collection.shared,
            ids : M.Collection.subsetOf( 'col' ) 
        }
    });

    var singleton = new M();

    it( 'Aggregated values are recursively disposed', function(){
        var m = new M();
        m.agg = {};

        var x = m.col, y = m.agg;
        m.dispose();
        expect( x._disposed ).to.be.true;
        expect( y._disposed ).to.be.true;
    } );

    it( 'Shared refs do not create a leak', function(){
        var m = new M();
        m.ref = singleton;
        m.refs = [ singleton ];

        expect( singleton._events.change.next.next ).to.be.null;

        m.dispose();
        expect( m._disposed ).to.be.true;

        expect( singleton._events.change ).to.be.empty;
        expect( singleton._disposed ).to.be.undefined;
    } );


});<commit_msg>Bug: aggregated items are not disposed when removed<commit_after>var Nested = require( '../../../dist/index' ),
    expect = require( 'chai' ).expect,
    sinon = require( 'sinon' );

var Model = Nested.Model, Collection = Nested.Collection;

describe( 'Memory management', function(){
    var M = Model.extend();
    
    M.define({
        attributes : {
            x : Number.integer,
            agg : M.value( null ),
            ref : M.shared,
            col : M.Collection,
            refs : M.Collection.shared,
            ids : M.Collection.subsetOf( 'col' ) 
        }
    });

    var singleton = new M();

    it( 'Aggregated values are recursively disposed', function(){
        var m = new M();
        m.agg = {};

        var x = m.col, y = m.agg;
        m.dispose();
        expect( x._disposed ).to.be.true;
        expect( y._disposed ).to.be.true;
    } );

    it( 'Aggregated value is disposed when overwritten', function(){
        var m = new M();
        m.agg = {};

        var y = m.agg;

        m.agg = null;

        expect( y._disposed ).to.be.true;        
    });

    it( 'Aggregated collection item is disposed when removed from collection', function(){
        var m = new M();
        m.col.add( {} );

        var y = m.col.first();

        m.col = [];

        expect( y._disposed ).to.be.true;        
    });

    it( 'Shared refs do not create a leak', function(){
        var m = new M();
        m.ref = singleton;
        m.refs = [ singleton ];

        expect( singleton._events.change.next.next ).to.be.null;

        m.dispose();
        expect( m._disposed ).to.be.true;

        expect( singleton._events.change ).to.be.empty;
        expect( singleton._disposed ).to.be.undefined;
    } );


});
{'fraction_non_alphanumeric': 0.15215215215215216, 'fraction_numerical': 0.0, 'mean_word_length': 1.7378995433789954, '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': '1776564', 'n_tokens_mistral': 952, 'n_tokens_neox': 904, 'n_words': 245}