From 2a37322d78c9ce1d27cbc12e24dd918407a931e3 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Mon, 3 Jun 2024 04:59:40 +0200 Subject: Update dependencies, some tests, other things --- .gitignore | 2 + ArcaneLibs | 2 +- ConsoleApp1/ConsoleApp1.csproj | 14 -- ConsoleApp1/Program.cs | 25 ---- .../BaseMatrixEventContent.cs | 15 ++- LibMatrix.EventTypes.Abstractions/MatrixEvent.cs | 5 +- .../MatrixEventCollection.cs | 146 +++++++++++---------- .../RoomMembershipEventContent.cs | 4 +- .../RoomMessageEventContent.cs | 113 ++++++++-------- LibMatrix.sln | 26 ++-- LibMatrix.sln.DotSettings.user | 5 + LibMatrix/LibMatrix.csproj | 4 +- .../LibMatrix.EventTypes.Abstractions.Tests.csproj | 43 ++++++ .../SerialisationTests.cs | 122 +++++++++++++++++ .../TestData/RoomMembershipEvent.json | 17 +++ .../TestData/RoomMembershipEventContent.json | 6 + .../TestData/RoomMessageEvent.json | 18 +++ .../TestData/RoomMessageEventContent.json | 11 ++ Tests/LibMatrix.Tests/GlobalUsings.cs | 1 - Tests/LibMatrix.Tests/LibMatrix.Tests.csproj | 17 ++- .../LibMatrix.EventTypes.BasicTests.csproj | 4 - .../LibMatrix.EventTypes.BasicTests/Program.cs | 77 ++++++----- .../LibMatrix.EventTypes.Benchmark.csproj | 24 ++++ .../LibMatrix.EventTypes.Benchmark/Program.cs | 37 ++++++ .../LibMatrix.Utilities.Bot.csproj | 4 +- 25 files changed, 506 insertions(+), 236 deletions(-) delete mode 100644 ConsoleApp1/ConsoleApp1.csproj delete mode 100644 ConsoleApp1/Program.cs create mode 100644 Tests/LibMatrix.EventTypes.Abstractions.Tests/LibMatrix.EventTypes.Abstractions.Tests.csproj create mode 100644 Tests/LibMatrix.EventTypes.Abstractions.Tests/SerialisationTests.cs create mode 100644 Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMembershipEvent.json create mode 100644 Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMembershipEventContent.json create mode 100644 Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMessageEvent.json create mode 100644 Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMessageEventContent.json delete mode 100644 Tests/LibMatrix.Tests/GlobalUsings.cs create mode 100644 Utilities/LibMatrix.EventTypes.Benchmark/LibMatrix.EventTypes.Benchmark.csproj create mode 100644 Utilities/LibMatrix.EventTypes.Benchmark/Program.cs diff --git a/.gitignore b/.gitignore index fba66db..7e72167 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ appsettings.Local*.json appservice.yaml appservice.json Tests/LibMatrix.Tests/appsettings.json +**/BenchmarkDotNet.Artifacts/ +Utilities/LibMatrix.EventTypes.Benchmark/*.json diff --git a/ArcaneLibs b/ArcaneLibs index 65d6fa9..805f3b7 160000 --- a/ArcaneLibs +++ b/ArcaneLibs @@ -1 +1 @@ -Subproject commit 65d6fa98cc05ed3c37e71797145e8eb7f2c072a1 +Subproject commit 805f3b7941536bb09970c577dd532ba4686b971f diff --git a/ConsoleApp1/ConsoleApp1.csproj b/ConsoleApp1/ConsoleApp1.csproj deleted file mode 100644 index f30d408..0000000 --- a/ConsoleApp1/ConsoleApp1.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - Exe - net8.0 - enable - enable - - - - - - - diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs deleted file mode 100644 index 7831ce8..0000000 --- a/ConsoleApp1/Program.cs +++ /dev/null @@ -1,25 +0,0 @@ -// See https://aka.ms/new-console-template for more information - -using LibMatrix.EventTypes; -using LibMatrix.EventTypes.Events; - -Console.WriteLine("Hello, World!"); - -MatrixEventCollection collection = new(); -collection.Add(new MatrixEvent() { - Content = new RoomMembershipEventContent() { - Membership = "yes" - } -}); - -List> collection2 = new(); -collection2.Add(new MatrixEvent() { - Content = new RoomMembershipEventContent() { - Membership = "yes" - } -}); - -List collection3 = new(); -collection3.Add(new RoomMembershipEventContent() { - Membership = "yes" -}); \ No newline at end of file diff --git a/LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs b/LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs index eba50a5..0906e30 100644 --- a/LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs +++ b/LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs @@ -6,15 +6,19 @@ using ArcaneLibs.Extensions; namespace LibMatrix.EventTypes; +public interface IMatrixEvent { + +} + // : MatrixEventContent where T : MatrixEventContent, new() { /// /// Extensible Event Content, aims to provide an API similar to JsonNode/JsonObject /// /// /// -[JsonConverter(typeof(MatrixEventContentConverter))] +// [JsonConverter(typeof(BaseMatrixEventContent.MatrixEventContentConverter))] // [JsonSerializable(typeof(MatrixEventContent))] -public class BaseMatrixEventContent { +public abstract class BaseMatrixEventContent { public JsonObject InternalJson { get; set; } = new(); public BaseMatrixEventContent() { } @@ -23,7 +27,7 @@ public class BaseMatrixEventContent { InternalJson = json.AsObject(); } - public static implicit operator BaseMatrixEventContent(JsonNode json) => new(json); + // public static implicit operator BaseMatrixEventContent(JsonNode json) => new(json); // public static implicit operator JsonNode(MatrixEventContent content) => content.InternalJson; @@ -33,7 +37,10 @@ public class BaseMatrixEventContent { [JsonIgnore] public string EventType => EventTypes.First(); - public JsonNode? this[string key] => InternalJson[key]; + public JsonNode? this[string key] { + get => InternalJson[key]; + set => InternalJson[key] = value; + } public string ToJson() => InternalJson.ToJson(); diff --git a/LibMatrix.EventTypes.Abstractions/MatrixEvent.cs b/LibMatrix.EventTypes.Abstractions/MatrixEvent.cs index 0e548c6..46e7757 100644 --- a/LibMatrix.EventTypes.Abstractions/MatrixEvent.cs +++ b/LibMatrix.EventTypes.Abstractions/MatrixEvent.cs @@ -2,7 +2,10 @@ using System.Text.Json.Serialization; namespace LibMatrix.EventTypes; -public interface IMatrixEvent where T : BaseMatrixEventContent; +public interface IBaseMatrixEvent { + +} +public partial interface IMatrixEvent : IBaseMatrixEvent where T : BaseMatrixEventContent; public class MatrixEvent : IMatrixEvent where T : BaseMatrixEventContent { [JsonPropertyName("content")] public T? Content { get; set; } diff --git a/LibMatrix.EventTypes.Abstractions/MatrixEventCollection.cs b/LibMatrix.EventTypes.Abstractions/MatrixEventCollection.cs index 78886d9..f789588 100644 --- a/LibMatrix.EventTypes.Abstractions/MatrixEventCollection.cs +++ b/LibMatrix.EventTypes.Abstractions/MatrixEventCollection.cs @@ -1,71 +1,75 @@ -// using System.Collections; -// -// namespace LibMatrix.EventTypes; -// -// public interface IMatrixEventCollection : IEnumerable> where T : MatrixEventContent { -// -// } -// public class MatrixEventCollection : IMatrixEventCollection, IList { -// private IList> _listImplementation; -// public IEnumerator> GetEnumerator() => _listImplementation.GetEnumerator(); -// -// IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator(); -// -// public void Add(MatrixEvent item) => _listImplementation.Add(item); -// -// public void Clear() => _listImplementation.Clear(); -// -// public bool Contains(MatrixEvent item) => _listImplementation.Contains(item); -// -// public void CopyTo(MatrixEvent[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex); -// -// public bool Remove(MatrixEvent item) => _listImplementation.Remove(item); -// -// public int Count => _listImplementation.Count; -// -// public bool IsReadOnly => _listImplementation.IsReadOnly; -// -// public int IndexOf(MatrixEvent item) => _listImplementation.IndexOf(item); -// -// public void Insert(int index, MatrixEvent item) => _listImplementation.Insert(index, item); -// -// public void RemoveAt(int index) => _listImplementation.RemoveAt(index); -// -// public MatrixEvent this[int index] { -// get => _listImplementation[index]; -// set => _listImplementation[index] = value; -// } -// } -// public class MatrixEventCollection : IMatrixEventCollection, IList> where T : MatrixEventContent { -// //TODO: implement -// -// private IList> _listImplementation = new List>(); -// public IEnumerator> GetEnumerator() => _listImplementation.GetEnumerator(); -// -// IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator(); -// -// public void Add(MatrixEvent item) => _listImplementation.Add(item); -// -// public void Clear() => _listImplementation.Clear(); -// -// public bool Contains(MatrixEvent item) => _listImplementation.Contains(item); -// -// public void CopyTo(MatrixEvent[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex); -// -// public bool Remove(MatrixEvent item) => _listImplementation.Remove(item); -// -// public int Count => _listImplementation.Count; -// -// public bool IsReadOnly => _listImplementation.IsReadOnly; -// -// public int IndexOf(MatrixEvent item) => _listImplementation.IndexOf(item); -// -// public void Insert(int index, MatrixEvent item) => _listImplementation.Insert(index, item); -// -// public void RemoveAt(int index) => _listImplementation.RemoveAt(index); -// -// public MatrixEvent this[int index] { -// get => _listImplementation[index]; -// set => _listImplementation[index] = value; -// } -// }a \ No newline at end of file +using System.Collections; + +namespace LibMatrix.EventTypes.Abstractions; + +public interface IMatrixEventCollection : IEnumerable> where T : BaseMatrixEventContent { + +} +public class MatrixEventCollection : IMatrixEventCollection, IList> { + private IList> _listImplementation; + IEnumerator> IEnumerable>.GetEnumerator() => GetEnumerator(); + + public IEnumerator> GetEnumerator() => _listImplementation.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator(); + + public void Add(MatrixEvent item) => _listImplementation.Add(item); + + public void Clear() => _listImplementation.Clear(); + + public bool Contains(MatrixEvent item) => _listImplementation.Contains(item); + + public void CopyTo(MatrixEvent[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex); + + public bool Remove(MatrixEvent item) => _listImplementation.Remove(item); + + public int Count => _listImplementation.Count; + + public bool IsReadOnly => _listImplementation.IsReadOnly; + + public int IndexOf(MatrixEvent item) => _listImplementation.IndexOf(item); + + public void Insert(int index, MatrixEvent item) => _listImplementation.Insert(index, item); + + public void RemoveAt(int index) => _listImplementation.RemoveAt(index); + + public MatrixEvent this[int index] { + get => _listImplementation[index]; + set => _listImplementation[index] = value; + } +} +public class MatrixEventCollection : IMatrixEventCollection, IList> where T : BaseMatrixEventContent { + //TODO: implement + + private IList> _listImplementation = new List>(); + IEnumerator> IEnumerable>.GetEnumerator() => GetEnumerator(); + + public IEnumerator> GetEnumerator() => _listImplementation.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator(); + + public void Add(MatrixEvent item) => _listImplementation.Add(item); + + public void Clear() => _listImplementation.Clear(); + + public bool Contains(MatrixEvent item) => _listImplementation.Contains(item); + + public void CopyTo(MatrixEvent[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex); + + public bool Remove(MatrixEvent item) => _listImplementation.Remove(item); + + public int Count => _listImplementation.Count; + + public bool IsReadOnly => _listImplementation.IsReadOnly; + + public int IndexOf(MatrixEvent item) => _listImplementation.IndexOf(item); + + public void Insert(int index, MatrixEvent item) => _listImplementation.Insert(index, item); + + public void RemoveAt(int index) => _listImplementation.RemoveAt(index); + + public MatrixEvent this[int index] { + get => _listImplementation[index]; + set => _listImplementation[index] = value; + } +} \ No newline at end of file diff --git a/LibMatrix.EventTypes.Spec/RoomMembershipEventContent.cs b/LibMatrix.EventTypes.Spec/RoomMembershipEventContent.cs index 370a192..5070ee4 100644 --- a/LibMatrix.EventTypes.Spec/RoomMembershipEventContent.cs +++ b/LibMatrix.EventTypes.Spec/RoomMembershipEventContent.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace LibMatrix.EventTypes.Events; +namespace LibMatrix.EventTypes.Spec; [MatrixEvent("m.room.member")] [JsonConverter(typeof(MatrixEventContentConverter))] @@ -9,6 +9,4 @@ public class RoomMembershipEventContent : BaseMatrixEventContent { get => InternalJson["membership"]!.GetValue(); set => InternalJson["membership"] = value; } - - public string? Something { get; set; } } \ No newline at end of file diff --git a/LibMatrix.EventTypes.Spec/RoomMessageEventContent.cs b/LibMatrix.EventTypes.Spec/RoomMessageEventContent.cs index f2a5483..3ff4486 100644 --- a/LibMatrix.EventTypes.Spec/RoomMessageEventContent.cs +++ b/LibMatrix.EventTypes.Spec/RoomMessageEventContent.cs @@ -1,57 +1,56 @@ -// using System.Text.Json.Serialization; -// using LibMatrix.EventTypes; -// -// namespace LibMatrix.LegacyEvents.EventTypes.Spec; -// -// [MatrixEvent(EventId)] -// public class RoomMessageEventContent : MatrixEventContent { -// public const string EventId = "m.room.message"; -// -// public RoomMessageEventContent(string messageType = "m.notice", string? body = null) { -// MessageType = messageType; -// Body = body ?? ""; -// } -// -// [JsonPropertyName("body")] -// public string Body { get; set; } -// -// [JsonPropertyName("msgtype")] -// public string MessageType { get; set; } = "m.notice"; -// -// [JsonPropertyName("formatted_body")] -// public string? FormattedBody { get; set; } -// -// [JsonPropertyName("format")] -// public string? Format { get; set; } -// -// /// -// /// Media URI for this message, if any -// /// -// [JsonPropertyName("url")] -// public string? Url { get; set; } -// -// public string? FileName { get; set; } -// -// [JsonPropertyName("info")] -// public FileInfoStruct? FileInfo { get; set; } -// -// [JsonIgnore] -// public string BodyWithoutReplyFallback => Body.Split('\n').SkipWhile(x => x.StartsWith(">")).SkipWhile(x=>x.Trim().Length == 0).Aggregate((x, y) => $"{x}\n{y}"); -// -// public class FileInfoStruct { -// [JsonPropertyName("mimetype")] -// public string? MimeType { get; set; } -// -// [JsonPropertyName("size")] -// public long Size { get; set; } -// -// [JsonPropertyName("thumbnail_url")] -// public string? ThumbnailUrl { get; set; } -// -// [JsonPropertyName("w")] -// public int? Width { get; set; } -// -// [JsonPropertyName("h")] -// public int? Height { get; set; } -// } -// } \ No newline at end of file +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec; + +[MatrixEvent(EventId)] +public class RoomMessageEventContent : BaseMatrixEventContent { + public const string EventId = "m.room.message"; + + public RoomMessageEventContent(string messageType = "m.notice", string? body = null) { + MessageType = messageType; + Body = body ?? ""; + } + + [JsonPropertyName("body")] + public string Body { get; set; } + + [JsonPropertyName("msgtype")] + public string MessageType { get; set; } = "m.notice"; + + [JsonPropertyName("formatted_body")] + public string? FormattedBody { get; set; } + + [JsonPropertyName("format")] + public string? Format { get; set; } + + /// + /// Media URI for this message, if any + /// + [JsonPropertyName("url")] + public string? Url { get; set; } + + public string? FileName { get; set; } + + [JsonPropertyName("info")] + public FileInfoStruct? FileInfo { get; set; } + + [JsonIgnore] + public string BodyWithoutReplyFallback => Body.Split('\n').SkipWhile(x => x.StartsWith(">")).SkipWhile(x=>x.Trim().Length == 0).Aggregate((x, y) => $"{x}\n{y}"); + + public class FileInfoStruct { + [JsonPropertyName("mimetype")] + public string? MimeType { get; set; } + + [JsonPropertyName("size")] + public long Size { get; set; } + + [JsonPropertyName("thumbnail_url")] + public string? ThumbnailUrl { get; set; } + + [JsonPropertyName("w")] + public int? Width { get; set; } + + [JsonPropertyName("h")] + public int? Height { get; set; } + } +} \ No newline at end of file diff --git a/LibMatrix.sln b/LibMatrix.sln index 396c5a0..2293a8d 100644 --- a/LibMatrix.sln +++ b/LibMatrix.sln @@ -37,10 +37,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.LegacyEvents.Even EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.HomeserverEmulator", "Tests\LibMatrix.HomeserverEmulator\LibMatrix.HomeserverEmulator.csproj", "{D44DB78D-9BAD-4AB6-A054-839ECA9D68D2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.EventTypes", "LibMatrix.EventTypes\LibMatrix.EventTypes.csproj", "{E9E9567D-58F4-4E17-BBC1-D4746C2526DB}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{6254A2BA-279F-49F7-B3F3-675397F0F644}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.Blazor.Components", "ArcaneLibs\ArcaneLibs.Blazor.Components\ArcaneLibs.Blazor.Components.csproj", "{C40726FB-C8D5-4D18-836E-9F7D394E2E0C}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.Legacy", "ArcaneLibs\ArcaneLibs.Legacy\ArcaneLibs.Legacy.csproj", "{2FF8C08A-9DC8-40A2-B9C8-A43615005B32}" @@ -63,6 +59,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.DevTestBot", "Uti EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.EventTypes.BasicTests", "Utilities\LibMatrix.EventTypes.BasicTests\LibMatrix.EventTypes.BasicTests.csproj", "{B14F6ED1-2222-454E-8632-D6CB25B7A66B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.EventTypes.Benchmark", "Utilities\LibMatrix.EventTypes.Benchmark\LibMatrix.EventTypes.Benchmark.csproj", "{3583DD22-BECD-4CDF-B600-24AE95F2E950}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.EventTypes.Abstractions.Tests", "Tests\LibMatrix.EventTypes.Abstractions.Tests\LibMatrix.EventTypes.Abstractions.Tests.csproj", "{F362550F-74F1-425A-9943-CEB30B7541DD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -124,14 +124,6 @@ Global {D44DB78D-9BAD-4AB6-A054-839ECA9D68D2}.Debug|Any CPU.Build.0 = Debug|Any CPU {D44DB78D-9BAD-4AB6-A054-839ECA9D68D2}.Release|Any CPU.ActiveCfg = Release|Any CPU {D44DB78D-9BAD-4AB6-A054-839ECA9D68D2}.Release|Any CPU.Build.0 = Release|Any CPU - {E9E9567D-58F4-4E17-BBC1-D4746C2526DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E9E9567D-58F4-4E17-BBC1-D4746C2526DB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E9E9567D-58F4-4E17-BBC1-D4746C2526DB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E9E9567D-58F4-4E17-BBC1-D4746C2526DB}.Release|Any CPU.Build.0 = Release|Any CPU - {6254A2BA-279F-49F7-B3F3-675397F0F644}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6254A2BA-279F-49F7-B3F3-675397F0F644}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6254A2BA-279F-49F7-B3F3-675397F0F644}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6254A2BA-279F-49F7-B3F3-675397F0F644}.Release|Any CPU.Build.0 = Release|Any CPU {C40726FB-C8D5-4D18-836E-9F7D394E2E0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C40726FB-C8D5-4D18-836E-9F7D394E2E0C}.Debug|Any CPU.Build.0 = Debug|Any CPU {C40726FB-C8D5-4D18-836E-9F7D394E2E0C}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -176,6 +168,14 @@ Global {B14F6ED1-2222-454E-8632-D6CB25B7A66B}.Debug|Any CPU.Build.0 = Debug|Any CPU {B14F6ED1-2222-454E-8632-D6CB25B7A66B}.Release|Any CPU.ActiveCfg = Release|Any CPU {B14F6ED1-2222-454E-8632-D6CB25B7A66B}.Release|Any CPU.Build.0 = Release|Any CPU + {3583DD22-BECD-4CDF-B600-24AE95F2E950}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3583DD22-BECD-4CDF-B600-24AE95F2E950}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3583DD22-BECD-4CDF-B600-24AE95F2E950}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3583DD22-BECD-4CDF-B600-24AE95F2E950}.Release|Any CPU.Build.0 = Release|Any CPU + {F362550F-74F1-425A-9943-CEB30B7541DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F362550F-74F1-425A-9943-CEB30B7541DD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F362550F-74F1-425A-9943-CEB30B7541DD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F362550F-74F1-425A-9943-CEB30B7541DD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {1B1B2197-61FB-416F-B6C8-845F2E5A0442} = {840309F0-435B-43A7-8471-8C2BE643889D} @@ -197,5 +197,7 @@ Global {93CED166-06E0-42E7-90C9-D18AA5286999} = {01A126FE-9D50-40F2-817B-E55F4065EA76} {5C588D95-79A9-49DD-9EDD-C9D6434434E5} = {A6345ECE-4C5E-400F-9130-886E343BF314} {B14F6ED1-2222-454E-8632-D6CB25B7A66B} = {A6345ECE-4C5E-400F-9130-886E343BF314} + {3583DD22-BECD-4CDF-B600-24AE95F2E950} = {A6345ECE-4C5E-400F-9130-886E343BF314} + {F362550F-74F1-425A-9943-CEB30B7541DD} = {BFE16D8E-EFC5-49F6-9854-DB001309B3B4} EndGlobalSection EndGlobal diff --git a/LibMatrix.sln.DotSettings.user b/LibMatrix.sln.DotSettings.user index c5570e2..c724579 100644 --- a/LibMatrix.sln.DotSettings.user +++ b/LibMatrix.sln.DotSettings.user @@ -4,8 +4,13 @@ <AssemblyExplorer> <Assembly Path="/home/root@Rory/.cache/NuGetPackages/microsoft.extensions.hosting.abstractions/7.0.0/lib/net7.0/Microsoft.Extensions.Hosting.Abstractions.dll" /> </AssemblyExplorer> + <SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &lt;Tests&gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <Project Location="/home/Rory/git/matrix/MatrixRoomUtils/LibMatrix" Presentation="&lt;Tests&gt;" /> +</SessionState> + True True + True True True diff --git a/LibMatrix/LibMatrix.csproj b/LibMatrix/LibMatrix.csproj index d6c9155..0478699 100644 --- a/LibMatrix/LibMatrix.csproj +++ b/LibMatrix/LibMatrix.csproj @@ -15,8 +15,8 @@ - - + + diff --git a/Tests/LibMatrix.EventTypes.Abstractions.Tests/LibMatrix.EventTypes.Abstractions.Tests.csproj b/Tests/LibMatrix.EventTypes.Abstractions.Tests/LibMatrix.EventTypes.Abstractions.Tests.csproj new file mode 100644 index 0000000..d4a45e0 --- /dev/null +++ b/Tests/LibMatrix.EventTypes.Abstractions.Tests/LibMatrix.EventTypes.Abstractions.Tests.csproj @@ -0,0 +1,43 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + Always + + + PreserveNewest + + + + diff --git a/Tests/LibMatrix.EventTypes.Abstractions.Tests/SerialisationTests.cs b/Tests/LibMatrix.EventTypes.Abstractions.Tests/SerialisationTests.cs new file mode 100644 index 0000000..fe1a575 --- /dev/null +++ b/Tests/LibMatrix.EventTypes.Abstractions.Tests/SerialisationTests.cs @@ -0,0 +1,122 @@ +using System.Text.Json; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; + +namespace LibMatrix.EventTypes.Abstractions.Tests; + +public class SerialisationTests { + private static readonly Dictionary TestData = Directory.GetFiles("TestData").Where(x=>x.EndsWith(".json")).ToDictionary(Path.GetFileNameWithoutExtension, File.ReadAllText); + [Fact] + public void DeserializeEvent() { + var evt = JsonSerializer.Deserialize>(TestData["RoomMembershipEvent"]); + Assert.NotNull(evt); + Assert.NotNull(evt.Content); + Assert.NotNull(evt.Content.Membership); + } + + [Fact] + public void DeserialiseEventContent() { + var evt = JsonSerializer.Deserialize(TestData["RoomMembershipEventContent"]); + Assert.NotNull(evt); + Assert.NotNull(evt.Membership); + } + + [Fact] + public void DeserializeUnknownEvent() { + var evt = JsonSerializer.Deserialize>(TestData["RoomMembershipEvent"]); + Assert.NotNull(evt); + Assert.NotNull(evt.Content); + Assert.NotNull(evt.Content["membership"]); + } + + [Fact] + public void DeserializeUnknownEventContent() { + var evt = JsonSerializer.Deserialize(TestData["RoomMembershipEventContent"]); + Assert.NotNull(evt); + Assert.NotNull(evt["membership"]); + } + + [Fact] + public void SerializeEvent() { + var evt = JsonSerializer.Deserialize>(TestData["RoomMembershipEvent"]); + var json = evt.ToJson(); + Assert.NotNull(json); + Assert.Contains("membership", json); + } + + [Fact] + public void SerializeEventContent() { + var evt = JsonSerializer.Deserialize(TestData["RoomMembershipEventContent"]); + Assert.NotNull(evt); + var json = evt.ToJson(); + Assert.NotNull(json); + Assert.Contains("membership", json); + } + + [Fact] + public void SerializeUnknownEvent() { + var evt = JsonSerializer.Deserialize>(TestData["RoomMembershipEvent"]); + var json = evt.ToJson(); + Assert.NotNull(json); + Assert.Contains("membership", json); + } + + [Fact] + public void SerializeUnknownEventContent() { + var evt = JsonSerializer.Deserialize(TestData["RoomMembershipEventContent"]); + Assert.NotNull(evt); + var json = evt.ToJson(); + Assert.NotNull(json); + Assert.Contains("membership", json); + } + + [Fact] + public void ModifyEvent() { + var evt = JsonSerializer.Deserialize>(TestData["RoomMembershipEvent"]); + Assert.NotNull(evt); + Assert.NotNull(evt.Content); + evt.Content.Membership = "meow"; + var json = evt.ToJson(); + Assert.NotNull(json); + Assert.Contains("meow", json); + } + + [Fact] + public void ModifyEventContent() { + var evt = JsonSerializer.Deserialize(TestData["RoomMembershipEventContent"]); + Assert.NotNull(evt); + evt.Membership = "meow"; + var json = evt.ToJson(); + Assert.NotNull(json); + Assert.Contains("meow", json); + } + + [Fact] + public void ModifyUnknownEvent() { + var evt = JsonSerializer.Deserialize>(TestData["RoomMembershipEvent"]); + Assert.NotNull(evt); + evt.Content["membership"] = "meow"; + var json = evt.ToJson(); + Assert.NotNull(json); + Assert.Contains("meow", json); + } + + [Fact] + public void ModifyUnknownEventContent() { + var evt = JsonSerializer.Deserialize(TestData["RoomMembershipEventContent"]); + Assert.NotNull(evt); + evt["membership"] = "meow"; + var json = evt.ToJson(); + Assert.NotNull(json); + Assert.Contains("meow", json); + } + + [Fact] + public void SerializeEventWithUnknownContent() { + var evt = JsonSerializer.Deserialize>(TestData["RoomMembershipEvent"]); + Assert.NotNull(evt); + var json = evt.ToJson(); + Assert.NotNull(json); + Assert.Contains("membership", json); + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMembershipEvent.json b/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMembershipEvent.json new file mode 100644 index 0000000..1134a58 --- /dev/null +++ b/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMembershipEvent.json @@ -0,0 +1,17 @@ +{ + "content": { + "avatar_url": "mxc://server1/abcdefgh", + "displayname": "User 1", + "is_direct": false, + "membership": "join" + }, + "event_id": "$DLgeeol91D36xqZQvEwRJrOoBKttZ_S66hco8Ci-qb0", + "origin_server_ts": 1717146801732, + "sender": "@user1:server1", + "state_key": "@user1:server1", + "type": "m.room.member", + "unsigned": { + "prev_sender": "@user1:server1", + "replaces_state": "$4QptWS_8YzaZfROeFzOdhoV2wzYPEzQDx0Q6u2UOt8c" + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMembershipEventContent.json b/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMembershipEventContent.json new file mode 100644 index 0000000..ca328d4 --- /dev/null +++ b/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMembershipEventContent.json @@ -0,0 +1,6 @@ +{ + "avatar_url": "mxc://server1/abcdefgh", + "displayname": "User 1", + "is_direct": false, + "membership": "join" +} \ No newline at end of file diff --git a/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMessageEvent.json b/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMessageEvent.json new file mode 100644 index 0000000..3072114 --- /dev/null +++ b/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMessageEvent.json @@ -0,0 +1,18 @@ +{ + "content": { + "body": "This is a test message", + "format": "org.matrix.custom.html", + "formatted_body": "This is a test message with HTML!", + "m.mentions": { + "user_ids": [ + "@emma:rory.gay" + ] + }, + "msgtype": "m.text" + }, + "event_id": "$qBmS2SyNAA-ODcOZpAd_RRfApzXuMmy0VHsCqnMebzI", + "origin_server_ts": 1717100265714, + "sender": "@user:example.xyz", + "type": "m.room.message", + "unsigned": null +} diff --git a/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMessageEventContent.json b/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMessageEventContent.json new file mode 100644 index 0000000..d9c7024 --- /dev/null +++ b/Tests/LibMatrix.EventTypes.Abstractions.Tests/TestData/RoomMessageEventContent.json @@ -0,0 +1,11 @@ +{ + "body": "This is a test message", + "format": "org.matrix.custom.html", + "formatted_body": "This is a test message with HTML!", + "m.mentions": { + "user_ids": [ + "@emma:rory.gay" + ] + }, + "msgtype": "m.text" +} \ No newline at end of file diff --git a/Tests/LibMatrix.Tests/GlobalUsings.cs b/Tests/LibMatrix.Tests/GlobalUsings.cs deleted file mode 100644 index 8c927eb..0000000 --- a/Tests/LibMatrix.Tests/GlobalUsings.cs +++ /dev/null @@ -1 +0,0 @@ -global using Xunit; \ No newline at end of file diff --git a/Tests/LibMatrix.Tests/LibMatrix.Tests.csproj b/Tests/LibMatrix.Tests/LibMatrix.Tests.csproj index d833d8b..085816f 100644 --- a/Tests/LibMatrix.Tests/LibMatrix.Tests.csproj +++ b/Tests/LibMatrix.Tests/LibMatrix.Tests.csproj @@ -10,22 +10,25 @@ - - - - - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + + runtime; build; native; contentfiles; analyzers; buildtransitive all + + + + + + diff --git a/Utilities/LibMatrix.EventTypes.BasicTests/LibMatrix.EventTypes.BasicTests.csproj b/Utilities/LibMatrix.EventTypes.BasicTests/LibMatrix.EventTypes.BasicTests.csproj index b769cb7..e13103f 100644 --- a/Utilities/LibMatrix.EventTypes.BasicTests/LibMatrix.EventTypes.BasicTests.csproj +++ b/Utilities/LibMatrix.EventTypes.BasicTests/LibMatrix.EventTypes.BasicTests.csproj @@ -12,10 +12,6 @@ - - - - Always diff --git a/Utilities/LibMatrix.EventTypes.BasicTests/Program.cs b/Utilities/LibMatrix.EventTypes.BasicTests/Program.cs index 8c1e15a..d694c76 100644 --- a/Utilities/LibMatrix.EventTypes.BasicTests/Program.cs +++ b/Utilities/LibMatrix.EventTypes.BasicTests/Program.cs @@ -1,37 +1,50 @@ -using System.Text.Json; +// See https://aka.ms/new-console-template for more information + +using System.Text.Json; using ArcaneLibs.Extensions; -using BenchmarkDotNet.Attributes; -using BenchmarkDotNet.Jobs; -using BenchmarkDotNet.Running; using LibMatrix.EventTypes; -using LibMatrix.EventTypes.Events; - -BenchmarkRunner.Run(); - -[ShortRunJob] -[MemoryDiagnoser] -public class Tests { - // public MatrixEventCollection Members = [ - // new MatrixEvent() { - // Content = new() { - // Membership = "join" - // } - // } - // ]; - - private static string eventJson = File.ReadAllText("test-event.json"); - private static MatrixEvent evt2 = JsonSerializer.Deserialize>(eventJson); - [Benchmark] - public void Deserialise() { - JsonSerializer.Deserialize>(eventJson); +using LibMatrix.EventTypes.Abstractions; +using LibMatrix.EventTypes.Spec; + +Console.WriteLine("Hello, World!"); + +MatrixEventCollection Members = [ + new MatrixEvent() { + Content = new() { + Membership = "join" + } + } +]; + +string eventJson = File.ReadAllText("test-event.json"); +MatrixEvent evt2 = JsonSerializer.Deserialize>(eventJson); + +JsonSerializer.Deserialize>(eventJson); + +evt2.ToJson(); +evt2.Content.Membership = "meow"; + +MatrixEventCollection collection = new(); +collection.Add(new MatrixEvent() { + Content = new RoomMembershipEventContent() { + Membership = "yes" } - [Benchmark] - public void Serialise() { - evt2.ToJson(); +}); +MatrixEventCollection collection4 = new(); +collection4.Add(new MatrixEvent() { + Content = new RoomMembershipEventContent() { + Membership = "yes" } - - [Benchmark] - public void Modify() { - evt2.Content.Membership = "meow"; +}); + +List> collection2 = new(); +collection2.Add(new MatrixEvent() { + Content = new RoomMembershipEventContent() { + Membership = "yes" } -} \ No newline at end of file +}); + +List collection3 = new(); +collection3.Add(new RoomMembershipEventContent() { + Membership = "yes" +}); \ No newline at end of file diff --git a/Utilities/LibMatrix.EventTypes.Benchmark/LibMatrix.EventTypes.Benchmark.csproj b/Utilities/LibMatrix.EventTypes.Benchmark/LibMatrix.EventTypes.Benchmark.csproj new file mode 100644 index 0000000..b769cb7 --- /dev/null +++ b/Utilities/LibMatrix.EventTypes.Benchmark/LibMatrix.EventTypes.Benchmark.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + + + + + Always + + + diff --git a/Utilities/LibMatrix.EventTypes.Benchmark/Program.cs b/Utilities/LibMatrix.EventTypes.Benchmark/Program.cs new file mode 100644 index 0000000..512c066 --- /dev/null +++ b/Utilities/LibMatrix.EventTypes.Benchmark/Program.cs @@ -0,0 +1,37 @@ +using System.Text.Json; +using ArcaneLibs.Extensions; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Jobs; +using BenchmarkDotNet.Running; +using LibMatrix.EventTypes; +using LibMatrix.EventTypes.Spec; + +BenchmarkRunner.Run(); + +[ShortRunJob] +[MemoryDiagnoser] +public class Tests { + // public MatrixEventCollection Members = [ + // new MatrixEvent() { + // Content = new() { + // Membership = "join" + // } + // } + // ]; + + private static string eventJson = File.ReadAllText("test-event.json"); + private static MatrixEvent evt2 = JsonSerializer.Deserialize>(eventJson); + [Benchmark] + public void Deserialise() { + JsonSerializer.Deserialize>(eventJson); + } + [Benchmark] + public void Serialise() { + evt2.ToJson(); + } + + [Benchmark] + public void Modify() { + evt2.Content.Membership = "meow"; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj b/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj index 89ea5af..6e67373 100644 --- a/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj +++ b/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj @@ -12,9 +12,9 @@ - + - + -- cgit 1.4.1