v2 / vlib / encoding / txtar / txtar_test.v
119 lines · 105 sloc · 2.89 KB · 0e2b6041b8b81e0593b7de65b216954f903fbf38
Raw
1import os
2import encoding.txtar
3
4// txtar implements a trivial text-based file archive format,
5// Ported from https://cs.opensource.google/go/x/tools/+/master:txtar/archive.go
6// It has some convenience additions (the txtar.pack/1 and txtar.unpack/1 functions).
7// See also the README.md file in this folder.
8
9fn test_parse_nothing() {
10 dump(@LOCATION)
11 content := ''
12 a := txtar.parse(content)
13 assert a.str() == content
14 assert a.comment == ''
15 assert a.files.len == 0
16}
17
18fn test_parse_no_files() {
19 dump(@LOCATION)
20 content := 'some
21comments
22'
23 a := txtar.parse(content)
24 assert a.str() == content
25 assert a.comment != ''
26 assert a.files.len == 0
27}
28
29fn test_parse_no_comments() {
30 dump(@LOCATION)
31 content := '-- abc.xyz --
32line1
33-- another.txt --
34z line1
35'
36 a := txtar.parse(content)
37 assert a.str() == content
38 assert a.comment == ''
39 assert a.files.len == 2
40 assert a.files[0].path == 'abc.xyz'
41 assert a.files[0].content.split_into_lines() == ['line1']
42 assert a.files[1].path == 'another.txt'
43 assert a.files[1].content.split_into_lines() == ['z line1']
44}
45
46const simple_archive_content = 'some
47
48comments on
49several lines
50-- abc.xyz --
51line1
52line2
53-- empty --
54-- folder2/another.txt --
55z line1
56z line2
57z line3
58-- folder3/final.txt --
59'
60
61fn test_parse() {
62 dump(@LOCATION)
63 a := txtar.parse(simple_archive_content)
64 assert a.str() == simple_archive_content
65 assert a.comment != ''
66 assert a.comment.split_into_lines().len == 4
67 assert a.comment.contains('\n\n')
68 assert a.files.len == 4
69 assert a.files[0].path == 'abc.xyz'
70 assert a.files[0].content.split_into_lines() == ['line1', 'line2']
71 assert a.files[1].path == 'empty'
72 assert a.files[1].content == ''
73 assert a.files[2].path == 'folder2/another.txt'
74 assert a.files[2].content.split_into_lines() == ['z line1', 'z line2', 'z line3']
75 assert a.files[3].path == 'folder3/final.txt'
76 assert a.files[3].content == ''
77}
78
79fn test_parse_file() {
80 dump(@LOCATION)
81 fpath := os.join_path(os.vtmp_dir(), 'txtar.txt')
82 defer {
83 os.rm(fpath) or {}
84 }
85 os.write_file(fpath, simple_archive_content)!
86 a := txtar.parse_file(fpath)!
87 assert a.comment != ''
88 assert a.files.len == 4
89 assert a.str() == simple_archive_content
90}
91
92fn test_unpack_to_folder_then_pack_same_folder() {
93 dump(@LOCATION)
94 folder := os.join_path(os.vtmp_dir(), 'txtar_folder')
95 a := txtar.parse(simple_archive_content)
96
97 txtar.unpack(a, folder)!
98 check_folder(folder)
99 os.rmdir_all(folder) or {}
100
101 a.unpack_to(folder)!
102 check_folder(folder)
103
104 b := txtar.pack(folder, 'abc')!
105 os.rmdir_all(folder) or {}
106
107 assert a.comment != b.comment
108 assert b.comment == 'abc'
109 assert b.files.len == a.files.len
110 ofiles := a.files.sorted(|x, y| x.path < y.path)
111 pfiles := b.files.sorted(|x, y| x.path < y.path)
112 assert ofiles == pfiles
113}
114
115fn check_folder(folder string) {
116 assert os.is_file(os.join_path(folder, 'empty'))
117 assert os.is_file(os.join_path(folder, 'folder2/another.txt'))
118 assert os.is_file(os.join_path(folder, 'folder3/final.txt'))
119}
120