|
5 | 5 | /// Unit tests for lib/src/experiment_options.dart.
|
6 | 6 | library dartdoc.experiment_options_test;
|
7 | 7 |
|
| 8 | +import 'dart:cli'; |
8 | 9 | import 'dart:io';
|
9 | 10 |
|
10 | 11 | import 'package:analyzer/src/dart/analysis/experiments.dart';
|
| 12 | +import 'package:analyzer/src/dart/analysis/experiments_impl.dart'; |
11 | 13 | import 'package:dartdoc/src/dartdoc_options.dart';
|
12 | 14 | import 'package:dartdoc/src/experiment_options.dart';
|
13 | 15 | import 'package:test/test.dart';
|
14 | 16 |
|
15 | 17 | void main() {
|
16 |
| - DartdocOptionSet experimentOptions; |
17 | 18 | Directory emptyTempDir;
|
18 | 19 | ExperimentalFeature defaultOnNotExpired, defaultOffNotExpired;
|
19 | 20 | ExperimentalFeature defaultOnExpired, defaultOffExpired;
|
20 | 21 |
|
21 |
| - setUp(() async { |
22 |
| - experimentOptions = await DartdocOptionSet.fromOptionGenerators( |
23 |
| - 'dartdoc', [createExperimentOptions]); |
24 |
| - }); |
| 22 | + void withSyntheticExperimentalFeatures( |
| 23 | + void Function() operation, |
| 24 | + ) { |
| 25 | + defaultOnNotExpired = ExperimentalFeature( |
| 26 | + index: 0, |
| 27 | + enableString: 'a', |
| 28 | + isEnabledByDefault: true, |
| 29 | + isExpired: false, |
| 30 | + documentation: 'a', |
| 31 | + firstSupportedVersion: '1.0.0', |
| 32 | + ); |
| 33 | + defaultOffNotExpired = ExperimentalFeature( |
| 34 | + index: 1, |
| 35 | + enableString: 'b', |
| 36 | + isEnabledByDefault: false, |
| 37 | + isExpired: false, |
| 38 | + documentation: 'b', |
| 39 | + firstSupportedVersion: null, |
| 40 | + ); |
| 41 | + defaultOnExpired = ExperimentalFeature( |
| 42 | + index: 2, |
| 43 | + enableString: 'c', |
| 44 | + isEnabledByDefault: true, |
| 45 | + isExpired: true, |
| 46 | + documentation: 'c', |
| 47 | + firstSupportedVersion: '1.0.0', |
| 48 | + ); |
| 49 | + defaultOffExpired = ExperimentalFeature( |
| 50 | + index: 3, |
| 51 | + enableString: 'd', |
| 52 | + isEnabledByDefault: false, |
| 53 | + isExpired: true, |
| 54 | + documentation: 'd', |
| 55 | + firstSupportedVersion: null, |
| 56 | + ); |
| 57 | + |
| 58 | + overrideKnownFeatures( |
| 59 | + { |
| 60 | + 'a': defaultOnNotExpired, |
| 61 | + 'b': defaultOffNotExpired, |
| 62 | + 'c': defaultOnExpired, |
| 63 | + 'd': defaultOffExpired, |
| 64 | + }, |
| 65 | + () { |
| 66 | + operation(); |
| 67 | + }, |
| 68 | + ); |
| 69 | + } |
25 | 70 |
|
26 | 71 | setUpAll(() {
|
27 | 72 | emptyTempDir =
|
28 | 73 | Directory.systemTemp.createTempSync('experiment_options_test_empty');
|
29 |
| - // We don't test our functionality at all unless ExperimentStatus has at least |
30 |
| - // one of these. TODO(jcollins-g): make analyzer+dartdoc connection |
31 |
| - // more amenable to testing. |
32 |
| - defaultOnNotExpired = ExperimentStatus.knownFeatures.values.firstWhere( |
33 |
| - (f) => f.isEnabledByDefault && !f.isExpired, |
34 |
| - orElse: () => null); |
35 |
| - defaultOffNotExpired = ExperimentStatus.knownFeatures.values.firstWhere( |
36 |
| - (f) => !f.isEnabledByDefault && !f.isExpired, |
37 |
| - orElse: () => null); |
38 |
| - assert(defaultOnNotExpired != null || defaultOffNotExpired != null, |
39 |
| - 'No experimental options that are not expired found'); |
40 |
| - |
41 |
| - // The "bogus" entries should always exist. |
42 |
| - defaultOnExpired = ExperimentStatus.knownFeatures.values |
43 |
| - .firstWhere((f) => f.isEnabledByDefault && f.isExpired); |
44 |
| - defaultOffExpired = ExperimentStatus.knownFeatures.values |
45 |
| - .firstWhere((f) => !f.isEnabledByDefault && f.isExpired); |
46 | 74 | });
|
47 | 75 |
|
48 | 76 | tearDownAll(() {
|
49 | 77 | emptyTempDir.deleteSync(recursive: true);
|
50 | 78 | });
|
51 | 79 |
|
52 | 80 | group('Experimental options test', () {
|
| 81 | + void withExperimentOptions( |
| 82 | + void Function(DartdocOptionSet) operation, |
| 83 | + ) { |
| 84 | + withSyntheticExperimentalFeatures(() { |
| 85 | + // The enclosing function expects only synchronous function argument. |
| 86 | + // But `fromOptionGenerators` is asynchronous. |
| 87 | + // So, we have to use `waitFor` to adapt it. |
| 88 | + var experimentOptions = waitFor( |
| 89 | + DartdocOptionSet.fromOptionGenerators( |
| 90 | + 'dartdoc', |
| 91 | + [createExperimentOptions], |
| 92 | + ), |
| 93 | + ); |
| 94 | + operation(experimentOptions); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
53 | 98 | test('Defaults work for all options', () {
|
54 |
| - experimentOptions.parseArguments([]); |
55 |
| - var tester = DartdocOptionContext(experimentOptions, emptyTempDir); |
56 |
| - if (defaultOnNotExpired != null) { |
| 99 | + withExperimentOptions((experimentOptions) { |
| 100 | + experimentOptions.parseArguments([]); |
| 101 | + var tester = DartdocOptionContext(experimentOptions, emptyTempDir); |
57 | 102 | expect(tester.experimentStatus.isEnabled(defaultOnNotExpired), isTrue);
|
58 |
| - } |
59 |
| - if (defaultOffNotExpired != null) { |
60 | 103 | expect(
|
61 | 104 | tester.experimentStatus.isEnabled(defaultOffNotExpired), isFalse);
|
62 |
| - } |
63 |
| - expect(tester.experimentStatus.isEnabled(defaultOnExpired), isTrue); |
64 |
| - expect(tester.experimentStatus.isEnabled(defaultOffExpired), isFalse); |
| 105 | + expect(tester.experimentStatus.isEnabled(defaultOnExpired), isTrue); |
| 106 | + expect(tester.experimentStatus.isEnabled(defaultOffExpired), isFalse); |
| 107 | + }); |
65 | 108 | });
|
66 | 109 |
|
67 | 110 | test('Overriding defaults works via args', () {
|
68 |
| - // Set all arguments to non-default values. |
69 |
| - experimentOptions.parseArguments([ |
70 |
| - '--enable-experiment', |
71 |
| - '${defaultOffNotExpired?.disableString},${defaultOnNotExpired?.disableString},${defaultOnExpired.disableString},${defaultOffExpired.enableString}' |
72 |
| - ]); |
73 |
| - var tester = DartdocOptionContext(experimentOptions, emptyTempDir); |
74 |
| - if (defaultOnNotExpired != null) { |
| 111 | + withExperimentOptions((experimentOptions) { |
| 112 | + // Set all experiments to non-default values. |
| 113 | + experimentOptions.parseArguments([ |
| 114 | + '--enable-experiment', |
| 115 | + [ |
| 116 | + defaultOnNotExpired.disableString, |
| 117 | + defaultOffNotExpired.enableString, |
| 118 | + defaultOnExpired.disableString, |
| 119 | + defaultOffExpired.enableString, |
| 120 | + ].join(',') |
| 121 | + ]); |
| 122 | + var tester = DartdocOptionContext(experimentOptions, emptyTempDir); |
75 | 123 | expect(tester.experimentStatus.isEnabled(defaultOnNotExpired), isFalse);
|
76 |
| - } |
77 |
| - if (defaultOffNotExpired != null) { |
78 |
| - expect( |
79 |
| - tester.experimentStatus.isEnabled(defaultOffNotExpired), isFalse); |
80 |
| - } |
81 |
| - expect(tester.experimentStatus.isEnabled(defaultOnExpired), isTrue); |
82 |
| - expect(tester.experimentStatus.isEnabled(defaultOffExpired), isFalse); |
| 124 | + expect(tester.experimentStatus.isEnabled(defaultOffNotExpired), isTrue); |
| 125 | + expect(tester.experimentStatus.isEnabled(defaultOnExpired), isTrue); |
| 126 | + expect(tester.experimentStatus.isEnabled(defaultOffExpired), isFalse); |
| 127 | + }); |
83 | 128 | });
|
84 | 129 | });
|
85 | 130 | }
|
0 commit comments