Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions source/Handlebars.Benchmark/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ namespace HandlebarsNet.Benchmark
{
public class Compilation
{
private IHandlebars _handlebars;
private IHandlebars _handlebars = null!;

[GlobalSetup]
public void Setup()
{
_handlebars = Handlebars.Create();
using(_handlebars.Configure())
{
_handlebars.RegisterHelper("pow1", (output, context, arguments) => output.WriteSafeString(((int)arguments[0] * (int) arguments[0]).ToString()));
_handlebars.RegisterHelper("pow2", (output, context, arguments) => output.WriteSafeString(((int)arguments[0] * (int) arguments[0]).ToString()));
_handlebars.RegisterHelper("pow5", (output, options, context, arguments) => output.WriteSafeString(((int)arguments[0] * (int) arguments[0]).ToString()));
_handlebars.RegisterHelper("pow1", (output, context, arguments) => output.WriteSafeString(((int)arguments[0]! * (int) arguments[0]!).ToString()));
_handlebars.RegisterHelper("pow2", (output, context, arguments) => output.WriteSafeString(((int)arguments[0]! * (int) arguments[0]!).ToString()));
_handlebars.RegisterHelper("pow5", (output, options, context, arguments) => output.WriteSafeString(((int)arguments[0]! * (int) arguments[0]!).ToString()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/Handlebars.Benchmark/CompileMany.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace HandlebarsNet.Benchmark
[MemoryDiagnoser]
public class CompileMany
{
private IHandlebars _env;
private IHandlebars _env = null!; // -> Setup()

// A realistic multi-expression template; keeps compilation non-trivial.
private const string Template =
Expand Down
16 changes: 8 additions & 8 deletions source/Handlebars.Benchmark/EndToEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace HandlebarsNet.Benchmark
{
public class EndToEnd
{
private object _data;
private HandlebarsTemplate<TextWriter, object, object> _default;
private object? _data;
private HandlebarsTemplate<TextWriter, object, object> _default = null!; // -> Setup()

[Params(5)]
public int N { get; set; }

[Params("object", "dictionary")]
public string DataType { get; set; }
public string DataType { get; set; } = null!;

[GlobalSetup]
public void Setup()
Expand Down Expand Up @@ -181,12 +181,12 @@ private class PowHelper : IHelperDescriptor<HelperOptions>

public object Invoke(in HelperOptions options, in Context context, in Arguments arguments)
{
return ((int)arguments[0] * (int)arguments[0]).ToString();
return ((int)arguments[0]! * (int)arguments[0]!).ToString();
}

public void Invoke(in EncodedTextWriter output, in HelperOptions options, in Context context, in Arguments arguments)
{
output.WriteSafeString(((int)arguments[0] * (int)arguments[0]).ToString());
output.WriteSafeString(((int)arguments[0]! * (int)arguments[0]!).ToString());
}
}

Expand All @@ -198,12 +198,12 @@ private class BlockPowHelper : IHelperDescriptor<BlockHelperOptions>

public object Invoke(in BlockHelperOptions options, in Context context, in Arguments arguments)
{
return ((int)arguments[0] * (int)arguments[0]).ToString();
return ((int)arguments[0]! * (int)arguments[0]!).ToString();
}

public void Invoke(in EncodedTextWriter output, in BlockHelperOptions options, in Context context, in Arguments arguments)
{
output.WriteSafeString(((int)arguments[0] * (int)arguments[0]).ToString());
output.WriteSafeString(((int)arguments[0]! * (int)arguments[0]!).ToString());
}
}

Expand Down
1 change: 1 addition & 0 deletions source/Handlebars.Benchmark/Handlebars.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<RootNamespace>HandlebarsNet.Benchmark</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions source/Handlebars.Benchmark/LargeArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace HandlebarsNet.Benchmark
{
public class LargeArray
{
private object _data;
private HandlebarsTemplate<TextWriter, object, object> _default;
private object _data = null!; // -> Setup()
private HandlebarsTemplate<TextWriter, object, object> _default = null!; // -> Setup()

[Params(20000, 40000, 80000)]
public int N { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions source/Handlebars.Benchmark/RenderList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace HandlebarsNet.Benchmark
[MemoryDiagnoser]
public class RenderList
{
private HandlebarsTemplate<TextWriter, object, object> _template;
private object _data;
private HandlebarsTemplate<TextWriter, object, object> _template = null!; // -> Setup()
private object _data = null!; // -> Setup()

private const string Source =
"{{#each items}}" +
Expand All @@ -26,7 +26,7 @@ public class RenderList
public int N { get; set; }

[Params("object", "dictionary")]
public string DataType { get; set; }
public string DataType { get; set; } = null!;

[GlobalSetup]
public void Setup()
Expand Down
16 changes: 8 additions & 8 deletions source/Handlebars.Benchmark/RenderNested.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace HandlebarsNet.Benchmark
[MemoryDiagnoser]
public class RenderNested
{
private HandlebarsTemplate<TextWriter, object, object> _template;
private object _data;
private HandlebarsTemplate<TextWriter, object, object> _template = null!; // -> Setup()
private object _data = null!; // -> Setup()

private const int SectionCount = 5;

Expand All @@ -32,7 +32,7 @@ public class RenderNested
public int RowsPerSection { get; set; }

[Params("object", "dictionary")]
public string DataType { get; set; }
public string DataType { get; set; } = null!;

[GlobalSetup]
public void Setup()
Expand All @@ -53,7 +53,7 @@ public void Setup()
private static readonly string[] SectionNames = ["Traffic", "Revenue", "Errors", "Performance", "Conversion"];
private static readonly string[] MetricLabels = ["Count", "Rate", "p50", "p99", "Total", "Delta", "Avg", "Max", "Min", "StdDev",
"Score", "Index", "Ratio", "Share", "Volume", "Trend", "Baseline", "Peak", "Trough", "Median"];
private static readonly string[] Units = ["req/s", "%", "ms", "$", null, null]; // nulls make ~half unitless
private static readonly string?[] Units = ["req/s", "%", "ms", "$", null, null]; // nulls make ~half unitless

private object BuildObjectData()
{
Expand All @@ -66,7 +66,7 @@ private object BuildObjectData()
{
bool withinTarget = (s * RowsPerSection + r) % 4 != 0;
if (!withinTarget) alertCount++;
string unit = Units[(s * RowsPerSection + r) % Units.Length];
string? unit = Units[(s * RowsPerSection + r) % Units.Length];
metrics.Add(new
{
label = MetricLabels[(s * RowsPerSection + r) % MetricLabels.Length],
Expand Down Expand Up @@ -94,13 +94,13 @@ private Dictionary<string, object> BuildDictionaryData()
var sections = new List<Dictionary<string, object>>(SectionCount);
for (int s = 0; s < SectionCount; s++)
{
var metrics = new List<Dictionary<string, object>>(RowsPerSection);
var metrics = new List<Dictionary<string, object?>>(RowsPerSection);
for (int r = 0; r < RowsPerSection; r++)
{
bool withinTarget = (s * RowsPerSection + r) % 4 != 0;
if (!withinTarget) alertCount++;
string unit = Units[(s * RowsPerSection + r) % Units.Length];
metrics.Add(new Dictionary<string, object>
string? unit = Units[(s * RowsPerSection + r) % Units.Length];
metrics.Add(new Dictionary<string, object?>
{
["label"] = MetricLabels[(s * RowsPerSection + r) % MetricLabels.Length],
["value"] = $"{42.0 + s * 10 + r:F1}",
Expand Down
6 changes: 3 additions & 3 deletions source/Handlebars.Benchmark/RenderSimple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace HandlebarsNet.Benchmark
[MemoryDiagnoser]
public class RenderSimple
{
private HandlebarsTemplate<TextWriter, object, object> _template;
private object _data;
private HandlebarsTemplate<TextWriter, object, object> _template = null!; // -> Setup()
private object _data = null!; // -> Setup()

private const string Source =
"Dear {{user.firstName}} {{user.lastName}},\n\n" +
Expand All @@ -23,7 +23,7 @@ public class RenderSimple
"— The {{store.name}} Team";

[Params("object", "dictionary", "expando")]
public string DataType { get; set; }
public string DataType { get; set; } = null!;

[GlobalSetup]
public void Setup()
Expand Down
35 changes: 18 additions & 17 deletions source/Handlebars.Test/BasicIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Linq;
using System.Reflection;
Expand All @@ -22,7 +23,7 @@ public class BasicIntegrationTests
private static string HtmlEncodeStringHelper(IHandlebars handlebars, string inputString)
{
using var stringWriter = new System.IO.StringWriter();
handlebars.Configuration.TextEncoder.Encode(inputString, stringWriter);
handlebars.Configuration.TextEncoder!.Encode(inputString, stringWriter);
return stringWriter.ToString();
}

Expand Down Expand Up @@ -208,7 +209,7 @@ public void BasicPathThrowOnNestedUnresolvedBindingExpression(IHandlebars handle

var data = new
{
foo = (object)null
foo = (object?)null
};
var ex = Assert.Throws<HandlebarsUndefinedBindingException>(() => template(data));

Expand All @@ -230,7 +231,7 @@ public void BasicPathNoThrowOnNullExpression(IHandlebars handlebars)

var data = new
{
foo = (string)null
foo = (string?)null
};
var result = template(data);
Assert.Contains("false", result);
Expand Down Expand Up @@ -1280,7 +1281,7 @@ public void BasicDeferredBlockNull(IHandlebars handlebars)

var data = new
{
person = (object)null
person = (object?)null
};

var result = template(data);
Expand Down Expand Up @@ -1384,7 +1385,7 @@ public void BasicNullOrMissingSubProperty(IHandlebars handlebars)

var data = new
{
name = (object)null
name = (object?)null
};

var result = template(data);
Expand Down Expand Up @@ -1416,7 +1417,7 @@ public void BasicNullFalsy(IHandlebars handlebars)

var data = new
{
falsy = (object)null
falsy = (object?)null
};

var result = template(data);
Expand Down Expand Up @@ -1978,10 +1979,10 @@ public void CollectionReturnFromHelper(IHandlebars handlebars)
{
handlebars.RegisterHelper($"getData", (context, arguments) =>
{
var data = new Dictionary<string, string>
var data = new Dictionary<string, string?>
{
{"Nils", arguments[0].ToString()},
{"Yehuda", arguments[1].ToString()}
{"Nils", arguments[0]!.ToString()},
{"Yehuda", arguments[1]!.ToString()}
};

return data;
Expand Down Expand Up @@ -2209,7 +2210,7 @@ public void ChainedPathIteratorHelper()

private class StringHelperResolver : IHelperResolver
{
public bool TryResolveHelper(PathInfo name, Type targetType, out IHelperDescriptor<HelperOptions> helper)
public bool TryResolveHelper(PathInfo name, Type? targetType, out IHelperDescriptor<HelperOptions> helper)
{
if (targetType == typeof(string))
{
Expand All @@ -2218,21 +2219,21 @@ public bool TryResolveHelper(PathInfo name, Type targetType, out IHelperDescript

if (method == null)
{
helper = null;
helper = null!;
return false;
}

helper = new HelperDescriptor(name, method);
return true;
}

helper = null;
helper = null!;
return false;
}

public bool TryResolveBlockHelper(PathInfo name, out IHelperDescriptor<BlockHelperOptions> helper)
{
helper = null;
helper = null!;
return false;
}

Expand All @@ -2249,7 +2250,7 @@ public HelperDescriptor(PathInfo name, MethodInfo methodInfo)
public PathInfo Name { get; }
public object Invoke(in HelperOptions options, in Context context, in Arguments arguments)
{
return _methodInfo.Invoke(arguments[0], arguments.AsEnumerable().Skip(1).ToArray());
return _methodInfo.Invoke(arguments[0], arguments.AsEnumerable().Skip(1).ToArray())!;
}

public void Invoke(in EncodedTextWriter output, in HelperOptions options, in Context context, in Arguments arguments)
Expand All @@ -2270,7 +2271,7 @@ public bool TryCreateFormatter(Type type, out IFormatter formatter)
{
if (type != typeof(UndefinedBindingResult))
{
formatter = null;
formatter = null!;
return false;
}

Expand All @@ -2297,7 +2298,7 @@ public bool TryCreateFormatter(Type type, out IFormatter formatter)
{
if (type != typeof(DateTime))
{
formatter = null;
formatter = null!;
return false;
}

Expand Down Expand Up @@ -2458,7 +2459,7 @@ public bool Remove(KeyValuePair<string, object> item)

public bool TryGetValue(string key, out object value)
{
return ((IDictionary<string, object>)data).TryGetValue(key, out value);
return ((IDictionary<string, object>)data).TryGetValue(key, out value!);
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
Expand Down
12 changes: 6 additions & 6 deletions source/Handlebars.Test/ClosureBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ public void GeneratesClosureWithOverflow()

Assert.Equal(paths[0], closure.PI0);
Assert.Equal(paths[3], closure.PI3);
Assert.Equal(paths[5], closure.PIA[1]);
Assert.Equal(paths[5], closure.PIA![1]);

Assert.Equal(helpers[0], closure.HD0);
Assert.Equal(helpers[3], closure.HD3);
Assert.Equal(helpers[5], closure.HDA[1]);
Assert.Equal(helpers[5], closure.HDA![1]);

Assert.Equal(blockHelpers[0], closure.BHD0);
Assert.Equal(blockHelpers[3], closure.BHD3);
Assert.Equal(blockHelpers[5], closure.BHDA[1]);
Assert.Equal(blockHelpers[5], closure.BHDA![1]);

Assert.Equal(decoratorDelegates[0], closure.DDD0);
Assert.Equal(decoratorDelegates[3], closure.DDD3);
Assert.Equal(decoratorDelegates[5], closure.DDDA[1]);
Assert.Equal(decoratorDelegates[5], closure.DDDA![1]);

Assert.Equal(others[0], closure.A[0]);
Assert.Equal(others[3], closure.A[3]);
Expand Down Expand Up @@ -97,7 +97,7 @@ private static List<Ref<IHelperDescriptor<BlockHelperOptions>>> GenerateBlockHel
var blockHelpers = new List<Ref<IHelperDescriptor<BlockHelperOptions>>>();
for (int i = 0; i < count; i++)
{
var blockHelper = new Ref<IHelperDescriptor<BlockHelperOptions>>();
var blockHelper = new Ref<IHelperDescriptor<BlockHelperOptions>>(null!);
builder.Add(Const(blockHelper));
blockHelpers.Add(blockHelper);
}
Expand All @@ -110,7 +110,7 @@ private static List<Ref<IHelperDescriptor<HelperOptions>>> GenerateHelpers(Closu
var helpers = new List<Ref<IHelperDescriptor<HelperOptions>>>();
for (int i = 0; i < count; i++)
{
var helper = new Ref<IHelperDescriptor<HelperOptions>>();
var helper = new Ref<IHelperDescriptor<HelperOptions>>(null!);
builder.Add(Const(helper));
helpers.Add(helper);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CollisionsComparer(int hash)
_hash = hash;
}

public new bool Equals(object x, object y) => false;
public new bool Equals(object? x, object? y) => false;

public int GetHashCode(object obj) => _hash;
}
Expand Down
2 changes: 1 addition & 1 deletion source/Handlebars.Test/ComplexIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void BlockHelperHelper()
});

Handlebars.RegisterHelper("block_helper", (writer, options, context, arguments) => {
foreach(var item in arguments[0] as IEnumerable)
foreach(var item in (IEnumerable)arguments[0]!)
{
options.Template(writer, item);
}
Expand Down
Loading
Loading