|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:test/test.dart'; |
| 8 | +import 'test_fixtures/test_templates.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + Directory? tempDir; |
| 12 | + String testPath() => tempDir!.path; |
| 13 | + |
| 14 | + group('gen_defaults templates', () { |
| 15 | + setUp(() { |
| 16 | + tempDir = Directory.systemTemp.createTempSync('gen_defaults'); |
| 17 | + }); |
| 18 | + |
| 19 | + tearDown(() { |
| 20 | + tempDir!.deleteSync(recursive: true); |
| 21 | + }); |
| 22 | + |
| 23 | + test('will generate a part file ending in _defaults.g.dart', () { |
| 24 | + final template = ButtonTemplate(testPath()); |
| 25 | + template.generateFile(verbose: true); |
| 26 | + |
| 27 | + final file = File('${testPath()}/button_defaults.g.dart'); |
| 28 | + expect(file.existsSync(), isTrue); |
| 29 | + }); |
| 30 | + |
| 31 | + test('will generate a file with the correct header text', () { |
| 32 | + final template = ButtonTemplate(testPath()); |
| 33 | + template.generateFile(); |
| 34 | + |
| 35 | + final file = File('${testPath()}/button_defaults.g.dart'); |
| 36 | + final String fileContents = file.readAsStringSync(); |
| 37 | + expect(fileContents, contains(_fileHeader)); |
| 38 | + }); |
| 39 | + |
| 40 | + test('will generate a file with the expected contents', () { |
| 41 | + final template = ButtonTemplate(testPath()); |
| 42 | + template.generateFile(); |
| 43 | + |
| 44 | + final file = File('${testPath()}/button_defaults.g.dart'); |
| 45 | + final String fileContents = file.readAsStringSync(); |
| 46 | + expect(fileContents, contains(_buttonDefaultsClass)); |
| 47 | + }); |
| 48 | + |
| 49 | + test('will completely overwrite any previous code', () { |
| 50 | + final file = File('${testPath()}/button_defaults.g.dart'); |
| 51 | + const randomText = 'Pre-existing random text.'; |
| 52 | + file.writeAsStringSync(randomText); |
| 53 | + |
| 54 | + final template = ButtonTemplate(testPath()); |
| 55 | + template.generateFile(); |
| 56 | + final String fileContents = file.readAsStringSync(); |
| 57 | + expect(fileContents, isNot(contains(randomText))); |
| 58 | + expect(fileContents, contains(_buttonDefaultsClass)); |
| 59 | + }); |
| 60 | + |
| 61 | + test('will run dart format over the generated file', () { |
| 62 | + final template = UnformattedTemplate(testPath()); |
| 63 | + template.generateFile(); |
| 64 | + |
| 65 | + final file = File('${testPath()}/unformatted_defaults.g.dart'); |
| 66 | + expect(file.readAsStringSync(), contains(formattedClass)); |
| 67 | + }); |
| 68 | + |
| 69 | + test('materialLib path resolves correctly based on MaterialVersion', () { |
| 70 | + final m3Template = TestM3Template(); |
| 71 | + final m3ExpressiveTemplate = TestM3ExpressiveTemplate(); |
| 72 | + const materialUiDir = 'packages/material_ui'; |
| 73 | + const generatedDir = 'lib/src/generated'; |
| 74 | + |
| 75 | + final bool hasPackageDir = Directory(materialUiDir).existsSync(); |
| 76 | + if (hasPackageDir) { |
| 77 | + expect(m3Template.materialLib, '$materialUiDir/$generatedDir'); |
| 78 | + expect( |
| 79 | + m3ExpressiveTemplate.materialLib, |
| 80 | + '$materialUiDir/$generatedDir/material_3_expressive', |
| 81 | + ); |
| 82 | + } else { |
| 83 | + expect(m3Template.materialLib, generatedDir); |
| 84 | + expect(m3ExpressiveTemplate.materialLib, '$generatedDir/material_3_expressive'); |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +const _fileHeader = ''' |
| 91 | +// Copyright 2013 The Flutter Authors |
| 92 | +// Use of this source code is governed by a BSD-style license that can be |
| 93 | +// found in the LICENSE file. |
| 94 | +
|
| 95 | +// Do not edit by hand. The code is generated from data in the Material |
| 96 | +// Design token database by the script: |
| 97 | +// packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart. |
| 98 | +'''; |
| 99 | + |
| 100 | +const _buttonDefaultsClass = ''' |
| 101 | +class _ButtonDefaults { |
| 102 | + static const double height = 40.0; |
| 103 | + static const double borderRadius = 8.0; |
| 104 | +} |
| 105 | +'''; |
| 106 | + |
| 107 | +const formattedClass = ''' |
| 108 | +class UnformattedClass { |
| 109 | + final int x = 1; |
| 110 | + final String y = 'hello'; |
| 111 | +} |
| 112 | +'''; |
0 commit comments