aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Isaac2024-10-08 01:16:48 +0100
committerArun Isaac2024-10-08 01:16:48 +0100
commit4359685a37dcbe76abe65a8d8b703ed4622a7a13 (patch)
treea509757440d7c421ef516992cc439ecfe0cd9540
parent4fe988e750e88ede4e0f1a3c9fa41afc1a54a80b (diff)
downloadravanan-4359685a37dcbe76abe65a8d8b703ed4622a7a13.tar.gz
ravanan-4359685a37dcbe76abe65a8d8b703ed4622a7a13.tar.lz
ravanan-4359685a37dcbe76abe65a8d8b703ed4622a7a13.zip
tests: Add tests for javascript G-expressions.
* tests/javascript.scm: Import (guix gexp) and (ice-9 match). (gexp->sexp-rec): New function. ("evaluate parameter reference (without context)", "evaluate parameter reference with string interpolation (without context)", "evaluate parameter reference with string interpolation of JSON trees (without context)", "evaluate parameter reference with node (without context)", "evaluate parameter reference with string interpolation using node (without context)", "evaluate parameter reference with string interpolation of JSON trees using node (without context)"): New tests.
-rw-r--r--tests/javascript.scm76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/javascript.scm b/tests/javascript.scm
index b6c4753..c766ac3 100644
--- a/tests/javascript.scm
+++ b/tests/javascript.scm
@@ -17,9 +17,22 @@
;;; along with ravanan. If not, see <https://www.gnu.org/licenses/>.
(use-modules (srfi srfi-64)
+ (guix gexp)
+ (ice-9 match)
(ravanan work monads)
(ravanan javascript))
+(define (gexp->sexp-rec exp)
+ "Recursively convert G-expression @var{exp} into an approximate S-expression."
+ (match exp
+ ((? gexp? exp)
+ (gexp->sexp-rec (gexp->approximate-sexp exp)))
+ ((head tail ...)
+ (cons (gexp->sexp-rec head)
+ (map gexp->sexp-rec tail)))
+ (atom
+ atom)))
+
(test-begin "javascript")
(test-equal "evaluate parameter reference"
@@ -66,4 +79,67 @@
("bar" . 2))
("vector" . #(0 1 2 3))))))
+(test-equal "evaluate parameter reference (without context)"
+ '(string-join
+ (map (lambda (token)
+ (if (string? token) token (scm->json-string (canonicalize-json token))))
+ (list (json-ref inputs "message" "bar" "foo" 2)))
+ "")
+ (gexp->sexp-rec
+ (evaluate-parameter-reference "$(inputs.message['bar'][\"foo\"][2])")))
+
+(test-equal "evaluate parameter reference with string interpolation (without context)"
+ '(string-join
+ (map (lambda (token)
+ (if (string? token) token (scm->json-string (canonicalize-json token))))
+ (list (json-ref runtime "cores")
+ "foo"
+ (json-ref inputs "threads")
+ (json-ref inputs "output_filename")))
+ "")
+ (gexp->sexp-rec
+ (evaluate-parameter-reference "$(runtime.cores)foo$(inputs.threads)$(inputs.output_filename)")))
+
+(test-equal "evaluate parameter reference with string interpolation of JSON trees (without context)"
+ '(string-join
+ (map (lambda (token)
+ (if (string? token) token (scm->json-string (canonicalize-json token))))
+ (list "foo" (json-ref inputs "vector") (json-ref inputs "object")))
+ "")
+ (gexp->sexp-rec
+ (evaluate-parameter-reference "foo$(inputs.vector)$(inputs.object)")))
+
+(test-equal "evaluate parameter reference with node (without context)"
+ '(string-join
+ (map (lambda (token)
+ (if (string? token) token (scm->json-string (canonicalize-json token))))
+ (list (evaluate-javascript (*approximate*) "(inputs.n + 1)" "")))
+ "")
+ (gexp->sexp-rec
+ (evaluate-parameter-reference "$(inputs.n + 1)")))
+
+(test-equal "evaluate parameter reference with string interpolation using node (without context)"
+ '(string-join
+ (map (lambda (token)
+ (if (string? token) token (scm->json-string (canonicalize-json token))))
+ (list (json-ref runtime "cores")
+ "foo"
+ (evaluate-javascript (*approximate*) "(inputs.threads*2)" "")
+ (json-ref inputs "output_filename")))
+ "")
+ (gexp->sexp-rec
+ (evaluate-parameter-reference "$(runtime.cores)foo$(inputs.threads*2)$(inputs.output_filename)")))
+
+(test-equal "evaluate parameter reference with string interpolation of JSON trees using node (without context)"
+ '(string-join
+ (map (lambda (token)
+ (if (string? token) token (scm->json-string (canonicalize-json token))))
+ (list "foo"
+ (json-ref inputs "vector")
+ (json-ref inputs "object")
+ (evaluate-javascript (*approximate*) "(inputs.object.foo*20)" "")))
+ "")
+ (gexp->sexp-rec
+ (evaluate-parameter-reference "foo$(inputs.vector)$(inputs.object)$(inputs.object.foo*20)")))
+
(test-end "javascript")