using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Dynamic; namespace ConsoleApplication1 { public class SearchResult { public string Title { get; set; } public string Excerpt { get; set; } public string URL { get; set; } public int Position { get; set; } public double Score { get; set; } public int Size { get; set; } public DateTime IndexedTime { get; set; } public string CacheURL { get; set; } } class Program { static void Main(string[] args) { CheckThatWikipediaIsInResultsForDogsDynamic(); } public static void CheckThatWikipediaIsInResultsForDogs() { SearchResult expected = new SearchResult(); expected.URL = "http://en.wikipedia.org/wiki/Dogs"; expected.Title = "Dog - Wikipedia, the free encyclopedia"; expected.Position = 0; SearchEngine engine = new SearchEngine(); SearchResult actual = engine.Search("dogs"); CompareResults(expected, actual); } public static void CompareResults(SearchResult expected, SearchResult actual) { if (expected.CacheURL != null) { Assert.AreEqual(expected.CacheURL, actual.CacheURL, "CacheURL mismatch"); } if (expected.Excerpt != null) { Assert.AreEqual(expected.Excerpt, actual.Excerpt, "Excerpt mismatch"); } if (expected.IndexedTime != default(DateTime)) { Assert.AreEqual(expected.IndexedTime, actual.IndexedTime, "IndexedTime mismatch"); } if (expected.Position != default(int)) { Assert.AreEqual(expected.Position, actual.Position, "Position mismatch"); } if (expected.Score != default(double)) { Assert.AreEqual(expected.Score, actual.Score, "Score mismatch"); } if (expected.Size != default(int)) { Assert.AreEqual(expected.Size, actual.Size, "Size mismatch"); } if (expected.Title != null) { Assert.AreEqual(expected.Title, actual.Title, "Title mismatch"); } if (expected.URL != null) { Assert.AreEqual(expected.URL, actual.URL, "URL mismatch"); } } public static void CheckThatWikipediaIsInResultsForDogsDynamic() { dynamic expected = new ExpandoObject(); expected.URL = "http://en.wikipedia.org/wiki/Dogs"; expected.Title = "Dog - Wikipedia, the free encyclopedia"; expected.Position = 0; SearchEngine engine = new SearchEngine(); SearchResult actual = engine.Search("dogs"); CompareResultsDynamic(expected, actual); } public static void CompareResultsDynamic(dynamic expected, SearchResult actual) { if (PropertyExists(expected, "CacheURL")) { Assert.AreEqual(expected.CacheURL, actual.CacheURL, "CacheURL mismatch"); } if (PropertyExists(expected, "Excerpt")) { Assert.AreEqual(expected.Excerpt, actual.Excerpt, "Excerpt mismatch"); } if (PropertyExists(expected, "IndexedTime")) { Assert.AreEqual(expected.IndexedTime, actual.IndexedTime, "IndexedTime mismatch"); } if (PropertyExists(expected, "Position")) { Assert.AreEqual(expected.Position, actual.Position, "Position mismatch"); } if (PropertyExists(expected, "Score")) { Assert.AreEqual(expected.Score, actual.Score, "Score mismatch"); } if (PropertyExists(expected, "Size")) { Assert.AreEqual(expected.Size, actual.Size, "Size mismatch"); } if (PropertyExists(expected, "Title")) { Assert.AreEqual(expected.Title, actual.Title, "Title mismatch"); } if (PropertyExists(expected, "URL")) { Assert.AreEqual(expected.URL, actual.URL, "URL mismatch"); } } public static bool PropertyExists(object dynamicObject, string propertyName) { if (dynamicObject is ExpandoObject) { IDictionary expando = (IDictionary)dynamicObject; return expando.ContainsKey(propertyName); } else { return dynamicObject.GetType().GetProperty(propertyName) != null; } } } public class SearchEngine { public SearchResult Search(string query) { return new SearchResult() { CacheURL = "http://www.mathpirate.net/cache?=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDogs", Excerpt = "The dog (Canis lupus familiaris) is a domesticated form of the gray wolf, a member of the Canidae family of the order Carnivora.", IndexedTime = new DateTime(2010, 12, 31, 10, 3, 54), Position = 5, Score = 1.0, Size = 159793, Title = "Dog - Wikipedia, the free encyclopedia", URL = "http://en.wikipedia.org/wiki/Dogs" }; } } public static class Assert { public static void AreEqual(object expected, object actual, string message) { if (expected != actual) { throw new AssertFailedException(string.Format("{0} != {1}", expected, actual), message); } } } public class AssertFailedException : Exception { public AssertFailedException(string failReason, string message) : base("Assert Failed! " + failReason + " : " + message) { } } }