aboutsummaryrefslogtreecommitdiffstats
path: root/src/tmz_test.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/tmz_test.zig')
-rw-r--r--src/tmz_test.zig117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/tmz_test.zig b/src/tmz_test.zig
new file mode 100644
index 0000000..221a871
--- /dev/null
+++ b/src/tmz_test.zig
@@ -0,0 +1,117 @@
+const std = @import("std");
+const equals = std.testing.expectEqual;
+const stringEquals = std.testing.expectEqualStrings;
+const tmz = @import("./tmz.zig");
+const Layer = tmz.Layer;
+const Map = tmz.Map;
+
+const full_map = @embedFile("test/map.tmj");
+
+test "parseMap works" {
+ const parsed_map = try tmz.parseMap(std.testing.allocator, full_map);
+ defer parsed_map.deinit();
+
+ const map = parsed_map.value;
+ try regularMapTests(map);
+ try equals(false, map.infinite);
+}
+
+test "infinite map with base64 chunks" {
+ const infinite_map = @embedFile("test/map-infinite-base64-zstd.tmj");
+ const parsed_map = try tmz.parseMap(std.testing.allocator, infinite_map);
+ defer parsed_map.deinit();
+
+ const map = parsed_map.value;
+ try equals(true, map.infinite);
+}
+
+test "infinite map with csv chunks" {
+ const infinite_map = @embedFile("test/map-infinite-csv.tmj");
+ const parsed_map = try tmz.parseMap(std.testing.allocator, infinite_map);
+ defer parsed_map.deinit();
+
+ const map = parsed_map.value;
+ try equals(true, map.infinite);
+}
+
+test "parseTileset works" {
+ const tileset_data = @embedFile("test/tiles.tsj");
+ const parsed_tileset = try tmz.parseTileset(std.testing.allocator, tileset_data);
+ defer parsed_tileset.deinit();
+
+ const tileset = parsed_tileset.value;
+
+ try stringEquals("tiles.png", tileset.image.?);
+}
+
+test "parseMapLeaky works" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const map = try tmz.parseMapLeaky(arena.allocator(), full_map);
+ try regularMapTests(map);
+}
+
+fn regularMapTests(map: Map) !void {
+ try equals(null, map.background_color);
+ try stringEquals("bar", map.class.?);
+ try equals(-1, map.compression_level);
+ try equals(30, map.height);
+ try equals(null, map.hex_side_length);
+ try equals(false, map.infinite);
+ try equals(4, map.next_layer_id);
+ try equals(3, map.next_object_id);
+ try equals(.orthogonal, map.orientation);
+ try equals(0, map.parallax_origin_x);
+ try equals(0, map.parallax_origin_y);
+
+ try equals(Map.RenderOrder.@"right-down", map.render_order);
+
+ try stringEquals("1.10", map.version);
+
+ try equals(3, map.layers.len);
+ const layer = map.layers[0];
+
+ try stringEquals("bar", layer.class.?);
+ try equals(Layer.Type.tilelayer, layer.type);
+ try equals(32, layer.width.?);
+ try equals(0, layer.x);
+ try equals(0, layer.y);
+
+ try equals(16, layer.data.?[0]);
+ try equals(11, layer.data.?[1]);
+}
+
+test "Map with CSV encoded layer data parses correctly" {
+ const parsed_map = try tmz.parseMap(std.testing.allocator, @embedFile("test/map-csv.tmj"));
+ defer parsed_map.deinit();
+
+ const map = parsed_map.value;
+
+ try equals(1, map.layers.len);
+
+ const layer = map.layers[0];
+ try equals(null, layer.compression);
+
+ try equals(10, layer.data.?[0]);
+ try equals(15, layer.data.?[1]);
+ try equals(5, layer.data.?[2]);
+}
+
+test "Map with Base64 encoded layer data parses correctly" {
+ inline for (@typeInfo(Layer.Compression).Enum.fields) |field| {
+ const map_file = @embedFile("test/map-base64-" ++ field.name ++ ".tmj");
+ const parsed_map = try tmz.parseMap(std.testing.allocator, map_file);
+ defer parsed_map.deinit();
+
+ const map = parsed_map.value;
+
+ try equals(1, map.layers.len);
+
+ const layer = map.layers[0];
+ try equals(@as(Layer.Compression, @enumFromInt(field.value)), layer.compression);
+
+ try equals(10, layer.data.?[0]);
+ try equals(15, layer.data.?[1]);
+ try equals(5, layer.data.?[2]);
+ }
+}