Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: tools/testing/dart/test_options.dart

Issue 8508054: Getting started on options parsing. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #library("test_options_parser");
6
7 /**
8 * Specification of a single test option.
9 *
10 * The name of the specification is used as the key for the option in
11 * the Map returned from the [TestOptionParser] parse method.
12 */
13 class _TestOptionSpecification {
14 _TestOptionSpecification(this.name,
15 this.description,
16 this.keys,
17 this.values,
18 this.defaultValue,
19 [this.type = 'string']);
20 String name;
21 String description;
22 List<String> keys;
23 List<String> values;
24 String defaultValue;
25 String type;
26 }
27
28
29 /**
30 * Parser of test options.
31 */
32 class TestOptionsParser {
33 /**
34 * Creates a test options parser initialized with the known options.
35 */
36 TestOptionsParser() {
37 _options =
38 [ new _TestOptionSpecification(
39 'mode',
40 'Mode in which to run the tests',
41 ['-m', '--mode'],
42 ['all', 'debug', 'release'],
43 'debug'),
44 new _TestOptionSpecification(
45 'component',
46 'The component to test against',
47 ['-c', '--component'],
48 ['most', 'vm', 'dartc', 'chromium', 'dartium'],
49 'vm'),
50 new _TestOptionSpecification(
51 'architecture',
52 'The architecture to run tests for',
53 ['-a', '--arch'],
54 ['all', 'ia32', 'x64', 'simarm', 'arm'],
55 'ia32'),
56 new _TestOptionSpecification(
57 'os',
58 'The operating system to run tests on',
59 ['-o', '--os'],
60 ['linux', 'macos', 'windows'],
61 new Platform().operatingSystem()),
62 new _TestOptionSpecification(
63 'checked',
64 'Run tests in checked mode',
65 ['--checked'],
66 [],
67 false,
68 'bool'),
69 new _TestOptionSpecification(
70 'tasks',
71 'The number of parallel tasks to run',
72 ['-j', '--tasks'],
73 [],
74 new Platform().numberOfProcessors(),
75 'int'),
76 new _TestOptionSpecification(
77 'help',
78 'Print list of options',
79 ['-h', '--help'],
80 [],
81 false,
82 'bool')];
83 }
84
85
86 /**
87 * Parse a list of strings as test options.
88 *
89 * Returns a Map mapping from option keys to values. When
90 * encountering the first non-option string, the rest of the
91 * arguments are stored in the returned Map under the 'rest' key.
92 */
93 Map parse(List<String> arguments) {
94 var configuration = new Map();
95 // Build configuration of default values.
96 for (var option in _options) {
97 configuration[option.name] = option.defaultValue;
98 }
99 // Overwrite with the arguments passed to the test script.
100 var numArguments = arguments.length;
101 for (var i = 0; i < numArguments; i++) {
102 // Extract name and value for options.
103 var arg = arguments[i];
104 var name = '';
105 var value = '';
106 if (arg.startsWith('--')) {
107 if (arg == '--help') {
108 _printHelp();
109 return null;
110 }
111 var split = arg.lastIndexOf('=');
112 name = arg;
113 value = '';
114 if (split != -1) {
115 name = arg.substring(0, split);
116 value = arg.substring(split + 1, arg.length);
117 }
118 } else if (arg.startsWith('-')) {
119 if (arg == '-h') {
120 _printHelp();
121 return null;
122 }
123 name = arg;
124 if ((i + 1) >= arguments.length) {
125 print('No value supplied for option $name');
126 return null;
127 }
128 value = arguments[++i];
129 } else {
130 // The option name does not start with '-' or '--' so we
131 // assume that the rest of the arguments specify tests or test
132 // suites to run.
133 configuration['rest'] = arguments.getRange(i, numArguments - i);
134 return configuration;
135 }
136 // Find the option specification for the name.
137 var spec = _getSpecification(name);
138 if (spec == null) {
139 print('Unknown test option $name');
140 return null;
141 }
142 // Parse the value for the option.
143 if (spec.type == 'bool') {
144 if (!value.isEmpty()) {
145 print('No value expected for bool option $name');
146 return null;
147 }
148 configuration[spec.name] = true;
149 } else if (spec.type == 'int') {
150 try {
151 configuration[spec.name] = Math.parseInt(value);
152 } catch (var e) {
153 print('Integer value expected for int option $name');
154 return null;
155 }
156 } else {
157 assert(spec.type == 'string');
158 if (spec.values.lastIndexOf(value) == -1) {
159 print('Unknown value ($value) for option $name');
160 return null;
161 }
162 configuration[spec.name] = value;
163 }
164 }
165
166 return configuration;
167 }
168
169
170 /**
171 * Print out usage information.
172 */
173 void _printHelp() {
174 print('usage: dart_bin test.dart [options]\n');
175 print('Options:\n');
176 for (var option in _options) {
177 print('${option.name}: ${option.description}.');
178 for (var name in option.keys) {
179 assert(name.startsWith('-'));
180 var buffer = new StringBuffer();;
181 buffer.add(name);
182 if (option.type == 'bool') {
183 assert(option.values.empty());
184 } else {
185 name.startsWith('--') ? buffer.add('=') : buffer.add(' ');
Bill Hesse 2011/11/10 14:59:38 Factor the buffer.ȧdd() outside the conditional ex
Mads Ager (google) 2011/11/10 15:42:46 Yes. Done!
186 if (option.type == 'int') {
187 assert(option.values.empty());
188 buffer.add('n (default: ${option.defaultValue})');
189 } else {
190 buffer.add('[');
191 bool first = true;
192 for (var value in option.values) {
193 if (!first) buffer.add(", ");
194 if (value == option.defaultValue) buffer.add('*');
195 buffer.add(value);
196 first = false;
197 }
198 buffer.add(']');
199 }
200 }
201 print(buffer.toString());
202 }
203 print('');
204 }
205 }
206
207
208 /**
209 * Find the test option specification for a given option key.
210 */
211 _TestOptionSpecification _getSpecification(String name) {
212 for (var option in _options) {
213 if (option.keys.lastIndexOf(name) != -1) {
Bill Hesse 2011/11/10 14:59:38 or option.keys.some((key)-> key == name)
Mads Ager (google) 2011/11/10 15:42:46 Yeah, or indexOf. Really didn't mean lastIndexOf h
214 return option;
215 }
216 }
217 return null;
218 }
219
220
221 List<_TestOptionSpecification> _options;
222 }
223
OLDNEW
« no previous file with comments | « tools/test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698