<commit_before>/**
 * @fileOverview A test runner compliant to the
 * [CommonJS Unit Testing](http://wiki.commonjs.org/wiki/Unit_Testing/1.0)
 * specification.
 */

var strings = require("ringo/utils/strings");
var term = require("ringo/term");
var fs = require("fs");

export(
    "run",
    "jsDump",
    "getStackTrace",
    "getType"
);

var {
    AssertionError,
    ArgumentsError
} = require("./assert");

/**
 * Converts the value passed as argument into a nicely formatted and
 * indented string
 * @param {Object} value The value to convert into a string
 * @param {Number} lvl Optional indentation level (defaults to zero)
 * @returns The string representation of the object passed as argument
 * @type String
 */
function jsDump(value, lvl) {
    if (!lvl) {
        lvl = 0;
    }

    switch (getType(value)) {
        case "string":
            return jsDump.quote(value);
        case "boolean":
        case "number":
        case "nan":
        case "date":
        case "regexp":
            return value.toString();
        case "undefined":
        case "null":
            return String(value);
        case "function":
            if (getType(value.name) === "string" && value.name.length > 0) {
                return value.name;
            }
            return value.toSource();
        case "array":
            var buf = value.map(function(val) {
                return jsDump.indent(lvl + 1) + jsDump(val, lvl + 1);
            });
            return ["[", buf.join(",\n"), jsDump.indent(lvl) + "]"].join("\n");
        case "object":
            var buf = [];
            for (var propName in value) {
                buf.push(jsDump.indent(lvl + 1) + '"' + propName + '": ' + jsDump(value[propName], lvl + 1));
            }
            return ["{", buf.join(",\n"), jsDump.indent(lvl) + "}"].join("\n");
        case "java":
            return '<java:' + value.class.name + '>';
    }
}
jsDump.indent = function(lvl) {
    return strings.repeat(" ", 4 * lvl);
};
jsDump.quote = function(str) {
    return '"' + str.toString().replace(/"/g, '\\"').replace(/\n/g, '\\n') + '"';
};

/**
 * Returns the type of the object passed as argument. This is
 * heavily inspired by http://philrathe.com/articles/equiv and
 * tlrobinson's narwhal test module (http://github.com/tlrobinson/narwhal/)
 * @returns The type of the object passed as argument
 * @type String
 */<commit_msg>Use JSON.stringify() to escape string in jsDump.quote()<commit_after>/**
 * @fileOverview A test runner compliant to the
 * [CommonJS Unit Testing](http://wiki.commonjs.org/wiki/Unit_Testing/1.0)
 * specification.
 */

var strings = require("ringo/utils/strings");
var term = require("ringo/term");
var fs = require("fs");

export(
    "run",
    "jsDump",
    "getStackTrace",
    "getType"
);

var {
    AssertionError,
    ArgumentsError
} = require("./assert");

/**
 * Converts the value passed as argument into a nicely formatted and
 * indented string
 * @param {Object} value The value to convert into a string
 * @param {Number} lvl Optional indentation level (defaults to zero)
 * @returns The string representation of the object passed as argument
 * @type String
 */
function jsDump(value, lvl) {
    if (!lvl) {
        lvl = 0;
    }

    switch (getType(value)) {
        case "string":
            return jsDump.quote(value);
        case "boolean":
        case "number":
        case "nan":
        case "date":
        case "regexp":
            return value.toString();
        case "undefined":
        case "null":
            return String(value);
        case "function":
            if (getType(value.name) === "string" && value.name.length > 0) {
                return value.name;
            }
            return value.toSource();
        case "array":
            var buf = value.map(function(val) {
                return jsDump.indent(lvl + 1) + jsDump(val, lvl + 1);
            });
            return ["[", buf.join(",\n"), jsDump.indent(lvl) + "]"].join("\n");
        case "object":
            var buf = [];
            for (var propName in value) {
                buf.push(jsDump.indent(lvl + 1) + '"' + propName + '": ' + jsDump(value[propName], lvl + 1));
            }
            return ["{", buf.join(",\n"), jsDump.indent(lvl) + "}"].join("\n");
        case "java":
            return '<java:' + value.class.name + '>';
    }
}
jsDump.indent = function(lvl) {
    return strings.repeat("    ", lvl);
};
jsDump.quote = function(str) {
    return JSON.stringify(str.toString());
};

/**
 * Returns the type of the object passed as argument. This is
 * heavily inspired by http://philrathe.com/articles/equiv and
 * tlrobinson's narwhal test module (http://github.com/tlrobinson/narwhal/)
 * @returns The type of the object passed as argument
 * @type String
 */
{'fraction_non_alphanumeric': 0.15626300457761133, 'fraction_numerical': 0.0035372451102788183, 'mean_word_length': 2.283469945355191, 'pattern_counts': {'":': 26, '<': 5, '<?xml version=': 0, '>': 7, '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': '6151252', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1466, 'n_words': 401}