New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Collections Extensions Tests

83 Views
Copy Code Show/Hide Line Numbers
[TestMethod]
public void CanGetValueItemfromCollection()
{
    var col = new Collection<string> {"Hello", "World"};
    var testItem = col.GetValueItem(c => c.Equals("World"));
    Assert.AreEqual(testItem, "World");
}
 
[TestMethod]
public void CanGetReferenceItemfromCollectionByProperty()
{
    var col = new Collection<TestPerson>();
    var person1 = new TestPerson {ID = 1, Name = "Bob"};
    col.Add(person1);
    var person2 = new TestPerson { ID = 2, Name = "Amy" };
    col.Add(person2);
    // known good by ID
    var testItem = col.GetReferenceItem(c => c.ID.Equals(2));
    Assert.AreEqual(testItem.Name,"Amy");
    // known does not exist
    testItem = col.GetReferenceItem(c => c.ID.Equals(3));
    Assert.IsNull(testItem.Name);
    // known good by Name
    testItem = col.GetReferenceItem(c => c.Name.Equals("Bob"));
    Assert.AreEqual(testItem.ID, 1);
}
 
/// <summary>
/// TestPerson class for simple mocking
/// </summary>
public class TestPerson
{
    /// <summary>
    /// ID
    /// </summary>
    public int ID { get; set; }
    /// <summary>
    /// Name
    /// </summary>
    public string Name { get; set; }
}
by Bob Baker
  December 18, 2009 @ 12:01pm
Tags:
Comment:
Test for the Collection Extensions

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate