]> gitweb.factorcode.org Git - factor.git/blob - extra/gdbm/gdbm-tests.factor
io.files: exists? -> file-exists? and rename primitive.
[factor.git] / extra / gdbm / gdbm-tests.factor
1 ! Copyright (C) 2010 Dmitry Shubin.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays continuations gdbm io.directories
4 io.files.temp kernel sequences sets system tools.test ;
5 IN: gdbm.tests
6
7 : db-path ( -- filename ) cpu name>> "-test.db" append temp-file ;
8
9 : CLEANUP ( -- ) db-path ?delete-file ;
10
11 : test.db ( -- gdbm ) <gdbm> db-path >>name ;
12
13 : with-test.db ( quot -- ) test.db swap with-gdbm ; inline
14
15 CLEANUP
16
17 [
18     test.db reader >>role [ ] with-gdbm
19 ] [ gdbm-file-open-error = ] must-fail-with
20
21 { f } [ [ "foo" file-exists? ] with-test.db ] unit-test
22
23 { } [ [ "foo" 41 insert ] with-test.db ] unit-test
24
25 [
26     db-path [ "foo" 42 insert ] with-gdbm-writer
27 ] [ gdbm-cannot-replace = ] must-fail-with
28
29 { }
30 [
31     [
32         "foo" 42 replace
33         "bar" 43 replace
34         "baz" 44 replace
35     ] with-test.db
36 ] unit-test
37
38 { 42 t } [ db-path [ "foo" fetch* ] with-gdbm-reader ] unit-test
39
40 { f f } [ [ "unknown" fetch* ] with-test.db ] unit-test
41
42 ! XXX: different behavior on macOS Big Sur and Monterey?
43 os macosx? [
44     [
45         [
46             300 set-cache-size 300 set-cache-size
47         ] with-test.db
48     ] [ gdbm-option-already-set = ] must-fail-with
49 ] unless
50
51 { t }
52 [
53     V{ } [ [ 2array append ] each-record ] with-test.db
54     V{ "foo" "bar" "baz" 42 43 44 } set=
55
56 ] unit-test
57
58 { f }
59 [
60     test.db newdb >>role [ "foo" file-exists? ] with-gdbm
61 ] unit-test
62
63 CLEANUP