mirror of
https://github.com/dcronqvist/DotTiled.git
synced 2025-07-08 06:37:52 +03:00
Make Optional<T> into readonly struct to avoid heap allocations
This commit is contained in:
parent
21470cb056
commit
9aa2c63e0b
35 changed files with 176 additions and 203 deletions
|
@ -59,30 +59,6 @@ public class OptionalTests
|
|||
Assert.Equal(expectedValue, optional.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ImplicitConversion_FromOptionalToValue_ShouldReturnValue_WhenHasValueIsTrue()
|
||||
{
|
||||
// Arrange
|
||||
int expectedValue = 10;
|
||||
var optional = new Optional<int>(expectedValue);
|
||||
|
||||
// Act
|
||||
int value = optional;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedValue, value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ImplicitConversion_FromOptionalToValue_ShouldThrowException_WhenHasValueIsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var optional = new Optional<int>();
|
||||
|
||||
// Act & Assert
|
||||
_ = Assert.Throws<InvalidOperationException>(() => { int value = optional; });
|
||||
}
|
||||
|
||||
// ToString Method Tests
|
||||
|
||||
[Fact]
|
||||
|
@ -237,18 +213,6 @@ public class OptionalTests
|
|||
_ = Assert.Throws<InvalidOperationException>(() => optional.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ImplicitConversion_WhenHasValueIsFalse_ShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
var optional = new Optional<int>();
|
||||
|
||||
// Act & Assert
|
||||
_ = Assert.Throws<InvalidOperationException>(() => { int value = optional; });
|
||||
}
|
||||
|
||||
// Edge Cases
|
||||
|
||||
[Fact]
|
||||
public void EmptyOptionalEquality_ShouldReturnTrue()
|
||||
{
|
||||
|
|
|
@ -71,8 +71,8 @@ public class LoaderTests
|
|||
""");
|
||||
|
||||
var resourceCache = Substitute.For<IResourceCache>();
|
||||
resourceCache.GetTileset(Arg.Any<string>()).Returns(Optional<Tileset>.Empty);
|
||||
resourceCache.GetTemplate(Arg.Any<string>()).Returns(Optional<Template>.Empty);
|
||||
resourceCache.GetTileset(Arg.Any<string>()).Returns(Optional.Empty);
|
||||
resourceCache.GetTemplate(Arg.Any<string>()).Returns(Optional.Empty);
|
||||
|
||||
var customTypeDefinitions = Enumerable.Empty<ICustomTypeDefinition>();
|
||||
var loader = new Loader(resourceReader, resourceCache, customTypeDefinitions);
|
||||
|
@ -134,8 +134,8 @@ public class LoaderTests
|
|||
""");
|
||||
|
||||
var resourceCache = Substitute.For<IResourceCache>();
|
||||
resourceCache.GetTileset(Arg.Any<string>()).Returns(Optional<Tileset>.Empty);
|
||||
resourceCache.GetTemplate(Arg.Any<string>()).Returns(Optional<Template>.Empty);
|
||||
resourceCache.GetTileset(Arg.Any<string>()).Returns(Optional.Empty);
|
||||
resourceCache.GetTemplate(Arg.Any<string>()).Returns(Optional.Empty);
|
||||
|
||||
var customTypeDefinitions = Enumerable.Empty<ICustomTypeDefinition>();
|
||||
var loader = new Loader(resourceReader, resourceCache, customTypeDefinitions);
|
||||
|
@ -219,7 +219,7 @@ public class LoaderTests
|
|||
""");
|
||||
|
||||
var resourceCache = Substitute.For<IResourceCache>();
|
||||
resourceCache.GetTileset(Arg.Any<string>()).Returns(Optional<Tileset>.Empty);
|
||||
resourceCache.GetTileset(Arg.Any<string>()).Returns(Optional.Empty);
|
||||
resourceCache.GetTemplate("template.tx").Returns(new Optional<Template>(new Template
|
||||
{
|
||||
Object = new PolygonObject
|
||||
|
|
|
@ -39,7 +39,7 @@ public partial class MapReaderTests
|
|||
return new Optional<ICustomTypeDefinition>(ctd);
|
||||
}
|
||||
|
||||
return Optional<ICustomTypeDefinition>.Empty;
|
||||
return Optional.Empty;
|
||||
}
|
||||
using var mapReader = new MapReader(mapString, ResolveTileset, ResolveTemplate, ResolveCustomType);
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ public partial class TmjMapReaderTests
|
|||
return new Optional<ICustomTypeDefinition>(ctd);
|
||||
}
|
||||
|
||||
return Optional<ICustomTypeDefinition>.Empty;
|
||||
return Optional.Empty;
|
||||
}
|
||||
using var mapReader = new TmjMapReader(json, ResolveTileset, ResolveTemplate, ResolveCustomType);
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ public partial class TmxMapReaderTests
|
|||
return new Optional<ICustomTypeDefinition>(ctd);
|
||||
}
|
||||
|
||||
return Optional<ICustomTypeDefinition>.Empty;
|
||||
return Optional.Empty;
|
||||
}
|
||||
using var mapReader = new TmxMapReader(reader, ResolveTileset, ResolveTemplate, ResolveCustomType);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue