iso8601-tests.el 8.66 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
;;; 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")
27
                 '(nil nil nil nil nil 1985 nil nil nil)))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
28
  (should (equal (iso8601-parse-date "-0003")
29
                 '(nil nil nil nil nil -4 nil nil nil)))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
30
  (should (equal (iso8601-parse-date "+1985")
31
                 '(nil nil nil nil nil 1985 nil nil nil))))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
32 33 34

(ert-deftest test-iso8601-date-dates ()
  (should (equal (iso8601-parse-date "1985-03-14")
35
                 '(nil nil nil 14 3 1985 nil nil nil)))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
36
  (should (equal (iso8601-parse-date "19850314")
37
                 '(nil nil nil 14 3 1985 nil nil nil)))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
38
  (should (equal (iso8601-parse-date "1985-02")
39
                 '(nil nil nil nil 2 1985 nil nil nil))))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
40 41 42

(ert-deftest test-iso8601-date-obsolete ()
  (should (equal (iso8601-parse-date "--02-01")
43
                 '(nil nil nil 1 2 nil nil nil nil)))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
44
  (should (equal (iso8601-parse-date "--0201")
45
                 '(nil nil nil 1 2 nil nil nil nil))))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
46 47 48

(ert-deftest test-iso8601-date-weeks ()
  (should (equal (iso8601-parse-date "2008W39-6")
49
                 '(nil nil nil 27 9 2008 nil nil nil)))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
50
  (should (equal (iso8601-parse-date "2009W01-1")
51
                 '(nil nil nil 29 12 2008 nil nil nil)))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
52
  (should (equal (iso8601-parse-date "2009W53-7")
53
                 '(nil nil nil 3 1 2010 nil nil nil))))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
54 55 56

(ert-deftest test-iso8601-date-ordinals ()
  (should (equal (iso8601-parse-date "1981-095")
57
                 '(nil nil nil 5 4 1981 nil nil nil))))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
58 59 60

(ert-deftest test-iso8601-time ()
  (should (equal (iso8601-parse-time "13:47:30")
61
                 '(30 47 13 nil nil nil nil nil nil)))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
62
  (should (equal (iso8601-parse-time "134730")
63
                 '(30 47 13 nil nil nil nil nil nil)))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
64
  (should (equal (iso8601-parse-time "1347")
65
                 '(0 47 13 nil nil nil nil nil nil))))
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
66 67 68

(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
(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))))
101 102 103 104
  (should (equal (iso8601-parse-interval "2007-03-01T13:00:00Z/P1Y2M10DT2H30M")
                 '((0 0 13 1 3 2007 nil 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
105
  (should (equal (iso8601-parse-interval "P1Y2M10DT2H30M/2008-05-11T15:30:00Z")
106
                 '((0 0 13 1 3 2007 nil nil 0)
Lars Ingebrigtsen's avatar
Lars Ingebrigtsen committed
107 108 109
                   (0 30 15 11 5 2008 nil nil 0)
                   (0 30 2 10 2 1 nil nil nil)))))

110 111
(ert-deftest standard-test-dates ()
  (should (equal (iso8601-parse-date "19850412")
112
                 '(nil nil nil 12 4 1985 nil nil nil)))
113
  (should (equal (iso8601-parse-date "1985-04-12")
114
                 '(nil nil nil 12 4 1985 nil nil nil)))
115 116

  (should (equal (iso8601-parse-date "1985102")
117
                 '(nil nil nil 12 4 1985 nil nil nil)))
118
  (should (equal (iso8601-parse-date "1985-102")
119
                 '(nil nil nil 12 4 1985 nil nil nil)))
120 121

  (should (equal (iso8601-parse-date "1985W155")
122
                 '(nil nil nil 12 4 1985 nil nil nil)))
123
  (should (equal (iso8601-parse-date "1985-W15-5")
124
                 '(nil nil nil 12 4 1985 nil nil nil)))
125 126

  (should (equal (iso8601-parse-date "1985W15")
127
                 '(nil nil nil 7 4 1985 nil nil nil)))
128
  (should (equal (iso8601-parse-date "1985-W15")
129
                 '(nil nil nil 7 4 1985 nil nil nil)))
130 131

  (should (equal (iso8601-parse-date "1985-04")
132
                 '(nil nil nil nil 4 1985 nil nil nil)))
133 134

  (should (equal (iso8601-parse-date "1985")
135
                 '(nil nil nil nil nil 1985 nil nil nil)))
136 137

  (should (equal (iso8601-parse-date "+1985-04-12")
138
                 '(nil nil nil 12 4 1985 nil nil nil)))
139
  (should (equal (iso8601-parse-date "+19850412")
140
                 '(nil nil nil 12 4 1985 nil nil nil))))
141

142
(ert-deftest standard-test-time-of-day-local-time ()
143 144 145 146
  (should (equal (iso8601-parse-time "152746")
                 '(46 27 15 nil nil nil nil nil nil)))
  (should (equal (iso8601-parse-time "15:27:46")
                 '(46 27 15 nil nil nil nil nil nil)))
147

148 149 150 151 152 153 154
  (should (equal (iso8601-parse-time "1528")
                 '(0 28 15 nil nil nil nil nil nil)))
  (should (equal (iso8601-parse-time "15:28")
                 '(0 28 15 nil nil nil nil nil nil)))

  (should (equal (iso8601-parse-time "15")
                 '(0 0 15 nil nil nil nil nil nil))))
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
(ert-deftest standard-test-time-of-day-fractions ()
  ;; decoded-time doesn't support sub-second times.
  ;; (should (equal (iso8601-parse-time "152735,5")
  ;;                '(46 27 15 nil nil nil nil nil nil)))
  ;; (should (equal (iso8601-parse-time "15:27:35,5")
  ;;                '(46 27 15 nil nil nil nil nil nil)))
  )

(ert-deftest standard-test-time-of-day-beginning-of-day ()
  (should (equal (iso8601-parse-time "000000")
                 '(0 0 0 nil nil nil nil nil nil)))
  (should (equal (iso8601-parse-time "00:00:00")
                 '(0 0 0 nil nil nil nil nil nil)))

  (should (equal (iso8601-parse-time "0000")
                 '(0 0 0 nil nil nil nil nil nil)))
  (should (equal (iso8601-parse-time "00:00")
                 '(0 0 0 nil nil nil nil nil nil))))

(ert-deftest standard-test-time-of-day-utc ()
  (should (equal (iso8601-parse-time "232030Z")
                 '(30 20 23 nil nil nil nil nil 0)))
  (should (equal (iso8601-parse-time "23:20:30Z")
                 '(30 20 23 nil nil nil nil nil 0)))

  (should (equal (iso8601-parse-time "2320Z")
                 '(0 20 23 nil nil nil nil nil 0)))
  (should (equal (iso8601-parse-time "23:20Z")
                 '(0 20 23 nil nil nil nil nil 0)))

  (should (equal (iso8601-parse-time "23Z")
                 '(0 0 23 nil nil nil nil nil 0))))


(ert-deftest standard-test-time-of-day-zone ()
  (should (equal (iso8601-parse-time "152746+0100")
                 '(46 27 15 nil nil nil nil nil 60)))
  (should (equal (iso8601-parse-time "15:27:46+0100")
                 '(46 27 15 nil nil nil nil nil 60)))

  (should (equal (iso8601-parse-time "152746+01")
                 '(46 27 15 nil nil nil nil nil 60)))
  (should (equal (iso8601-parse-time "15:27:46+01")
                 '(46 27 15 nil nil nil nil nil 60)))

  (should (equal (iso8601-parse-time "152746-0500")
                 '(46 27 15 nil nil nil nil nil -300)))
  (should (equal (iso8601-parse-time "15:27:46-0500")
                 '(46 27 15 nil nil nil nil nil -300)))

  (should (equal (iso8601-parse-time "152746-05")
                 '(46 27 15 nil nil nil nil nil -300)))
  (should (equal (iso8601-parse-time "15:27:46-05")
                 '(46 27 15 nil nil nil nil nil -300))))

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