LibAC-dart/test/nbt_test.dart

99 lines
3.3 KiB
Dart

import 'dart:io';
import 'package:libac_dart/nbt/NbtIo.dart';
import 'package:libac_dart/nbt/NbtUtils.dart';
import 'package:libac_dart/nbt/SnbtIo.dart';
import 'package:libac_dart/nbt/Stream.dart';
import 'package:libac_dart/nbt/Tag.dart';
import 'package:libac_dart/nbt/impl/CompoundTag.dart';
import 'package:libac_dart/nbt/impl/StringTag.dart';
import 'package:libac_dart/utils/uuid/NbtUUID.dart';
import 'package:libac_dart/utils/uuid/UUID.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';
void main() {
test('read non-compressed helloworld NBT', () async {
print("READING : ${Directory.current.path}/test/hello_world.nbt");
CompoundTag tag =
await NbtIo.read("${Directory.current.path}/test/hello_world.nbt");
expect(tag.getKey(), "hello world");
expect(tag.containsKey("name"), true);
expect(tag.get("name")!.asString(), "Bananrama");
});
test("write a hello-world NBT", () async {
CompoundTag tag = CompoundTag();
tag.setKey("hello world");
tag.put("name", StringTag.valueOf("Bananrama"));
var path = "${Directory.current.path}/build/hello_world.nbt";
await NbtIo.write(path, tag);
expect(File(path).existsSync(), true);
});
test('read non-compressed self-made helloworld NBT', () async {
print("READING : ${Directory.current.path}/build/hello_world.nbt");
CompoundTag tag =
await NbtIo.read("${Directory.current.path}/build/hello_world.nbt");
expect(tag.getKey(), "hello world");
expect(tag.containsKey("name"), true);
expect(tag.get("name")!.asString(), "Bananrama");
});
test('read compressed bigtest.nbt', () async {
var path = "${Directory.current.path}/test/bigtest.nbt";
CompoundTag tag = await NbtIo.read(path);
expect(tag.getKey(), "Level");
expect(tag.get("shortTest")!.asShort(), 32767);
expect(tag.get("doubleTest")!.asDouble(), 0.4931287132182315);
expect(tag.get("floatTest")!.asFloat(), 0.49823147);
});
test("Generate a UUID v4, save to NBT, and read it back again", () async {
var id = UUID.generate(4);
CompoundTag tag = CompoundTag();
NbtUtils.writeUUID(tag, "test", NbtUUID.fromUUID(id));
var newID = NbtUtils.readUUID(tag, "test");
expect(id.toString(), newID.toString());
});
test("Read HelloWorld, Output to SNBT", () async {
CompoundTag ct =
await NbtIo.read("${Directory.current.path}/test/hello_world.nbt");
StringBuilder sb = StringBuilder();
Tag.writeStringifiedNamedTag(ct, sb, 0);
print(sb.toString());
});
test("Read BigTest, Output to SNBT", () async {
CompoundTag ct =
await NbtIo.read("${Directory.current.path}/test/bigtest.nbt");
StringBuilder sb = StringBuilder();
Tag.writeStringifiedNamedTag(ct, sb, 0);
print(sb.toString());
});
test("Write BigTest to SNBT file", () async {
CompoundTag ct =
await NbtIo.read("${Directory.current.path}/test/bigtest.nbt");
String output = "${Directory.current.path}/build/bigtest.snbt";
File file = File(output);
SnbtIo.write(output, ct);
// Expect that the file exists
expect(file.existsSync(), true);
});
test("Write NULL UUID to NBT", () async {
CompoundTag tag = CompoundTag();
NbtUtils.writeUUID(tag, "test", NbtUUID.fromUUID(UUID.ZERO));
NbtUUID ID = NbtUtils.readUUID(tag, "test");
expect(ID.MSB, 0);
expect(ID.LSB, 0);
});
}