Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/tiled/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class ParsingException implements Exception {
final String reason;

ParsingException(this.name, this.valueFound, this.reason);

@override
String toString() {
final found = valueFound == null ? '' : ', found: "$valueFound"';
return 'ParsingException: $reason (field: "$name"$found)';
}
}

class XmlParser extends Parser {
Expand Down
33 changes: 33 additions & 0 deletions packages/tiled/test/image_layer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,37 @@ void main() {
expect(layer.image.width, equals(64));
expect(layer.image.height, equals(32));
});

test('parses an image layer without an image from xml', () {
final map = TiledMap.parseTmx('''
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal"
renderorder="right-down" width="1" height="1" tilewidth="16" tileheight="16"
infinite="0" nextlayerid="4" nextobjectid="1">
<imagelayer id="3" name="Image Layer 1"/>
</map>
''');
final layer = map.layerByName('Image Layer 1') as ImageLayer;
expect(layer.image.source, isNull);
expect(layer.image.width, isNull);
expect(layer.image.height, isNull);
});

test('parses an image layer without an image from json', () {
final map = TiledMap.parseJson('''
{
"height":1, "width":1, "tileheight":16, "tilewidth":16,
"orientation":"orthogonal", "renderorder":"right-down", "version":"1.8",
"layers":[
{
"type":"imagelayer", "id":1, "name":"Background", "opacity":1,
"visible":true, "x":0, "y":0
}
],
"tilesets":[]
}
''');
final layer = map.layerByName('Background') as ImageLayer;
expect(layer.image.source, isNull);
});
}
43 changes: 43 additions & 0 deletions packages/tiled/test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,49 @@ void main() {
);
});

group('ParsingException', () {
test('has a descriptive message', () {
final exception = ParsingException(
'image',
null,
'Required child missing',
);
expect(
exception.toString(),
equals('ParsingException: Required child missing (field: "image")'),
);
});

test('includes the found value in the message', () {
final exception = ParsingException('width', 'abc', 'Not an integer');
expect(
exception.toString(),
equals(
'ParsingException: Not an integer (field: "width", found: "abc")',
),
);
});

test('is thrown with a descriptive message for a missing field', () {
const missingWidth = '''
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" orientation="orthogonal" renderorder="right-down"
height="1" tilewidth="16" tileheight="16" infinite="0">
</map>
''';
expect(
() => TiledMap.parseTmx(missingWidth),
throwsA(
isA<ParsingException>().having(
(e) => e.toString(),
'toString',
contains('field: "width"'),
),
),
);
});
});

group('Parser.parse returns a populated Map that', () {
test('has its tileWidth = 32', () => expect(map.tileWidth, equals(32)));
test('has its tileHeight = 32', () => expect(map.tileHeight, equals(32)));
Expand Down