Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Jun 27, 2023
1 parent 6d675bf commit 62850e6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion generator-plugins/testdata/__init__.py
Original file line number Diff line number Diff line change
@@ -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
from .testdata_utils import generate_from_spec as generate
36 changes: 27 additions & 9 deletions tests/dotnet/lsprotocol_tests/LSPTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ namespace lsprotocol_tests;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


public class LSPTests
{
public static IEnumerable<object[]> 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)
{
Expand All @@ -22,17 +24,33 @@ public void ValidateLSPTypes(string filePath)
string original = File.ReadAllText(filePath);

// Get the class name from the file name
// format: <class-name>_<test-id>.json
// format: <class-name>-<valid>-<test-id>.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<Exception>(() => JsonConvert.DeserializeObject(data, type));
}
}
}
1 change: 1 addition & 0 deletions tests/dotnet/lsprotocol_tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
global using Xunit;
global using Microsoft.LanguageServer.Protocol;

0 comments on commit 62850e6

Please sign in to comment.