vpac / src / help_test.v
126 lines · 118 sloc · 4.16 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import term
4
5fn widest(text string) int {
6 mut w := 0
7 for line in text.split('\n') {
8 if visible_width(line) > w {
9 w = visible_width(line)
10 }
11 }
12 return w
13}
14
15fn test_wrap_names_fits_the_width() {
16 names := ['name', 'version', 'description', 'architecture', 'url', 'licenses', 'groups', 'provides',
17 'depends']
18 text := wrap_names(names, 30)
19 assert widest(text) <= 30, text
20 // every name, in order, once
21 assert text.split('\n').map(it.trim_space()).join(' ').split(', ').map(it.trim_space()).join(',') == names.join(',')
22 assert text.starts_with(' name, version,')
23 assert !text.ends_with(',')
24}
25
26fn test_wrap_names_counts_what_shows() {
27 // a multibyte name is as wide as its characters: three of these fit a
28 // line of 22 columns, which bytes (two a character) would have broken
29 wide := ['ääää', 'öööö', 'üüüü']
30 assert wrap_names(wide, 22) == ' ääää, öööö, üüüü'
31 // a styled name is as wide as its text, its escapes take no column
32 styled := ['name', 'version', 'url'].map(term.bold(it))
33 assert wrap_names(styled, 23).split('\n').len == 1
34 // one name wider than the line still gets a line of its own
35 assert wrap_names(['a-very-long-field-name', 'x'], 10) == ' a-very-long-field-name,\n x'
36}
37
38fn test_overview_examples_are_the_modes_own() {
39 // every example the overview names is one mode's, found exactly once,
40 // with that mode's description: renaming it in its mode fails here
41 rows := overview_example_rows()
42 assert rows.len == overview_examples.len
43 text := help_text()
44 for cmd in overview_examples {
45 mut found := []string{}
46 for m in help_modes {
47 for e in m.examples {
48 if e.cmd == cmd {
49 found << e.desc
50 }
51 }
52 }
53 assert found.len == 1, cmd
54 assert text.contains(found[0]), cmd
55 }
56}
57
58// flag_longs are the long names a mode's struct parses, as the command line
59// spells them
60fn flag_longs[T]() []string {
61 mut out := []string{}
62 $for field in T.fields {
63 for attr in field.attrs {
64 if attr.starts_with('long: ') {
65 out << attr.all_after('long: ').trim_space().replace('_', '-')
66 }
67 }
68 }
69 return out
70}
71
72fn test_help_names_every_flag_a_mode_parses() {
73 // a flag the parser takes and the help does not name is a flag nobody
74 // finds; --help itself is the one every mode has and none lists
75 mut parsed := map[string][]string{}
76 parsed['s'] = flag_longs[ConfigSearch]()
77 parsed['q'] = flag_longs[ConfigInfo]()
78 parsed['w'] = flag_longs[ConfigWhich]()
79 parsed['n'] = flag_longs[ConfigDeptest]()
80 parsed['m'] = flag_longs[ConfigMark]()
81 parsed['b'] = flag_longs[ConfigBuild]()
82 parsed['i'] = flag_longs[ConfigInstall]()
83 parsed['r'] = flag_longs[ConfigRemove]()
84 parsed['u'] = flag_longs[ConfigUpgrade]()
85 parsed['l'] = flag_longs[ConfigFiles]()
86 parsed['o'] = flag_longs[ConfigOwner]()
87 parsed['d'] = flag_longs[ConfigOrphans]()
88 parsed['c'] = flag_longs[ConfigClean]()
89 parsed['t'] = flag_longs[ConfigTree]()
90 parsed['a'] = flag_longs[ConfigAutoRemove]()
91 parsed['k'] = flag_longs[ConfigRank]()
92 parsed['v'] = flag_longs[ConfigVerify]()
93 parsed['f'] = flag_longs[ConfigFind]()
94 for m in help_modes {
95 longs := parsed[m.key] or {
96 assert false, 'no struct for mode ${m.key}'
97 continue
98 }
99 named := m.options.map(it.name).join(' ') + ' '
100 for long in longs {
101 if long == 'help' {
102 continue
103 }
104 assert named.contains('--${long} ') || named.contains('--${long},'), '${m.name}: --${long} is parsed and not in the help'
105 }
106 }
107}
108
109fn test_wrap_names_edges() {
110 // nothing to lay out is no text, one name is one indented line
111 assert wrap_names([], 30) == ''
112 assert wrap_names(['name'], 30) == ' name'
113 // a width nothing fits gives every name its own line, never an empty
114 // one and never a lost name
115 for width in [0, 1, 4, -3] {
116 assert wrap_names(['aa', 'bb', 'cc'], width) == ' aa,\n bb,\n cc', '${width}'
117 }
118 // a line that goes on keeps room for its comma within the width
119 text := wrap_names(['aaaa', 'bbbb', 'cccc', 'dddd'], 16)
120 assert widest(text) <= 16, text
121 assert text == ' aaaa, bbbb,\n cccc, dddd', text
122 // info's wrapping is the same rule without the indent
123 assert wrap_items(['aaaa', 'bbbb', 'cccc', 'dddd'], 12) == ['aaaa, bbbb', 'cccc, dddd']
124 assert wrap_items([], 12) == ['']
125 assert wrap_items(['x'], 0) == ['x']
126}
127