From 62850e61cd8bfe46ccb4558b1cdfa44c620e7b7d Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Tue, 27 Jun 2023 15:01:34 -0700 Subject: [PATCH] Fix tests --- generator-plugins/testdata/__init__.py | 2 +- tests/dotnet/lsprotocol_tests/LSPTests.cs | 36 +++++++++++++++++------ tests/dotnet/lsprotocol_tests/Usings.cs | 1 + 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/generator-plugins/testdata/__init__.py b/generator-plugins/testdata/__init__.py index 27b58a8..35cc498 100644 --- a/generator-plugins/testdata/__init__.py +++ b/generator-plugins/testdata/__init__.py @@ -1,4 +1,4 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from .testdata_utils import generate_from_spec as generate \ No newline at end of file +from .testdata_utils import generate_from_spec as generate diff --git a/tests/dotnet/lsprotocol_tests/LSPTests.cs b/tests/dotnet/lsprotocol_tests/LSPTests.cs index 8eea6f3..4b90c09 100644 --- a/tests/dotnet/lsprotocol_tests/LSPTests.cs +++ b/tests/dotnet/lsprotocol_tests/LSPTests.cs @@ -3,11 +3,13 @@ namespace lsprotocol_tests; using Newtonsoft.Json; using Newtonsoft.Json.Linq; + public class LSPTests { public static IEnumerable JsonTestData() { - string folderPath = Environment.GetEnvironmentVariable("JSON_FOLDER_PATH"); + string folderPath = "C:\\GIT\\LSP\\lsprotocol\\packages\\testdata"; + string[] jsonFiles = Directory.GetFiles(folderPath, "*.json"); foreach (string filePath in jsonFiles) { @@ -22,17 +24,33 @@ public void ValidateLSPTypes(string filePath) string original = File.ReadAllText(filePath); // Get the class name from the file name - // format: _.json + // 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[] nameParts = fileName.Split('-'); string className = nameParts[0]; + bool valid = nameParts[1] == "True"; - Type type = Type.GetType(className) ?? throw new Exception($"Type {className} not found"); - object? deserializedObject = JsonConvert.DeserializeObject(original, type); - string newJson = JsonConvert.SerializeObject(deserializedObject); + Type type = Type.GetType($"Microsoft.LanguageServer.Protocol.{className}, lsprotocol") ?? throw new Exception($"Type {className} not found"); + RunTest(valid, original, type); + } - JToken token1 = JToken.Parse(original); - JToken token2 = JToken.Parse(newJson); - Assert.True(JToken.DeepEquals(token1, token2)); + private static void RunTest(bool valid, string data, Type type) + { + if (valid) + { + object? deserializedObject = JsonConvert.DeserializeObject(data, type); + string newJson = JsonConvert.SerializeObject(deserializedObject); + + JToken token1 = JToken.Parse(data); + JToken token2 = JToken.Parse(newJson); + Assert.True(JToken.DeepEquals(token1, token2)); + } + else + { + Assert.Throws(() => JsonConvert.DeserializeObject(data, type)); + } } } \ No newline at end of file diff --git a/tests/dotnet/lsprotocol_tests/Usings.cs b/tests/dotnet/lsprotocol_tests/Usings.cs index c802f44..5d95411 100644 --- a/tests/dotnet/lsprotocol_tests/Usings.cs +++ b/tests/dotnet/lsprotocol_tests/Usings.cs @@ -1 +1,2 @@ global using Xunit; +global using Microsoft.LanguageServer.Protocol; \ No newline at end of file