Add some tests.

This commit is contained in:
Kitteh 2021-06-04 14:14:19 +01:00
parent d05aec9b66
commit b3a83eff9c
15 changed files with 181 additions and 67 deletions

View file

@ -3,7 +3,7 @@ const read = @import("./qtshit/read.zig");
const write = @import("./qtshit/write.zig");
const QVariantType = @import("./qtshit/types/QVariant.zig").QVariant;
const prettyPrintQVariant = @import("./qtshit/utils/prettyPrintQVariant.zig").prettyPrintQVariant;
const freeQVariant = @import("./qtshit/utils/freeQVariant.zig").freeQVariant;
const freeQVariant = @import("./qtshit/utils/free/freeQVariant.zig").freeQVariant;
const QVariantMapToQVariantList = @import("./qtshit/utils/QVariantMapToQVariantList.zig").QVariantMapToQVariantList;
@ -52,6 +52,7 @@ pub const Client = struct {
var featureList = std.ArrayList([]const u8).init(s.allocator);
defer featureList.deinit();
try featureList.append("LongTime");
try featureList.append("LongMessageID");
try featureList.append("SenderPrefixes");
try featureList.append("RichMessages");

View file

@ -4,6 +4,7 @@ const readInt = @import("../readInt.zig").readInt;
const readShort = @import("../readShort.zig").readShort;
const readByte = @import("../readByte.zig").readByte;
const readQByteArray = @import("../readQByteArray.zig").readQByteArray;
const freeBufferInfo = @import("../../utils/free/freeBufferInfo.zig").freeBufferInfo;
pub fn readBufferInfo(reader: anytype, allocator: *std.mem.Allocator) !BufferInfo {
var id = try readInt(reader);
@ -23,3 +24,18 @@ pub fn readBufferInfo(reader: anytype, allocator: *std.mem.Allocator) !BufferInf
.Name = name,
};
}
test "deserialize BufferInfo" {
var bytes = &[_]u8{
0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 5, 35, 116, 101, 115, 116,
};
var fBS = std.io.fixedBufferStream(bytes);
var bufferInfo = try readBufferInfo(fBS.reader(), std.testing.allocator);
defer freeBufferInfo(bufferInfo, std.testing.allocator);
try std.testing.expect(bufferInfo.ID == 1);
try std.testing.expect(bufferInfo.NetworkID == 1);
try std.testing.expect(bufferInfo.Type == 2);
try std.testing.expectEqualStrings("#test", bufferInfo.Name);
}

View file

@ -5,10 +5,11 @@ const readLong = @import("../readLong.zig").readLong;
const readByte = @import("../readByte.zig").readByte;
const readBufferInfo = @import("./readBufferInfo.zig").readBufferInfo;
const readQByteArray = @import("../readQByteArray.zig").readQByteArray;
const freeMessage = @import("../../utils/free/freeMessage.zig").freeMessage;
pub fn readMessage(reader: anytype, allocator: *std.mem.Allocator) !Message {
return Message{
.MessageID = try readInt(reader),
.MessageID = try readLong(reader),
.Timestamp = try readLong(reader),
.Type = try readInt(reader),
.Flags = try readByte(reader),
@ -20,3 +21,31 @@ pub fn readMessage(reader: anytype, allocator: *std.mem.Allocator) !Message {
.Content = try readQByteArray(reader, allocator),
};
}
test "deserialize Message" {
var bytes = &[_]u8{
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 95, 244, 79, 69, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 5, 35, 116, 101, 115, 116, 0, 0, 0, 4, 116, 101, 115, 116, 0, 0,
0, 6, 98, 108, 97, 98, 108, 97, 0, 0, 0, 9, 116, 101, 115, 116, 32, 117, 115, 101, 114, 0, 0, 0, 28, 104, 116,
116, 112, 115, 58, 47, 47, 106, 102, 107, 97, 108, 115, 100, 107, 106, 102, 106, 46, 99, 111, 109, 47, 107, 106, 107, 106, 0,
0, 0, 22, 116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 32, 109, 101, 115, 115, 97, 103, 101,
};
var fBS = std.io.fixedBufferStream(bytes);
var message = try readMessage(fBS.reader(), std.testing.allocator);
defer freeMessage(message, std.testing.allocator);
try std.testing.expect(message.MessageID == 1);
try std.testing.expect(message.Timestamp == 1609846597);
try std.testing.expect(message.Type == 1);
try std.testing.expect(message.Flags == 0);
try std.testing.expect(message.Buffer.ID == 1);
try std.testing.expect(message.Buffer.NetworkID == 1);
try std.testing.expect(message.Buffer.Type == 2);
try std.testing.expectEqualStrings("#test", message.Buffer.Name);
try std.testing.expectEqualStrings("test", message.Sender);
try std.testing.expectEqualStrings("this is a test message", message.Content);
try std.testing.expectEqualStrings("blabla", message.SenderPrefixes);
try std.testing.expectEqualStrings("test user", message.RealName);
try std.testing.expectEqualStrings("https://jfkalsdkjfj.com/kjkj", message.AvatarUrl);
}

View file

@ -24,7 +24,7 @@ pub const BufferInfo = struct {
};
pub const Message = struct {
MessageID: i32,
MessageID: i64,
Timestamp: i64,
Type: i32,
Flags: u8,

View file

@ -0,0 +1,7 @@
const std = @import("std");
const BufferInfo = @import("../../types/UserType.zig").BufferInfo;
const freeString = @import("./freeString.zig").freeString;
pub fn freeBufferInfo(bufferInfo: BufferInfo, allocator: *std.mem.Allocator) void {
freeString(bufferInfo.Name, allocator);
}

View file

@ -0,0 +1,13 @@
const std = @import("std");
const Message = @import("../../types/UserType.zig").Message;
const freeQByteArray = @import("./freeQByteArray.zig").freeQByteArray;
const freeBufferInfo = @import("./freeBufferInfo.zig").freeBufferInfo;
pub fn freeMessage(message: Message, allocator: *std.mem.Allocator) void {
freeBufferInfo(message.Buffer, allocator);
freeQByteArray(message.SenderPrefixes, allocator);
freeQByteArray(message.Sender, allocator);
freeQByteArray(message.RealName, allocator);
freeQByteArray(message.AvatarUrl, allocator);
freeQByteArray(message.Content, allocator);
}

View file

@ -0,0 +1,5 @@
const std = @import("std");
pub fn freeQByteArray(qbytearray: []u8, allocator: *std.mem.Allocator) void {
allocator.free(qbytearray);
}

View file

@ -0,0 +1,9 @@
const std = @import("std");
const freeString = @import("./freeString.zig").freeString;
pub fn freeQStringList(qstringlist: [][]const u8, allocator: *std.mem.Allocator) void {
for (qstringlist) |string| {
freeString(string, allocator);
}
allocator.free(qstringlist);
}

View file

@ -0,0 +1,37 @@
const std = @import("std");
const QVariant = @import("../../types/QVariant.zig").QVariant;
const freeQVariantMap = @import("./freeQVariantMap.zig").freeQVariantMap;
const freeQVariantList = @import("./freeQVariantList.zig").freeQVariantList;
const freeQStringList = @import("./freeQStringList.zig").freeQStringList;
const freeQByteArray = @import("./freeQByteArray.zig").freeQByteArray;
const freeString = @import("./freeString.zig").freeString;
const freeUserType = @import("./freeUserType.zig").freeUserType;
pub fn freeQVariant(variant: QVariant, allocator: *std.mem.Allocator) void {
switch (variant) {
.Byte, .Int, .UInt, .Short, .QDateTime => {
// Static
},
.QVariantMap => |value| {
freeQVariantMap(value, allocator);
},
.QVariantList => |value| {
freeQVariantList(value, allocator);
},
.String => |value| {
freeString(value, allocator);
},
.QStringList => |value| {
freeQStringList(value, allocator);
},
.QByteArray => |value| {
freeQByteArray(value, allocator);
},
.UserType => |value| {
freeUserType(value, allocator);
},
}
}

View file

@ -0,0 +1,10 @@
const std = @import("std");
const QVariant = @import("../../types/QVariant.zig").QVariant;
const freeQVariant = @import("./freeQVariant.zig").freeQVariant;
pub fn freeQVariantList(qvariantlist: []QVariant, allocator: *std.mem.Allocator) void {
for (qvariantlist) |varient, i| {
freeQVariant(varient, allocator);
}
allocator.free(qvariantlist);
}

View file

@ -0,0 +1,15 @@
const std = @import("std");
const QVariant = @import("../../types/QVariant.zig").QVariant;
const freeString = @import("./freeString.zig").freeString;
const freeQVariant = @import("./freeQVariant.zig").freeQVariant;
pub fn freeQVariantMap(qvariantmap: std.StringHashMap(QVariant), allocator: *std.mem.Allocator) void {
var qMapIter = qvariantmap.iterator();
while (qMapIter.next()) |v| {
freeString(v.key, allocator);
freeQVariant(v.value, allocator);
}
// Compiler bug maybe? doesnt want me to drop const so have to store it in a variable
var qi = qvariantmap;
qi.deinit();
}

View file

@ -0,0 +1,5 @@
const std = @import("std");
pub fn freeString(string: []const u8, allocator: *std.mem.Allocator) void {
allocator.free(string);
}

View file

@ -0,0 +1,22 @@
const std = @import("std");
const UserType = @import("../../types/UserType.zig").UserType;
const freeQVariantMap = @import("./freeQVariantMap.zig").freeQVariantMap;
const freeMessage = @import("./freeMessage.zig").freeMessage;
const freeBufferInfo = @import("./freeBufferInfo.zig").freeBufferInfo;
pub fn freeUserType(usertype: UserType, allocator: *std.mem.Allocator) void {
switch (usertype) {
.BufferId, .IdentityId, .NetworkId, .MsgId, .PeerPtr => {
// Static
},
.IrcUser, .IrcChannel, .Identity, .NetworkInfo, .NetworkServer => |value| {
freeQVariantMap(value, allocator);
},
.BufferInfo => |value| {
freeBufferInfo(value, allocator);
},
.Message => |value| {
freeMessage(value, allocator);
},
}
}

View file

@ -1,64 +0,0 @@
const std = @import("std");
const QVariant = @import("../types/QVariant.zig").QVariant;
pub fn freeQVariantMap(qvariantmap: std.StringHashMap(QVariant), allocator: *std.mem.Allocator) void {
var qMapIter = qvariantmap.iterator();
while (qMapIter.next()) |v| {
allocator.free(v.key);
freeQVariant(v.value, allocator);
}
// Compiler bug maybe? doesnt want me to drop const so have to store it in a variable
var qi = qvariantmap;
qi.deinit();
}
pub fn freeQVariant(variant: QVariant, allocator: *std.mem.Allocator) void {
switch (variant) {
.Byte, .Int, .UInt, .Short, .QDateTime => {
// Static
},
.QVariantMap => |q| {
freeQVariantMap(q, allocator);
},
.QVariantList => |l| {
for (l) |varient, i| {
freeQVariant(varient, allocator);
}
allocator.free(l);
},
.String => |s| {
allocator.free(s);
},
.QStringList => |l| {
for (l) |varient, i| {
allocator.free(varient);
}
allocator.free(l);
},
.QByteArray => |ba| {
allocator.free(ba);
},
.UserType => |usertype| {
switch (usertype) {
.BufferId, .IdentityId, .NetworkId, .MsgId, .PeerPtr => {
// Statid
},
.IrcUser, .IrcChannel, .Identity, .NetworkInfo, .NetworkServer => |value| {
freeQVariantMap(value, allocator);
},
.BufferInfo => |value| {
allocator.free(value.Name);
},
.Message => |value| {
allocator.free(value.Buffer.Name);
allocator.free(value.SenderPrefixes);
allocator.free(value.RealName);
allocator.free(value.AvatarUrl);
allocator.free(value.Content);
}
}
},
}
}

9
src/tests.zig Normal file
View file

@ -0,0 +1,9 @@
const readBufferInfo = @import("qtshit/read/usertypes/readBufferInfo.zig");
const readMessage = @import("qtshit/read/usertypes/readMessage.zig");
const unicode = @import("qtshit/utils/unicode.zig");
// import anything with tests.
comptime {
@import("std").testing.refAllDecls(@This());
}