config

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

csv-mode-tests.el (4786B)


      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-beginning-of-field ()
     56   (with-temp-buffer
     57     (csv-mode)
     58     (insert "aaa,bbb")
     59     (csv-beginning-of-field)
     60     (should (equal (buffer-substring (point) (point-max)) "bbb"))
     61     (backward-char)
     62     (csv-beginning-of-field)
     63     (should (equal (buffer-substring (point) (point-max))
     64                    "aaa,bbb"))))
     65 
     66 (ert-deftest csv-tests-beginning-of-field-with-quotes ()
     67   (with-temp-buffer
     68     (csv-mode)
     69     (insert "aaa,\"b,b\"")
     70     (csv-beginning-of-field)
     71     (should (equal (buffer-substring (point) (point-max)) "\"b,b\""))
     72     (backward-char)
     73     (csv-beginning-of-field)
     74     (should (equal (buffer-substring (point) (point-max))
     75                    "aaa,\"b,b\""))))
     76 
     77 (defun csv-tests--align-fields (before after)
     78   (with-temp-buffer
     79     (insert (string-join before "\n"))
     80     (csv-align-fields t (point-min) (point-max))
     81     (should (equal (buffer-string) (string-join after "\n")))))
     82 
     83 (ert-deftest csv-tests-align-fields ()
     84   (csv-tests--align-fields
     85    '("aaa,bbb,ccc"
     86      "1,2,3")
     87    '("aaa, bbb, ccc"
     88      "1  , 2  , 3")))
     89 
     90 (ert-deftest csv-tests-align-fields-with-quotes ()
     91   (csv-tests--align-fields
     92    '("aaa,\"b,b\",ccc"
     93      "1,2,3")
     94    '("aaa, \"b,b\", ccc"
     95      "1  , 2    , 3")))
     96 
     97 ;; Bug#14053
     98 (ert-deftest csv-tests-align-fields-double-quote-comma ()
     99   (csv-tests--align-fields
    100    '("1,2,3"
    101      "a,\"b\"\"c,\",d")
    102    '("1, 2      , 3"
    103      "a, \"b\"\"c,\", d")))
    104 
    105 (defvar csv-tests--data
    106   "1,4;Sun, 2022-04-10;4,12
    107 8;Mon, 2022-04-11;3,19
    108 3,2;Tue, 2022-04-12;1,00
    109 2;Wed, 2022-04-13;0,37
    110 9;Wed, 2022-04-13;0,37")
    111 
    112 (ert-deftest csv-tests-guess-separator ()
    113   (should-not (csv-guess-separator ""))
    114   (should (= (csv-guess-separator csv-tests--data 3) ?,))
    115   (should (= (csv-guess-separator csv-tests--data) ?\;))
    116   (should (= (csv-guess-separator csv-tests--data)
    117              (csv-guess-separator csv-tests--data
    118                                   (length csv-tests--data)))))
    119 
    120 (ert-deftest csv-tests-separator-candidates ()
    121   (should-not (csv--separator-candidates ""))
    122   (should-not (csv--separator-candidates csv-tests--data 0))
    123   (should
    124    (equal (sort (csv--separator-candidates csv-tests--data 4) #'<)
    125           '(?, ?\;)))
    126   (should
    127    (equal (sort (csv--separator-candidates csv-tests--data) #'<)
    128           '(?\s ?, ?- ?\;)))
    129   (should
    130    (equal
    131     (sort (csv--separator-candidates csv-tests--data) #'<)
    132     (sort (csv--separator-candidates csv-tests--data
    133                                      (length csv-tests--data))
    134           #'<))))
    135 
    136 (ert-deftest csv-tests-separator-score ()
    137   (should (< (csv--separator-score ?, csv-tests--data)
    138              (csv--separator-score ?\s csv-tests--data)
    139              (csv--separator-score ?- csv-tests--data)))
    140   (should (= (csv--separator-score ?- csv-tests--data)
    141              (csv--separator-score ?\; csv-tests--data)))
    142   (should (= 0 (csv--separator-score ?\; csv-tests--data 0)))
    143   (should (= (csv--separator-score ?\; csv-tests--data)
    144              (csv--separator-score ?\; csv-tests--data
    145                                    (length csv-tests--data)))))
    146 
    147 (provide 'csv-mode-tests)
    148 ;;; csv-mode-tests.el ends here