File size: 16,739 Bytes
83fa459
 
 
 
b7f6949
 
 
 
 
 
 
83fa459
 
 
 
 
 
 
 
 
 
b7f6949
 
83fa459
 
b7f6949
 
 
 
 
 
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
 
 
 
 
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
 
b7f6949
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
 
b7f6949
83fa459
 
 
 
 
 
b7f6949
 
 
 
 
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
 
b7f6949
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
 
 
 
 
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
 
b7f6949
83fa459
 
 
 
 
 
 
b7f6949
 
 
 
 
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
 
b7f6949
83fa459
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
 
b7f6949
83fa459
 
 
 
 
 
 
b7f6949
 
 
 
 
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
 
b7f6949
83fa459
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
 
b7f6949
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
 
b7f6949
 
 
 
83fa459
 
 
 
 
 
 
 
 
 
 
b7f6949
83fa459
b7f6949
83fa459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f6949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83fa459
 
 
 
 
 
 
 
 
 
 
 
b7f6949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// TODO(davidmartos96): Remove this tag once this test's state leaks/test
// dependencies have been fixed.
// https://github.com/flutter/flutter/issues/142716
// Fails with "flutter test --test-randomize-ordering-seed=20240201"
@Tags(<String>['no-shuffle'])
library;

import 'dart:async';
import 'dart:io';

import 'package:gen_defaults/template.dart';
import 'package:gen_defaults/token_logger.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

void main() {
  final TokenLogger logger = tokenLogger;
  // Required init with empty at least once to init late fields.
  // Then we can use the `clear` method.
  logger.init(allTokens: <String, dynamic>{}, versionMap: <String, List<String>>{});

  setUp(() {
    // Cleanup the global token logger before each test, to not be tied to a particular
    // test order.
    logger.clear();
  });

  test('Templates will append to the end of a file', () {
    final Directory tempDir = Directory.systemTemp.createTempSync('gen_defaults');
    try {
      // Create a temporary file with some content.
      final File tempFile = File(path.join(tempDir.path, 'test_template.txt'));
      tempFile.createSync();
      tempFile.writeAsStringSync('''
// This is a file with stuff in it.
// This part shouldn't be changed by
// the template.
''');

      // Have a test template append new parameterized content to the end of
      // the file.
      final Map<String, dynamic> tokens = <String, dynamic>{
        'version': '0.0',
        'foo': 'Foobar',
        'bar': 'Barfoo',
      };
      TestTemplate('Test', tempFile.path, tokens).updateFile();

      expect(tempFile.readAsStringSync(), '''
// This is a file with stuff in it.
// This part shouldn't be changed by
// the template.

// BEGIN GENERATED TOKEN PROPERTIES - Test

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
//   dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
static final String tokenFoo = 'Foobar';
static final String tokenBar = 'Barfoo';
// dart format on

// END GENERATED TOKEN PROPERTIES - Test
''');
    } finally {
      tempDir.deleteSync(recursive: true);
    }
  });

  test('Templates will update over previously generated code at the end of a file', () {
    final Directory tempDir = Directory.systemTemp.createTempSync('gen_defaults');
    try {
      // Create a temporary file with some content.
      final File tempFile = File(path.join(tempDir.path, 'test_template.txt'));
      tempFile.createSync();
      tempFile.writeAsStringSync('''
// This is a file with stuff in it.
// This part shouldn't be changed by
// the template.

// BEGIN GENERATED TOKEN PROPERTIES - Test

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
//   dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
static final String tokenFoo = 'Foobar';
static final String tokenBar = 'Barfoo';
// dart format on

// END GENERATED TOKEN PROPERTIES - Test
''');

      // Have a test template append new parameterized content to the end of
      // the file.
      final Map<String, dynamic> tokens = <String, dynamic>{
        'version': '0.0',
        'foo': 'foo',
        'bar': 'bar',
      };
      TestTemplate('Test', tempFile.path, tokens).updateFile();

      expect(tempFile.readAsStringSync(), '''
// This is a file with stuff in it.
// This part shouldn't be changed by
// the template.

// BEGIN GENERATED TOKEN PROPERTIES - Test

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
//   dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
static final String tokenFoo = 'foo';
static final String tokenBar = 'bar';
// dart format on

// END GENERATED TOKEN PROPERTIES - Test
''');
    } finally {
      tempDir.deleteSync(recursive: true);
    }
  });

  test('Multiple templates can modify different code blocks in the same file', () {
    final Directory tempDir = Directory.systemTemp.createTempSync('gen_defaults');
    try {
      // Create a temporary file with some content.
      final File tempFile = File(path.join(tempDir.path, 'test_template.txt'));
      tempFile.createSync();
      tempFile.writeAsStringSync('''
// This is a file with stuff in it.
// This part shouldn't be changed by
// the template.
''');

      // Update file with a template for 'Block 1'
      {
        final Map<String, dynamic> tokens = <String, dynamic>{
          'version': '0.0',
          'foo': 'foo',
          'bar': 'bar',
        };
        TestTemplate('Block 1', tempFile.path, tokens).updateFile();
      }
      expect(tempFile.readAsStringSync(), '''
// This is a file with stuff in it.
// This part shouldn't be changed by
// the template.

// BEGIN GENERATED TOKEN PROPERTIES - Block 1

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
//   dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
static final String tokenFoo = 'foo';
static final String tokenBar = 'bar';
// dart format on

// END GENERATED TOKEN PROPERTIES - Block 1
''');

      // Update file with a template for 'Block 2', which should append but not
      // disturb the code in 'Block 1'.
      {
        final Map<String, dynamic> tokens = <String, dynamic>{
          'version': '0.0',
          'foo': 'bar',
          'bar': 'foo',
        };
        TestTemplate('Block 2', tempFile.path, tokens).updateFile();
      }
      expect(tempFile.readAsStringSync(), '''
// This is a file with stuff in it.
// This part shouldn't be changed by
// the template.

// BEGIN GENERATED TOKEN PROPERTIES - Block 1

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
//   dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
static final String tokenFoo = 'foo';
static final String tokenBar = 'bar';
// dart format on

// END GENERATED TOKEN PROPERTIES - Block 1

// BEGIN GENERATED TOKEN PROPERTIES - Block 2

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
//   dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
static final String tokenFoo = 'bar';
static final String tokenBar = 'foo';
// dart format on

// END GENERATED TOKEN PROPERTIES - Block 2
''');

      // Update 'Block 1' again which should just update that block,
      // leaving 'Block 2' undisturbed.
      {
        final Map<String, dynamic> tokens = <String, dynamic>{
          'version': '0.0',
          'foo': 'FOO',
          'bar': 'BAR',
        };
        TestTemplate('Block 1', tempFile.path, tokens).updateFile();
      }
      expect(tempFile.readAsStringSync(), '''
// This is a file with stuff in it.
// This part shouldn't be changed by
// the template.

// BEGIN GENERATED TOKEN PROPERTIES - Block 1

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
//   dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
static final String tokenFoo = 'FOO';
static final String tokenBar = 'BAR';
// dart format on

// END GENERATED TOKEN PROPERTIES - Block 1

// BEGIN GENERATED TOKEN PROPERTIES - Block 2

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
//   dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
static final String tokenFoo = 'bar';
static final String tokenBar = 'foo';
// dart format on

// END GENERATED TOKEN PROPERTIES - Block 2
''');
    } finally {
      tempDir.deleteSync(recursive: true);
    }
  });

  test('Templates can get proper shapes from given data', () {
    const Map<String, dynamic> tokens = <String, dynamic>{
      'foo.shape': 'shape.large',
      'bar.shape': 'shape.full',
      'shape.large': <String, dynamic>{
        'family': 'SHAPE_FAMILY_ROUNDED_CORNERS',
        'topLeft': 1.0,
        'topRight': 2.0,
        'bottomLeft': 3.0,
        'bottomRight': 4.0,
      },
      'shape.full': <String, dynamic>{'family': 'SHAPE_FAMILY_CIRCULAR'},
    };
    final TestTemplate template = TestTemplate('Test', 'foobar.dart', tokens);
    expect(
      template.shape('foo'),
      'const RoundedRectangleBorder(borderRadius: BorderRadius.only(topLeft: Radius.circular(1.0), topRight: Radius.circular(2.0), bottomLeft: Radius.circular(3.0), bottomRight: Radius.circular(4.0)))',
    );
    expect(template.shape('bar'), 'const StadiumBorder()');
  });

  group('Tokens logger', () {
    final List<String> printLog = List<String>.empty(growable: true);
    final Map<String, List<String>> versionMap = <String, List<String>>{};
    final Map<String, dynamic> allTokens = <String, dynamic>{};

    // Add to printLog instead of printing to stdout
    void Function() overridePrint(void Function() testFn) => () {
      final ZoneSpecification spec = ZoneSpecification(
        print: (_, _, _, String msg) {
          printLog.add(msg);
        },
      );
      return Zone.current.fork(specification: spec).run<void>(testFn);
    };

    setUp(() {
      logger.init(allTokens: allTokens, versionMap: versionMap);
    });

    tearDown(() {
      logger.clear();
      printLog.clear();
      versionMap.clear();
      allTokens.clear();
    });

    String errorColoredString(String str) => '\x1B[31m$str\x1B[0m';

    const Map<String, List<String>> testVersions = <String, List<String>>{
      'v1.0.0': <String>['file_1.json'],
      'v2.0.0': <String>['file_2.json, file_3.json'],
    };

    test(
      'can print empty usage',
      overridePrint(() {
        logger.printVersionUsage(verbose: true);
        expect(printLog, contains('Versions used: '));

        logger.printTokensUsage(verbose: true);
        expect(printLog, contains('Tokens used: 0/0'));
      }),
    );

    test(
      'can print version usage',
      overridePrint(() {
        versionMap.addAll(testVersions);

        logger.printVersionUsage(verbose: false);

        expect(printLog, contains('Versions used: v1.0.0, v2.0.0'));
      }),
    );

    test(
      'can print version usage (verbose)',
      overridePrint(() {
        versionMap.addAll(testVersions);

        logger.printVersionUsage(verbose: true);

        expect(printLog, contains('Versions used: v1.0.0, v2.0.0'));
        expect(printLog, contains('  v1.0.0:'));
        expect(printLog, contains('    file_1.json'));
        expect(printLog, contains('  v2.0.0:'));
        expect(printLog, contains('    file_2.json, file_3.json'));
      }),
    );

    test(
      'can log and print tokens usage',
      overridePrint(() {
        allTokens['foo'] = 'value';

        logger.log('foo');
        logger.printTokensUsage(verbose: false);

        expect(printLog, contains('Tokens used: 1/1'));
      }),
    );

    test(
      'can log and print tokens usage (verbose)',
      overridePrint(() {
        allTokens['foo'] = 'value';

        logger.log('foo');
        logger.printTokensUsage(verbose: true);

        expect(printLog, contains('βœ… foo'));
        expect(printLog, contains('Tokens used: 1/1'));
      }),
    );

    test(
      'detects invalid logs',
      overridePrint(() {
        allTokens['foo'] = 'value';

        logger.log('baz');
        logger.log('foobar');
        logger.printTokensUsage(verbose: true);

        expect(printLog, contains('❌ foo'));
        expect(printLog, contains('Tokens used: 0/1'));
        expect(printLog, contains(errorColoredString('Some referenced tokens do not exist: 2')));
        expect(printLog, contains('  baz'));
        expect(printLog, contains('  foobar'));
      }),
    );

    test(
      "color function doesn't log when providing a default",
      overridePrint(() {
        allTokens['color_foo_req'] = 'value';

        // color_foo_opt is not available, but because it has a default value, it won't warn about it

        TestColorTemplate('block', 'filename', allTokens).generate();
        logger.printTokensUsage(verbose: true);

        expect(printLog, contains('βœ… color_foo_req'));
        expect(printLog, contains('Tokens used: 1/1'));
      }),
    );

    test(
      'color function logs when not providing a default',
      overridePrint(() {
        // Nor color_foo_req or color_foo_opt are available, but only color_foo_req will be logged.
        // This mimics a token being removed, but expected to exist.

        TestColorTemplate('block', 'filename', allTokens).generate();
        logger.printTokensUsage(verbose: true);

        expect(printLog, contains('Tokens used: 0/0'));
        expect(printLog, contains(errorColoredString('Some referenced tokens do not exist: 1')));
        expect(printLog, contains('  color_foo_req'));
      }),
    );

    test(
      'border function logs width token when available',
      overridePrint(() {
        allTokens['border_foo.color'] = 'red';
        allTokens['border_foo.width'] = 3.0;

        TestBorderTemplate('block', 'filename', allTokens).generate();
        logger.printTokensUsage(verbose: true);

        expect(printLog, contains('βœ… border_foo.color'));
        expect(printLog, contains('βœ… border_foo.width'));
        expect(printLog, contains('Tokens used: 2/2'));
      }),
    );

    test(
      'border function logs height token when width token not available',
      overridePrint(() {
        allTokens['border_foo.color'] = 'red';
        allTokens['border_foo.height'] = 3.0;

        TestBorderTemplate('block', 'filename', allTokens).generate();
        logger.printTokensUsage(verbose: true);

        expect(printLog, contains('βœ… border_foo.color'));
        expect(printLog, contains('βœ… border_foo.height'));
        expect(printLog, contains('Tokens used: 2/2'));
      }),
    );

    test(
      "border function doesn't log when width or height tokens not available",
      overridePrint(() {
        allTokens['border_foo.color'] = 'red';

        TestBorderTemplate('block', 'filename', allTokens).generate();
        logger.printTokensUsage(verbose: true);

        expect(printLog, contains('βœ… border_foo.color'));
        expect(printLog, contains('Tokens used: 1/1'));
      }),
    );

    test(
      'can log and dump versions & tokens to a file',
      overridePrint(() {
        versionMap.addAll(testVersions);
        allTokens['foo'] = 'value';
        allTokens['bar'] = 'value';

        logger.log('foo');
        logger.log('bar');
        logger.dumpToFile('test.json');

        final String fileContent = File('test.json').readAsStringSync();
        expect(fileContent, contains('Versions used, v1.0.0, v2.0.0'));
        expect(fileContent, contains('bar,'));
        expect(fileContent, contains('foo'));
      }),
    );

    test(
      'integration test',
      overridePrint(() {
        allTokens['foo'] = 'value';
        allTokens['bar'] = 'value';

        TestTemplate('block', 'filename', allTokens).generate();
        logger.printTokensUsage(verbose: true);

        expect(printLog, contains('βœ… foo'));
        expect(printLog, contains('βœ… bar'));
        expect(printLog, contains('Tokens used: 2/2'));
      }),
    );
  });
}

class TestTemplate extends TokenTemplate {
  TestTemplate(super.blockName, super.fileName, super.tokens);

  @override
  String generate() => '''
static final String tokenFoo = '${getToken('foo')}';
static final String tokenBar = '${getToken('bar')}';
''';
}

class TestColorTemplate extends TokenTemplate {
  TestColorTemplate(super.blockName, super.fileName, super.tokens);

  @override
  String generate() => '''
static final Color color_1 = '${color('color_foo_req')}';
static final Color color_2 = '${color('color_foo_opt', 'Colors.red')}';
''';
}

class TestBorderTemplate extends TokenTemplate {
  TestBorderTemplate(super.blockName, super.fileName, super.tokens);

  @override
  String generate() => '''
static final BorderSide border = '${border('border_foo')}';
''';
}