config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

csv-mode-tests.el (6541B)


      1 ;;; csv-mode-tests.el --- Tests for CSV mode         -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2020-2022 Free Software Foundation, Inc
      4 
      5 ;; Author: Simen Heggestøyl <simenheg@runbox.com>
      6 ;; Keywords:
      7 
      8 ;; This program is free software; you can redistribute it and/or modify
      9 ;; it under the terms of the GNU General Public License as published by
     10 ;; the Free Software Foundation, either version 3 of the License, or
     11 ;; (at your option) any later version.
     12 
     13 ;; This program is distributed in the hope that it will be useful,
     14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 ;; GNU General Public License for more details.
     17 
     18 ;; You should have received a copy of the GNU General Public License
     19 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     20 
     21 ;;; Commentary:
     22 
     23 ;;
     24 
     25 ;;; Code:
     26 
     27 (require 'ert)
     28 (require 'csv-mode)
     29 (eval-when-compile (require 'subr-x))
     30 
     31 (ert-deftest csv-tests-end-of-field ()
     32   (with-temp-buffer
     33     (csv-mode)
     34     (insert "aaa,bbb")
     35     (goto-char (point-min))
     36     (csv-end-of-field)
     37     (should (equal (buffer-substring (point-min) (point)) "aaa"))
     38     (forward-char)
     39     (csv-end-of-field)
     40     (should (equal (buffer-substring (point-min) (point))
     41                    "aaa,bbb"))))
     42 
     43 (ert-deftest csv-tests-end-of-field-with-quotes ()
     44   (with-temp-buffer
     45     (csv-mode)
     46     (insert "aaa,\"b,b\"")
     47     (goto-char (point-min))
     48     (csv-end-of-field)
     49     (should (equal (buffer-substring (point-min) (point)) "aaa"))
     50     (forward-char)
     51     (csv-end-of-field)
     52     (should (equal (buffer-substring (point-min) (point))
     53                    "aaa,\"b,b\""))))
     54 
     55 (ert-deftest csv-tests-end-of-field-unclosed-quotes ()
     56   (with-temp-buffer
     57     (csv-mode)
     58     (insert "a,b,c
     59 1,2,3
     60 1,2,\"
     61 \"1,
     62 \"")
     63     (goto-char (point-min))
     64     (while (not (eobp))
     65       ;; Should not error
     66       (csv-end-of-field)
     67       (unless (eobp)
     68         (forward-char)))))
     69 
     70 (ert-deftest csv-tests-beginning-of-field ()
     71   (with-temp-buffer
     72     (csv-mode)
     73     (insert "aaa,bbb")
     74     (csv-beginning-of-field)
     75     (should (equal (buffer-substring (point) (point-max)) "bbb"))
     76     (backward-char)
     77     (csv-beginning-of-field)
     78     (should (equal (buffer-substring (point) (point-max))
     79                    "aaa,bbb"))))
     80 
     81 (ert-deftest csv-tests-beginning-of-field-with-quotes ()
     82   (with-temp-buffer
     83     (csv-mode)
     84     (insert "aaa,\"b,b\"")
     85     (csv-beginning-of-field)
     86     (should (equal (buffer-substring (point) (point-max)) "\"b,b\""))
     87     (backward-char)
     88     (csv-beginning-of-field)
     89     (should (equal (buffer-substring (point) (point-max))
     90                    "aaa,\"b,b\""))))
     91 
     92 (defun csv-tests--align-fields (before after)
     93   (with-temp-buffer
     94     (insert (string-join before "\n"))
     95     (csv-align-fields t (point-min) (point-max))
     96     (should (equal (buffer-string) (string-join after "\n")))))
     97 
     98 (ert-deftest csv-tests-align-fields ()
     99   (csv-tests--align-fields
    100    '("aaa,bbb,ccc"
    101      "1,2,3")
    102    '("aaa, bbb, ccc"
    103      "1  , 2  , 3")))
    104 
    105 (ert-deftest csv-tests-align-fields-with-quotes ()
    106   (csv-tests--align-fields
    107    '("aaa,\"b,b\",ccc"
    108      "1,2,3")
    109    '("aaa, \"b,b\", ccc"
    110      "1  , 2    , 3")))
    111 
    112 ;; Bug#14053
    113 (ert-deftest csv-tests-align-fields-double-quote-comma ()
    114   (csv-tests--align-fields
    115    '("1,2,3"
    116      "a,\"b\"\"c,\",d")
    117    '("1, 2      , 3"
    118      "a, \"b\"\"c,\", d")))
    119 
    120 (defvar csv-tests--data
    121   "1,4;Sun, 2022-04-10;4,12
    122 8;Mon, 2022-04-11;3,19
    123 3,2;Tue, 2022-04-12;1,00
    124 2;Wed, 2022-04-13;0,37
    125 9;Wed, 2022-04-13;0,37")
    126 
    127 (ert-deftest csv-tests-guess-separator ()
    128   (should-not (csv-guess-separator ""))
    129   (should (= (csv-guess-separator csv-tests--data 3) ?,))
    130   (should (= (csv-guess-separator csv-tests--data) ?\;))
    131   (should (= (csv-guess-separator csv-tests--data)
    132              (csv-guess-separator csv-tests--data
    133                                   (length csv-tests--data)))))
    134 
    135 (ert-deftest csv-tests-separator-candidates ()
    136   (should-not (csv--separator-candidates ""))
    137   (should-not (csv--separator-candidates csv-tests--data 0))
    138   (should
    139    (equal (sort (csv--separator-candidates csv-tests--data 4) #'<)
    140           '(?, ?\;)))
    141   (should
    142    (equal (sort (csv--separator-candidates csv-tests--data) #'<)
    143           '(?\s ?, ?- ?\;)))
    144   (should
    145    (equal
    146     (sort (csv--separator-candidates csv-tests--data) #'<)
    147     (sort (csv--separator-candidates csv-tests--data
    148                                      (length csv-tests--data))
    149           #'<))))
    150 
    151 (ert-deftest csv-tests-separator-score ()
    152   (should (< (csv--separator-score ?, csv-tests--data)
    153              (csv--separator-score ?\s csv-tests--data)
    154              (csv--separator-score ?- csv-tests--data)))
    155   (should (= (csv--separator-score ?- csv-tests--data)
    156              (csv--separator-score ?\; csv-tests--data)))
    157   (should (= 0 (csv--separator-score ?\; csv-tests--data 0)))
    158   (should (= (csv--separator-score ?\; csv-tests--data)
    159              (csv--separator-score ?\; csv-tests--data
    160                                    (length csv-tests--data)))))
    161 
    162 (ert-deftest csv-tests-unquote-value ()
    163   (should (equal (csv--unquote-value "Hello, World")
    164                  "Hello, World"))
    165   (should (equal (csv--unquote-value "\"Hello, World\"")
    166                  "Hello, World"))
    167   (should (equal (csv--unquote-value "Hello, \"\"World")
    168                  "Hello, \"\"World"))
    169   (should (equal (csv--unquote-value "\"Hello, \"\"World\"\"\"")
    170                  "Hello, \"World\""))
    171   (should (equal (csv--unquote-value "'Hello, World'")
    172                  "'Hello, World'"))
    173   (should (equal (let ((csv-field-quotes '("\"" "'")))
    174                    (csv--unquote-value "\"Hello, World'"))
    175                  "\"Hello, World'"))
    176   (should (equal (let ((csv-field-quotes '("\"" "'")))
    177                    (csv--unquote-value "'Hello, World'"))
    178                  "Hello, World"))
    179   (should (equal (let ((csv-field-quotes '("\"" "'")))
    180                    (csv--unquote-value "'Hello, ''World'''"))
    181                  "Hello, 'World'"))
    182   (should (equal (let ((csv-field-quotes '("\"" "'")))
    183                    (csv--unquote-value "'Hello, \"World\"'"))
    184                  "Hello, \"World\""))
    185   (should (equal (csv--unquote-value "|Hello, World|")
    186                  "|Hello, World|")))
    187 
    188 (ert-deftest csv-tests-guess-separator-avoid-comment ()
    189   ;; bug#71042
    190   (let ((testdata "###
    191 ###
    192 Foo;Bar;Quux
    193 123;456;blah, blah
    194 "))
    195     (message "Guessed separator: %c" (csv-guess-separator testdata))
    196     (should-not (equal (csv-guess-separator testdata) ?#))))
    197 
    198 (provide 'csv-mode-tests)
    199 ;;; csv-mode-tests.el ends here