summaryrefslogtreecommitdiff
path: root/test/f-io-test.el
blob: 89869e282a3584dde7a5e4cb00087cea2f26808d (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
;;; f-io-test.el --- F: IO related tests  -*- lexical-binding: t; -*-

;; Copyright (C) 2013 Sebastian Wiesner
;; Copyright (C) 2013-2014 Johan Andersson

;; Author: Johan Andersson <johan.rejeep@gmail.com>
;; Maintainer: Johan Andersson <johan.rejeep@gmail.com>
;; URL: http://github.com/rejeep/f.el

;; This file is NOT part of GNU Emacs.

;; This program 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.

;; This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:


;;;; f-write-bytes

(ert-deftest f-write-bytes-test/multibyte-string ()
  (with-playground
   (let ((err (should-error (f-write-bytes "☺ ☹" "foo.txt")
                            :type 'wrong-type-argument)))
     (should (equal (cdr err)
                    (list 'f-unibyte-string-p "☺ ☹"))))))

(ert-deftest f-write-bytes-test/unibyte-string ()
  (with-playground
   ;; Let's take some random bytes
   (let ((bytes (apply #'unibyte-string (-map #'random (-repeat 100 255)))))
     ;; Make a string of our bytes
     (f-write-bytes bytes "foo.txt")
     (should-exist "foo.txt" bytes))))


;;;; f-write/f-write-text

(ert-deftest f-write-text-test/unibyte-string ()
  (with-playground
   (f-write-text (unibyte-string 1 2 3 4 5) 'utf-8 "foo.txt")
   ;; Emacs only makes multibyte strings if actually required.
   (f-write-text "bar" 'utf-8 "bar.txt")
   (should-exist "bar.txt" "bar")))

(ert-deftest f-write-text-test/multibyte-string ()
  (with-playground
   (f-write-text "☺ ☹" 'utf-8 "foo.txt")
   (should-exist "foo.txt" (unibyte-string 226 152 186 32 226 152 185))
   (f-write-text "blök" 'iso-8859-1 "foo.txt")
   (should-exist "foo.txt" (unibyte-string 98 108 246 107))))

(ert-deftest f-write-test/alias ()
  (with-playground
   (f-write-text (unibyte-string 1 2 3 4 5) 'utf-8 "foo.txt")
   (f-write (unibyte-string 1 2 3 4 5) 'utf-8 "bar.txt")
   (should (equal (f-read "foo.txt") (f-read "bar.txt")))))


;;;; f-read-bytes

(ert-deftest f-read-bytes-test/ ()
  (with-playground
   (let ((bytes (apply #'unibyte-string (-map #'random (-repeat 100 255)))))
     (f-write-bytes bytes "foo.txt")
     (let ((content (f-read-bytes "foo.txt")))
       (should-not (multibyte-string-p content))
       (should (f-unibyte-string-p content))
       (should (string= content bytes))))))


;;;; f-read/f-read-text

(ert-deftest f-read-text-test/ ()
  (with-playground
   (f-write-bytes (unibyte-string 226 152 185 32 226 152 186) "foo.txt")
   (let ((text (f-read-text "foo.txt" 'utf-8)))
     (should (string= text "☹ ☺"))
     (should (multibyte-string-p text)))
   (f-write-bytes (unibyte-string 252 98 101 114) "foo.txt")
   (let ((text (f-read-text "foo.txt" 'iso-8859-1)))
     (should (string= text "über"))
     (should (multibyte-string-p text)))))

(ert-deftest f-read-text-test/no-coding-specified ()
  (with-playground
   (f-write-text "text" 'utf-8 "foo.txt")
   (should (equal (f-read-text "foo.txt") "text"))))

(ert-deftest f-read-test/alias ()
  (with-playground
   (f-write-bytes (unibyte-string 226 152 185 32 226 152 186) "foo.txt")
   (should (equal (f-read-text "foo.txt") (f-read "foo.txt")))))


;;;; f-append-text

(ert-deftest f-append-text/does-not-exist ()
  (with-playground
   (f-append-text "foo" 'utf-8 "foo.txt")
   (should-exist "foo.txt" "foo")))

(ert-deftest f-append-text/exists ()
  (with-playground
   (f-write "foo" 'utf-8 "foo.txt")
   (f-append-text "bar" 'utf-8 "foo.txt")
   (should-exist "foo.txt" "foobar")))

(ert-deftest f-append/alias ()
  (with-playground
   (f-append "foo" 'utf-8 "foo.txt")
   (should-exist "foo.txt" "foo")))


;;;; f-append-bytes

(ert-deftest f-append-bytes-test/does-not-exist ()
  (with-playground
   (let ((bytes (apply #'unibyte-string (-map #'random (-repeat 100 255)))))
     (f-append-bytes bytes "foo.txt")
     (should-exist "foo.txt" bytes))))

(ert-deftest f-append-bytes-test/exists ()
  (with-playground
   (let ((bytes (apply #'unibyte-string (-map #'random (-repeat 100 255)))))
     (f-write-bytes bytes "foo.txt")
     (f-append-bytes bytes "foo.txt")
     (should-exist "foo.txt" (concat bytes bytes)))))

(provide 'f-io-test)

;;; f-io-test.el ends here