PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/AddIns/Misc/PackageManagement/Test/Src/RecentPackageRepositoryTests.cs

https://github.com/ajadex/SharpDevelop
C# | 288 lines | 208 code | 63 blank | 17 comment | 0 complexity | 4b093a3a0c3743a40aace16ec3169f08 MD5 | raw file
  1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using System.Collections.Generic;
 21using System.Linq;
 22using ICSharpCode.Core;
 23using ICSharpCode.PackageManagement;
 24using ICSharpCode.PackageManagement.Design;
 25using NuGet;
 26using NUnit.Framework;
 27using PackageManagement.Tests.Helpers;
 28
 29namespace PackageManagement.Tests
 30{
 31	[TestFixture]
 32	public class RecentPackageRepositoryTests
 33	{
 34		RecentPackageRepository repository;
 35		FakePackageRepository aggregateRepository;
 36		PackageManagementEvents packageManagementEvents;
 37		List<RecentPackageInfo> recentPackages;
 38		
 39		void CreateRepository()
 40		{
 41			CreateRecentPackages();
 42			CreateRepository(recentPackages);
 43		}
 44		
 45		void CreateRecentPackages()
 46		{
 47			recentPackages =  new List<RecentPackageInfo>();
 48			aggregateRepository = new FakePackageRepository();
 49		}
 50		
 51		void CreateRepository(IList<RecentPackageInfo> recentPackages)
 52		{
 53			packageManagementEvents = new PackageManagementEvents();
 54			repository = new RecentPackageRepository(recentPackages, aggregateRepository, packageManagementEvents);
 55		}
 56		
 57		FakePackage AddOnePackageToRepository(string id)
 58		{
 59			var package = new FakePackage(id);
 60			repository.AddPackage(package);
 61			return package;
 62		}
 63		
 64		IEnumerable<IPackage> AddTwoDifferentPackagesToRepository()
 65		{
 66			yield return AddOnePackageToRepository("Test.Package.1");
 67			yield return AddOnePackageToRepository("Test.Package.2");
 68		}
 69
 70		IEnumerable<IPackage> AddFourDifferentPackagesToRepository()
 71		{
 72			yield return AddOnePackageToRepository("Test.Package.1");
 73			yield return AddOnePackageToRepository("Test.Package.2");
 74			yield return AddOnePackageToRepository("Test.Package.3");
 75			yield return AddOnePackageToRepository("Test.Package.4");
 76		}
 77		
 78		FakePackage CreateRepositoryWithOneRecentPackageSavedInOptions()
 79		{
 80			CreateRecentPackages();
 81			var package = new FakePackage("Test");
 82			aggregateRepository.FakePackages.Add(package);
 83			recentPackages.Add(new RecentPackageInfo(package));
 84			CreateRepository(recentPackages);
 85			return package;
 86		}
 87		
 88		[Test]
 89		public void Source_NewRecentRepositoryCreated_IsRecentPackages()
 90		{
 91			CreateRepository();
 92			Assert.AreEqual("RecentPackages", repository.Source);
 93		}
 94		
 95		[Test]
 96		public void GetPackages_RepositoryIsEmptyAndOnePackageAdded_ReturnsPackageAdded()
 97		{
 98			CreateRepository();
 99			var package = AddOnePackageToRepository("Test.Package");
100			
101			var packages = repository.GetPackages();
102			
103			var expectedPackages = new FakePackage[] {
104				package
105			};
106			
107			PackageCollectionAssert.AreEqual(expectedPackages, packages);
108		}
109		
110		[Test]
111		public void GetPackages_RepositoryIsEmptyAndTwoDifferentPackagesAdded_ReturnsPackagesInReverseOrderWithLastAddedFirst()
112		{
113			CreateRepository();
114			var packagesAdded = AddTwoDifferentPackagesToRepository();
115			
116			var packages = repository.GetPackages();
117			
118			var expectedPackages = packagesAdded.Reverse();
119			
120			PackageCollectionAssert.AreEqual(expectedPackages, packages);
121		}
122		
123		[Test]
124		public void GetPackages_RepositoryCanHoldThreePackagesAndFourPackagesAdded_ReturnsLastThreePackagesAddedInReverseOrder()
125		{
126			CreateRepository();
127			repository.MaximumPackagesCount = 3;
128			var packagesAdded = AddFourDifferentPackagesToRepository();
129			
130			var packages = repository.GetPackages();
131			
132			var expectedPackages = packagesAdded.Reverse().Take(3);
133			
134			PackageCollectionAssert.AreEqual(expectedPackages, packages);
135		}
136		
137		[Test]
138		public void GetPackages_RepositoryIsEmptyAndSamePackageIsAddedTwice_OnePackageReturned()
139		{
140			CreateRepository();
141			AddOnePackageToRepository("Test");
142			var package = AddOnePackageToRepository("Test");
143			
144			var packages = repository.GetPackages();
145			
146			var expectedPackages = new FakePackage[] {
147				package
148			};
149			
150			PackageCollectionAssert.AreEqual(expectedPackages, packages);
151		}
152		
153		[Test]
154		public void AddPackage_RepositoryIsEmptyAndOnePackageAdded_RecentPackageAddedToOptions()
155		{
156			CreateRepository();
157			var package = AddOnePackageToRepository("Test");
158			
159			var expectedPackages = new RecentPackageInfo[] {
160				new RecentPackageInfo(package)
161			};
162			
163			RecentPackageInfoCollectionAssert.AreEqual(expectedPackages, recentPackages);
164		}
165		
166		[Test]
167		public void AddPackage_RepositoryIsEmptyAndTwoPackagesAddedFromDifferentSources_BothRecentPackagesAddedToOptions()
168		{
169			CreateRepository();
170			var package1 = AddOnePackageToRepository("Test1");
171			var package2 = AddOnePackageToRepository("Test2");
172			
173			var expectedPackages = new RecentPackageInfo[] {
174				new RecentPackageInfo(package2),
175				new RecentPackageInfo(package1)
176			};
177			
178			RecentPackageInfoCollectionAssert.AreEqual(expectedPackages, recentPackages);
179		}
180		
181		[Test]
182		public void GetPackages_SavedOptionsHasOneRecentPackage_ContainsPackageTakenFromAggregateRepositoryMatchingSavedRecentPackageInfo()
183		{
184			var package = CreateRepositoryWithOneRecentPackageSavedInOptions();
185			
186			var recentPackages = repository.GetPackages();
187			
188			var expectedPackages = new FakePackage[] {
189				package
190			};
191			
192			PackageCollectionAssert.AreEqual(expectedPackages, recentPackages);
193		}
194		
195		[Test]
196		public void GetPackages_SavedOptionsHasOneRecentPackageAndGetPackagesCalledTwice_OnePackageReturned()
197		{
198			var package = CreateRepositoryWithOneRecentPackageSavedInOptions();
199			
200			repository.GetPackages();
201			var recentPackages = repository.GetPackages();
202			
203			var expectedPackages = new FakePackage[] {
204				package
205			};
206			
207			PackageCollectionAssert.AreEqual(expectedPackages, recentPackages);
208		}
209		
210		[Test]
211		public void GetPackages_OneRecentPackageAndAggregrateRepositoryHasTwoPackagesWithSameIdButDifferentVersions_OnePackageReturnedWithMatchingVersion()
212		{
213			var package1 = CreateRepositoryWithOneRecentPackageSavedInOptions();
214			var package2 = new FakePackage(package1.Id);
215			package2.Version = new SemanticVersion(2, 0, 0, 0);
216			aggregateRepository.FakePackages.Add(package2);
217			
218			var recentPackages = repository.GetPackages();
219			
220			var expectedPackages = new FakePackage[] {
221				package1
222			};
223			
224			PackageCollectionAssert.AreEqual(expectedPackages, recentPackages);
225		}
226		
227		[Test]
228		public void ParentPackageInstalled_EventFires_PackageAddedToRecentPackages()
229		{
230			CreateRepository();
231			
232			var package = new FakePackage("Test");
233			packageManagementEvents.OnParentPackageInstalled(package);
234			
235			var recentPackages = repository.GetPackages();
236			
237			var expectedPackages = new FakePackage[] {
238				package
239			};
240			
241			PackageCollectionAssert.AreEqual(expectedPackages, recentPackages);
242		}
243
244		public void Clear_OneRecentPackage_PackagesRemoved()
245		{
246			CreateRepository();
247			AddOnePackageToRepository("Test1");
248			
249			repository.Clear();
250			
251			int count = repository.GetPackages().Count();
252			
253			Assert.AreEqual(0, count);
254		}
255		
256		[Test]
257		public void Clear_OneRecentPackageInOptions_RecentPackagesAreRemovedFromOptions()
258		{
259			CreateRepositoryWithOneRecentPackageSavedInOptions();
260			
261			repository.Clear();
262			
263			int count = recentPackages.Count;
264			
265			Assert.AreEqual(0, count);
266		}
267		
268		[Test]
269		public void HasRecentPackages_NoSavedRecentPackages_ReturnsFalse()
270		{
271			CreateRepository();
272			
273			bool hasRecentPackages = repository.HasRecentPackages;
274			
275			Assert.IsFalse(hasRecentPackages);
276		}
277		
278		[Test]
279		public void HasRecentPackages_OneSavedRecentPackages_ReturnsTrue()
280		{
281			CreateRepositoryWithOneRecentPackageSavedInOptions();
282			
283			bool hasRecentPackages = repository.HasRecentPackages;
284			
285			Assert.IsTrue(hasRecentPackages);
286		}
287	}
288}