#!/usr/bin/env sibilant -x (include "./testHelper.sibilant") (assert-translation "()" "null") (assert-translation "5" "5") (assert-translation "1,000,000.01" "1000000.01") (assert-translation "1,000.00" "1000") (assert-translation "$" "$") (assert-translation "-10.2" "-10.2") (assert-translation "hello" "hello") (assert-translation "hi-world" "hiWorld") (assert-translation "(Object.to-string)" "Object.toString();") (assert-translation "1two" "1\ntwo") (assert-translation "t1" "t1") (assert-translation "JSON" "JSON") (assert-translation "time-zone-1" "timeZone1") (assert-translation "'t1" "\"t1\"") (assert-translation "*hello*" "_hello_") (assert-translation "\"this\nstring\"" "\"this\\n\" +\n\"string\"") (assert-translation "hello?" "hello__QUERY") (assert-translation "hello!" "hello__BANG") (assert-translation "-math" "Math") (assert-translation "\"string\"" "\"string\"") (assert-translation "\"\"" "\"\"") (assert-translation "$.make-array" "$.makeArray") (assert-translation "($.make-array 1)" "$.makeArray(1);") (assert-translation "(mod 5 2)" "(5 % 2)") (assert-translation "(and a b c-d)" "(a && b && cD)") (assert-translation "(or a b c-d)" "(a || b || cD)") (assert-translation "(> a b)" "(a > b)") (assert-translation "(> a b c)" "(a > b && b > c)") (assert-translation "(> a (+ 1 b) c d)" "(a > (1 + b) && (1 + b) > c && c > d)") (each (infix) [ ">" "<" ">=" "<=" ] (assert-translation (concat "(" infix " a b c)") (concat "(a " infix " b && b " infix " c)"))) (assert (> 5 3 2 1) "> comparator works") (assert (< 1 2 3 4 5) "< comparator works") (assert (<= 1 2 2 3 3) "<= comparator works") (assert (>= 3 3 2 2 1 1) ">= comparator works") (assert-translation-error "(" "Error: unexpected EOF, probably missing a )") (assert-translation-error ")" "Error: unbalanced parens:\n[]") ;; I'm not entirely sure if this is what makes the most sense. (assert-translation "(!= a b c)" "(a !== b && b !== c)") ;; Maybe (!= a b c) should be (a !== b && a !== c) (assert-translation "/regex/" "/regex/") (assert-translation "(regex \"regex\")" "(new RegExp(\"regex\", undefined))") (assert-translation "(regex \"regex\" 'g)" "(new RegExp(\"regex\", \"g\"))") (assert-translation "(pow a b)" "Math.pow(a, b)") (assert-translation "(incr x)" "((x)++)") (assert-translation "(decr x)" "((x)--)") (assert-translation "'hello" "\"hello\"") (assert-translation "(quote hello)" "\"hello\"") (assert-translation "'(hello world)" "[ \"hello\", \"world\" ]") (assert-translation "(quote (a b c))" "[ \"a\", \"b\", \"c\" ]") ; lists (assert-translation "(list)" "[ ]") (assert-translation "(list a b)" "[ a, b ]") ; hashes (assert-translation "(hash)" "{ }") (assert-translation "(hash a b)" "{ a: b }") (assert-translation "(hash a b c d)" "{\n a: b,\n c: d\n}") ; when (assert-translation "(when a b)" "(function() { if (a) { return b; } })()") (assert-translation "(when a b c d)" "(function() { if (a) { b; c; return d; } })()") ; if (assert-translation "(if a b c)" "(function() { if (a) { return b; } else { return c; } })()") ; progn (assert-translation "(progn a b c d e)" "a;\nb;\nc;\nd;\nreturn e;") ; join (assert-translation "(join \" \" (list a b c))" "([ a, b, c ]).join(\" \")") ; meta (assert-translation "(meta (+ 5 2))" "7") ; comment (assert-translation "(comment hello)" "// hello") (assert-translation "(comment (lambda () hello))" (concat "// (function() {\n" "// return hello;\n" "// })")) ; new (assert-translation "(new (prototype a b c))" "(new prototype(a, b, c))") (assert-translation "(thunk a b c)" "(function() { a; b; return c; })") (assert-translation "(keys some-object)" "Object.keys(someObject)") (assert-translation "(delete (get foo 'bar))" "delete (foo)[\"bar\"];") (assert-translation "(delete (get foo 'bar) bam.bibble)" "delete (foo)[\"bar\"]; delete bam.bibble;") (assert-translation "(thunk (delete a.b c.d e.f))" "(function() { delete a.b; delete c.d; return delete e.f; })") (assert-translation "(defvar a b c d)" "var a = b,\n c = d;") (assert-translation "(function? x)" "typeof(x) === 'function'") (assert-translation "(number? x)" "typeof(x) === 'number'") (assert-translation "(defun foo.bar (a) (* a 2))" "foo.bar = (function(a) { return (a * 2); });") (assert-translation "(each-key key hash a b c)" "(function() { for (var key in hash) (function() { a; b; return c; })(); })();") (assert-translation "(lambda (&optional first-arg second-arg) true)" "(function(firstArg, secondArg) { if (arguments.length < 2) var secondArg = firstArg, firstArg = undefined; return true; })") (assert-translation "(scoped a b c)" "(function() { a; b; return c; })()") (assert-translation "(arguments)" "(Array.prototype.slice.apply(arguments))") (assert-translation "(set hash k1 v1 k2 v2)" "(hash)[k1] = v1; (hash)[k2] = v2;") (assert-translation "(defhash hash a b c d)" "var hash = { a: b, c: d };") (assert-translation "(each (x) arr a b c)" "arr.forEach((function(x) { a; b; return c; }))") (assert-translation "(switch a (q 1))" "(function() { switch(a) { case q: return 1; } })()") (assert-translation "(switch a ('q 2))" "(function() { switch(a) { case \"q\": return 2; } })()" ) (assert-translation "(switch a ((a b) t))" "(function() { switch(a) { case a: case b: return t; } })()") (assert-translation "(switch a ((r 's) l))" "(function() { switch(a) { case r: case \"s\": return l; } })()") (assert-translation "(switch 1 ((1 2) 'one))" "(function() { switch(1) { case 1: case 2: return \"one\"; } })()") (assert-translation "(switch (+ 5 2) ('(u v) (wibble) (foo bar)))" "(function() { switch((5 + 2)) { case \"u\": case \"v\": wibble(); return foo(bar); } })()") (assert-translation "(match? /regexp/ foo)" "foo.match(/regexp/)") (assert-translation "(before-include) (include \"./includeFile1\") (after-include-1) (include \"./includeFile2\") (after-include-2)" "beforeInclude(); 1 afterInclude1(); 2 afterInclude2();") (assert-equal 2 (switch 'a ('a 1 2))) (assert-equal 'default (switch 27 ('foo 1) (default 'default))) (assert-equal undefined (switch 10 (1 1))) (assert-equal 'hello (switch (+ 5 2) ((1 7) (concat 'he 'llo)) (7 "doesn't match because it's second") (default 10))) (assert-translation "(thunk (setf a b c d e f))" "(function() { a = b; c = d; return e = f; })") (assert-translation "(thunk (set b c d e f))" "(function() { (b)[c] = d; return (b)[e] = f; })") (assert-translation "(defmacro foo? () 1) (foo?) (delmacro foo?) (foo?)" "1\nfoo__QUERY();") (assert-translation "(while (< i 10) (console.log 'here) (alert 'there) 'everywhere)" "(function() { var __returnValue__ = undefined; while ((i < 10)) { __returnValue__ = (function() { console.log(\"here\"); alert(\"there\"); return \"everywhere\"; })(); }; return __returnValue__; })()") (scoped (defvar i 0) (defvar return-string (while (< i 10) (setf i (+ i 1)) (concat "stopped at iteration: " i))) (assert-equal "stopped at iteration: 10" return-string)) (assert-translation "(return (progn (return a)))" "return a;") (assert-translation "(return (progn (switch a (b c))))" "return (function() { switch(a) { case b: return c; } })();") (assert-translation "(= a b c)" " (a === b && a === c)") (assert-translation "(progn)" "return undefined;") (assert-translation "{foo : bar wibble : wam }" "{ foo: bar, wibble: wam }") (assert-translation "[ foo bar (baz) ]" "[ foo, bar, baz() ]") (assert-translation "[[] {} baz {q r s [t]}]" "[ [ ], { }, baz, { q: r, s: [ t ] } ]") (assert-translation "{ this: is, valid: [\"json\"]}", "{ this: is, valid: [ \"json\" ] }") (assert-translation "(cons a [ b c d ])" "[ a ].concat([ b, c, d ])") (assert-deep-equal '(a b c d) (cons 'a '(b c d))) (assert-translation "(defmacro -> () 1) (->)" "1") (assert-translation "(alias-macro lambda ->) (-> (a) a) (delmacro ->) (-> (a) a)" "(function(a) { return a; }) ->(a(), a);") (assert-translation "(rename-macro thunk =>) (=> 'hello) (thunk 'world) (rename-macro => thunk) (thunk 'foo) (=> 'bar)" "(function() { return \"hello\"; }) thunk(\"world\"); (function() { return \"foo\"; }) =>(\"bar\");") (assert-translation "(if-else a b c d e (progn (g) h))" "(function() { if (a) { return b; } else if (c) { return d; } else if (e) { g(); return h; } })()") (assert-translation "(if-else a b c d e)" "(function() { if (a) { return b; } else if (c) { return d; } else { return e; } })()") (assert-translation "(list? thing)" "(thing) && (thing).constructor.name === \"Array\"") (assert-translation "(thunk (setf x 1))" "(function() { return x = 1; })") ; this test is for duplicated semicolons (assert-translation "(when a (progn (setf b c)))" "(function() { if (a) { return b = c; } })()") (assert-translation "(defvar a (setf b c))" "var a = b = c;") (assert-translation "(chainable $) ($ 'div (data 'jquery true) (css {background 'blue color 'light-blue}))" "$(\"div\") .data(\"jquery\", true) .css({ background: \"blue\", color: \"lightBlue\" })") (assert-translation "(chainable a b) (a target-a (fn-a 1) (fn-a 2)) (b target-b (fn-b 1) (fn-b 2))" "a(targetA) .fnA(1) .fnA(2) b(targetB) .fnB(1) .fnB(2)")