summaryrefslogtreecommitdiff
path: root/tests/generate-tests.lisp
blob: da1301bd2a8fba129f847bc025ccb32b74556a32 (plain)
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
;; This file is part of eris-cl.
;; Copyright (C) 2022 Piotr Szarmański

;; eris-cl is free software: you can redistribute it and/or modify it under the
;; terms of the GNU Lesser General Public License as published by the Free
;; Software Foundation, either version 3 of the License, or (at your option) any
;; later versqion.

;; eris-cl is distributed in the hope that it will be useful, but WITHOUT ANY
;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
;; A PARTICULAR PURPOSE. See the GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License along with
;; eris-cl. If not, see <https://www.gnu.org/licenses/>.

;; This generates the autogenerated-tests.lisp file. It requires you to tar -xf
;; the test vectors tarball right in the tests directory.
;; Also requires the jonathan package in addition to the regular dependencies.

(asdf:load-system :jonathan)
(defpackage :eris-json-files
  (:use common-lisp jonathan))

(in-package :eris-json-files)
(defparameter *json-files* (loop for i from 0 to 12 collecting (format nil "test-vectors/eris-test-vector-positive-~2,'0d.json" i)))
(defparameter *json-files-negative* (loop for i from 13 to 24 collecting (format nil "test-vectors/eris-test-vector-negative-~2,'0d.json" i)))


(defmacro with-json (json &body expr)
  (alexandria:with-gensyms (json-sym)
    `(let ((,json-sym (jonathan:parse ,json)))
       (macrolet ((json (symbol) (list (quote getf) (quote ,json-sym) symbol)))
         ,@expr))))

(with-open-file (stream "autogenerated-tests.lisp" :direction :output :if-does-not-exist :create :if-exists :supersede)
  (format stream ";;;; Autogenerated file.~%")
  (print '(in-package :eris/test) stream)
  (print '(def-suite* machine-generated-tests :in eris-tests) stream)
  (map 'nil (lambda (file)
              (with-json (with-open-file (json-file file)
                           (alexandria:read-stream-content-into-string json-file))
                (print `(test ,(intern (format nil "test-~d" (json :|id|)) *package*)
                          (positive-test ,(json :|urn|) ,(json :|content|) (quote ,(json :|blocks|)) ,(json :|convergence-secret|) ,(json :|block-size|)))
                       stream)))
       *json-files*)

  (map 'nil (lambda (file)
              (with-json (with-open-file (json-file file)
                           (alexandria:read-stream-content-into-string json-file))
                (print `(test ,(intern (format nil "test-~d" (json :|id|)) *package*)
                          (negative-test ,(json :|urn|) (quote ,(json :|blocks|))))
                       stream)))
       *json-files-negative*))