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

Supporting Multiple TestProperty with the same key value. #328

Merged
merged 4 commits into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,29 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
{
using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(data)))
{
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Dictionary<string, string>));
var dict = serializer.ReadObject(stream) as Dictionary<string, string>;
List<KeyValuePair<string, string>> listOfKvps = new List<KeyValuePair<string, string>>();

return dict?.ToArray();
// Converting Json data to array of KeyValuePairs with duplicate keys.
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<TraitObject>));

var listOfTratiObjects = serializer.ReadObject(stream) as List<TraitObject>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/listOfTratiObjects/listOfTraitObjects

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved it.

listOfTratiObjects.ForEach(o=>listOfKvps.Add(new KeyValuePair<string, string>(o.Key, o.Value)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra allocation and insertion into listOfKvps may not be required. Is something like below possible?

listOfTratiObjects.Select(t => new KeyValuePair<string, string>(t.Key, t.Value)).ToArray()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes possible. Changed it.


return listOfKvps?.ToArray();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is null check required for listOfKvps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

}
}

return base.ConvertFrom(context, culture, value);
}

[System.Runtime.Serialization.DataContract]
private class TraitObject
{
[System.Runtime.Serialization.DataMember(Name = "Key")]
public string Key { get; set; }

[System.Runtime.Serialization.DataMember(Name = "Value")]
public string Value { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,30 @@ public void CustomKeyValueConverterShouldDeserializeEmptyArray()
}

[TestMethod]
public void CustomKeyValueConverterShouldThrowOnDeserializeNullKeyOrValue()
public void CustomKeyValueConverterShouldDeserializeEmptyKeyOrValue()
{
var json = "[{ \"Key\": null, \"Value\": \"\" }]";
var json = "[{ \"Key\": \"\", \"Value\": \"\" }]";

var action = new Action(() => this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json));
var data = this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair<string, string>[];

Assert.ThrowsException<ArgumentNullException>(action);
Assert.AreEqual(1, data.Length);
Assert.AreEqual(string.Empty, data[0].Key);
Assert.AreEqual(string.Empty, data[0].Value);
}

[TestMethod]
public void CustomKeyValueConverterShouldDeserializeEmptyKeyOrValue()
public void CustomKeyValueConverterShouldDeserializeDuplicateKeysKvps()
{
var json = "[{ \"Key\": \"\", \"Value\": \"\" }]";
var json = "[{ \"Key\": \"key1\", \"Value\": \"val1\" }, { \"Key\": \"key1\", \"Value\": \"val2\" }]";

var data = this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair<string, string>[];

Assert.AreEqual(1, data.Length);
Assert.AreEqual(string.Empty, data[0].Key);
Assert.AreEqual(string.Empty, data[0].Value);
Assert.IsNotNull(data);
Assert.AreEqual(2, data.Length);
Assert.AreEqual("key1", data[0].Key);
Assert.AreEqual("val1", data[0].Value);
Assert.AreEqual("key1", data[1].Key);
Assert.AreEqual("val2", data[1].Value);
}
}
}