Home | History | Annotate | Download | only in emacs
      1 ;;; gyp-tests.el - unit tests for gyp-mode.
      2 
      3 ;; Copyright (c) 2012 Google Inc. All rights reserved.
      4 ;; Use of this source code is governed by a BSD-style license that can be
      5 ;; found in the LICENSE file.
      6 
      7 ;; The recommended way to run these tests is to run them from the command-line,
      8 ;; with the run-unit-tests.sh script.
      9 
     10 (require 'cl)
     11 (require 'ert)
     12 (require 'gyp)
     13 
     14 (defconst samples (directory-files "testdata" t ".gyp$")
     15   "List of golden samples to check")
     16 
     17 (defun fontify (filename)
     18   (with-temp-buffer
     19     (insert-file-contents-literally filename)
     20     (gyp-mode)
     21     (font-lock-fontify-buffer)
     22     (buffer-string)))
     23 
     24 (defun read-golden-sample (filename)
     25   (with-temp-buffer
     26     (insert-file-contents-literally (concat filename ".fontified"))
     27     (read (current-buffer))))
     28 
     29 (defun equivalent-face (face)
     30   "For the purposes of face comparison, we're not interested in the
     31    differences between certain faces. For example, the difference between
     32    font-lock-comment-delimiter and font-lock-comment-face."
     33   (case face
     34     ((font-lock-comment-delimiter-face) font-lock-comment-face)
     35     (t face)))
     36 
     37 (defun text-face-properties (s)
     38   "Extract the text properties from s"
     39   (let ((result (list t)))
     40     (dotimes (i (length s))
     41       (setq result (cons (equivalent-face (get-text-property i 'face s))
     42                          result)))
     43     (nreverse result)))
     44 
     45 (ert-deftest test-golden-samples ()
     46   "Check that fontification produces the same results as the golden samples"
     47   (dolist (sample samples)
     48     (let ((golden (read-golden-sample sample))
     49           (fontified (fontify sample)))
     50       (should (equal golden fontified))
     51       (should (equal (text-face-properties golden)
     52                      (text-face-properties fontified))))))
     53 
     54 (defun create-golden-sample (filename)
     55   "Create a golden sample by fontifying filename and writing out the printable
     56    representation of the fontified buffer (with text properties) to the
     57    FILENAME.fontified"
     58   (with-temp-file (concat filename ".fontified")
     59     (print (fontify filename) (current-buffer))))
     60 
     61 (defun create-golden-samples ()
     62   "Recreate the golden samples"
     63   (dolist (sample samples) (create-golden-sample sample)))
     64