Skip to content

Instantly share code, notes, and snippets.

@kisp
Last active April 21, 2024 10:49
Show Gist options
  • Save kisp/8bcfe11900d40f116bfadcb4cab5a164 to your computer and use it in GitHub Desktop.
Save kisp/8bcfe11900d40f116bfadcb4cab5a164 to your computer and use it in GitHub Desktop.
Using MithrilJS/ospec with parenscript
;; MithrilJS/ospec: Noiseless testing framework
;; https://github.com/MithrilJS/ospec
;; {
;; "devDependencies": {
;; "ospec": "^4.2.0"
;; }
;; }
;; Parenscript
;; https://cliki.net/parenscript
;; https://parenscript.common-lisp.dev/
(defvar o (require "ospec"))
(defmacro deftest (name &body body)
`(o ,(string name) (lambda () ,@body)))
(defmacro deftest* (name &body body)
`(o ,(string name) (lambda (done) ,@body)))
(defmacro deftest-only (name &body body)
`(chain o (only ,(string name) (lambda () ,@body))))
(defmacro deftest*-only (name &body body)
`(chain o (only ,(string name) (lambda (done) ,@body))))
(defmacro defspec (name &body body)
`(chain o (spec ,(string name) (lambda () ,@body))))
(defmacro is (form)
(ecase (car form)
(= (destructuring-bind (expected actual) (cdr form)
`(chain (o ,actual) (deep-equals ,expected))))))
(defun spec-timeout (ms)
(chain o (spec-timeout ms)))
(defmacro run! ()
`(chain o (run)))
(defmacro with-timeout (ms &body body)
`(set-timeout (lambda () ,@body) ,ms))
#+nil
(macrolet ((log (&rest args) `(chain console (log ,@args))))
(log (array 1 2 3))
(log 10 20 30))
(defspec math
(deftest foo.1
(is (= 100 (+ 50 50))))
(deftest foo.2
(is (= (array 1 2 3) (array 1 2 3)))))
(defspec async
(deftest* test.1
(let ((a 100))
(with-timeout 50
(setq a 200))
(with-timeout 100
(is (= 200 a))
(done))))
(defspec longer-timeout
(spec-timeout 500)
(deftest* test.2
(let ((a 100))
(with-timeout 350
(setq a 200))
(with-timeout 400
(is (= 200 a))
(done))))))
(run!)
// parenscript compilation output
"use strict";
var __PS_MV_REG;
if ("undefined" === typeof o) {
var o = require("ospec");
}
function specTimeout(ms) {
return o.specTimeout(ms);
}
o.spec("MATH", function () {
o("FOO.1", function () {
__PS_MV_REG = [];
return o(50 + 50).deepEquals(100);
});
__PS_MV_REG = [];
return o("FOO.2", function () {
__PS_MV_REG = [];
return o([1, 2, 3]).deepEquals([1, 2, 3]);
});
});
o.spec("ASYNC", function () {
o("TEST.1", function (done) {
var a = 100;
setTimeout(function () {
return (a = 200);
}, 50);
__PS_MV_REG = [];
return setTimeout(function () {
o(a).deepEquals(200);
__PS_MV_REG = [];
return done();
}, 100);
});
__PS_MV_REG = [];
return o.spec("LONGER-TIMEOUT", function () {
specTimeout(500);
__PS_MV_REG = [];
return o("TEST.2", function (done) {
var a = 100;
setTimeout(function () {
return (a = 200);
}, 350);
__PS_MV_REG = [];
return setTimeout(function () {
o(a).deepEquals(200);
__PS_MV_REG = [];
return done();
}, 400);
});
});
});
o.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment