<commit_before>// obj_diff policy tests
//
// Copyright 2011 <NAME>
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

var test = require('tap').test
  , obj_diff = require('../api')
  ;

var JS = JSON.stringify
  , JP = JSON.parse
  , JDUP = function(x) { return JSON.parse(JSON.stringify(x)) }
  ;

var fixtures = {
  server: {
    _id: 'Server/foo'
  , _rev: '1-blah'
  , state: 'running' },

  transfer: {
    _id: 'Server/foo'
  , _rev: '1-blah'
  , state: 'transfer'
  , transfer: {"to":"Manager/somebody"}
  }
}

function make_tester(t) {
  return tester;

function tester(method, from_id, to_id, policy, expected) {
  var from_merge = {}, to_merge = {};

  if(typeof from_id == 'object') {
    from_merge = from_id;
    from_id = from_merge.base;
    delete from_merge.base;
  }
  if(typeof to_id == 'object') {
    to_merge = to_id;
    to_id = to_merge.base;
    delete to_merge.base;
  }

  var from = JSON.parse(JSON.stringify(fixtures[from_id]));
  var to   = JSON.parse(JSON.stringify(fixtures[to_id]));

  var k;
  for(k in from_merge) {
    if(from_merge[k] === undefined)
      delete from[k];
    else
      from[k] = from_merge[k];
  }

  for(k in to_merge) {
    if(to_merge[k] === undefined)
      delete to[k];
    else
      to[k] = to_merge[k];
  }

  method = method.replace(/^doc_diff_/, "");
  var result = obj_diff[method](from, to, policy);
  var message = "from="+JS(from) + " to="+JS(to) + " policy="+JS(policy);

  return t.same(result, expected, message);
}

}

test('At most', function(t) {
  var go = make_tester(t);

  go('doc_diff_atmost', 'server', 'server', {}, true);
  go('doc_diff_atmost', 'server', 'server', {}, true);
  go('doc_diff_atmost', 'server', 'server', {change: {}}, true);<commit_msg>Indentation refactor<commit_after>// obj_diff policy tests
//
// Copyright 2011 <NAME>
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

var test = require('tap').test
  , obj_diff = require('../api')
  ;

var JS = JSON.stringify
  , JP = JSON.parse
  , JDUP = function(x) { return JSON.parse(JSON.stringify(x)) }
  ;

var fixtures = {
  server: {
    _id: 'Server/foo'
  , _rev: '1-blah'
  , state: 'running' },

  transfer: {
    _id: 'Server/foo'
  , _rev: '1-blah'
  , state: 'transfer'
  , transfer: {"to":"Manager/somebody"}
  }
}

function make_tester(t) {
  return tester;

  function tester(method, from_id, to_id, policy, expected) {
    var from_merge = {}, to_merge = {};

    if(typeof from_id == 'object') {
      from_merge = from_id;
      from_id = from_merge.base;
      delete from_merge.base;
    }
    if(typeof to_id == 'object') {
      to_merge = to_id;
      to_id = to_merge.base;
      delete to_merge.base;
    }

    var from = JSON.parse(JSON.stringify(fixtures[from_id]));
    var to   = JSON.parse(JSON.stringify(fixtures[to_id]));

    var k;
    for(k in from_merge) {
      if(from_merge[k] === undefined)
        delete from[k];
      else
        from[k] = from_merge[k];
    }

    for(k in to_merge) {
      if(to_merge[k] === undefined)
        delete to[k];
      else
        to[k] = to_merge[k];
    }

    method = method.replace(/^doc_diff_/, "");
    var result = obj_diff[method](from, to, policy);
    var message = "from="+JS(from) + " to="+JS(to) + " policy="+JS(policy);

    return t.same(result, expected, message);
  }

}

test('At most', function(t) {
  var go = make_tester(t);

  go('doc_diff_atmost', 'server', 'server', {}, true);
  go('doc_diff_atmost', 'server', 'server', {}, true);
  go('doc_diff_atmost', 'server', 'server', {change: {}}, true);
{'fraction_non_alphanumeric': 0.154578913184672, 'fraction_numerical': 0.004329941545789132, 'mean_word_length': 3.003466204506066, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5468350', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1577, 'n_words': 422}