<commit_before>var _ = require("lodash");
var test = require("tape");
var compile = require("./");

test("basics", function(t){
  t.equals(compile("(add 1 2)"), "add(1, 2);");
  t.equals(compile('( log "hello world" )'), "log('hello world');");
  t.equals(compile("(js/+ 1 2)"), "1 + 2;");
  t.equals(compile("(js/=== a b)"), "a === b;");
  t.equals(compile("(js/typeof a)"), "typeof a;");
  t.equals(compile("(js/&& a b)"), "a && b;");
  t.equals(compile("(js/! a)"), "!a;");
  t.equals(compile(
    "(log js/null js/false js/true js/undefined js/this js/arguments)"),
    "log(null, false, true, undefined, this, arguments);"
  );
  t.equals(compile("(js/var a 1 b 2)"), "var a = 1, b = 2;");
  t.equals(compile("(js/property-access a b)"), "a[b];");
  t.equals(compile("(js/property-access a :b)"), "a['b'];");
  t.equals(compile("(js/= a :b)"), "a = 'b';");
  t.equals(compile(
    "(js/function add4 (a) (js/block-statement\n"
    + "(js/var b 4)\n"
    + "(log :hello-world)\n"
    + "(js/return (js/+ a b))))"
    ),
    "(function add4(a) {\n"
    + "    var b = 4;\n"
    + "    log('hello-world');\n"
    + "    return a + b;\n"
    + "});"
  );
  t.equals(compile(
    "(js/function js/null (a b) (js/block-statement\n"
    + "(js/return (js/+ a b))))"
    ),
    "(function (a, b) {\n"
    + "    return a + b;\n"
    + "});"
  );
  t.equals(compile(
    "(js/while (js/lt= i 3) (js/block-statement\n"
    + "(log :loop-again)\n"
    + "(js/= i (js/+ i 1))))"
    ),
    "while (i <= 3) {\n"
    + "    log('loop-again');\n"
    + "    i = i + 1;\n"
    + "}"
  );
  t.equals(compile("(js/ternary (js/=== a 1) :one a)"), "a === 1 ? 'one' : a;");
  t.equals(compile(
    "(js/if (js/=== a 1)\n"
    + "(js/block-statement (log :true))"
    + "(js/block-statement (log :false)))"),

    "if (a === 1) {\n"
    + "    log('true');\n"
    + "} else {\n"
    + "    log('false');\n"
    + "}"
  );
  t.equals(compile(
    "(js/if (js/=== a 1)\n"
    + "(log :true)"
    + "(log :false))"),

    "if (a === 1)\n"
    + "    log('true');\n"
    + "else\n"
    + "    log('false');"
  );
  t.equals(compile(
    "(js/try-catch\n"
    + "(run)\n"
    + "err\n"
    + "(log \"error\" err))"),

    "try\n"
    + "    run();\n"
    + "catch (err)\n"
    + "    log('error', err);"
  );
  t.equals(compile("((js/property-access console :log) 1 2)"),
    "console['log'](1, 2);"
  );
  t.equals(compile("(js/array)"), "[];");
  t.equals(compile("(js/array a 1 :2)"), "[\n    a,\n    1,\n    '2'\n];");

  t.equals(compile("(js/object)"), "({});");
  t.equals(compile("(js/object :a)"), "({ 'a': undefined });");
  t.equals(compile("(js/object :a 1)"), "({ 'a': 1 });");
  t.equals(compile("(js/object :a 1 :2 3)"), "({\n    'a': 1,\n    '2': 3\n});");

  t.equals(compile("(js/throw (js/new Error :msg))"), "throw new Error('msg');");

  t.end();
});<commit_msg>rm whitespace from test js output<commit_after>var _ = require("lodash");
var test = require("tape");
var compile = require("./");

var testCompile = function(t, src, expected){
  t.equals(compile(src, {
    escodegen: {format: {compact: true}}
  }), expected);
};

test("basics", function(t){
  var tc = _.partial(testCompile, t);

  tc("(add 1 2)", "add(1,2);");
  tc('( log "hello world" )', "log('hello world');");
  tc("(js/+ 1 2)", "1+2;");
  tc("(js/=== a b)", "a===b;");
  tc("(js/typeof a)", "typeof a;");
  tc("(js/&& a b)", "a&&b;");
  tc("(js/! a)", "!a;");
  tc(
    "(log js/null js/false js/true js/undefined js/this js/arguments)",
    "log(null,false,true,undefined,this,arguments);"
  );
  tc("(js/var a 1 b 2)", "var a=1,b=2;");
  tc("(js/property-access a b)", "a[b];");
  tc("(js/property-access a :b)", "a['b'];");
  tc("(js/= a :b)", "a='b';");
  tc(
    "(js/function add4 (a) (js/block-statement\n"
    + "(js/var b 4)\n"
    + "(log :hello-world)\n"
    + "(js/return (js/+ a b))))"
    ,
    "(function add4(a){var b=4;log('hello-world');return a+b;});"
  );
  tc(
    "(js/function js/null (a b) (js/block-statement\n"
    + "(js/return (js/+ a b))))"
    ,
    "(function(a,b){return a+b;});"
  );
  tc(
    "(js/while (js/lt= i 3) (js/block-statement\n"
    + "(log :loop-again)\n"
    + "(js/= i (js/+ i 1))))"
    ,
    "while(i<=3){log('loop-again');i=i+1;}"
  );
  tc("(js/ternary (js/=== a 1) :one a)", "a===1?'one':a;");
  tc(
    "(js/if (js/=== a 1)\n"
    + "(js/block-statement (log :true))"
    + "(js/block-statement (log :false)))",

    "if(a===1){log('true');}else{log('false');}"
  );
  tc(
    "(js/if (js/=== a 1)\n"
    + "(log :true)"
    + "(log :false))",

    "if(a===1)log('true');else log('false');"
  );
  tc(
    "(js/try-catch\n"
    + "(run)\n"
    + "err\n"
    + "(log \"error\" err))",

    "tryrun();catch(err)log('error',err);"
  );
  tc("((js/property-access console :log) 1 2)",
    "console['log'](1,2);"
  );
  tc("(js/array)", "[];");
  tc("(js/array a 1 :2)", "[a,1,'2'];");

  tc("(js/object)", "({});");
  tc("(js/object :a)", "({'a':undefined});");
  tc("(js/object :a 1)", "({'a':1});");
  tc("(js/object :a 1 :2 3)", "({'a':1,'2':3});");

  tc("(js/throw (js/new Error :msg))", "throw new Error('msg');");

  t.end();
});
{'fraction_non_alphanumeric': 0.3139422144657747, 'fraction_numerical': 0.016288539848749273, 'mean_word_length': 2.892830188679245, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4388086', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2210, 'n_words': 446}