1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
namespace lsprotocol_tests;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class LSPTests
{
public static IEnumerable<object[]> 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: <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 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);
}
}
}
}
}
|