Skip to content

Commit 98f10cf

Browse files
author
Eric James Michael Ritz
committed
Begin to implement unit tests using ERT
I said that I would do this a looooong time ago, but am only now getting around to it (as a result of feeling envigorated to write code lately). This patch implements some basic unit tests for our `php-runtime` library, and in particular the `php-runtime-expr` part of the API. The unit test code provides a `run-simple-php` function to help with testing, allowing us to hand some short code to the PHP executable for execution. However, the `php-runtime` library provides this exact functionality---obviously it must. Therefore in the future we should remove our utility from the unit test code and instead rely on the relevant code from the `php-runtime` library itself. The only reason I did not use it in this first draft is because I wanted to re-examine the code to see if I can simplify it in any meaningful and useful way. Finally, this file of unit tests is incomplete and we will be adding more tests in the future, particularly for such things as reading standard-input into PHP code we wish to run. GitHub-Issue: #2 Signed-off-by: Eric James Michael Ritz <[email protected]>
1 parent a205c8d commit 98f10cf

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

php-runtime-test.el

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
;;; php-runtime-test.el --- Unit tests for php-runtime package.
22

33
;; Copyright (C) 2017 USAMI Kenta
4+
;; Copyright (C) 2018 Eric James Michael Ritz
45

5-
;; Author: USAMI Kenta <[email protected]>
6+
;; Authors: USAMI Kenta <[email protected]>
67
;; Created: 28 Aug 2017
78
;; Version: 0.0.1
89
;; Keywords: processes php
@@ -37,39 +38,47 @@
3738
(require 'php-runtime)
3839
(require 'ert)
3940

40-
(ert-deftest php-runtime-test-eval-with-stdin ()
41-
(dolist (v (list '("Input buffer as string"
42-
:expected "1 apple\n2 orange\n3 banana\n"
43-
:code "$i = 0;
44-
while (($line = fgets(STDIN)) !== false) {
45-
echo ++$i, ' ', trim($line), \"\n\";
46-
}"
47-
:input-buf "apple\norange\nbanana")))
48-
(let ((description (car v))
49-
(expected (plist-get (cdr v) :expected))
50-
(code (plist-get (cdr v) :code))
51-
(input-buf (plist-get (cdr v) :input-buf)))
52-
(should (string= expected (php-runtime-eval code input-buf))))))
53-
54-
(ert-deftest php-runtime-test-eval-without-stdin ()
55-
(dolist (v (list '("No input buffer"
56-
:expected ""
57-
:code "$i = 0;
58-
while (($line = fgets(STDIN)) !== false) {
59-
echo ++$i, ' ', trim($line), \"\n\";
60-
}")))
61-
(let ((description (car v))
62-
(expected (plist-get (cdr v) :expected))
63-
(code (plist-get (cdr v) :code)))
64-
(should (string= expected (php-runtime-eval code))))))
65-
66-
(ert-deftest php-runtime-test-expr ()
67-
"Test that PHP expressions are evaluated and get results."
41+
(defun run-simple-php (code)
42+
"Utility function to run CODE for simple PHP testing.
43+
44+
This function accepts PHP code as a string and runs it using
45+
whichever PHP executable is available on the system. In order to
46+
reduce sources of potential problems, we do not load any PHP INI
47+
or other configurations.
48+
49+
The function returns the result of the code as a string."
50+
;; We call PHP with two options:
51+
;;
52+
;; 1. `-n` - Causes PHP to ignore any system-wide INI, because we
53+
;; don't want any such configuration screwing up our tests.
54+
;;
55+
;; 2. `-r` - Tells PHP to run the code which follows, which we must
56+
;; quote appropriately as a shell argument.
57+
(shell-command-to-string (concat "php -n -r" (shell-quote-argument code))))
58+
59+
(ert-deftest runtime-expressions ()
60+
"Test using `php-runtime-expr'.
61+
62+
We can make explicit invocations of the PHP executable to obtain
63+
the values of various runtime expressions. These tests compare
64+
those to the values we obtain via `php-runtime-expr', which
65+
should always be the same."
66+
;; Testing simple constants.
67+
(should (string= (php-runtime-expr "PHP_VERSION")
68+
(run-simple-php "echo PHP_VERSION;")))
69+
;; Testing simple function calls.
70+
(should (string= (php-runtime-expr "strtoupper('foobar')")
71+
(run-simple-php "echo strtoupper('foobar');")))
72+
;; Testing basic math.
6873
(should (string= "200" (php-runtime-expr "(1+1)*100")))
69-
(should (string= "foo" (php-runtime-expr "'f' . 'oo'"))))
74+
;; Testing string concatenation.
75+
(should (string= "foo" (php-runtime-expr "'f' . 'oo'")))
76+
;; Converting output to numbers.
77+
(should (= (string-to-number (php-runtime-expr "PHP_INT_MAX"))
78+
(string-to-number (run-simple-php "echo PHP_INT_MAX;")))))
7079

71-
(ert-deftest php-runtime-test-expr-syntax-error ()
72-
"Test that PHP code syntax errors are reported as errors in Emacs."
80+
(ert-deftest php-syntax-errors ()
81+
"Test that PHP code with errors cause appropriate failures."
7382
(should-error (php-runtime-expr "a:b")))
7483

7584
(provide 'php-runtime-test)

0 commit comments

Comments
 (0)