vpac / src / format_test.v
93 lines · 85 sloc · 2.84 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1module main
2
3fn test_listing_renders_plain_entries() {
4 old := color_flag
5 defer { color_flag = old }
6 color_flag = 'never'
7
8 // the entries are plain values; the render styles them, and shows an
9 // empty one as '-'
10 l := Listing{
11 fields: ['name', 'version', 'description']
12 entries: [ListEntry{
13 name: 'zsh'
14 values: ['zsh', '5.9-1', '']
15 }, ListEntry{
16 name: 'bash'
17 values: ['bash', '5.2-1', 'shell']
18 }]
19 }
20 assert table_text(l.entries.map(listing_row(l.fields, it)), '', ' ') == 'zsh 5.9-1 -\nbash 5.2-1 shell'
21}
22
23// a row built against another listing's fields is shortened, not tripped
24// over: an entry always carries one value per field, but a mismatched one
25// fills its tail with empties and renders as '-' instead of panicking
26fn test_listing_row_survives_a_short_entry() {
27 old := color_flag
28 defer { color_flag = old }
29 color_flag = 'never'
30
31 r := listing_row(['name', 'version', 'description'], ListEntry{
32 name: 'zsh'
33 values: ['zsh']
34 })
35 assert r.cols.len == 3
36 assert table_text([r], '', ' ') == 'zsh - -'
37}
38
39fn test_info_table() {
40 old := color_flag
41 defer { color_flag = old }
42 color_flag = 'never'
43
44 assert info_table([]) == ''
45 // -F all on a package without optional deps: an optdepends with no
46 // items shows '-' and draws no continuation line
47 assert info_table([InfoField{
48 field: 'optdepends'
49 }, InfoField{
50 field: 'install_reason'
51 value: 'explicitly installed'
52 }]) == 'optdepends -\ninstall reason explicitly installed'
53 // optdepends puts each item on its own line, padded to the value column
54 assert info_table([InfoField{
55 field: 'optdepends'
56 value: 'a, b'
57 items: ['a: x', 'b']
58 }]) == 'optdepends a: x\n b'
59}
60
61fn test_reason_spellings() ! {
62 // every reason in both spellings, and the frontend's key is a word mark
63 // reads back as that reason
64 for reason, want in {
65 nspkg.PackageReason.explicit: ['explicit', 'explicitly installed']
66 nspkg.PackageReason.depend: ['depend', 'installed as a dependency']
67 nspkg.PackageReason.unknown: ['unknown', 'unknown']
68 } {
69 assert [reason_key(reason), reason_to_str(reason)] == want
70 assert parse_reason(reason_key(reason))! == reason
71 }
72 for short, reason in {
73 'e': nspkg.PackageReason.explicit
74 'dep': nspkg.PackageReason.depend
75 'dependency': nspkg.PackageReason.depend
76 'u': nspkg.PackageReason.unknown
77 } {
78 assert parse_reason(short)! == reason
79 }
80 if _ := parse_reason('optional') {
81 assert false, 'an unknown reason was read'
82 }
83}
84
85fn test_info_width() {
86 // no terminal (a pipe answers 0), or a size no terminal has: the fallback
87 assert info_width(0) == info_fallback_cols
88 assert info_width(-1) == info_fallback_cols
89 assert info_width(info_min_cols - 1) == info_fallback_cols
90 // a terminal's own width, from the narrowest one taken on
91 assert info_width(info_min_cols) == info_min_cols
92 assert info_width(132) == 132
93}
94