1// Copyright 2020 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @fileoverview Test all expressions in DB.
7 */
8
9const fs = require('fs');
10const fsPath = require('path');
11const program = require('commander');
12const sinon = require('sinon');
13
14const crossOverMutator = require('./mutators/crossover_mutator.js');
15const db = require('./db.js');
16const random = require('./random.js');
17const sourceHelpers = require('./source_helpers.js');
18
19const sandbox = sinon.createSandbox();
20
21function main() {
22  program
23    .version('0.0.1')
24    .option('-i, --input_dir <path>', 'DB directory.')
25    .parse(process.argv);
26
27  if (!program.input_dir) {
28    console.log('Need to specify DB dir.');
29    return;
30  }
31
32  const mutateDb = new db.MutateDb(program.input_dir);
33  const mutator = new crossOverMutator.CrossOverMutator(
34      { MUTATE_CROSSOVER_INSERT: 1.0, testing: true }, mutateDb);
35
36  let nPass = 0;
37  let nFail = 0;
38  // Iterate over all statements saved in the DB.
39  for (const statementPath of mutateDb.index.all) {
40    const expression = JSON.parse(fs.readFileSync(
41        fsPath.join(program.input_dir, statementPath)), 'utf-8');
42    // Stub out choosing random variables in cross-over mutator.
43    sandbox.stub(random, 'single').callsFake((a) => { return a[0]; });
44    // Ensure we are selecting the statement of the current iteration.
45    sandbox.stub(mutateDb, 'getRandomStatement').callsFake(
46        () => { return expression; });
47    // Use a source that will try to insert one statement, allowing
48    // super.
49    const source = sourceHelpers.loadSource(
50        __dirname,
51        'test_data/cross_over_mutator_class_input.js');
52    try {
53      mutator.mutate(source);
54      nPass++;
55    } catch (e) {
56      console.log('******************************************************')
57      console.log(expression);
58      console.log(e.message);
59      nFail++;
60    }
61    sandbox.restore();
62  }
63  console.log(`Result: ${nPass} passed, ${nFail} failed.`)
64}
65
66main();
67