iso8601-tests.el 4.65 KB
Newer Older
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
;;; iso8601-tests.el --- tests for calendar/iso8601.el    -*- lexical-binding:t -*-

;; Copyright (C) 2019 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs 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 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs 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 GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'ert)
(require 'iso8601)

(ert-deftest test-iso8601-date-years ()
  (should (equal (iso8601-parse-date "1985")
                 '(0 0 0 1 1 1985 nil nil nil)))
  (should (equal (iso8601-parse-date "-0003")
                 '(0 0 0 1 1 2 nil nil nil)))
  (should (equal (iso8601-parse-date "+1985")
                 '(0 0 0 1 1 1985 nil nil nil))))

(ert-deftest test-iso8601-date-dates ()
  (should (equal (iso8601-parse-date "1985-03-14")
                 '(0 0 0 14 3 1985 nil nil nil)))
  (should (equal (iso8601-parse-date "19850314")
                 '(0 0 0 14 3 1985 nil nil nil)))
  (should (equal (iso8601-parse-date "1985-02")
                 '(0 0 0 1 2 1985 nil nil nil))))

(ert-deftest test-iso8601-date-obsolete ()
  (should (equal (iso8601-parse-date "--02-01")
                 '(0 0 0 1 2 0 nil nil nil)))
  (should (equal (iso8601-parse-date "--0201")
                 '(0 0 0 1 2 0 nil nil nil))))

(ert-deftest test-iso8601-date-weeks ()
  (should (equal (iso8601-parse-date "2008W39-6")
                 '(0 0 0 27 9 2008 nil nil nil)))
  (should (equal (iso8601-parse-date "2009W01-1")
                 '(0 0 0 29 12 2008 nil nil nil)))
  (should (equal (iso8601-parse-date "2009W53-7")
                 '(0 0 0 3 1 2010 nil nil nil))))

(ert-deftest test-iso8601-date-ordinals ()
  (should (equal (iso8601-parse-date "1981-095")
                 '(0 0 0 5 4 1981 nil nil nil))))

(ert-deftest test-iso8601-time ()
  (should (equal (iso8601-parse-time "13:47:30")
                 '(30 47 13 1 1 0 nil nil nil)))
  (should (equal (iso8601-parse-time "134730")
                 '(30 47 13 1 1 0 nil nil nil)))
  (should (equal (iso8601-parse-time "1347")
                 '(0 47 13 1 1 0 nil nil nil))))

(ert-deftest test-iso8601-combined ()
  (should (equal (iso8601-parse "2008-03-02T13:47:30")
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
69 70 71 72 73 74 75
                 '(30 47 13 2 3 2008 nil nil nil)))
  (should (equal (iso8601-parse "2008-03-02T13:47:30Z")
                 '(30 47 13 2 3 2008 nil nil 0)))
  (should (equal (iso8601-parse "2008-03-02T13:47:30+01:00")
                 '(30 47 13 2 3 2008 nil nil 3600)))
  (should (equal (iso8601-parse "2008-03-02T13:47:30-01")
                 '(30 47 13 2 3 2008 nil nil -3600))))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
76 77 78 79 80 81 82 83 84 85 86

(ert-deftest test-iso8601-duration ()
  (should (equal (iso8601-parse-duration "P3Y6M4DT12H30M5S")
                 '(5 30 12 4 6 3 nil nil nil)))
  (should (equal (iso8601-parse-duration "P1M")
                 '(0 0 0 0 1 0 nil nil nil)))
  (should (equal (iso8601-parse-duration "PT1M")
                 '(0 1 0 0 0 0 nil nil nil)))
  (should (equal (iso8601-parse-duration "P0003-06-04T12:30:05")
                 '(5 30 12 4 6 3 nil nil nil))))

87 88 89 90 91 92 93
(ert-deftest test-iso8601-invalid ()
  (should-not (iso8601-valid-p " 2008-03-02T13:47:30-01"))
  (should-not (iso8601-valid-p "2008-03-02T13:47:30-01:200"))
  (should-not (iso8601-valid-p "2008-03-02T13:47:30-01 "))
  (should-not (iso8601-valid-p "2008-03-02 T 13:47:30-01 "))
  (should-not (iso8601-valid-p "20008-03-02T13:47:30-01")))

Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
(ert-deftest test-iso8601-intervals ()
  (should (equal
           (iso8601-parse-interval "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z")
           '((0 0 13 1 3 2007 nil nil 0)
             (0 30 15 11 5 2008 nil nil 0)
             ;; Hm...  can't really use decode-time for time differences...
             (0 30 2 14 3 1971 0 nil 0))))
  (should
   (equal (iso8601-parse-interval "2007-03-01T13:00:00Z/P1Y2M10DT2H30M")
          '((0 0 13 1 3 2007 nil nil 0)
            ;; Well, that's wrong...
            (0 47 14 10 4 38 6 nil 0)
            (0 30 2 10 2 1 nil nil nil))))
  (should (equal (iso8601-parse-interval "P1Y2M10DT2H30M/2008-05-11T15:30:00Z")
                 ;; I think I have to add decoded-time math functions,
                 ;; really.
                 '((0 43 13 1 4 3977 5 nil 0)
                   (0 30 15 11 5 2008 nil nil 0)
                   (0 30 2 10 2 1 nil nil nil)))))

Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
114
;;; iso8601-tests.el ends here