diff --git a/packages/tiled/lib/src/parser.dart b/packages/tiled/lib/src/parser.dart index 08f8204..59a3861 100644 --- a/packages/tiled/lib/src/parser.dart +++ b/packages/tiled/lib/src/parser.dart @@ -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 { diff --git a/packages/tiled/test/image_layer_test.dart b/packages/tiled/test/image_layer_test.dart index b36a0ff..6383cfc 100644 --- a/packages/tiled/test/image_layer_test.dart +++ b/packages/tiled/test/image_layer_test.dart @@ -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(''' + + + + +'''); + 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); + }); } diff --git a/packages/tiled/test/parser_test.dart b/packages/tiled/test/parser_test.dart index 720cbf1..6e6dab4 100644 --- a/packages/tiled/test/parser_test.dart +++ b/packages/tiled/test/parser_test.dart @@ -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 = ''' + + + +'''; + expect( + () => TiledMap.parseTmx(missingWidth), + throwsA( + isA().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)));