PageRenderTime 44ms CodeModel.GetById 14ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://github.com/ajadex/SharpDevelop
C# | 183 lines | 137 code | 29 blank | 17 comment | 2 complexity | e1af443cdf0f7daff45b4dd2dbe50267 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;
 22
 23using ICSharpCode.PackageManagement;
 24using NuGet;
 25using NUnit.Framework;
 26using PackageManagement.Tests.Helpers;
 27using Rhino.Mocks;
 28
 29namespace PackageManagement.Tests
 30{
 31	[TestFixture]
 32	public class ReducedPackageOperationsTests
 33	{
 34		ReducedPackageOperations reducedPackageOperations;
 35		IPackageOperationResolver fakePackageOperationResolver;
 36		List<IPackage> packages;
 37		
 38		void CreateReducedPackageOperations()
 39		{
 40			packages = new List<IPackage>();
 41			fakePackageOperationResolver = MockRepository.GenerateStub<IPackageOperationResolver>();
 42			reducedPackageOperations = new ReducedPackageOperations(fakePackageOperationResolver, packages);
 43		}
 44		
 45		IPackage AddPackage(string id, string version)
 46		{
 47			IPackage package = CreatePackage(id, version);
 48			packages.Add(package);
 49			
 50			return package;
 51		}
 52		
 53		IPackage CreatePackage(string id, string version)
 54		{
 55			return new TestPackageHelper(id, version).Package;
 56		}
 57		
 58		PackageOperation AddInstallOperationForPackage(IPackage package)
 59		{
 60			var operation = new PackageOperation(package, PackageAction.Install);
 61			AddInstallOperationsForPackage(package, operation);
 62			return operation;
 63		}
 64		
 65		void AddInstallOperationsForPackage(IPackage package, params PackageOperation[] operations)
 66		{
 67			fakePackageOperationResolver
 68				.Stub(resolver => resolver.ResolveOperations(package))
 69				.Return(operations);
 70		}
 71		
 72		PackageOperation CreatePackageOperation(string id, string version, PackageAction action)
 73		{
 74			IPackage package = CreatePackage(id, version);
 75			return new PackageOperation(package, action);
 76		}
 77		
 78		void AssertReducedOperationsContains(PackageOperation operation)
 79		{
 80			Assert.Contains(operation, reducedPackageOperations.Operations.ToList());
 81		}
 82		
 83		[Test]
 84		public void Reduce_OnePackage_ReturnsPackageOperationsFromResolverForPackage()
 85		{
 86			CreateReducedPackageOperations();
 87			IPackage package = AddPackage("Test", "1.0");
 88			PackageOperation operation = AddInstallOperationForPackage(package);
 89			
 90			reducedPackageOperations.Reduce();
 91			
 92			Assert.AreEqual(1, reducedPackageOperations.Operations.Count());
 93			Assert.AreEqual(operation, reducedPackageOperations.Operations.First());
 94		}
 95		
 96		[Test]
 97		public void Reduce_TwoPackages_ReturnsPackageOperationsForBothPackages()
 98		{
 99			CreateReducedPackageOperations();
100			IPackage package1 = AddPackage("Test", "1.0");
101			IPackage package2 = AddPackage("Test2", "1.0");
102			PackageOperation operation1 = AddInstallOperationForPackage(package1);
103			PackageOperation operation2 = AddInstallOperationForPackage(package2);
104			
105			reducedPackageOperations.Reduce();
106			
107			Assert.AreEqual(2, reducedPackageOperations.Operations.Count());
108			AssertReducedOperationsContains(operation1);
109			AssertReducedOperationsContains(operation2);
110		}
111		
112		[Test]
113		public void Reduce_OncePackageOperationInstallsPackageWhilstOneUninstallsSamePackage_PackageOperationNotIncludedInReducedSet()
114		{
115			CreateReducedPackageOperations();
116			IPackage package = AddPackage("Test", "1.0");
117			PackageOperation installOperation = CreatePackageOperation("Foo", "1.0", PackageAction.Install);
118			PackageOperation uninstallOperation = CreatePackageOperation("Foo", "1.0", PackageAction.Uninstall);
119			AddInstallOperationsForPackage(package, installOperation, uninstallOperation);
120			
121			reducedPackageOperations.Reduce();
122			
123			Assert.AreEqual(0, reducedPackageOperations.Operations.Count());
124		}
125		
126		[Test]
127		public void Reduce_OnePackageOperationMatchesPackageBeingInstalled_ReturnsOnlyOnePackageInstallOperationForThisPackage()
128		{
129			CreateReducedPackageOperations();
130			IPackage package1 = AddPackage("Test", "1.0");
131			IPackage package2 = AddPackage("Test2", "1.0");
132			PackageOperation operation1a = CreatePackageOperation("Test", "1.0", PackageAction.Install);
133			PackageOperation operation1b = CreatePackageOperation("Test2", "1.0", PackageAction.Install);
134			PackageOperation operation2 = CreatePackageOperation("Test2", "1.0", PackageAction.Install);
135			AddInstallOperationsForPackage(package1, operation1a, operation1b);
136			AddInstallOperationsForPackage(package2, operation2);
137			
138			reducedPackageOperations.Reduce();
139			
140			PackageOperation operation = reducedPackageOperations
141				.Operations
142				.SingleOrDefault(o => o.Package.Id == "Test2");
143			Assert.AreEqual(2, reducedPackageOperations.Operations.Count());
144		}
145		
146		[Test]
147		public void Reduce_OnePackageOperationMatchesPackageBeingInstalledOnlyById_MatchingPackageOperationByIdIncludedInSet()
148		{
149			CreateReducedPackageOperations();
150			IPackage package1 = AddPackage("Test", "1.0");
151			IPackage package2 = AddPackage("Test2", "1.0");
152			PackageOperation operation1a = CreatePackageOperation("Test", "1.0", PackageAction.Install);
153			PackageOperation operation1b = CreatePackageOperation("Test2", "1.1", PackageAction.Install);
154			PackageOperation operation2 = CreatePackageOperation("Test2", "1.0", PackageAction.Install);
155			AddInstallOperationsForPackage(package1, operation1a, operation1b);
156			AddInstallOperationsForPackage(package2, operation2);
157			
158			reducedPackageOperations.Reduce();
159			
160			Assert.AreEqual(3, reducedPackageOperations.Operations.Count());
161		}
162		
163		[Test]
164		public void Reduce_OnePackageOperationMatchesPackageBeingInstalledByIdAndVersionButOneIsInstallAndOneIsUninstall_BothOperationsNotIncludedInSet()
165		{
166			CreateReducedPackageOperations();
167			IPackage package1 = AddPackage("Test", "1.0");
168			IPackage package2 = AddPackage("Test2", "1.0");
169			PackageOperation operation1a = CreatePackageOperation("Test", "1.0", PackageAction.Install);
170			PackageOperation operation1b = CreatePackageOperation("Test2", "1.0", PackageAction.Uninstall);
171			PackageOperation operation2 = CreatePackageOperation("Test2", "1.0", PackageAction.Install);
172			AddInstallOperationsForPackage(package1, operation1a, operation1b);
173			AddInstallOperationsForPackage(package2, operation2);
174			
175			reducedPackageOperations.Reduce();
176			
177			PackageOperation operation = reducedPackageOperations
178				.Operations
179				.SingleOrDefault(o => o.Package.Id == "Test");
180			Assert.AreEqual(1, reducedPackageOperations.Operations.Count());
181		}
182	}
183}