|
| 1 | +import 'package:args/command_runner.dart'; |
| 2 | +import 'package:cli_tools/cli_tools.dart'; |
| 3 | +import 'package:test/test.dart'; |
| 4 | + |
| 5 | +class AddSpiceCommand extends Command { |
| 6 | + @override |
| 7 | + final String name = 'add'; |
| 8 | + |
| 9 | + @override |
| 10 | + final String description = 'Add something to the spice mix'; |
| 11 | + |
| 12 | + AddSpiceCommand() { |
| 13 | + argParser.addOption('curry', help: 'Include curry in the spice mix.'); |
| 14 | + argParser.addOption('pepper', help: 'Include pepper in the spice mix.'); |
| 15 | + } |
| 16 | + |
| 17 | + @override |
| 18 | + void run() {} |
| 19 | +} |
| 20 | + |
| 21 | +class RemoveSpiceCommand extends Command { |
| 22 | + @override |
| 23 | + final String name = 'remove'; |
| 24 | + |
| 25 | + @override |
| 26 | + final String description = 'Remove something from the spice mix'; |
| 27 | + |
| 28 | + RemoveSpiceCommand() { |
| 29 | + argParser.addOption('curry', help: 'Remove curry from the spice mix.'); |
| 30 | + argParser.addOption('pepper', help: 'Remove pepper from the spice mix.'); |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + void run() {} |
| 35 | +} |
| 36 | + |
| 37 | +class AddVegetableCommand extends Command { |
| 38 | + @override |
| 39 | + final String name = 'add'; |
| 40 | + |
| 41 | + @override |
| 42 | + final String description = 'Add a vegetable to your dish.'; |
| 43 | + |
| 44 | + AddVegetableCommand() { |
| 45 | + argParser.addOption('carrot', help: 'Adds a fresh carrot to the dish.'); |
| 46 | + } |
| 47 | + |
| 48 | + @override |
| 49 | + void run() {} |
| 50 | +} |
| 51 | + |
| 52 | +class SpiceCommand extends BetterCommand { |
| 53 | + @override |
| 54 | + final String name = 'spice'; |
| 55 | + |
| 56 | + @override |
| 57 | + final String description = 'Modifies the spice mix in your dish.'; |
| 58 | + |
| 59 | + SpiceCommand() { |
| 60 | + addSubcommand(AddSpiceCommand()); |
| 61 | + addSubcommand(RemoveSpiceCommand()); |
| 62 | + } |
| 63 | + |
| 64 | + @override |
| 65 | + void run() {} |
| 66 | +} |
| 67 | + |
| 68 | +class VegetableCommand extends BetterCommand { |
| 69 | + @override |
| 70 | + final String name = 'vegetable'; |
| 71 | + |
| 72 | + @override |
| 73 | + final String description = 'Add or remove vegatables to your dish.'; |
| 74 | + |
| 75 | + VegetableCommand() { |
| 76 | + addSubcommand(AddVegetableCommand()); |
| 77 | + } |
| 78 | + |
| 79 | + @override |
| 80 | + void run() {} |
| 81 | +} |
| 82 | + |
| 83 | +void main() { |
| 84 | + group('Given commands when generating markdown', () { |
| 85 | + late Map<String, String> output; |
| 86 | + |
| 87 | + setUpAll(() async { |
| 88 | + var commandRunner = |
| 89 | + BetterCommandRunner('cookcli', 'A cli to create wonderful dishes.') |
| 90 | + ..addCommand(SpiceCommand()) |
| 91 | + ..addCommand(VegetableCommand()); |
| 92 | + var generator = CommandDocumentationGenerator(commandRunner); |
| 93 | + output = generator.generateMarkdown(); |
| 94 | + }); |
| 95 | + |
| 96 | + test('then outputs each command into a separate file', () { |
| 97 | + expect(output.keys, containsAll(['vegetable.md', 'spice.md'])); |
| 98 | + }); |
| 99 | + |
| 100 | + test('then output starts with the main command', () async { |
| 101 | + var vegetableCommandOutput = output['spice.md']; |
| 102 | + |
| 103 | + expect( |
| 104 | + vegetableCommandOutput, |
| 105 | + startsWith( |
| 106 | + '## Usage\n' |
| 107 | + '\n' |
| 108 | + '```console\n' |
| 109 | + 'Modifies the spice mix in your dish.\n' |
| 110 | + '\n' |
| 111 | + 'Usage: cookcli spice <subcommand> [arguments]\n' |
| 112 | + '-h, --help Print this usage information.\n' |
| 113 | + '\n' |
| 114 | + 'Available subcommands:\n' |
| 115 | + ' add Add something to the spice mix\n' |
| 116 | + ' remove Remove something from the spice mix\n' |
| 117 | + '\n' |
| 118 | + 'Run "cookcli help" to see global options.\n' |
| 119 | + '```\n' |
| 120 | + '\n', |
| 121 | + )); |
| 122 | + }); |
| 123 | + |
| 124 | + test('then output ends with the sub commands', () async { |
| 125 | + var vegetableCommandOutput = output['spice.md']; |
| 126 | + |
| 127 | + expect( |
| 128 | + vegetableCommandOutput, |
| 129 | + endsWith( |
| 130 | + '### Sub commands\n' |
| 131 | + '\n' |
| 132 | + '#### `add`\n' |
| 133 | + '\n' |
| 134 | + '```console\n' |
| 135 | + 'Add something to the spice mix\n' |
| 136 | + '\n' |
| 137 | + 'Usage: cookcli spice add [arguments]\n' |
| 138 | + '-h, --help Print this usage information.\n' |
| 139 | + ' --curry Include curry in the spice mix.\n' |
| 140 | + ' --pepper Include pepper in the spice mix.\n' |
| 141 | + '\n' |
| 142 | + 'Run "cookcli help" to see global options.\n' |
| 143 | + '```\n' |
| 144 | + '\n' |
| 145 | + '#### `remove`\n' |
| 146 | + '\n' |
| 147 | + '```console\n' |
| 148 | + 'Remove something from the spice mix\n' |
| 149 | + '\n' |
| 150 | + 'Usage: cookcli spice remove [arguments]\n' |
| 151 | + '-h, --help Print this usage information.\n' |
| 152 | + ' --curry Remove curry from the spice mix.\n' |
| 153 | + ' --pepper Remove pepper from the spice mix.\n' |
| 154 | + '\n' |
| 155 | + 'Run "cookcli help" to see global options.\n' |
| 156 | + '```\n', |
| 157 | + )); |
| 158 | + }); |
| 159 | + }); |
| 160 | +} |
0 commit comments