Skip to content

Commit

Permalink
don't mix errors and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sethladd committed Feb 16, 2015
1 parent 056d47e commit d13824d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
49 changes: 35 additions & 14 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ import 'package_utils.dart';
List<Class> getAllImplementorsFor(Class c) =>
[new MockClass('SampleClass'), new MockClass('AnotherClass')];

bool _isClassErrorOrException(ClassElement element) {
// TODO can I get dart.core statically, without going through _library?
LibraryElement coreLib =
element.library.importedLibraries.firstWhere((i) => i.name == 'dart.core');
ClassElement exception = coreLib.getType('Exception');
ClassElement error = coreLib.getType('Error');

return element.type.isSubtypeOf(exception.type) ||
element.type.isSubtypeOf(error.type);
}

// TODO: remove once getAllImplementorsFor is implemented,
// ignore warning
// if only we had an @ignorewarnings!! :)
Expand All @@ -33,6 +44,8 @@ class MockClass implements Class {
String get linkedName => '<a href="${href}">$className</a>';
@override
String get href => 'library/$className.html';
@override
bool get hasMethods => true;
}

abstract class ModelElement {
Expand Down Expand Up @@ -365,8 +378,10 @@ class Package {
}

class Library extends ModelElement {

List<Variable> _variables;
Package package;
List<Class> _classes;

LibraryElement get _library => (element as LibraryElement);

Expand Down Expand Up @@ -445,28 +460,30 @@ class Library extends ModelElement {
}).toList();
}

// TODO: rename this to getClasses
List<Class> getTypes() {
List<Class> get allClasses {
if (_classes != null) return _classes;

List<ClassElement> types = [];
types.addAll(_library.definingCompilationUnit.types);
for (CompilationUnitElement cu in _library.parts) {
types.addAll(cu.types);
}
types
..removeWhere(isPrivate);
return types.map((e) => new Class(e, this, source)).toList();

_classes = types
.where(isPublic)
.map((e) => new Class(e, this, source)) // is source a bug? it's the library's source
.toList(growable: true);

return _classes;
}

// TODO: rename this to getClasses
List<Class> getTypes() {
return allClasses.where((c) => !c.isErrorOrException).toList(growable:false);
}

List<Class> getExceptions() {
LibraryElement coreLib =
_library.importedLibraries.firstWhere((i) => i.name == 'dart.core');
ClassElement exception = coreLib.getType('Exception');
ClassElement error = coreLib.getType('Error');
bool isExceptionOrError(Class t) {
return t._cls.type.isSubtypeOf(exception.type) ||
t._cls.type.isSubtypeOf(error.type);
}
return getTypes().where(isExceptionOrError).toList();
return allClasses.where((c) => c.isErrorOrException).toList(growable:false);
}

@override
Expand Down Expand Up @@ -510,6 +527,7 @@ class Class extends ModelElement {
var lib = new Library(f.element.library, p);
return new ElementType(f, new ModelElement.from(f.element, lib));
}).toList(growable: false);

}

bool get isAbstract => _cls.isAbstract;
Expand Down Expand Up @@ -571,8 +589,11 @@ class Class extends ModelElement {

bool get hasMethods => methods.isNotEmpty;

bool get isErrorOrException => _isClassErrorOrException(element);

@override
String get _href => '${library.name}/$name.html';

}

class ModelFunction extends ModelElement {
Expand Down
20 changes: 10 additions & 10 deletions test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main() {
});

test('libraries', () {
expect(p.libraries.length, 1);
expect(p.libraries, hasLength(1));
});

test('is documented', () {
Expand Down Expand Up @@ -85,7 +85,7 @@ void main() {
var D = classes[3];

test('no of classes', () {
expect(classes.length, 5);
expect(classes, hasLength(5));
});

test('name', () {
Expand All @@ -110,30 +110,30 @@ void main() {
});

test('mixins', () {
expect(A.mixins.length, 0);
expect(A.mixins, hasLength(0));
});

test('interfaces', () {
var interfaces = D.interfaces;
expect(interfaces.length, 2);
expect(interfaces, hasLength(2));
expect(interfaces[0].name, 'C');
expect(interfaces[1].name, 'E');
});

test('get constructors', () {
expect(A.constructors.length, 1);
expect(A.constructors, hasLength(1));
});

test('get static fields', () {
expect(A.getStaticFields().length, 2);
expect(A.getStaticFields(), hasLength(2));
});

test('get instance fields', () {
expect(A.getInstanceFields().length, 3);
expect(A.getInstanceFields(), hasLength(3));
});

test('get methods', () {
expect(B.methods.length, 1);
expect(B.methods, hasLength(1));
});
});

Expand Down Expand Up @@ -211,7 +211,7 @@ void main() {
});

test('found two properties', () {
expect(l.getProperties().length, 2);
expect(l.getProperties(), hasLength(2));
});

test('linked return type is a double', () {
Expand All @@ -231,7 +231,7 @@ void main() {
});

test('found one constant', () {
expect(l.getConstants().length, 1);
expect(l.getConstants(), hasLength(1));
});

test('is constant', () {
Expand Down

0 comments on commit d13824d

Please sign in to comment.