#!/usr/bin/env -S emacs --script

;; This script must be run from the test directory
;; With no argument, run all tests. Otherwise run only mentioned tests.
;; Possible arguments: --rstats --rstats-indent

(let ((current-directory (file-name-directory load-file-name)))
  (setq ess-test-path (expand-file-name "." current-directory))
  (setq ess-root-path (expand-file-name "../lisp/" current-directory)))

(add-to-list 'load-path ess-root-path)
(add-to-list 'load-path ess-test-path)

(require 'ess-site)
(require 'ess-r-tests-utils)
(setq ess-inhibit-message-in-tests t)
(setq ert-batch-backtrace-right-margin 130)

;; lintr probably isn't installed in the test suite and flymake will
;; complain about that. Disable it during tests.
(setq ess-use-flymake nil)

(when (= (length argv) 0)
  (setq argv '("--ess" "--inf" "--rstats" "--rstats-indent" "--literate")))

(when (member "--ess" argv)
  (load (expand-file-name "ess-tests.el" ess-test-path) nil t))
(when (member "--inf" argv)
  (load (expand-file-name "ess-inf-tests.el" ess-test-path) nil t)
  (load (expand-file-name "ess-org-tests.el" ess-test-path) nil t))
(when (member "--rstats" argv)
  (load (expand-file-name "ess-r-tests.el" ess-test-path) nil t))
(when (member "--rstats-indent" argv)
  (load (expand-file-name "ess-indentation-tests.el" ess-test-path) nil t))
(when (member "--literate" argv)
  (load (expand-file-name "ess-literate-tests.el" ess-test-path) nil t)
  (elt-deftest test-elt () "elt.R")
  (elt-deftest test-ess-roxy-literate () "roxy.R")
  (elt-deftest test-ess-r-code-fill () "code-fill.R")
  (elt-deftest test-ess-r-misc () "misc.R")
  (elt-deftest test-ess-r-syntax () "syntax.R")
  (elt-deftest test-ess-r-tokens () "tokens.R")
  (elt-deftest test-ess-r-fontification () "fontification.R")
  (elt-deftest test-ess-r-fontification () "keybindings.R"))

;; run tests
(ert-run-tests-batch-and-exit t)
