test.lisp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. (in-package #:cl-user)
  2. (defpackage #:cl-i18n-tests
  3. (:use
  4. #:alexandria
  5. #:common-lisp
  6. #:cl-i18n
  7. #:clunit)
  8. (:export #:run))
  9. (in-package #:cl-i18n-tests)
  10. (defsuite cl-i18n-suite ())
  11. (defun package-path ()
  12. (uiop:pathname-parent-directory-pathname
  13. (asdf:component-pathname
  14. (asdf:find-component (symbolicate (string-upcase "cl-i18n"))
  15. nil))))
  16. (defun file-in-package (name)
  17. (concatenate 'string (namestring (package-path)) name))
  18. (setf *translation-file-root* (file-in-package "cl-i18n/examples/locale/"))
  19. (defmacro with-italian-native-translation-table (&body body)
  20. ;; Native file format example ;;;;
  21. ;; change accordingly to the actual location of the directory cointaining
  22. ;; the translation files
  23. `(handler-bind ((i18n-conditions:no-translation-table-error ;; Or just (load-language "italian.lisp")
  24. #'(lambda(e)
  25. (declare (ignore e))
  26. (invoke-restart 'load-language "italian.lisp" t))))
  27. ,@body))
  28. (defmacro standard-singular-tests ()
  29. `(progn
  30. (assert-equality #'string= "mela" (translate "apple"))
  31. (assert-equality #'string= "torta" (translate "pie"))))
  32. (defmacro standard-plural-tests ()
  33. `(progn
  34. (assert-equality #'string=
  35. "mele"
  36. (ntranslate "apple" "apples" 4))
  37. (assert-equality #'string=
  38. "mela"
  39. (ntranslate "apple" "apples" 1))))
  40. (deftest native-translation (cl-i18n-suite)
  41. (with-italian-native-translation-table
  42. (standard-singular-tests)))
  43. (deftest native-translation-plural ((cl-i18n-suite) (native-translation))
  44. (standard-plural-tests))
  45. (defmacro with-italian-po-translation-table (&body body)
  46. `(cl-i18n:with-translation ((cl-i18n:init-translation-table "it.po"
  47. :store-hashtable nil
  48. :store-plural-function t
  49. :update-translation-table nil)
  50. cl-i18n:*plural-form-function*)
  51. ;; or pass a previously loaded hashtable of course
  52. ,@body))
  53. (deftest po-translation (cl-i18n-suite)
  54. (with-italian-po-translation-table
  55. (standard-singular-tests)))
  56. (deftest po-translation-plural (cl-i18n-suite)
  57. (with-italian-po-translation-table
  58. (standard-plural-tests)))
  59. (defparameter *italian-from-lisp*
  60. (multiple-value-list (load-language "italian.lisp"
  61. :locale nil
  62. :store-hashtable nil
  63. :store-plural-function nil
  64. :update-translation-table nil)))
  65. (defparameter *italian-from-po*
  66. (multiple-value-list (load-language "it.po"
  67. :locale nil
  68. :store-hashtable nil
  69. :store-plural-function nil
  70. :update-translation-table nil)))
  71. (defparameter *it-from-utx*
  72. (multiple-value-list (load-language "it.utx"
  73. :locale nil
  74. :store-hashtable nil
  75. :store-plural-function nil
  76. :update-translation-table nil)))
  77. (deftest runtime-dictionary-switching (cl-i18n-suite)
  78. (with-translation ((first *italian-from-lisp*)
  79. (second *italian-from-lisp*))
  80. (standard-singular-tests)
  81. (standard-plural-tests)
  82. (with-translation ((first *italian-from-po*)
  83. (second *italian-from-po*))
  84. (standard-singular-tests)
  85. (standard-plural-tests)
  86. (with-translation ((first *it-from-utx*)
  87. (second *it-from-utx*))
  88. (standard-singular-tests)
  89. (standard-plural-tests)))))
  90. (defun run-all-tests (&key (use-debugger nil))
  91. "Run all the tests."
  92. (clunit:run-suite 'cl-i18n-suite :use-debugger use-debugger))