Skip to content

Commit

Permalink
Write ERT tests for ede-php-autoload-project
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenremot committed May 24, 2015
1 parent 2589109 commit e7ba96f
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 5 deletions.
1 change: 1 addition & 0 deletions .ert-runner
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-l test/test-helper.el
8 changes: 3 additions & 5 deletions TODO.org
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#+TITLE: ede-php-autoload TODO List

* Autodetect project with a =composer.json= file

* Write ERT tests

* Detect more project types
- Autodetect project with a =composer.json= file
- Write more ERT tests
- Detect more project types
86 changes: 86 additions & 0 deletions test/ede-php-autoload-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
;;; ede-php-autoload-test.el --- ERT tests for ede-php-autoload-project -*- lexical-binding: t -*-

;; Copyright (C) 2015, Steven Rémot

;; Author: Steven Rémot <[email protected]>
;; Homepage: https://github.com/stevenremot/ede-php-autoload

;; This file is not part of GNU Emacs.

;; This program 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 2, or (at
;; your option) any later version.

;; This program 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 this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:
;;

;;; Code:

;; Without composer
(ert-deftest ede-php-autoload-wt-composer-project-is-defined ()
"The EDE php autoload project should be defined."
(with-current-project-file "main.php" "without-composer"
(should (ede-php-autoload-project-p (ede-current-project)))))

(define-class-definition-test ede-php-autoload-wt-composer-find-psr0 ()
"The definition for a PSR-4 class should be found."
:class "Psr0Ns_TheClass"
:file-name "src/Psr0Ns/TheClass.php"
:project "without-composer")

(define-class-definition-test ede-php-autoload-wt-composer-find-psr4 ()
"The definition for a PSR-4 class should be found."
:class "Psr4Ns\\TheClass"
:file-name "src/Psr4Ns/TheClass.php"
:project "without-composer")

(define-class-definition-test ede-php-autoload-wt-composer-find-nothing ()
"nil should be returned when no definition have been found."
:class "Psr4Ns\\DoesNotExist"
:file-name nil
:project "without-composer")

;; With composer
(ert-deftest ede-php-autoload-composer-project-is-defined ()
"The EDE php autoload project should be defined."
(with-current-project-file "main.php" "with-composer"
(should (ede-php-autoload-project-p (ede-current-project)))))

(define-class-definition-test ede-php-autoload-composer-find-psr0 ()
"The definition for a PSR-4 class should be found."
:class "Psr0Ns_TheClass"
:file-name "src/Psr0Ns/TheClass.php"
:project "with-composer")

(define-class-definition-test ede-php-autoload-composer-find-psr4 ()
"The definition for a PSR-4 class should be found."
:class "Psr4Ns\\TheClass"
:file-name "src/Psr4Ns/TheClass.php"
:project "with-composer")

(define-class-definition-test ede-php-autoload-composer-find-nothing ()
"nil should be returned when no definition have been found."
:class "Psr4Ns\\DoesNotExist"
:file-name nil
:project "with-composer")

(define-class-definition-test ede-php-autoload-composer-find-third-party ()
"The definition for a composer dependency's class should be found."
:class "ThirdParty\\ThirdClass"
:file-name "vendor/third-party/third-party/src/ThirdClass.php"
:project "with-composer")

(provide 'ede-php-autoload-test)

;;; ede-php-autoload-test.el ends here
13 changes: 13 additions & 0 deletions test/projects/with-composer/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"autoload": {
"psr-0": {
"Psr0Ns": "src/"
},
"psr-4": {
"Psr4Ns": "src/Psr4Ns"
}
},
"require": {
"third-party/third-party": "*"
}
}
5 changes: 5 additions & 0 deletions test/projects/with-composer/src/Psr0Ns/TheClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
class Psr0Ns_TheClass
{

}
6 changes: 6 additions & 0 deletions test/projects/with-composer/src/Psr4Ns/TheClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Psr4Ns;

class TheClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"autoload": {
"psr-4": {
"ThirdParty": "src"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace ThirdParty;

class ThirdClass
{
}
Empty file.
5 changes: 5 additions & 0 deletions test/projects/without-composer/src/Psr0Ns/TheClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
class Psr0Ns_TheClass
{

}
6 changes: 6 additions & 0 deletions test/projects/without-composer/src/Psr4Ns/TheClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Psr4Ns;

class TheClass
{
}
97 changes: 97 additions & 0 deletions test/test-helper.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
;;; test-helper.el --- ERT test helpers -*- lexical-binding: t -*-

;; Copyright (C) 2015, Steven Rémot

;; Author: Steven Rémot <[email protected]>
;; Homepage: https://github.com/stevenremot/ede-php-autoload

;; This file is not part of GNU Emacs.

;; This program 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 2, or (at
;; your option) any later version.

;; This program 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 this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:
;;
(require 'cl-lib)
;;; Code:

;; Define directory variables
(defconst test-helper--test-root (file-name-directory
(or load-file-name (buffer-file-name))))

(defconst test-helper--projects-root (concat test-helper--test-root "projects/"))

(defconst test-helper--project-root (file-name-as-directory
(expand-file-name ".." test-helper--test-root)))

;; Load ede-php-autoload sources
(mapc #'(lambda (file)
(load (concat test-helper--project-root file)))
'("ede-php-autoload"
"ede-php-autoload-semanticdb"
"ede-php-autoload-mode"))

;; Define project
(ede-php-autoload-project "Without composer"
:file (concat test-helper--projects-root
"without-composer/main.php")
:class-autoloads '(:psr-0 (("Psr0Ns" . "src/Psr0Ns"))
:psr-4 (("Psr4Ns" . "src/Psr4Ns"))))

(ede-php-autoload-project "With composer"
:file (concat test-helper--projects-root
"with-composer/composer.json"))

;; Define function helpers
(defun test-helper-get-project-file-path (file project)
"Return the absolute path for FILE relative to PROJECT."
(expand-file-name file (concat test-helper--projects-root project)))

(defmacro with-current-project-file (file project &rest body)
"Open FILE located in PROJECT and execute BODY."
(declare (indent defun))
`(with-current-buffer (find-file ,(test-helper-get-project-file-path file project))
,@body))

(cl-defmacro define-class-definition-test (name () doc &key class file-name project)
"Test the matching of a class definition file.
NAME is the ert test name.
DOC is the test documentation.
CLASS is the name of the class to test.
If FILE-NAME is a string, it is the file path relative to the
project to test. It it is nil, it means CLASS must not have a
definition file.
PROJECT is the project in which the test is done."
(declare (indent defun))
`(ert-deftest ,name ()
,doc
(with-current-project-file "main.php" ,project
,(if file-name
`(should (string=
,(test-helper-get-project-file-path file-name
project)
(ede-php-autoload-find-class-def-file (ede-current-project)
,class)))
`(should-not (ede-php-autoload-find-class-def-file (ede-current-project)
,class))))))

(provide 'test-helper)

;;; test-helper.el ends here

0 comments on commit e7ba96f

Please sign in to comment.