<commit_before>'use strict';

var diff = require('../src/diff');
var Immutable = require('Immutable');
var JSC = require('jscheck');
var assert = require('assert');

describe('Map diff', function(){
  var failure = null;

  before(function(){
    JSC.on_report(function(report){
      console.log(report);
    });

    JSC.on_fail(function(jsc_failure){
      failure = jsc_failure;
    });
  });

  it('check properties', function(){
    JSC.test(
      'returns [] when equal',
      function(veredict, obj){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.fromJS(obj);

        var result = diff.diff(map1, map2);

        return veredict(result.length === 0);
      },
      [
        JSC.object(5)
      ]
    );


    JSC.test(
      'returns add op when missing attribute',
      function(veredict, obj, obj2){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.fromJS(obj).set('key2', obj2.key2);

        var result = diff.diff(map1, map2);
        var expected = {op: 'add', path: '/key2', value: obj2.key2};

        return veredict(
          result.length !== 0 &&
          result.every(function(op){ return opsAreEqual(op, expected); })
        );
      },
      [
        JSC.object({
          key: JSC.integer()
        }),
        JSC.object({
          key2: JSC.integer()
        })
      ]
    );

    JSC.test(
      'returns replace op when same attribute with different values',
      function(veredict, obj, newValue){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.fromJS(obj).set('key', newValue);

        var result = diff.diff(map1, map2);
        var expected = {op: 'replace', path: '/key', value: newValue};

        return veredict(
          result.length !== 0 &&
          result.every(function(op){ return opsAreEqual(op, expected); })
        );
      },
      [
        JSC.object({
          key: JSC.integer(1, 100)
        }),
        JSC.integer(101, 200)
      ]
    );

    JSC.test(
      'returns remove op when attribute is missing',
      function(veredict, obj){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.Map();

        var result = diff.diff(map1, map2);
        var expected = {op: 'remove', path: '/key'};

        return veredict(
          result.length !== 0 &&
          result.every(function(op){ return opsAreEqual(op, expected); })
        );
      },
      [
        JSC.object({
          key: JSC.integer()
        })
      ]
    );

    if(failure){
      console.error(failure);
      throw failure;
    }
  });

  it('check nested structures', function(){
    JSC.test(
      'returns add op when missing attribute in nested structure',
      function(veredict, obj, obj2){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.fromJS(obj).setIn(['b', 'd'], obj2.d);

        var result = diff.diff(map1, map2);
        var expected = {op: 'add', path: '/b/d', value: obj2.d};

        return veredict(
          result.length !== 0 &&
          result.every(function(op){ return opsAreEqual(op, expected); })
        );
      },
      [
        JSC.object({
          a: JSC.integer(),
          b: JSC.object({
            c: JSC.integer()
          })
        }),
        JSC.object({
          d: JSC.integer()
        })
      ]
    );
  });
});

var opsAreEqual = function(value, expected){
  return value.op === expected.op &&
      value.path === expected.path &&
      value.value === expected.value;
};<commit_msg>tests for replace ops in nested structures<commit_after>'use strict';

var diff = require('../src/diff');
var Immutable = require('Immutable');
var JSC = require('jscheck');
var assert = require('assert');

describe('Map diff', function(){
  var failure = null;

  before(function(){
    JSC.on_report(function(report){
      console.log(report);
    });

    JSC.on_fail(function(jsc_failure){
      failure = jsc_failure;
    });
  });

  it('check properties', function(){
    JSC.test(
      'returns [] when equal',
      function(veredict, obj){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.fromJS(obj);

        var result = diff.diff(map1, map2);

        return veredict(result.length === 0);
      },
      [
        JSC.object(5)
      ]
    );


    JSC.test(
      'returns add op when missing attribute',
      function(veredict, obj, obj2){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.fromJS(obj).set('key2', obj2.key2);

        var result = diff.diff(map1, map2);
        var expected = {op: 'add', path: '/key2', value: obj2.key2};

        return veredict(
          result.length !== 0 &&
          result.every(function(op){ return opsAreEqual(op, expected); })
        );
      },
      [
        JSC.object({
          key: JSC.integer()
        }),
        JSC.object({
          key2: JSC.integer()
        })
      ]
    );

    JSC.test(
      'returns replace op when same attribute with different values',
      function(veredict, obj, newValue){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.fromJS(obj).set('key', newValue);

        var result = diff.diff(map1, map2);
        var expected = {op: 'replace', path: '/key', value: newValue};

        return veredict(
          result.length !== 0 &&
          result.every(function(op){ return opsAreEqual(op, expected); })
        );
      },
      [
        JSC.object({
          key: JSC.integer(1, 100)
        }),
        JSC.integer(101, 200)
      ]
    );

    JSC.test(
      'returns remove op when attribute is missing',
      function(veredict, obj){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.Map();

        var result = diff.diff(map1, map2);
        var expected = {op: 'remove', path: '/key'};

        return veredict(
          result.length !== 0 &&
          result.every(function(op){ return opsAreEqual(op, expected); })
        );
      },
      [
        JSC.object({
          key: JSC.integer()
        })
      ]
    );

    if(failure){
      console.error(failure);
      throw failure;
    }
  });

  it('check nested structures', function(){
    JSC.test(
      'returns add op when missing attribute in nested structure',
      function(veredict, obj, obj2){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.fromJS(obj).setIn(['b', 'd'], obj2.d);

        var result = diff.diff(map1, map2);
        var expected = {op: 'add', path: '/b/d', value: obj2.d};

        return veredict(
          result.length !== 0 &&
          result.every(function(op){ return opsAreEqual(op, expected); })
        );
      },
      [
        JSC.object({
          a: JSC.integer(),
          b: JSC.object({
            c: JSC.integer()
          })
        }),
        JSC.object({
          d: JSC.integer()
        })
      ]
    );

    JSC.test(
      'returns replace op when different value in nested structure',
      function(veredict, obj, obj2){
        var map1 = Immutable.fromJS(obj);
        var map2 = Immutable.fromJS(obj).setIn(['b', 'c'], obj2.c);

        var result = diff.diff(map1, map2);
        var expected = {op: 'replace', path: '/b/c', value: obj2.c};

        return veredict(
          result.length !== 0 &&
          result.every(function(op){ return opsAreEqual(op, expected); })
        );
      },
      [
        JSC.object({
          a: JSC.integer(),
          b: JSC.object({
            c: JSC.integer(1, 100)
          })
        }),
        JSC.object({
          c: JSC.integer(101, 200)
        })
      ]
    );
  });
});

var opsAreEqual = function(value, expected){
  return value.op === expected.op &&
      value.path === expected.path &&
      value.value === expected.value;
};
{'fraction_non_alphanumeric': 0.1524987120041216, 'fraction_numerical': 0.014425553838227717, 'mean_word_length': 1.902803738317757, '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': '3704280', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2415, 'n_words': 549}