Skip to content

Commit

Permalink
Add nullable to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Jan 25, 2022
1 parent c1c2c5a commit acb33ce
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Tomlyn.Tests/AssertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Tomlyn.Tests
{
public static class AssertHelper
{
public static void AreEqualNormalizeNewLine(string expected, string actual, bool alwaysDisplay = false, string message = null)
public static void AreEqualNormalizeNewLine(string expected, string actual, bool alwaysDisplay = false, string? message = null)
{
expected = NormalizeEndOfLine(expected);
actual = NormalizeEndOfLine(actual);
Expand Down
4 changes: 2 additions & 2 deletions src/Tomlyn.Tests/ModelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Tomlyn.Tests
{
public static class ModelHelper
{
public static JToken ToJson(object obj)
public static JToken ToJson(object? obj)
{
switch (obj)
{
Expand Down Expand Up @@ -94,7 +94,7 @@ public static JToken ToJson(object obj)
return json;
}
}
throw new NotSupportedException($"The type element `{obj.GetType()}` is not supported");
throw new NotSupportedException($"The type element `{obj?.GetType()}` is not supported");
}
}
}
23 changes: 12 additions & 11 deletions src/Tomlyn.Tests/ModelTests/ReflectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See license.txt file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json.Serialization;
using NUnit.Framework;
using Tomlyn.Model;
Expand Down Expand Up @@ -146,18 +147,18 @@ public void TestReflectionModelWithErrors()

var result = syntax.TryToModel<SimpleModel>(out var model, out var diagnostics);

Assert.False(result);
Assert.NotNull(diagnostics);
Assert.NotNull(model);

foreach (var diagnostic in diagnostics)
foreach (var message in diagnostics)
{
Console.WriteLine(diagnostic);
Console.WriteLine(message);
}

Assert.False(result);
Assert.NotNull(model);

// Expecting 3 errors
Assert.AreEqual(3, diagnostics.Count);

Debug.Assert(model is not null);
// The model is still partially valid
Assert.AreEqual("this is a name", model.Name);

Expand Down Expand Up @@ -206,11 +207,11 @@ public SimpleModel()
SubModels = new List<SimpleSubModel>();
}

public string Name { get; set; }
public string? Name { get; set; }

public List<string> Values { get; }

public List<int> IntValues { get; set; }
public List<int>? IntValues { get; set; }

public int IntValue { get; set; }

Expand All @@ -222,7 +223,7 @@ public SimpleModel()

public class SimpleSubModel
{
public string Id { get; set; }
public string? Id { get; set; }

public bool Publish { get; set; }

Expand All @@ -245,14 +246,14 @@ public class PrimitiveModel : IEquatable<PrimitiveModel>
public DateTimeOffset DateTimeOffset { get; set; }
public TomlDateTime TomlDateTime { get; set; }

public bool Equals(PrimitiveModel other)
public bool Equals(PrimitiveModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Int8Value == other.Int8Value && Int16Value == other.Int16Value && Int32Value == other.Int32Value && Int64Value == other.Int64Value && UInt8Value == other.UInt8Value && UInt16Value == other.UInt16Value && UInt32Value == other.UInt32Value && UInt64Value == other.UInt64Value && Float32Value.Equals(other.Float32Value) && Float64Value.Equals(other.Float64Value) && DateTime.Equals(other.DateTime) && DateTimeOffset.Equals(other.DateTimeOffset) && TomlDateTime.Equals(other.TomlDateTime);
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
Expand Down
8 changes: 5 additions & 3 deletions src/Tomlyn.Tests/ModelTests/TomlTableModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See license.txt file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
Expand Down Expand Up @@ -60,7 +61,8 @@ public static void TestBasic()
Assert.AreEqual((long)1, model["a"]);
Assert.AreEqual(true, model["b"]);
Assert.IsInstanceOf<TomlTable>(model["c"]);
var subTable = ((TomlTable) model["c"]);
var subTable = ((TomlTable?) model["c"]);
Debug.Assert(subTable is not null);
Assert.True(subTable.ContainsKey("d"));
Assert.AreEqual(1, subTable.Count);
Assert.AreEqual("yo", subTable["d"]);
Expand Down Expand Up @@ -109,7 +111,7 @@ public static void TestTable()
Assert.AreEqual(2, model.Count);
Assert.AreEqual((long)1, model["a"]);
Assert.IsInstanceOf<TomlTable>(model["b"]);
var subTable = ((TomlTable)model["b"]);
var subTable = ((TomlTable)model["b"]!);
Assert.True(subTable.ContainsKey("c"));
Assert.True(subTable.ContainsKey("d"));
Assert.AreEqual(2, subTable.Count);
Expand Down Expand Up @@ -139,7 +141,7 @@ public static void TestTableArray()
Assert.AreEqual(2, model.Count);
Assert.AreEqual((long)1, model["a"]);
Assert.IsInstanceOf<TomlTableArray>(model["b"]);
var tableArray = ((TomlTableArray)model["b"]);
var tableArray = ((TomlTableArray)model["b"]!);
Assert.AreEqual(2, tableArray.Count);

// first [[b]]
Expand Down
12 changes: 6 additions & 6 deletions src/Tomlyn.Tests/StandardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static void ValidateSpec(string type, string inputName, string toml, str
Assert.AreEqual(toml, roundtrip, "The roundtrip doesn't match");

// Read the original json
var expectedJson = (JObject)NormalizeJson(JObject.Parse(json));
var expectedJson = (JObject)NormalizeJson(JObject.Parse(json))!;
// Convert the syntax tree into a model
var model = doc.ToModel();
// Convert the model into the expected json
Expand Down Expand Up @@ -98,14 +98,14 @@ private static void ValidateSpec(string type, string inputName, string toml, str
}
}

private static JToken NormalizeJson(JToken token)
private static JToken? NormalizeJson(JToken? token)
{
if (token is JArray array)
{
var newArray = new JArray();
foreach (var item in array)
{
newArray.Add(NormalizeJson(item));
newArray.Add(NormalizeJson(item)!);
}

return newArray;
Expand All @@ -114,7 +114,7 @@ private static JToken NormalizeJson(JToken token)
{
var newObject = new JObject();

var items = new List<KeyValuePair<string, JToken>>();
var items = new List<KeyValuePair<string, JToken?>>();
foreach (var item in obj)
{
items.Add(item);
Expand Down Expand Up @@ -185,7 +185,7 @@ public static IEnumerable ListTomlFiles(string type)

var input = Encoding.UTF8.GetString(File.ReadAllBytes(file));

string json = null;
string? json = null;
if (type == "valid")
{
var jsonFile = Path.ChangeExtension(file, "json");
Expand All @@ -202,7 +202,7 @@ private static string BaseDirectory
get
{
var assembly = Assembly.GetExecutingAssembly();
return Path.GetDirectoryName(assembly.Location);
return Path.GetDirectoryName(assembly.Location) ?? Environment.CurrentDirectory;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Tomlyn.Tests/SyntaxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public void TestDocument()
table.AddLeadingComment("This is a comment");
table.AddLeadingTriviaNewLine();

var firstElement = table.Items.GetChild(0);
var firstElement = table.Items.GetChild(0)!;
firstElement.AddTrailingComment("This is an item comment");

var secondElement = table.Items.GetChild(2);
var secondElement = table.Items.GetChild(2)!;
secondElement.AddLeadingTriviaNewLine();
secondElement.AddLeadingComment("This is a comment in a middle of a table");
secondElement.AddLeadingTriviaNewLine();
Expand Down Expand Up @@ -92,8 +92,8 @@ public void Sample()

// Gets a runtime representation of the syntax tree
var table = doc.ToModel();
var key = (long) ((TomlTable) table["mytable"])["key"];
var value = (bool) ((TomlTable) table["mytable"])["val"];
var key = (long) ((TomlTable) table["mytable"]!)["key"]!;
var value = (bool) ((TomlTable) table["mytable"]!)["val"]!;
Console.WriteLine($"key = {key}, val = {value}");
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Tomlyn.Tests/Tomlyn.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit acb33ce

Please sign in to comment.