gyp-tests.el 2.1 KB

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