@@ -19,6 +19,7 @@ import { ExclusiveTestFunction, PendingTestFunction } from 'mocha';
19
19
20
20
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence' ;
21
21
import { assert } from '../../../src/util/assert' ;
22
+ import { primitiveComparator } from '../../../src/util/misc' ;
22
23
import { addEqualityMatcher } from '../../util/equality_matcher' ;
23
24
24
25
import { SpecBuilder } from './spec_builder' ;
@@ -247,7 +248,10 @@ export function describeSpec(
247
248
// Note: We use json-stable-stringify instead of JSON.stringify() to ensure
248
249
// that the generated JSON does not produce diffs merely due to the order
249
250
// of the keys in an object changing.
250
- const output = stringify ( specsInThisTest , { space : 2 , cmp : stringifyCmp } ) ;
251
+ const output = stringify ( specsInThisTest , {
252
+ space : 2 ,
253
+ cmp : stringifyComparator
254
+ } ) ;
251
255
writeJSONFile ( output ) ;
252
256
}
253
257
}
@@ -258,6 +262,7 @@ export function describeSpec(
258
262
* ordered in the generated JSON in the same relative order of this array.
259
263
*/
260
264
const stringifyCustomOrdering = [
265
+ 'comment' ,
261
266
'describeName' ,
262
267
'itName' ,
263
268
'tags' ,
@@ -266,42 +271,39 @@ const stringifyCustomOrdering = [
266
271
] ;
267
272
268
273
/**
269
- * A comparator function for stringify() that sorts keys in the JSON output .
274
+ * Assigns a "group number" to the given key in the generated JSON .
270
275
*
271
- * In order to produce semi-readable JSON that has an intuitive top-level key
272
- * arrangement, some keys are sorted non-alphabetically; namely, the ordering
273
- * defined in `stringifyCustomOrdering` is used, falling back to alphabetical
274
- * ordering if one or both of the key are not overridden.
276
+ * Keys with lower group numbers should be ordered before keys with greater
277
+ * group numbers. Keys with equal group numbers should be ordered
278
+ * alphabetically.
275
279
*/
276
- function stringifyCmp ( a : stringify . Element , b : stringify . Element ) : number {
277
- // If the keys are equal, then avoid the costly computations below.
278
- if ( a . key === b . key ) {
279
- return 0 ;
280
- }
281
-
282
- // If the keys have a custom ordering, then use it.
283
- const aIndex = stringifyCustomOrdering . indexOf ( a . key ) ;
284
- const bIndex = stringifyCustomOrdering . indexOf ( b . key ) ;
285
- if ( aIndex >= 0 && bIndex >= 0 ) {
286
- return aIndex - bIndex ;
287
- }
288
-
289
- // Order "expected" blocks after other blocks since the expectations logically
290
- // occur after the steps are executed.
291
- if ( a . key . startsWith ( 'expected' ) ) {
292
- if ( ! b . key . startsWith ( 'expected' ) ) {
293
- return 1 ;
294
- }
295
- } else if ( b . key . startsWith ( 'expected' ) ) {
296
- return - 1 ;
280
+ function stringifyGroup ( s : string ) : number {
281
+ const index = stringifyCustomOrdering . indexOf ( s ) ;
282
+ if ( index >= 0 ) {
283
+ return index ;
284
+ } else if ( ! s . startsWith ( 'expected' ) ) {
285
+ return stringifyCustomOrdering . length ;
286
+ } else {
287
+ return stringifyCustomOrdering . length + 1 ;
297
288
}
289
+ }
298
290
299
- // If all else fails, order alphabetically.
300
- if ( a . key < b . key ) {
301
- return - 1 ;
302
- } else if ( a . key > b . key ) {
303
- return 1 ;
291
+ /**
292
+ * A comparator function for stringify() that sorts keys in the JSON output.
293
+ *
294
+ * In order to produce JSON that has somewhat-intuitively-ordered keys, this
295
+ * comparator intentionally deviates from pure alphabetic ordering, placing
296
+ * some logically-first keys before others.
297
+ */
298
+ function stringifyComparator (
299
+ a : stringify . Element ,
300
+ b : stringify . Element
301
+ ) : number {
302
+ const aGroup = stringifyGroup ( a . key ) ;
303
+ const bGroup = stringifyGroup ( b . key ) ;
304
+ if ( aGroup === bGroup ) {
305
+ return primitiveComparator ( a . key , b . key ) ;
304
306
} else {
305
- return 0 ;
307
+ return primitiveComparator ( aGroup , bGroup ) ;
306
308
}
307
309
}
0 commit comments