<commit_before>var assert = require('assert');
var co = require('co');
var Document = require('../').Document;

describe('Document', function() {
  it('tracks changes on non-new docs', function(done) {
    co(function*() {
      var obj = Document({}, false);
      obj.a = 1;
      assert.deepEqual(yield obj.$delta(), { $set: { a: 1 }, $unset: {} });
      done();
    }).catch(function(err) {
      done(err);
    });
  });

  it('tracks changes on nested docs', function() {
    var obj = Document({}, false);

    obj.nested = { x: 2 };
    assert.deepEqual(obj.$delta(),
      { $set: { nested: { x: 2 } }, $unset: {} });

    ++obj.nested.x;
    assert.deepEqual(obj.$delta(),
      { $set: { nested: { x: 3 } }, $unset: {} });

    obj.nested = { y: 2 };
    assert.deepEqual(obj.$delta(),
      { $set: { nested: { y: 2 } }, $unset: {} });
  });

  it('clears changes', function() {
    var obj = Document({ nested: { x: 1 } }, false);

    obj.nested.x = 5;
    assert.deepEqual(obj.$delta(),
      { $set: { 'nested.x': 5 }, $unset: {} });

    obj.nested = { x: 3 };
    assert.deepEqual(obj.$delta(),
      { $set: { nested: { x: 3 } }, $unset: {} });
  });

  it('handles deletes', function() {
    var obj = Document({}, false);

    obj.top = 1;
    obj.nested = { x: 2 };

    assert.deepEqual(obj.$delta(),
      { $set: { top: 1, nested: { x: 2 } }, $unset: {} });

    delete obj.nested['x'];
    delete obj.top;

    assert.deepEqual(obj.$delta(),
      { $set: { nested: {} }, $unset: { 'nested.x': true, top: true } });

    delete obj['nested'];
    assert.deepEqual(obj.$delta(),
      { $set: { }, $unset: { top: true, nested: true } });
  });

  it('handles arrays', function(done) {
    co(function*() {
      var obj = Document({}, false);

      obj.arr = [1, 2];

      assert.deepEqual(yield obj.$delta(),
        { $set: { arr: [1, 2] }, $unset: {} });

      obj.arr[1] = 3;

      assert.deepEqual(yield obj.$delta(),
        { $set: { arr: [1, 3] }, $unset: {} });

      obj.arr.push(5);

      assert.deepEqual(yield obj.$delta(),
        { $set: { arr: [1, 3, 5] }, $unset: {} });

      done();
    }).catch(function(err) {
      done(err);
    });
  });
});<commit_msg>remove co from document tests<commit_after>var assert = require('assert');
var Document = require('../').Document;

describe('Document', function() {
  it('tracks changes on non-new docs', function() {
    var obj = Document({}, false);
    obj.a = 1;
    assert.deepEqual(obj.$delta(), { $set: { a: 1 }, $unset: {} });
  });

  it('tracks changes on nested docs', function() {
    var obj = Document({}, false);

    obj.nested = { x: 2 };
    assert.deepEqual(obj.$delta(),
      { $set: { nested: { x: 2 } }, $unset: {} });

    ++obj.nested.x;
    assert.deepEqual(obj.$delta(),
      { $set: { nested: { x: 3 } }, $unset: {} });

    obj.nested = { y: 2 };
    assert.deepEqual(obj.$delta(),
      { $set: { nested: { y: 2 } }, $unset: {} });
  });

  it('clears changes', function() {
    var obj = Document({ nested: { x: 1 } }, false);

    obj.nested.x = 5;
    assert.deepEqual(obj.$delta(),
      { $set: { 'nested.x': 5 }, $unset: {} });

    obj.nested = { x: 3 };
    assert.deepEqual(obj.$delta(),
      { $set: { nested: { x: 3 } }, $unset: {} });
  });

  it('handles deletes', function() {
    var obj = Document({}, false);

    obj.top = 1;
    obj.nested = { x: 2 };

    assert.deepEqual(obj.$delta(),
      { $set: { top: 1, nested: { x: 2 } }, $unset: {} });

    delete obj.nested['x'];
    delete obj.top;

    assert.deepEqual(obj.$delta(),
      { $set: { nested: {} }, $unset: { 'nested.x': true, top: true } });

    delete obj['nested'];
    assert.deepEqual(obj.$delta(),
      { $set: { }, $unset: { top: true, nested: true } });
  });

  it('handles arrays', function() {
    var obj = Document({}, false);

    obj.arr = [1, 2];

    assert.deepEqual(obj.$delta(),
      { $set: { arr: [1, 2] }, $unset: {} });

    obj.arr[1] = 3;

    assert.deepEqual(obj.$delta(),
      { $set: { arr: [1, 3] }, $unset: {} });

    obj.arr.push(5);

    assert.deepEqual(obj.$delta(),
      { $set: { arr: [1, 3, 5] }, $unset: {} });
  });
});
{'fraction_non_alphanumeric': 0.24095808383233533, 'fraction_numerical': 0.01341317365269461, 'mean_word_length': 2.48, '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': '6776485', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1472, 'n_words': 337}