<commit_before>/*
Name:           pastecapi - test.js
Author:         <NAME> (https://frankl.in)
Source & docs:  https://github.com/fvdm/nodejs-pastecapi
Feedback:       https://github.com/fvdm/nodejs-pastecapi/issues
License:        Unlicense (Public Domain, see LICENSE file)
                (https://github.com/fvdm/nodejs-pastecapi/raw/develop/LICENSE)
*/

'use strict';
var path = require ('path');
var dir = path.dirname (module.filename);

var pkg = require (path.join (dir, 'package.json'));
var app = require (dir) (
  process.env.PASTEC_ENDPOINT || 'http://localhost:4212',
  process.env.PASTEC_TIMEOUT || 5000
);

var colors = process.env.PASTEC_COLORS || 'dark';
var errors = 0;
var warnings = 0;
var queue = [];
var next = 0;


// Color string
function log (type, str) {
  var c = colors === 'bright' ? 9 : 3;

  if (!str) {
    str = type;
    type = 'plain';
  }

  switch (type) {
    case 'error': console.log (`\u001b[1m\u001b[${c}1mERR\u001b[0m  - ${str}\n`); break;
    case 'fail': console.log (`\u001b[${c}1mFAIL\u001b[0m - ${str}`); break;
    case 'good': console.log (`\u001b[${c}2mgood\u001b[0m - ${str}`); break;
    case 'warn': console.log (`\u001b[${c}3mwarn\u001b[0m - ${str}`); break;
    case 'info': console.log (`\u001b[${c}6minfo\u001b[0m - ${str}`); break;
    case 'note': console.log (`\u001b[1m${str}\u001b[0m`); break;
    case 'plain': default: console.log (str); break;
  }
}

// handle exits
process.on ('exit', function () {
  console.log ();
  log ('info', errors + ' errors');
  log ('info', warnings + ' warnings');

  if (errors) {
    log ('note', '\nTest FAILED');
    process.exit (1);
  } else {
    log ('note', '\nTest DONE');
    process.exit (0);
  }
});

// prevent errors from killing the process
process.on ('uncaughtException', function (err) {
  console.log (err);
  console.log ();
  console.log (err.stack);
  console.log ();
  errors++;
});

// Queue to prevent flooding
function doNext () {
  next++;
  if (queue [next]) {
    queue [next] ();
  }
}

// doTest( passErr, 'methods', [
//   ['fail', 'feeds', typeof feeds, 'object']
// ])
function doTest (err, label, tests) {
  var testErrors = [];
  var testWarnings = [];
  var test;
  var i;

  if (err instanceof Error) {
    log ('error', label);
    console.dir (err, { depth: null, colors: true });
    console.log ();
    console.log (err.stack);
    console.log ();
    errors++;
  } else {
    for (i = 0; i < tests.length; i++) {
      test = {
        level: tests [i] [0],
        label: tests [i] [1],
        result: tests [i] [2],
        expect: tests [i] [3]
      };

      switch (test.level) {
        case 'fail':
          if (test.result !== test.expect) {
            testErrors.push (test);
            errors++;
          }
        break;

        case 'warn':
          if (test.result !== test.expect) {
            testWarnings.push (test);
            warnings++;
          }
        break;

        case 'info':
        default:
          log ('info', test.label + ' - ' + test.result);
        break;
      }
    }

    if (!testErrors.length && !testWarnings.length) {
      log ('good', label);
    } else if (testErrors.length) {<commit_msg>Test: fixed switch break indentation<commit_after>/*
Name:           pastecapi - test.js
Author:         <NAME> (https://frankl.in)
Source & docs:  https://github.com/fvdm/nodejs-pastecapi
Feedback:       https://github.com/fvdm/nodejs-pastecapi/issues
License:        Unlicense (Public Domain, see LICENSE file)
                (https://github.com/fvdm/nodejs-pastecapi/raw/develop/LICENSE)
*/

'use strict';
var path = require ('path');
var dir = path.dirname (module.filename);

var pkg = require (path.join (dir, 'package.json'));
var app = require (dir) (
  process.env.PASTEC_ENDPOINT || 'http://localhost:4212',
  process.env.PASTEC_TIMEOUT || 5000
);

var colors = process.env.PASTEC_COLORS || 'dark';
var errors = 0;
var warnings = 0;
var queue = [];
var next = 0;


// Color string
function log (type, str) {
  var c = colors === 'bright' ? 9 : 3;

  if (!str) {
    str = type;
    type = 'plain';
  }

  switch (type) {
    case 'error': console.log (`\u001b[1m\u001b[${c}1mERR\u001b[0m  - ${str}\n`); break;
    case 'fail': console.log (`\u001b[${c}1mFAIL\u001b[0m - ${str}`); break;
    case 'good': console.log (`\u001b[${c}2mgood\u001b[0m - ${str}`); break;
    case 'warn': console.log (`\u001b[${c}3mwarn\u001b[0m - ${str}`); break;
    case 'info': console.log (`\u001b[${c}6minfo\u001b[0m - ${str}`); break;
    case 'note': console.log (`\u001b[1m${str}\u001b[0m`); break;
    case 'plain': default: console.log (str); break;
  }
}

// handle exits
process.on ('exit', function () {
  console.log ();
  log ('info', errors + ' errors');
  log ('info', warnings + ' warnings');

  if (errors) {
    log ('note', '\nTest FAILED');
    process.exit (1);
  } else {
    log ('note', '\nTest DONE');
    process.exit (0);
  }
});

// prevent errors from killing the process
process.on ('uncaughtException', function (err) {
  console.log (err);
  console.log ();
  console.log (err.stack);
  console.log ();
  errors++;
});

// Queue to prevent flooding
function doNext () {
  next++;
  if (queue [next]) {
    queue [next] ();
  }
}

// doTest( passErr, 'methods', [
//   ['fail', 'feeds', typeof feeds, 'object']
// ])
function doTest (err, label, tests) {
  var testErrors = [];
  var testWarnings = [];
  var test;
  var i;

  if (err instanceof Error) {
    log ('error', label);
    console.dir (err, { depth: null, colors: true });
    console.log ();
    console.log (err.stack);
    console.log ();
    errors++;
  } else {
    for (i = 0; i < tests.length; i++) {
      test = {
        level: tests [i] [0],
        label: tests [i] [1],
        result: tests [i] [2],
        expect: tests [i] [3]
      };

      switch (test.level) {
        case 'fail':
          if (test.result !== test.expect) {
            testErrors.push (test);
            errors++;
          }
          break;

        case 'warn':
          if (test.result !== test.expect) {
            testWarnings.push (test);
            warnings++;
          }
          break;

        case 'info':
        default:
          log ('info', test.label + ' - ' + test.result);
          break;
      }
    }

    if (!testErrors.length && !testWarnings.length) {
      log ('good', label);
    } else if (testErrors.length) {
{'fraction_non_alphanumeric': 0.18758771245906752, 'fraction_numerical': 0.02245438952128489, 'mean_word_length': 2.446534121440086, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 8, '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': '2505586', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2240, 'n_words': 560}