Relax the warning of unavailable tokens in gen_defaults when a default value is provided (#140872)
Fixes some false positives and false negatives from the `gen_defaults` tool regarding unavailable tokens in the Material spec. Fixes #140871 # Before  # After  These new issues that are logged are being fixed in https://github.com/flutter/flutter/pull/140573 --- *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
This commit is contained in:
@@ -325,10 +325,69 @@ static final String tokenBar = 'foo';
|
||||
logger.log('foobar');
|
||||
logger.printTokensUsage(verbose: true);
|
||||
|
||||
expect(printLog, contains(errorColoredString('Token unavailable: baz')));
|
||||
expect(printLog, contains(errorColoredString('Token unavailable: foobar')));
|
||||
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(() {
|
||||
@@ -369,3 +428,22 @@ 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')}';
|
||||
''';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user