Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using System.Collections.Generic;
- using System.Linq;
- using Wintellect.PowerCollections;
- using CatalogOfFreeContent;
- using System.Reflection;
- namespace CatalogTests
- {
- [TestClass]
- public class CatalogTests
- {
- [TestMethod]
- public void TestAddTitleWithSingleElement()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "16148072", "http://www.mozilla.org" }));
- // Test Reflection
- FieldInfo title = typeof(Catalog).GetField("title", BindingFlags.NonPublic | BindingFlags.Instance);
- OrderedMultiDictionary<string, IContent> titleList = (OrderedMultiDictionary<string, IContent>)title.GetValue(catalog);
- Assert.AreEqual(titleList.Count, 1);
- Assert.IsTrue(titleList.ContainsKey("Firefox v.11.0"));
- }
- [TestMethod]
- public void TestAddUrlWithSingleElement()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "16148072", "http://www.mozilla.org" }));
- FieldInfo url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
- MultiDictionary<string, IContent> urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
- Assert.AreEqual(urlList.Count, 1);
- Assert.IsTrue(urlList.ContainsKey("http://www.mozilla.org"));
- }
- [TestMethod]
- public void TestAddDublicateValue()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "16148072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "16148072", "http://www.mozilla.org" }));
- Assert.AreEqual(2, catalog.Count);
- }
- [TestMethod]
- public void TestAddMultipalItems()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
- Assert.AreEqual(5, catalog.Count);
- }
- [TestMethod]
- public void TestGetListContentWithMatchesLessThanCount()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
- IEnumerable<IContent> actual = catalog.GetListContent("intro Firefox", 5);
- Assert.AreEqual(actual.Count(), 2);
- }
- [TestMethod]
- public void TestGetListContentWhetherReturnsLimitedMatches()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "2148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho2", "161480722", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho3", "16148072111", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
- IEnumerable<IContent> actual = catalog.GetListContent("intro Firefox", 2);
- Assert.AreEqual(actual.Count(), 2);
- }
- [TestMethod]
- public void TestGetListContentWithSingleMatch()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
- IEnumerable<IContent> actual = catalog.GetListContent("intro Firefox", 10);
- Assert.AreEqual(actual.Count(), 1);
- }
- [TestMethod]
- public void TestGetListContentWithNoMatches()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
- IEnumerable<IContent> actual = catalog.GetListContent("No match element", 10);
- Assert.AreEqual(actual.Count(), 0);
- }
- [TestMethod]
- public void TestGetListContentSorting()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Intro Firefox", "Gosho", "261472", "http://www.btv.org" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Intro Firefox", "Ani", "261472", "http://www.btv.org" }));
- IEnumerable<IContent> actual = catalog.GetListContent("Intro Firefox", 10);
- string[] actualTextRepresentation = new string[actual.Count()];
- int index = 0;
- foreach (var item in actual)
- {
- actualTextRepresentation[index] = item.TextRepresentation;
- index++;
- }
- // Ordered by Movie than ordered by Name -> Ani, Gosho, Pesho
- string[] expected = new string[] {
- "Movie: Intro Firefox; Ani; 261472; http://www.btv.org",
- "Movie: Intro Firefox; Gosho; 261472; http://www.btv.org",
- "Movie: Intro Firefox; Pesho; 16148072; http://www.firefox.com"
- };
- CollectionAssert.AreEqual(actualTextRepresentation, expected);
- }
- [TestMethod]
- public void TestUpdateContentWithOneMatch()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
- catalog.UpdateContent("http://www.firefox.com", "www.yahoo.com");
- FieldInfo url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
- MultiDictionary<string, IContent> urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
- Assert.IsTrue(urlList.ContainsKey("www.yahoo.com"));
- }
- [TestMethod]
- public void TestUpdateContentNoMatch()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
- catalog.UpdateContent("invalid.url.com", "www.yahoo.com");
- FieldInfo url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
- MultiDictionary<string, IContent> urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
- Assert.IsFalse(urlList.ContainsKey("www.yahoo.com"));
- }
- [TestMethod]
- public void TestUpdateContentMultipleUpdates()
- {
- Catalog catalog = new Catalog();
- catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
- catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
- catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
- catalog.UpdateContent("http://www.firefox.com", "www.yahoo.com");
- FieldInfo url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
- MultiDictionary<string, IContent> urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
- Assert.IsTrue(urlList.ContainsKey("www.yahoo.com"));
- catalog.UpdateContent("www.yahoo.com", "http://www.firefox.com");
- url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
- urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
- Assert.IsTrue(urlList.ContainsKey("http://www.firefox.com"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment