Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
chore(symbol_inspector): Move and test the assert
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdeboer authored and [email protected] committed Apr 18, 2014
1 parent aae2c1f commit 39d4f42
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 59 deletions.
61 changes: 61 additions & 0 deletions lib/tools/symbol_inspector/symbol_inspector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,64 @@ getSymbolsFromLibrary(String libraryName) {

var _SYMBOL_NAME = new RegExp('"(.*)"');
unwrapSymbol(sym) => _SYMBOL_NAME.firstMatch(sym.toString()).group(1);

assertSymbolNamesAreOk(List<String> allowedNames, LibraryInfo libraryInfo) {
var _nameMap = {};
var _qualifiedNameMap = {};

allowedNames.forEach((x) => _nameMap[x] = true);

libraryInfo.names.forEach((x) => _qualifiedNameMap[x.qualified] = true);

var usedButNotExported = {};
var exported = [];


libraryInfo.names.forEach((nameInfo) {
String name = unwrapSymbol(nameInfo.qualified);
String libName = unwrapSymbol(nameInfo.libraryName);

var key = "$name";
if (_nameMap.containsKey(key)) {
_nameMap[key] = false;

// Check that all the exposed types are also exported
assert(libraryInfo.symbolsUsedForName.containsKey(nameInfo.qualified));
libraryInfo.symbolsUsedForName[nameInfo.qualified].forEach((usedSymbol) {
if ("$usedSymbol".contains('"dart.')) return;
if ("$usedSymbol" == 'Symbol("dynamic")') return;
if ("$usedSymbol" == 'Symbol("void")') return;

if (!_qualifiedNameMap.containsKey(usedSymbol)) {
usedButNotExported.putIfAbsent(usedSymbol, () => []);
usedButNotExported[usedSymbol].add(nameInfo.qualified);
}
});
return;
}

exported.add(key);
});
if (exported.isNotEmpty) {
throw "These symbols are exported thru the angular library, but it shouldn't be:\n"
"${exported.join('\n')}";
}

bool needHeader = true;
usedButNotExported.forEach((used, locs) {
print(" ${unwrapSymbol(used)} : unexported, used from:");
locs.forEach((l) {
print(" ${unwrapSymbol(l)}");
});
print("");
});

// If there are keys that no longer need to be in the ALLOWED_NAMES list, complain.
var keys = [];
_nameMap.forEach((k,v) {
if (v) keys.add(k);
});
if (keys.isNotEmpty) {
throw "These whitelisted symbols are not used:\n${keys.join('\n')}";
}
}
60 changes: 1 addition & 59 deletions test/angular_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,65 +245,7 @@ main() {
"url_matcher.UrlMatcher"
];

var _nameMap = {};
var _qualifiedNameMap = {};

ALLOWED_NAMES.forEach((x) => _nameMap[x] = true);

names.forEach((x) => _qualifiedNameMap[x.qualified] = true);

var usedButNotExported = {};

var exported = [];
assertSymbolNameIsOk(QualifiedSymbol nameInfo) {
String name = unwrapSymbol(nameInfo.qualified);
String libName = unwrapSymbol(nameInfo.libraryName);

var key = "$name";
if (_nameMap.containsKey(key)) {
_nameMap[key] = false;

// Check that all the exposed types are also exported
assert(libraryInfo.symbolsUsedForName.containsKey(nameInfo.qualified));
libraryInfo.symbolsUsedForName[nameInfo.qualified].forEach((usedSymbol) {
if ("$usedSymbol".contains('"dart.')) return;
if ("$usedSymbol" == 'Symbol("dynamic")') return;
if ("$usedSymbol" == 'Symbol("void")') return;

if (!_qualifiedNameMap.containsKey(usedSymbol)) {
usedButNotExported.putIfAbsent(usedSymbol, () => []);
usedButNotExported[usedSymbol].add(nameInfo.qualified);
}
});
return;
}

exported.add(key);
};

names.forEach(assertSymbolNameIsOk);

if (exported.isNotEmpty) {
throw "These symbols are exported thru the angular library, but it shouldn't be:\n"
"${exported.join('\n')}";
}

usedButNotExported.forEach((used, locs) {
print("${unwrapSymbol(used)} : unexported, used from:");
locs.forEach((l) {
print(" ${unwrapSymbol(l)}");
});
print("");
});

// If there are keys that no longer need to be in the ALLOWED_NAMES list, complain.
var keys = [];
_nameMap.forEach((k,v) {
if (v) keys.add(k);
});
if (keys.isNotEmpty) {
throw "Missing symbols:\n${keys.join('\n')}";
}
assertSymbolNamesAreOk(ALLOWED_NAMES, libraryInfo);
});
});
}
Expand Down
35 changes: 35 additions & 0 deletions test/tools/symbol_inspector/symbol_inspector_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,39 @@ void main() => describe('symbol inspector', () {
'simple_library.Generic'
]));
});

describe('assert', () {
var SIMPLE_LIBRARY_SYMBOLS = [
"simple_library.ClosureParam",
"simple_library.StaticFieldType",
"simple_library.FieldType",
"simple_library.ConsParamType",
"simple_library.A",
"simple_library.TypedefType",
"simple_library.MethodReturnType",
"simple_library.TypedefReturnType",
"simple_library.GetterType",
"simple_library.ParamType",
"simple_library.Generic",
"simple_library.ClosureReturn",
"simple_library.TypedefParam"
];
it('should assert symbol names are correct', () {
assertSymbolNamesAreOk(SIMPLE_LIBRARY_SYMBOLS,
getSymbolsFromLibrary(("simple_library")));
});

it('should throw if the list is missing symbols', () {
expect(() => assertSymbolNamesAreOk([],
getSymbolsFromLibrary(("simple_library"))),
throwsA(contains("These symbols are exported thru the angular "
"library, but it shouldn't be")));
});

it('should throw if the list has unused symbols', () {
expect(() => assertSymbolNamesAreOk(SIMPLE_LIBRARY_SYMBOLS..add('hello'),
getSymbolsFromLibrary(("simple_library"))),
throwsA(contains("These whitelisted symbols are not used")));
});
});
});

0 comments on commit 39d4f42

Please sign in to comment.