diff --git a/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj b/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj index 49b11252b7..cffdd5c06f 100644 --- a/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj +++ b/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj @@ -127,6 +127,7 @@ + diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/Globals.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/Globals.cs index 82075fcfff..1535538647 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/Globals.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/Globals.cs @@ -12,29 +12,21 @@ namespace ICSharpCode.PackageManagement.EnvDTE public class Globals { SD.Solution solution; + SolutionExtensibilityGlobals extensibilityGlobals; public Globals(SD.Solution solution) { this.solution = solution; + this.extensibilityGlobals = new SolutionExtensibilityGlobals(solution); } - public virtual bool VariableExists(string name) - { - SD.ProjectSection section = GetExtensibilityGlobalsSection(); - if (section != null) { - return section.Items.Any(item => IsMatchIgnoringCase(item.Name, name)); - } - return false; - } - - bool IsMatchIgnoringCase(string a, string b) - { - return String.Equals(a, b, StringComparison.OrdinalIgnoreCase); + public virtual SolutionExtensibilityGlobals VariableValue { + get { return extensibilityGlobals; } } - SD.ProjectSection GetExtensibilityGlobalsSection() + public virtual bool VariableExists(string name) { - return solution.Sections.SingleOrDefault(section => section.Name == "ExtensibilityGlobals"); + return extensibilityGlobals.GetItemFromSolution(name) != null; } } } diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/SolutionExtensibilityGlobals.cs b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/SolutionExtensibilityGlobals.cs new file mode 100644 index 0000000000..26a0b2309b --- /dev/null +++ b/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/SolutionExtensibilityGlobals.cs @@ -0,0 +1,95 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using System.Collections.Generic; +using System.Linq; + +using SD = ICSharpCode.SharpDevelop.Project; + +namespace ICSharpCode.PackageManagement.EnvDTE +{ + public class SolutionExtensibilityGlobals + { + SD.Solution solution; + const string ExtensibilityGlobalsSectionName = "ExtensibilityGlobals"; + List nonPersistedSolutionItems = new List(); + + public SolutionExtensibilityGlobals(SD.Solution solution) + { + this.solution = solution; + } + + public object this[string name] { + get { + SD.SolutionItem item = GetItemFromSolutionOrNonPersistedItems(name); + if (item == null) { + ThrowNoVariableExistsException(name); + } + return item.Location; + } + set { + GetOrCreateSolutionItem(name, value as string); + } + } + + void ThrowNoVariableExistsException(string name) + { + throw new ArgumentException("Variable name does not exist.", name); + } + + SD.SolutionItem GetItemFromSolutionOrNonPersistedItems(string name) + { + SD.SolutionItem item = GetNonPersistedSolutionItem(name); + if (item != null) { + return item; + } + return GetItemFromSolution(name); + } + + SD.SolutionItem GetNonPersistedSolutionItem(string name) + { + return GetMatchingSolutionItem(nonPersistedSolutionItems, name); + } + + SD.SolutionItem GetMatchingSolutionItem(List items, string name) + { + return items.SingleOrDefault(item => IsMatchIgnoringCase(item.Name, name)); + } + + internal SD.SolutionItem GetItemFromSolution(string name) + { + SD.ProjectSection section = GetExtensibilityGlobalsSection(); + if (section != null) { + return GetMatchingSolutionItem(section.Items, name); + } + return null; + } + + SD.ProjectSection GetExtensibilityGlobalsSection() + { + return solution.Sections.SingleOrDefault(section => section.Name == ExtensibilityGlobalsSectionName); + } + + bool IsMatchIgnoringCase(string a, string b) + { + return String.Equals(a, b, StringComparison.OrdinalIgnoreCase); + } + + void GetOrCreateSolutionItem(string name, string value) + { + SD.SolutionItem item = GetItemFromSolution(name); + if (item != null) { + item.Location = value; + } else { + CreateNonPersistedSolutionItem(name, value); + } + } + + void CreateNonPersistedSolutionItem(string name, string value) + { + var item = new SD.SolutionItem(name, value); + nonPersistedSolutionItems.Add(item); + } + } +} diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/SolutionGlobalsTests.cs b/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/SolutionGlobalsTests.cs index 3e5f7e053e..2c73a4392d 100644 --- a/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/SolutionGlobalsTests.cs +++ b/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/SolutionGlobalsTests.cs @@ -5,6 +5,7 @@ using System; using ICSharpCode.PackageManagement.EnvDTE; using NUnit.Framework; using PackageManagement.Tests.Helpers; +using SD = ICSharpCode.SharpDevelop.Project; namespace PackageManagement.Tests.EnvDTE { @@ -35,6 +36,16 @@ namespace PackageManagement.Tests.EnvDTE solutionHelper.AddExtensibilityGlobalsSection(); } + SD.SolutionItem GetExtensibilityGlobalsSolutionItem(string name) + { + return solutionHelper.GetExtensibilityGlobalsSolutionItem(name); + } + + SD.ProjectSection GetExtensibilityGlobalsSection() + { + return solutionHelper.GetExtensibilityGlobalsSection(); + } + [Test] public void VariableExists_VariableExistsInExtensibilityGlobalsSection_ReturnsTrue() { @@ -79,5 +90,105 @@ namespace PackageManagement.Tests.EnvDTE Assert.IsTrue(exists); } + + [Test] + public void VariableValue_VariableAddedToExtensibilityGlobalsSection_ReturnsVariableValue() + { + CreateSolution(); + AddExtensibilityGlobalsSection(); + AddVariableToExtensibilityGlobals("test", "test-value"); + + object value = globals.VariableValue["test"]; + + Assert.AreEqual("test-value", value); + } + + [Test] + public void VariableValue_VariableAddedToExtensibilityGlobalsSectionWithDifferentCasing_ReturnsVariableValue() + { + CreateSolution(); + AddExtensibilityGlobalsSection(); + AddVariableToExtensibilityGlobals("TEST", "test-value"); + + object value = globals.VariableValue["test"]; + + Assert.AreEqual("test-value", value); + } + + [Test] + public void VariableValue_NoExtensibilityGlobalsSection_ThrowInvalidArgumentException() + { + CreateSolution(); + + Assert.Throws(delegate { object value = globals.VariableValue["test"]; }); + } + + [Test] + public void VariableValue_ExtensibilityGlobalsSectionButNoVariable_ThrowInvalidArgumentException() + { + CreateSolution(); + AddExtensibilityGlobalsSection(); + + Assert.Throws(delegate { object value = globals.VariableValue["test"]; }); + } + + [Test] + public void VariableValue_SetNewValueForVariableAddedToExtensibilityGlobalsSection_SolutionItemUpdated() + { + CreateSolution(); + AddExtensibilityGlobalsSection(); + AddVariableToExtensibilityGlobals("test", "value"); + + globals.VariableValue["test"] = "new-value"; + + SD.SolutionItem item = GetExtensibilityGlobalsSolutionItem("test"); + Assert.AreEqual("new-value", item.Location); + } + + [Test] + public void VariableValue_SetNewValueForVariableWhenExtensibilityGlobalsExistsButVariableDoesNotExist_SolutionItemUpdated() + { + CreateSolution(); + AddExtensibilityGlobalsSection(); + + globals.VariableValue["test"] = "new-value"; + object value = globals.VariableValue["test"]; + + Assert.AreEqual("new-value", value); + } + + [Test] + public void VariableValue_SetNewValueForVariableWhenExtensibilityGlobalsExistsButVariableDoesNotExist_SolutionNotUpdatedWithNewSolutionItem() + { + CreateSolution(); + AddExtensibilityGlobalsSection(); + + globals.VariableValue["test"] = "new-value"; + + SD.SolutionItem item = GetExtensibilityGlobalsSolutionItem("test"); + Assert.IsNull(item); + } + + [Test] + public void VariableValue_SetNewValueForVariableWhenExtensibilityGlobalsDoesNotExist_ExtensibilityGlobalsSectionCreated() + { + CreateSolution(); + + globals.VariableValue["test"] = "new-value"; + + SD.ProjectSection section = GetExtensibilityGlobalsSection(); + Assert.IsNull(section); + } + + [Test] + public void VariableValue_SetNewValueForVariableWhenExtensibilityGlobalsDoesNotExistAndRetrieveItUsingDifferentCase_NewValueReturned() + { + CreateSolution(); + + globals.VariableValue["test"] = "new-value"; + object value = globals.VariableValue["TEST"]; + + Assert.AreEqual("new-value", value); + } } } diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/SolutionHelper.cs b/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/SolutionHelper.cs index b54fc9e6bb..63e4dc30c2 100644 --- a/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/SolutionHelper.cs +++ b/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/SolutionHelper.cs @@ -2,6 +2,8 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; +using System.Linq; + using ICSharpCode.PackageManagement.Design; using ICSharpCode.PackageManagement.EnvDTE; using SD = ICSharpCode.SharpDevelop.Project; @@ -18,7 +20,6 @@ namespace PackageManagement.Tests.Helpers public Solution Solution; public FakePackageManagementProjectService FakeProjectService; public SD.Solution MSBuildSolution; - public SD.ProjectSection ExtensibilityGlobalsSection; void OpenSolution() { @@ -53,14 +54,24 @@ namespace PackageManagement.Tests.Helpers public void AddExtensibilityGlobalsSection() { - ExtensibilityGlobalsSection = new SD.ProjectSection("ExtensibilityGlobals", "postSolution"); - MSBuildSolution.Sections.Add(ExtensibilityGlobalsSection); + var section = new SD.ProjectSection("ExtensibilityGlobals", "postSolution"); + MSBuildSolution.Sections.Add(section); } public void AddVariableToExtensibilityGlobals(string name, string value) { var solutionItem = new SD.SolutionItem(name, value); - ExtensibilityGlobalsSection.Items.Add(solutionItem); + GetExtensibilityGlobalsSection().Items.Add(solutionItem); + } + + public SD.SolutionItem GetExtensibilityGlobalsSolutionItem(string name) + { + return GetExtensibilityGlobalsSection().Items.SingleOrDefault(item => item.Name == name); + } + + public SD.ProjectSection GetExtensibilityGlobalsSection() + { + return MSBuildSolution.Sections.SingleOrDefault(section => section.Name == "ExtensibilityGlobals"); } } }