<commit_before>var glob = require('glob')
var t = require('../lib/root.js')
var spawn = require('child_process').spawn
var node = process.execPath
var fs = require('fs')
var dir = __dirname + '/test/'
var path = require('path')

function regEsc(str) {
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

if (process.argv[2])
  runTest(path.resolve(dir, process.argv[2]))
else {
  glob.sync(dir + '*.js').forEach(function (file) {
    runTest(file)
  })
}

function runTest (file) {
  var resfile = file.replace(/\.js$/, '\.tap')
  try {
    var want = fs.readFileSync(resfile, 'utf8').split('\n')
  } catch (er) {
    console.error(er)
    console.error(file)
    console.error(resfile)
    return
  }

  t.test(file.substr(dir.length), function (t) {
    var child = spawn(node, [file], {
      stdio: [ 0, 'pipe', 'pipe' ]
    })

    var found = ''

    child.stdout.setEncoding('utf8')
    child.stdout.on('data', function (c) {
      found += c
    })
    child.on('close', function (er) {
      found = found.split('\n')
      // walk line by line so {{{STACK}}} can be handled
      // otherwise making any changes in this lib would hurt
      for (var f = 0, w = 0;
           f < found.length && w < want.length;
           f++, w++) {
        var wline = want[w]
        var fline = found[f]
        if (wline === '{{{STACK}}}') {
          var fstack = new RegExp('^\\s*\\- "').test(fline)

          if (fstack)
            w--
          else
            f--

          continue
        } else {
          var wmatch = wline.match(/\{\{\{\/(.*?)\/\}\}\}/)
          if (wmatch) {
            t.match(fline, new RegExp(
              regEsc(wline.slice(0, wmatch.index)) +
              wmatch[1] +
              regEsc(wline.slice(wmatch.index + wmatch[0].length))
            ), 'line ' + f + ' ' + wline)
          } else {
            t.equal(fline, wline, 'line ' + f + ' ' + wline.replace(/# (todo|skip)/gi, '- $1'))
          }
          if (!t.passing())
            return t.end()
        }
      }
      t.end()
    })
  })
}<commit_msg>test/test: Handle test cases with multiple regexps<commit_after>var glob = require('glob')
var t = require('../lib/root.js')
var spawn = require('child_process').spawn
var node = process.execPath
var fs = require('fs')
var dir = __dirname + '/test/'
var path = require('path')

function regEsc(str) {
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

if (process.argv[2])
  runTest(path.resolve(dir, process.argv[2]))
else {
  glob.sync(dir + '*.js').forEach(function (file) {
    runTest(file)
  })
}

function runTest (file) {
  var resfile = file.replace(/\.js$/, '\.tap')
  try {
    var want = fs.readFileSync(resfile, 'utf8').split('\n')
  } catch (er) {
    console.error(er)
    console.error(file)
    console.error(resfile)
    return
  }

  t.test(file.substr(dir.length), function (t) {
    var child = spawn(node, [file], {
      stdio: [ 0, 'pipe', 'pipe' ]
    })

    var found = ''

    child.stdout.setEncoding('utf8')
    child.stdout.on('data', function (c) {
      found += c
    })
    child.on('close', function (er) {
      found = found.split('\n')
      // walk line by line so {{{STACK}}} can be handled
      // otherwise making any changes in this lib would hurt
      for (var f = 0, w = 0;
           f < found.length && w < want.length;
           f++, w++) {
        var wline = want[w]
        var fline = found[f]
        if (wline === '{{{STACK}}}') {
          var fstack = new RegExp('^\\s*\\- "').test(fline)

          if (fstack)
            w--
          else
            f--

          continue
        } else {
          var re = /\{\{\{\/(.*?)\/\}\}\}/
          var wmatch = wline.match(re)
          if (wmatch) {
            var wl = wline.split('{{{/')
            var p = '^' + regEsc(wl.shift())
            wl.forEach(function (wlpart) {
              var wlp = wlpart.split('/}}}')
              p += wlp.shift()
              p += regEsc(wlp.join('/}}}'))
            })
            p += '$'
            t.match(fline, new RegExp(p), 'line ' + f + ' ' + wline)
          } else {
            t.equal(fline, wline, 'line ' + f + ' ' + wline.replace(/# (todo|skip)/gi, '- $1'))
          }
          if (!t.passing())
            return t.end()
        }
      }
      t.end()
    })
  })
}
{'fraction_non_alphanumeric': 0.20405343159834177, 'fraction_numerical': 0.004375863657300783, 'mean_word_length': 1.7662420382165605, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3457896', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1501, 'n_words': 333}