# All files / src/utils test-utils.js

100% Statements 27/27
80% Branches 4/5
100% Functions 7/7
100% Lines 26/26

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    633x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x

    12x
    12x

    6x

    3x

    3x

    3x

    3x

    3x
    3x
    3x
    3x

    3x

    3x

    58x

    58x

    3x

     import buildTools from 'walt-buildtools';
    import path from 'path';
    import fs from 'fs';
    import { getText } from '../utils/string';
    import semantics from '../semantics';
    import validate from '../validation';
    import parser from '../parser';
    import emitter from '../emitter';
    import generator from '../generator';
    import { mapNode, walkNode, prettyPrintNode } from '..';

    function resolve(file, parent) {
      const root = parent ? path.dirname(parent) : __dirname;
      return path.resolve(root, file.slice(-5) === '.walt' ? file : file + '.walt');
    }

    function getFileContents(file, parent, mode) {
      return fs.readFileSync(resolve(file, parent), mode);
    }
    function link(file, api) {
      const tree = buildTools.compile(file, api);

      function walt(importsObj = {}) {
        return buildTools.build(importsObj, tree, api);
      }

      walt.tree = tree;

      return walt;
    }

    export const harness = (filepath, env) => t => {
      const memory = new WebAssembly.Memory({ initial: 1 });
      const view = new DataView(memory.buffer);
      const decodeText = getText(view);

      const build = link(filepath, {
        resolve,
        getFileContents,
        mapNode,
        walkNode,
        parser,
        semantics,
        validate,
        emitter,
        generator,
        prettyPrintNode,
      });
      return build({
        env: {
          memory,
          MEMORY_OFFSET: 0,
          log: console.log,
          assert(strPointer, value, expected) {
            const text = decodeText(strPointer);

            t.is(value, expected, text);
          },
          ...env,
        },
      }).then(module => module.instance.exports.run());
    };