Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves NullReferenceException When Getting Invalid Key From TiledMapProperties #903

Merged
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
2 changes: 1 addition & 1 deletion source/MonoGame.Extended.Tiled/TiledMapProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class TiledMapProperties : Dictionary<string, TiledMapPropertyValue>
public bool TryGetValue(string key, out string value)
{
bool result = TryGetValue(key, out TiledMapPropertyValue tmpVal);
value = result ? null : tmpVal.Value;
value = result ? tmpVal.Value : null;
return result;
}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/MonoGame.Extended.Tiled.Tests/TileMapPropertiesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Craftwork Games. All rights reserved.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.

namespace MonoGame.Extended.Tiled.Tests;

public class TileMapPropertiesTest
{
// NullReferenceException in TiledMapProperties
// https://github.com/craftworkgames/MonoGame.Extended/issues/901
[Fact]
public void TryGetValue_InvalidKey_ReturnsFalse()
{
TiledMapProperties properties = new TiledMapProperties();
var result = properties.TryGetValue("invalid", out string value);
Assert.False(result);
Assert.True(string.IsNullOrEmpty(value));
}

[Fact]
public void TryGetValue_ValidKey_ReturnsFalse()
{
var expected = "value";
TiledMapProperties properties = new TiledMapProperties();
properties.Add("test", new TiledMapPropertyValue(expected));

var result = properties.TryGetValue("test", out string actual);

Assert.True(result);
Assert.Equal(expected, actual);
}
}
Loading