Browse Source

Add assembly info provider reading tests

pull/624/head
olegbevz 11 years ago
parent
commit
6f7eb3b7da
  1. 55
      src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/AssemblyInfo/AssemblyInfoProvider.cs
  2. 7
      src/Main/Base/Test/ICSharpCode.SharpDevelop.Tests.csproj
  3. 253
      src/Main/Base/Test/ProjectOptions/AssemblyInfoProviderTests.cs

55
src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/AssemblyInfo/AssemblyInfoProvider.cs

@ -28,9 +28,9 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels @@ -28,9 +28,9 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels
{
public class AssemblyInfoProvider
{
public AssemblyInfo Read(string fileName)
public AssemblyInfo Read(Stream stream)
{
var syntaxTree = ReadSyntaxTree(fileName);
var syntaxTree = ReadSyntaxTree(stream);
if (syntaxTree == null)
throw new Exception("Can't read assembly info syntax tree.");
@ -104,9 +104,14 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels @@ -104,9 +104,14 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels
return assemblyInfo;
}
public void Write(string fileName, AssemblyInfo assemblyInfo)
public void Write(AssemblyInfo assemblyInfo, string fileName)
{
var syntaxTree = ReadSyntaxTree(fileName);
SyntaxTree syntaxTree;
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
syntaxTree = ReadSyntaxTree(fileStream);
}
if (syntaxTree == null)
throw new Exception("Can't read assembly info syntax tree.");
@ -134,25 +139,31 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels @@ -134,25 +139,31 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels
SetAttributeValueOrAddAttributeIfNotDefault(syntaxTree, "AssemblyFlags", (int)assemblyFlags, (int)AssemblyNameFlags.PublicKey);
WriteSyntaxTree(syntaxTree, fileName);
using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
{
WriteSyntaxTree(syntaxTree, fileStream);
}
}
private SyntaxTree ReadSyntaxTree(string fileName)
private SyntaxTree ReadSyntaxTree(Stream stream)
{
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
using (var streamReader = new StreamReader(stream))
{
using (var streamReader = new StreamReader(fileStream))
{
var codeParser = new CSharpParser();
var codeParser = new CSharpParser();
return codeParser.Parse(streamReader);
}
return codeParser.Parse(streamReader);
}
}
private void WriteSyntaxTree(SyntaxTree syntaxTree, string fileName)
private void WriteSyntaxTree(SyntaxTree syntaxTree, Stream stream)
{
File.WriteAllText(fileName, syntaxTree.ToString());
var content = syntaxTree.ToString();
using (var streamWriter = new StreamWriter(stream))
{
streamWriter.Write(content);
streamWriter.Flush();
}
}
private T GetAttributeValue<T>(Attribute attribute, T defaultValue = default(T))
@ -164,13 +175,23 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels @@ -164,13 +175,23 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels
return defaultValue;
}
private Guid GetAttributeValueAsGuid(Attribute attribute)
private Guid? GetAttributeValueAsGuid(Attribute attribute)
{
var attributeArguments = attribute.Arguments.OfType<PrimitiveExpression>().ToArray();
if (attributeArguments.Length == 1)
return Guid.Parse(attributeArguments[0].Value as string);
{
var guidString = attributeArguments[0].Value as string;
if (!string.IsNullOrEmpty(guidString))
{
Guid guid;
if (Guid.TryParse(guidString, out guid))
{
return guid;
}
}
}
return Guid.Empty;
return null;
}
private Version GetAttributeValueAsVersion(Attribute attribute)

7
src/Main/Base/Test/ICSharpCode.SharpDevelop.Tests.csproj

@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.40607</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{4980B743-B32F-4aba-AABD-45E2CAD3568D}</ProjectGuid>
<ProjectGuid>{4980B743-B32F-4ABA-AABD-45E2CAD3568D}</ProjectGuid>
<RootNamespace>ICSharpCode.SharpDevelop</RootNamespace>
<AssemblyName>ICSharpCode.SharpDevelop.Tests</AssemblyName>
<OutputTarget>Library</OutputTarget>
@ -97,6 +97,7 @@ @@ -97,6 +97,7 @@
<Compile Include="ProcessRunner\ProcessExitedTestFixture.cs" />
<Compile Include="ProcessRunner\ProcessRunnerNotStartedTestFixture.cs" />
<Compile Include="ProcessRunner\StandardOutputFromProcessTestFixture.cs" />
<Compile Include="ProjectOptions\AssemblyInfoProviderTests.cs" />
<Compile Include="Project\AddExistingProjectTests.cs" />
<Compile Include="Project\BeforeBuildCustomToolProjectItemsTests.cs" />
<Compile Include="Project\MockSolution.cs" />
@ -192,9 +193,7 @@ @@ -192,9 +193,7 @@
<EmbeddedResource Include="mime_utf-16_be_test.txt" />
<EmbeddedResource Include="mime_utf-16_le_test.txt" />
</ItemGroup>
<ItemGroup>
<Folder Include="ProcessRunner" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<EmbeddedResource Include="mime_bug1.txt" />
</ItemGroup>

253
src/Main/Base/Test/ProjectOptions/AssemblyInfoProviderTests.cs

@ -0,0 +1,253 @@ @@ -0,0 +1,253 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.IO;
using System.Text;
using ICSharpCode.SharpDevelop.Gui.OptionPanels;
using NUnit.Framework;
using System;
namespace ICSharpCode.SharpDevelop.ProjectOptions
{
[TestFixture]
public class AssemblyInfoProviderTests
{
[TestCase]
public void ReadEmptyAssemblyInfoTest()
{
var assemblyInfoFile = string.Empty;
var assemblyInfo = ReadAssemblyInfo(assemblyInfoFile);
Assert.IsNull(assemblyInfo.Title);
Assert.IsNull(assemblyInfo.Description);
Assert.IsNull(assemblyInfo.Company);
Assert.IsNull(assemblyInfo.Product);
Assert.IsNull(assemblyInfo.Copyright);
Assert.IsNull(assemblyInfo.Trademark);
Assert.IsNull(assemblyInfo.DefaultAlias);
Assert.IsNull(assemblyInfo.AssemblyVersion);
Assert.IsNull(assemblyInfo.AssemblyFileVersion);
Assert.IsNull(assemblyInfo.InformationalVersion);
Assert.IsNull(assemblyInfo.Guid);
Assert.IsNull(assemblyInfo.NeutralLanguage);
Assert.IsFalse(assemblyInfo.ComVisible);
Assert.IsFalse(assemblyInfo.ClsCompliant);
Assert.IsTrue(assemblyInfo.JitOptimization);
Assert.IsFalse(assemblyInfo.JitTracking);
}
[TestCase]
public void ReadNotEmptyAssemblyInfoTest()
{
var assemblyInfoFile =
@"using System;
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle (""SharpDevelop"")]
[assembly: AssemblyDescription (""OpenSource IDE"")]
[assembly: AssemblyCompany (""Company"")]
[assembly: AssemblyProduct (""Product"")]
[assembly: AssemblyCopyright (""Copyright 2014"")]
[assembly: AssemblyTrademark (""Trademark"")]
[assembly: AssemblyDefaultAlias (""Alias"")]
// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible (true)]
// The assembly version has following format :
//
// Major.Minor.Build.Revision
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion (""1.2.3.1"")]
[assembly: AssemblyFileVersion (""1.2.3.2"")]
[assembly: AssemblyInformationalVersion (""1.2.3.3"")]
[assembly: Guid (""0c8c889f-ced2-4167-b155-2d48a99d8c72"")]
[assembly: NeutralResourcesLanguage (""ru-RU"")]
[assembly: AssemblyFlags (32769)]
[assembly: CLSCompliant (true)]";
var assemblyInfo = ReadAssemblyInfo(assemblyInfoFile);
Assert.AreEqual("SharpDevelop", assemblyInfo.Title);
Assert.AreEqual("OpenSource IDE" , assemblyInfo.Description);
Assert.AreEqual("Company", assemblyInfo.Company);
Assert.AreEqual("Product", assemblyInfo.Product);
Assert.AreEqual("Copyright 2014", assemblyInfo.Copyright);
Assert.AreEqual("Trademark", assemblyInfo.Trademark);
Assert.AreEqual("Alias", assemblyInfo.DefaultAlias);
Assert.AreEqual(new Version(1, 2, 3, 1), assemblyInfo.AssemblyVersion);
Assert.AreEqual(new Version(1, 2, 3, 2), assemblyInfo.AssemblyFileVersion);
Assert.AreEqual(new Version(1, 2, 3, 3), assemblyInfo.InformationalVersion);
Assert.AreEqual(new Guid("0c8c889f-ced2-4167-b155-2d48a99d8c72"), assemblyInfo.Guid);
Assert.AreEqual("ru-RU", assemblyInfo.NeutralLanguage);
Assert.IsTrue(assemblyInfo.ComVisible);
Assert.IsTrue(assemblyInfo.ClsCompliant);
Assert.IsTrue(assemblyInfo.JitOptimization);
Assert.IsTrue(assemblyInfo.JitTracking);
}
[TestCase]
public void ReadAssemblyInfoWithIncorrectVersionsTest()
{
var assemblyInfoFile =
@"using System;
using System.Reflection;
using System.Runtime.InteropServices;
// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible (true)]
// The assembly version has following format :
//
// Major.Minor.Build.Revision
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion (""Incorrect version"")]
[assembly: AssemblyFileVersion (""Incorrect version"")]
[assembly: AssemblyInformationalVersion (""Incorrect version"")]";
var assemblyInfo = ReadAssemblyInfo(assemblyInfoFile);
Assert.IsNull(assemblyInfo.AssemblyVersion);
Assert.IsNull(assemblyInfo.AssemblyFileVersion);
Assert.IsNull(assemblyInfo.InformationalVersion);
}
[TestCase]
public void ReadAssemblyInfoWithIncorrectGuidTest()
{
var assemblyInfoFile =
@"using System;
using System.Reflection;
using System.Runtime.InteropServices;
// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible (true)]
[assembly: Guid (""Incorrect GUID"")]";
var assemblyInfo = ReadAssemblyInfo(assemblyInfoFile);
Assert.IsNull(assemblyInfo.Guid);
}
[TestCase]
public void ReadAssemblyInfoWithEmptyStringsTest()
{
var assemblyInfoFile =
@"using System;
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle ("""")]
[assembly: AssemblyDescription ("""")]
[assembly: AssemblyCompany ("""")]
[assembly: AssemblyProduct ("""")]
[assembly: AssemblyCopyright ("""")]
[assembly: AssemblyTrademark ("""")]
[assembly: AssemblyDefaultAlias ("""")]
// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible (true)]
// The assembly version has following format :
//
// Major.Minor.Build.Revision
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion ("""")]
[assembly: AssemblyFileVersion ("""")]
[assembly: AssemblyInformationalVersion ("""")]
[assembly: Guid ("""")]
[assembly: NeutralResourcesLanguage ("""")]";
var assemblyInfo = ReadAssemblyInfo(assemblyInfoFile);
Assert.AreEqual("", assemblyInfo.Title);
Assert.AreEqual("", assemblyInfo.Description);
Assert.AreEqual("", assemblyInfo.Company);
Assert.AreEqual("", assemblyInfo.Product);
Assert.AreEqual("", assemblyInfo.Copyright);
Assert.AreEqual("", assemblyInfo.Trademark);
Assert.AreEqual("", assemblyInfo.DefaultAlias);
Assert.IsNull(assemblyInfo.AssemblyVersion);
Assert.IsNull(assemblyInfo.AssemblyFileVersion);
Assert.IsNull(assemblyInfo.InformationalVersion);
Assert.IsNull(assemblyInfo.Guid);
Assert.AreEqual("", assemblyInfo.NeutralLanguage);
}
[TestCase]
public void ReadAssemblyInfoWithFalseBooleanValuesTest()
{
var assemblyInfoFile =
@"using System;
using System.Reflection;
using System.Runtime.InteropServices;
// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible (false)]
[assembly: CLSCompliant (false)]";
var assemblyInfo = ReadAssemblyInfo(assemblyInfoFile);
Assert.IsFalse(assemblyInfo.ComVisible);
Assert.IsFalse(assemblyInfo.ClsCompliant);
}
[TestCase]
public void ReadAssemblyInfoWithTrueBooleanValuesTest()
{
var assemblyInfoFile =
@"using System;
using System.Reflection;
using System.Runtime.InteropServices;
// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible (true)]
[assembly: CLSCompliant (true)]";
var assemblyInfo = ReadAssemblyInfo(assemblyInfoFile);
Assert.IsTrue(assemblyInfo.ComVisible);
Assert.IsTrue(assemblyInfo.ClsCompliant);
}
private AssemblyInfo ReadAssemblyInfo(string assemblyInfoFile)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(assemblyInfoFile));
var assemblyInfoProvider = new AssemblyInfoProvider();
var assemblyInfo = assemblyInfoProvider.Read(stream);
return assemblyInfo;
}
}
}
Loading…
Cancel
Save