module main import term fn widest(text string) int { mut w := 0 for line in text.split('\n') { if visible_width(line) > w { w = visible_width(line) } } return w } fn test_wrap_names_fits_the_width() { names := ['name', 'version', 'description', 'architecture', 'url', 'licenses', 'groups', 'provides', 'depends'] text := wrap_names(names, 30) assert widest(text) <= 30, text // every name, in order, once assert text.split('\n').map(it.trim_space()).join(' ').split(', ').map(it.trim_space()).join(',') == names.join(',') assert text.starts_with(' name, version,') assert !text.ends_with(',') } fn test_wrap_names_counts_what_shows() { // a multibyte name is as wide as its characters: three of these fit a // line of 22 columns, which bytes (two a character) would have broken wide := ['ääää', 'öööö', 'üüüü'] assert wrap_names(wide, 22) == ' ääää, öööö, üüüü' // a styled name is as wide as its text, its escapes take no column styled := ['name', 'version', 'url'].map(term.bold(it)) assert wrap_names(styled, 23).split('\n').len == 1 // one name wider than the line still gets a line of its own assert wrap_names(['a-very-long-field-name', 'x'], 10) == ' a-very-long-field-name,\n x' } fn test_overview_examples_are_the_modes_own() { // every example the overview names is one mode's, found exactly once, // with that mode's description: renaming it in its mode fails here rows := overview_example_rows() assert rows.len == overview_examples.len text := help_text() for cmd in overview_examples { mut found := []string{} for m in help_modes { for e in m.examples { if e.cmd == cmd { found << e.desc } } } assert found.len == 1, cmd assert text.contains(found[0]), cmd } } // flag_longs are the long names a mode's struct parses, as the command line // spells them fn flag_longs[T]() []string { mut out := []string{} $for field in T.fields { for attr in field.attrs { if attr.starts_with('long: ') { out << attr.all_after('long: ').trim_space().replace('_', '-') } } } return out } fn test_help_names_every_flag_a_mode_parses() { // a flag the parser takes and the help does not name is a flag nobody // finds; --help itself is the one every mode has and none lists mut parsed := map[string][]string{} parsed['s'] = flag_longs[ConfigSearch]() parsed['q'] = flag_longs[ConfigInfo]() parsed['w'] = flag_longs[ConfigWhich]() parsed['n'] = flag_longs[ConfigDeptest]() parsed['m'] = flag_longs[ConfigMark]() parsed['b'] = flag_longs[ConfigBuild]() parsed['i'] = flag_longs[ConfigInstall]() parsed['r'] = flag_longs[ConfigRemove]() parsed['u'] = flag_longs[ConfigUpgrade]() parsed['l'] = flag_longs[ConfigFiles]() parsed['o'] = flag_longs[ConfigOwner]() parsed['d'] = flag_longs[ConfigOrphans]() parsed['c'] = flag_longs[ConfigClean]() parsed['t'] = flag_longs[ConfigTree]() parsed['a'] = flag_longs[ConfigAutoRemove]() parsed['k'] = flag_longs[ConfigRank]() parsed['v'] = flag_longs[ConfigVerify]() parsed['f'] = flag_longs[ConfigFind]() for m in help_modes { longs := parsed[m.key] or { assert false, 'no struct for mode ${m.key}' continue } named := m.options.map(it.name).join(' ') + ' ' for long in longs { if long == 'help' { continue } assert named.contains('--${long} ') || named.contains('--${long},'), '${m.name}: --${long} is parsed and not in the help' } } } fn test_wrap_names_edges() { // nothing to lay out is no text, one name is one indented line assert wrap_names([], 30) == '' assert wrap_names(['name'], 30) == ' name' // a width nothing fits gives every name its own line, never an empty // one and never a lost name for width in [0, 1, 4, -3] { assert wrap_names(['aa', 'bb', 'cc'], width) == ' aa,\n bb,\n cc', '${width}' } // a line that goes on keeps room for its comma within the width text := wrap_names(['aaaa', 'bbbb', 'cccc', 'dddd'], 16) assert widest(text) <= 16, text assert text == ' aaaa, bbbb,\n cccc, dddd', text // info's wrapping is the same rule without the indent assert wrap_items(['aaaa', 'bbbb', 'cccc', 'dddd'], 12) == ['aaaa, bbbb', 'cccc, dddd'] assert wrap_items([], 12) == [''] assert wrap_items(['x'], 0) == ['x'] }