From 4359685a37dcbe76abe65a8d8b703ed4622a7a13 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Tue, 8 Oct 2024 01:16:48 +0100 Subject: 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. --- tests/javascript.scm | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) 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 . (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") -- cgit v1.2.3