gash/tests/unit/eval.scm

301 lines
7.3 KiB
Scheme
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; Gash -- Guile As SHell
;;; Copyright © 2018, 2019, 2021 Timothy Sample <samplet@ngyro.com>
;;;
;;; This file is part of Gash.
;;;
;;; Gash is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Gash 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 Gash. If not, see <http://www.gnu.org/licenses/>.
(define-module (test-eval)
#:use-module (gash environment)
#:use-module (gash eval)
#:use-module (srfi srfi-64)
#:use-module (tests unit automake))
;;; Commentary:
;;;
;;; Tests for the eval module.
;;;
;;; Code:
(test-begin "eval")
;;; Basic parameter references.
;;;
;;; FIXME: Test "nounset" ("set -u").
(test-equal "Resolves parameters"
'("foo")
(with-variables '(("x" . "foo"))
(lambda ()
(eval-word '(<sh-ref> "x")))))
(test-equal "Splits parameter results"
'("foo" "bar")
(with-variables '(("x" . "foo bar"))
(lambda ()
(eval-word '(<sh-ref> "x")))))
(test-equal "Resolves quoted parameters"
'("foo")
(with-variables '(("x" . "foo"))
(lambda ()
(eval-word '(<sh-quote> (<sh-ref> "x"))))))
(test-equal "Ignores spaces in quoted parameters"
'("foo bar")
(with-variables '(("x" . "foo bar"))
(lambda ()
(eval-word '(<sh-quote> (<sh-ref> "x"))))))
(test-equal "Treats empty variables as nothing"
'()
(with-variables '(("x" . ""))
(lambda ()
(eval-word '(<sh-ref> "x")))))
(test-equal "Treats unset variables as nothing"
'()
(with-variables '()
(lambda ()
(eval-word '(<sh-ref> "x")))))
(test-equal "Preserves empty variables when quoted"
'("")
(with-variables '(("x" . ""))
(lambda ()
(eval-word '(<sh-quote> (<sh-ref> "x"))))))
(test-equal "Preserves unset variables when quoted"
'("")
(with-variables '()
(lambda ()
(eval-word '(<sh-quote> (<sh-ref> "x"))))))
;;; Parameter operations.
;;; or
(test-equal "Handles 'or' when parameter is set"
'("foo")
(with-variables '(("x" . "foo"))
(lambda ()
(eval-word '(<sh-ref-or> "x" "bar")))))
(test-equal "Handles 'or' when parameter is set and empty"
'()
(with-variables '(("x" . ""))
(lambda ()
(eval-word '(<sh-ref-or> "x" "bar")))))
(test-equal "Handles 'or' when parameter is unset"
'("bar")
(with-variables '()
(lambda ()
(eval-word '(<sh-ref-or> "x" "bar")))))
(test-equal "Handles 'or' fall-through without default"
'()
(with-variables '()
(lambda ()
(eval-word '(<sh-ref-or> "x" #f)))))
;;; or*
(test-equal "Handles 'or*' when parameter is set"
'("foo")
(with-variables '(("x" . "foo"))
(lambda ()
(eval-word '(<sh-ref-or*> "x" "bar")))))
(test-equal "Handles 'or*' when parameter is set and empty"
'("bar")
(with-variables '(("x" . ""))
(lambda ()
(eval-word '(<sh-ref-or*> "x" "bar")))))
(test-equal "Handles 'or*' when parameter is unset"
'("bar")
(with-variables '()
(lambda ()
(eval-word '(<sh-ref-or*> "x" "bar")))))
(test-equal "Handles 'or*' fall-through without default"
'()
(with-variables '()
(lambda ()
(eval-word '(<sh-ref-or*> "x" #f)))))
;;; or!
(test-equal "Handles 'or!' when parameter is set"
'(("foo") "foo")
(with-variables '(("x" . "foo"))
(lambda ()
(list (eval-word '(<sh-ref-or!> "x" "bar"))
(getvar "x")))))
(test-equal "Handles 'or!' when parameter is set and empty"
'(() "")
(with-variables '(("x" . ""))
(lambda ()
(list (eval-word '(<sh-ref-or!> "x" "bar"))
(getvar "x")))))
(test-equal "Handles 'or!' when parameter is unset"
'(("bar") "bar")
(with-variables '()
(lambda ()
(list (eval-word '(<sh-ref-or!> "x" "bar"))
(getvar "x")))))
(test-equal "Handles 'or!' fall-through without default"
'(() "")
(with-variables '()
(lambda ()
(list (eval-word '(<sh-ref-or!> "x" #f))
(getvar "x")))))
;;; or!*
(test-equal "Handles 'or!*' when parameter is set"
'(("foo") "foo")
(with-variables '(("x" . "foo"))
(lambda ()
(list (eval-word '(<sh-ref-or!*> "x" "bar"))
(getvar "x")))))
(test-equal "Handles 'or!*' when parameter is set and empty"
'(("bar") "bar")
(with-variables '(("x" . ""))
(lambda ()
(list (eval-word '(<sh-ref-or!*> "x" "bar"))
(getvar "x")))))
(test-equal "Handles 'or!*' when parameter is unset"
'(("bar") "bar")
(with-variables '()
(lambda ()
(list (eval-word '(<sh-ref-or!*> "x" "bar"))
(getvar "x")))))
(test-equal "Handles 'or!*' fall-through without default"
'(() "")
(with-variables '()
(lambda ()
(list (eval-word '(<sh-ref-or!*> "x" #f))
(getvar "x")))))
(test-equal "Does not split fields on assignment"
'(("foo" "bar") "foo bar")
(with-variables '(("y" . "foo bar"))
(lambda ()
(list (eval-word '(<sh-ref-or!*> "x" (<sh-ref> "y")))
(getvar "x")))))
;;; FIXME: Test 'assert'.
;;; and
(test-equal "Handles 'and' when parameter is set"
'("bar")
(with-variables '(("x" . "foo"))
(lambda ()
(eval-word '(<sh-ref-and> "x" "bar")))))
(test-equal "Handles 'and' when parameter is set and empty"
'("bar")
(with-variables '(("x" . ""))
(lambda ()
(eval-word '(<sh-ref-and> "x" "bar")))))
(test-equal "Handles 'and' when parameter is unset"
'()
(with-variables '()
(lambda ()
(eval-word '(<sh-ref-and> "x" "bar")))))
(test-equal "Handles 'and' fall-through without default"
'()
(with-variables '(("x" . "foo"))
(lambda ()
(eval-word '(<sh-ref-and> "x" #f)))))
;;; and*
(test-equal "Handles 'and*' when parameter is set"
'("bar")
(with-variables '(("x" . "foo"))
(lambda ()
(eval-word '(<sh-ref-and*> "x" "bar")))))
(test-equal "Handles 'and*' when parameter is set and empty"
'()
(with-variables '(("x" . ""))
(lambda ()
(eval-word '(<sh-ref-and*> "x" "bar")))))
(test-equal "Handles 'and*' when parameter is unset"
'()
(with-variables '()
(lambda ()
(eval-word '(<sh-ref-and*> "x" "bar")))))
(test-equal "Handles 'and*' fall-through without default"
'()
(with-variables '(("x" . "foo"))
(lambda ()
(eval-word '(<sh-ref-and*> "x" #f)))))
;;; length
(test-equal "Handles 'length' when parameter is set"
'("3")
(with-variables '(("x" . "foo"))
(lambda ()
(eval-word '(<sh-ref-length> "x")))))
(test-equal "Handles 'length' when parameter is unset"
'("0")
(with-variables '()
(lambda ()
(eval-word '(<sh-ref-length> "x")))))
;;; Command substition.
(test-equal "Resolves commands"
'("foo")
(eval-word '(<sh-cmd-sub> (<sh-exec> "echo" "foo"))))
(test-equal "Splits command results"
'("foo" "bar")
(eval-word '(<sh-cmd-sub> (<sh-exec> "echo" "foo bar"))))
(test-equal "Resolves quoted commands"
'("foo")
(eval-word '(<sh-quote> (<sh-cmd-sub> (<sh-exec> "echo" "foo")))))
(test-equal "Ignores spaces in quoted commands"
'("foo bar")
(eval-word '(<sh-quote> (<sh-cmd-sub> (<sh-exec> "echo" "foo bar")))))
;;; Arithmetic expansion.
;;;
;;; Not yet implemented.
(test-end "eval")