public void GetInvalidKey_ThrowsInvalidPathSegmentException() { // Arrange var dictionaryAdapter = new DictionaryAdapter <int, object>(); var resolver = new DefaultContractResolver(); var key = 1; var dictionary = new Dictionary <int, object>(); // Act var addStatus = dictionaryAdapter.TryAdd(dictionary, key.ToString(CultureInfo.InvariantCulture), resolver, "James", out var message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Single(dictionary); Assert.Equal("James", dictionary[key]); // Act var guidKey = new Guid(); var getStatus = dictionaryAdapter.TryGet(dictionary, guidKey.ToString(), resolver, out var outValue, out message); // Assert Assert.False(getStatus); Assert.Equal($"The provided path segment '{guidKey.ToString()}' cannot be converted to the target type.", message); Assert.Null(outValue); }
public void Get_UsingCaseSensitiveKey_FailureScenario() { // Arrange var dictionaryAdapter = new DictionaryAdapter <string, object>(); var resolver = new DefaultContractResolver(); var nameKey = "Name"; var dictionary = new Dictionary <string, object>(StringComparer.Ordinal); // Act var addStatus = dictionaryAdapter.TryAdd(dictionary, nameKey, resolver, "James", out var message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Single(dictionary); Assert.Equal("James", dictionary[nameKey]); // Act var getStatus = dictionaryAdapter.TryGet(dictionary, nameKey.ToUpperInvariant(), resolver, out var outValue, out message); // Assert Assert.False(getStatus); Assert.Equal("The target location specified by path segment 'NAME' was not found.", message); Assert.Null(outValue); }
public void Get_UsingCaseSensitiveKey_SuccessScenario() { // Arrange var dictionaryAdapter = new DictionaryAdapter <string, object>(); var resolver = new DefaultContractResolver(); var nameKey = "Name"; var dictionary = new Dictionary <string, object>(StringComparer.Ordinal); // Act var addStatus = dictionaryAdapter.TryAdd(dictionary, nameKey, resolver, "James", out var message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Single(dictionary); Assert.Equal("James", dictionary[nameKey]); // Act addStatus = dictionaryAdapter.TryGet(dictionary, nameKey, resolver, out var outValue, out message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Equal("James", outValue?.ToString()); }
public void GetInvalidKey_ThrowsInvalidPathSegmentException() { // Arrange var dictionaryAdapter = new DictionaryAdapter <int, object>(); var key = 1; var dictionary = new Dictionary <int, object>(); var options = new JsonSerializerOptions(); // Act var addStatus = dictionaryAdapter.TryAdd(dictionary, typeof(Dictionary <int, object>), key.ToString(), options, "James", out var message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Single(dictionary); Assert.Equal("James", dictionary[key]); // Act var guidKey = new Guid(); var getStatus = dictionaryAdapter.TryGet(dictionary, typeof(Dictionary <int, object>), guidKey.ToString(), options, out var outValue, out message); // Assert Assert.False(getStatus); Assert.Equal($"The provided path segment '{guidKey}' cannot be converted to the target type.", message); Assert.Null(outValue); }
public void Get_UsingCaseSensitiveKey_SuccessScenario() { // Arrange var dictionaryAdapter = new DictionaryAdapter <string, object>(); var nameKey = "Name"; var dictionary = new Dictionary <string, object>(StringComparer.Ordinal); var options = new JsonSerializerOptions(); // Act var addStatus = dictionaryAdapter.TryAdd(dictionary, typeof(Dictionary <string, object>), nameKey, options, "James", out var message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Single(dictionary); Assert.Equal("James", dictionary[nameKey]); // Act addStatus = dictionaryAdapter.TryGet(dictionary, typeof(Dictionary <string, object>), nameKey, options, out var outValue, out message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Equal("James", outValue?.ToString()); }
public void Add_IntKeyWhichAlreadyExists_ReplacesExistingValue() { // Arrange var intKey = 1; var dictionary = new Dictionary <int, object>(); dictionary[intKey] = "Mike"; var dictionaryAdapter = new DictionaryAdapter <int, object>(); var resolver = new DefaultContractResolver(); // Act var addStatus = dictionaryAdapter.TryAdd(dictionary, intKey.ToString(CultureInfo.InvariantCulture), resolver, "James", out var message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Single(dictionary); Assert.Equal("James", dictionary[intKey]); }
public void Add_KeyWhichAlreadyExists_ReplacesExistingValue() { // Arrange var key = "Status"; var dictionary = new Dictionary <string, int>(StringComparer.Ordinal); dictionary[key] = 404; var dictionaryAdapter = new DictionaryAdapter <string, int>(); var resolver = new DefaultContractResolver(); // Act var addStatus = dictionaryAdapter.TryAdd(dictionary, key, resolver, 200, out var message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Single(dictionary); Assert.Equal(200, dictionary[key]); }
public void Add_IntKeyWhichAlreadyExists_ReplacesExistingValue() { // Arrange var intKey = 1; var dictionary = new Dictionary <int, object> { [intKey] = "Mike" }; var dictionaryAdapter = new DictionaryAdapter <int, object>(); var options = new JsonSerializerOptions(); // Act var addStatus = dictionaryAdapter.TryAdd(dictionary, typeof(Dictionary <int, object>), intKey.ToString(), options, "James", out var message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Single(dictionary); Assert.Equal("James", dictionary[intKey]); }
public void Add_KeyWhichAlreadyExists_ReplacesExistingValue() { // Arrange var key = "Status"; var dictionary = new Dictionary <string, int>(StringComparer.Ordinal) { [key] = 404 }; var dictionaryAdapter = new DictionaryAdapter <string, int>(); var options = new JsonSerializerOptions(); // Act var addStatus = dictionaryAdapter.TryAdd(dictionary, typeof(Dictionary <string, int>), key, options, 200, out var message); // Assert Assert.True(addStatus); Assert.True(string.IsNullOrEmpty(message), "Expected no error message"); Assert.Single(dictionary); Assert.Equal(200, dictionary[key]); }