From 8be448d3881909fb0ce4b033cad71aa7575de0aa Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 21:55:48 +0200 Subject: Adding upstream version 2023.0.0. Signed-off-by: Daniel Baumann --- tests/dotnet/lsprotocol_tests/.gitignore | 2 + tests/dotnet/lsprotocol_tests/LSPTests.cs | 123 +++++++++++++++++++++ tests/dotnet/lsprotocol_tests/Usings.cs | 2 + .../lsprotocol_tests/lsprotocol_tests.csproj | 29 +++++ 4 files changed, 156 insertions(+) create mode 100644 tests/dotnet/lsprotocol_tests/.gitignore create mode 100644 tests/dotnet/lsprotocol_tests/LSPTests.cs create mode 100644 tests/dotnet/lsprotocol_tests/Usings.cs create mode 100644 tests/dotnet/lsprotocol_tests/lsprotocol_tests.csproj (limited to 'tests/dotnet/lsprotocol_tests') diff --git a/tests/dotnet/lsprotocol_tests/.gitignore b/tests/dotnet/lsprotocol_tests/.gitignore new file mode 100644 index 0000000..cbbd0b5 --- /dev/null +++ b/tests/dotnet/lsprotocol_tests/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ \ No newline at end of file diff --git a/tests/dotnet/lsprotocol_tests/LSPTests.cs b/tests/dotnet/lsprotocol_tests/LSPTests.cs new file mode 100644 index 0000000..80a19ae --- /dev/null +++ b/tests/dotnet/lsprotocol_tests/LSPTests.cs @@ -0,0 +1,123 @@ + +namespace lsprotocol_tests; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + + +public class LSPTests +{ + public static IEnumerable JsonTestData() + { + string folderPath; + // Read test data path from environment variable + if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("LSP_TEST_DATA_PATH"))) + { + folderPath = Environment.GetEnvironmentVariable("LSP_TEST_DATA_PATH"); + } + else + { + throw new Exception("LSP_TEST_DATA_PATH environment variable not set"); + } + + string[] jsonFiles = Directory.GetFiles(folderPath, "*.json"); + foreach (string filePath in jsonFiles) + { + yield return new object[] { filePath }; + } + } + + [Theory] + [MemberData(nameof(JsonTestData))] + public void ValidateLSPTypes(string filePath) + { + string original = File.ReadAllText(filePath); + + // Get the class name from the file name + // format: --.json + // classname => Class name of the type to deserialize to + // valid => true if the file is valid, false if it is invalid + // test-id => unique id for the test + string fileName = Path.GetFileNameWithoutExtension(filePath); + string[] nameParts = fileName.Split('-'); + string className = nameParts[0]; + bool valid = nameParts[1] == "True"; + + Type type = Type.GetType($"Microsoft.LanguageServer.Protocol.{className}, lsprotocol") ?? throw new Exception($"Type {className} not found"); + RunTest(valid, original, type); + } + + private static void RunTest(bool valid, string data, Type type) + { + if (valid) + { + try + { + var settings = new JsonSerializerSettings + { + MissingMemberHandling = MissingMemberHandling.Error + }; + object? deserializedObject = JsonConvert.DeserializeObject(data, type, settings); + string newJson = JsonConvert.SerializeObject(deserializedObject, settings); + + JToken token1 = JToken.Parse(data); + JToken token2 = JToken.Parse(newJson); + RemoveNullProperties(token1); + RemoveNullProperties(token2); + Assert.True(JToken.DeepEquals(token1, token2), $"JSON before and after serialization don't match:\r\nBEFORE:{data}\r\nAFTER:{newJson}"); + } + catch (Exception e) + { + // Explicitly fail the test + Assert.True(false, $"Should not have thrown an exception for [{type.Name}]: {data} \r\n{e}"); + } + } + else + { + try + { + JsonConvert.DeserializeObject(data, type); + // Explicitly fail the test + Assert.True(false, $"Should have thrown an exception for [{type.Name}]: {data}"); + } + catch + { + // Worked as expected. + } + } + } + + private static void RemoveNullProperties(JToken token) + { + if (token.Type == JTokenType.Object) + { + var obj = (JObject)token; + + var propertiesToRemove = obj.Properties() + .Where(p => p.Value.Type == JTokenType.Null) + .ToList(); + + foreach (var property in propertiesToRemove) + { + property.Remove(); + } + + foreach (var property in obj.Properties()) + { + RemoveNullProperties(property.Value); + } + } + else if (token.Type == JTokenType.Array) + { + var array = (JArray)token; + + for (int i = array.Count - 1; i >= 0; i--) + { + RemoveNullProperties(array[i]); + if (array[i].Type == JTokenType.Null) + { + array.RemoveAt(i); + } + } + } + } +} \ No newline at end of file diff --git a/tests/dotnet/lsprotocol_tests/Usings.cs b/tests/dotnet/lsprotocol_tests/Usings.cs new file mode 100644 index 0000000..5d95411 --- /dev/null +++ b/tests/dotnet/lsprotocol_tests/Usings.cs @@ -0,0 +1,2 @@ +global using Xunit; +global using Microsoft.LanguageServer.Protocol; \ No newline at end of file diff --git a/tests/dotnet/lsprotocol_tests/lsprotocol_tests.csproj b/tests/dotnet/lsprotocol_tests/lsprotocol_tests.csproj new file mode 100644 index 0000000..194c872 --- /dev/null +++ b/tests/dotnet/lsprotocol_tests/lsprotocol_tests.csproj @@ -0,0 +1,29 @@ + + + + net6.0 + enable + enable + + false + true + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + -- cgit v1.2.3