Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,174 @@
//
// StronglyTypedResourceBuilderBaseNameTests.cs - tests the baseName
// parameter passed to main Create overload
//
// Author:
// Gary Barnett (gary.barnett.mono@gmail.com)
//
// Copyright (C) Gary Barnett (2012)
//
//
// 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.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Resources.Tools;
using System.CodeDom;
using Microsoft.CSharp;
using System.Collections.Generic;
namespace MonoTests.System.Resources.Tools {
[TestFixture]
public class StronglyTypedResourceBuilderBaseNameTests {
static string [] keywords = {"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char",
"checked", "class", "const", "continue", "decimal", "default", "delegate",
"do", "double", "else", "enum", "event", "explicit", "extern", "FALSE",
"false", "finally", "fixed", "float", "for", "foreach", "goto", "if",
"implicit", "in", "int", "interface", "internal", "is", "lock", "long",
"namespace", "new", "null", "object", "operator", "out", "override", "params",
"private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed",
"short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this",
"throw", "TRUE", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe",
"ushort", "using", "virtual", "volatile", "void", "while" };
static char [] specialChars = { ' ', '\u00A0', '.', ',', ';', '|', '~', '@', '#', '%', '^', '&',
'*', '+', '-', '/', '\\', '<', '>', '?', '[', ']', '(', ')', '{',
'}', '\"', '\'', ':', '!'};
CSharpCodeProvider provider = new CSharpCodeProvider ();
Dictionary<string, object> testResources;
[SetUp]
public void Setup ()
{
testResources = new Dictionary<string, object> ();
testResources.Add ("akey", String.Empty);
}
[Test]
public void BaseNameEmpty ()
{
// empty class name should change to _
string [] unmatchables;
CodeCompileUnit ccu;
string input, expected;
input = String.Empty;
ccu = StronglyTypedResourceBuilder.Create (testResources,
input,
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
expected = "_";
Assert.AreEqual (expected,ccu.Namespaces [0].Types [0].Name);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void BaseNameInvalidIdentifier ()
{
// identifier invalid after Going through provider.CreateValidIdentifier throw exception in .NET framework
string [] unmatchables;
string input;
input = "cla$ss";
StronglyTypedResourceBuilder.Create (testResources,
input,
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
[Test]
public void BaseNameKeywords ()
{
// provider.CreateValidIdentifier used to return valid identifier
string expected;
string [] unmatchables;
CodeCompileUnit ccu;
foreach (string input in keywords) {
ccu = StronglyTypedResourceBuilder.Create (testResources,
input,
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
expected = provider.CreateValidIdentifier (input);
Assert.AreEqual (expected,ccu.Namespaces [0].Types [0].Name);
}
}
[Test, ExpectedException (typeof (ArgumentNullException))]
public void BaseNameNull ()
{
// should throw exception
string [] unmatchables;
string input;
input = null;
StronglyTypedResourceBuilder.Create (testResources,
input,
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
[Test]
public void BaseNameSpecialChars ()
{
// StronglyTypedResourceBuilder.VerifyResourceName seems to be used
string [] unmatchables;
CodeCompileUnit ccu;
string input, expected;
foreach (char c in specialChars) {
input = c.ToString ();
ccu = StronglyTypedResourceBuilder.Create (testResources,
input,
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
expected = StronglyTypedResourceBuilder.VerifyResourceName (input, provider);
Assert.AreEqual (expected,ccu.Namespaces [0].Types [0].Name);
}
}
}
}
#endif

View File

@@ -0,0 +1,359 @@
//
// StronglyTypedResourceBuilderCodeDomTest.cs - tests a CodeCompileUnit
// is correctly created for a test set of resources when calling main Create
// overload
//
// Author:
// Gary Barnett (gary.barnett.mono@gmail.com)
//
// Copyright (C) Gary Barnett (2012)
//
//
// 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.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Resources.Tools;
using System.CodeDom;
using Microsoft.CSharp;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
namespace MonoTests.System.Resources.Tools {
[TestFixture]
public class StronglyTypedResourceBuilderCodeDomTest {
CodeCompileUnit sampleCcu;
string [] unmatchables;
Dictionary<string, object> testResources;
public static T Get<T> (string propertyName, CodeCompileUnit ccu) where T:CodeTypeMember
{
foreach (CodeTypeMember ctm in ccu.Namespaces [0].Types [0].Members) {
if (ctm.GetType () == typeof (T) && ctm.Name == propertyName)
return (T) ctm;
}
return null;
}
[TestFixtureSetUp]
public void TestFixtureSetup ()
{
testResources = new Dictionary<string, object> ();
Bitmap bmp = new Bitmap (100, 100);
MemoryStream wav = new MemoryStream (1000);
// generate icon
Bitmap icoBmp = new Bitmap (50, 50);
Icon ico = Icon.FromHandle (icoBmp.GetHicon ());
DateTime dt = DateTime.Now;
testResources.Add ("astring", "myvalue"); // dont use key of "string" as its a keyword
testResources.Add ("bmp", bmp);
testResources.Add ("wav", wav);
testResources.Add ("ico", ico);
testResources.Add ("datetime", dt);
sampleCcu = StronglyTypedResourceBuilder.Create (testResources,
"TestRes",
"TestNamespace",
"TestResourcesNamespace",
new CSharpCodeProvider (),
true,
out unmatchables);
wav.Close();
}
[Test]
public void GeneratedCodeNamespace ()
{
Assert.AreEqual ("TestNamespace", sampleCcu.Namespaces [0].Name);
Assert.AreEqual (1, sampleCcu.ReferencedAssemblies.Count);
Assert.AreEqual ("System.dll", sampleCcu.ReferencedAssemblies [0]);
Assert.AreEqual ("System", sampleCcu.Namespaces [0].Imports [0].Namespace);
}
[Test]
public void ClassNameAndAccess ()
{
CodeTypeDeclaration resType = sampleCcu.Namespaces [0].Types [0];
Assert.AreEqual ("TestRes", resType.Name);
Assert.IsTrue (resType.IsClass);
Assert.IsTrue (resType.TypeAttributes == TypeAttributes.NotPublic);
}
[Test]
public void ClassAttributes ()
{
CodeTypeDeclaration resType = sampleCcu.Namespaces [0].Types [0];
//attributes
Assert.AreEqual (3,resType.CustomAttributes.Count);
Assert.AreEqual ("System.CodeDom.Compiler.GeneratedCodeAttribute",
resType.CustomAttributes [0].Name);
Assert.AreEqual (2, resType.CustomAttributes [0].Arguments.Count);
CodePrimitiveExpression cpe1 = (CodePrimitiveExpression) resType.CustomAttributes [0].Arguments [0].Value;
CodePrimitiveExpression cpe2 = (CodePrimitiveExpression) resType.CustomAttributes [0].Arguments [1].Value;
Assert.AreEqual ("System.Resources.Tools.StronglyTypedResourceBuilder", (string)cpe1.Value);
Assert.IsTrue (Regex.IsMatch((string)cpe2.Value,@"^\d\.\d\.\d\.\d$")); // coming back as 4.0.0.0 even with project set to .net 2.0 running on MD under .NET?
Assert.AreEqual ("System.Diagnostics.DebuggerNonUserCodeAttribute",
resType.CustomAttributes [1].Name);
Assert.AreEqual (0, resType.CustomAttributes [1].Arguments.Count);
Assert.AreEqual ("System.Runtime.CompilerServices.CompilerGeneratedAttribute",
resType.CustomAttributes [2].Name);
Assert.AreEqual (0, resType.CustomAttributes [2].Arguments.Count);
}
[Test]
public void Constructor ()
{
CodeMemberMethod ctor = Get<CodeConstructor> (".ctor", sampleCcu); //not checking for overloads
Assert.IsNotNull (ctor);
//access modifier
Assert.IsTrue (ctor.Attributes == MemberAttributes.FamilyAndAssembly);
//attributes
Assert.AreEqual (1,ctor.CustomAttributes.Count);
Assert.AreEqual ("System.Diagnostics.CodeAnalysis.SuppressMessageAttribute",
ctor.CustomAttributes [0].Name);
Assert.AreEqual (2, ctor.CustomAttributes [0].Arguments.Count);
CodePrimitiveExpression cpe1 = (CodePrimitiveExpression) ctor.CustomAttributes [0].Arguments[0].Value;
CodePrimitiveExpression cpe2 = (CodePrimitiveExpression) ctor.CustomAttributes [0].Arguments[1].Value;
Assert.AreEqual ("Microsoft.Performance", (string) cpe1.Value);
Assert.AreEqual ("CA1811:AvoidUncalledPrivateCode", (string) cpe2.Value);
}
[Test]
public void ResourceManField ()
{
CodeMemberField resourceMan = Get<CodeMemberField> ("resourceMan", sampleCcu);
Assert.IsNotNull (resourceMan);
Assert.AreEqual ("System.Resources.ResourceManager", resourceMan.Type.BaseType);
//access modifier : outputs to private static but following settings found in .net tests
Assert.IsTrue (resourceMan.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.Assembly
| MemberAttributes.FamilyOrAssembly));
}
[Test]
public void ResourceCultureField ()
{
CodeMemberField resourceCulture = Get<CodeMemberField> ("resourceCulture", sampleCcu);
Assert.IsNotNull (resourceCulture);
Assert.AreEqual ("System.Globalization.CultureInfo", resourceCulture.Type.BaseType);
//access modifier
Assert.IsTrue (resourceCulture.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.Assembly
| MemberAttributes.FamilyOrAssembly));
}
[Test]
public void ResourceManagerProperty ()
{
CodeMemberProperty resourceManager = Get<CodeMemberProperty> ("ResourceManager", sampleCcu);
Assert.IsNotNull (resourceManager);
Assert.AreEqual ("System.Resources.ResourceManager", resourceManager.Type.BaseType);
//access modifer
Assert.IsTrue (resourceManager.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.Assembly));
// attributes
Assert.AreEqual (1, resourceManager.CustomAttributes.Count);
Assert.AreEqual ("System.ComponentModel.EditorBrowsableAttribute",
resourceManager.CustomAttributes [0].Name);
Assert.AreEqual (1, resourceManager.CustomAttributes [0].Arguments.Count);
CodeFieldReferenceExpression cfe1 = (CodeFieldReferenceExpression)resourceManager.CustomAttributes [0].Arguments [0].Value;
Assert.AreEqual ("Advanced", cfe1.FieldName);
Assert.AreEqual ("System.ComponentModel.EditorBrowsableState",
((CodeTypeReferenceExpression)cfe1.TargetObject).Type.BaseType);
//getter
Assert.IsInstanceOfType (typeof (CodeConditionStatement), resourceManager.GetStatements [0]);
Assert.AreEqual (2,
((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements.Count);
CodeVariableDeclarationStatement cvds = ((CodeVariableDeclarationStatement)((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements [0]);
Assert.AreEqual ("System.Resources.ResourceManager",
((CodeObjectCreateExpression)cvds.InitExpression).CreateType.BaseType);
Assert.AreEqual ("TestResourcesNamespace.TestRes",
((CodePrimitiveExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [0]).Value);
CodePropertyReferenceExpression cpre = (CodePropertyReferenceExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [1];
Assert.AreEqual ("Assembly" ,cpre.PropertyName);
Assert.AreEqual ("TestRes", ((CodeTypeOfExpression)cpre.TargetObject).Type.BaseType);
//return
Assert.AreEqual ("resourceMan",
((CodeFieldReferenceExpression)((CodeMethodReturnStatement)resourceManager.GetStatements [1]).Expression).FieldName);
}
[Test]
public void CultureProperty ()
{
CodeMemberProperty culture = Get<CodeMemberProperty> ("Culture", sampleCcu);
Assert.IsNotNull (culture);
Assert.AreEqual ("System.Globalization.CultureInfo", culture.Type.BaseType);
Assert.IsTrue (culture.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.Assembly));
Assert.AreEqual (1,culture.CustomAttributes.Count);
Assert.AreEqual ("System.ComponentModel.EditorBrowsableAttribute",
culture.CustomAttributes [0].Name);
CodeFieldReferenceExpression cfe1 = (CodeFieldReferenceExpression) culture.CustomAttributes [0].Arguments [0].Value;
Assert.AreEqual ("Advanced", cfe1.FieldName);
Assert.AreEqual ("System.ComponentModel.EditorBrowsableState",
((CodeTypeReferenceExpression)cfe1.TargetObject).Type.BaseType);
// getter
Assert.AreEqual ("resourceCulture",
((CodeFieldReferenceExpression)((CodeMethodReturnStatement)culture.GetStatements [0]).Expression).FieldName);
// setter
Assert.AreEqual (1, culture.SetStatements.Count);
Assert.AreEqual("resourceCulture",
((CodeFieldReferenceExpression)((CodeAssignStatement)culture.SetStatements [0]).Left).FieldName);
Assert.IsInstanceOfType (typeof (CodePropertySetValueReferenceExpression),
((CodeAssignStatement)culture.SetStatements [0]).Right);
}
[Test]
public void ResourcePropertiesForStandard()
{
// property declares variable obj and set it to Object ResourceManager.GetObject(..)
// then returns obj cast back to its original type
foreach ( KeyValuePair<string,object> kvp in testResources) {
if (kvp.Value is String || kvp.Value is Stream)
continue;
CodeMemberProperty cmp = Get<CodeMemberProperty> (kvp.Key, sampleCcu);
Assert.IsNotNull (cmp);
Type thisType = kvp.Value.GetType ();
// property type
Assert.AreEqual (thisType.FullName,cmp.Type.BaseType);
CodeVariableDeclarationStatement cvds = (CodeVariableDeclarationStatement)cmp.GetStatements [0];
Assert.AreEqual ("obj", cvds.Name);
Assert.AreEqual ("GetObject",
((CodeMethodInvokeExpression)cvds.InitExpression).Method.MethodName);
CodeMethodInvokeExpression cmie = ((CodeMethodInvokeExpression)cvds.InitExpression);
Assert.AreEqual ("ResourceManager",
((CodePropertyReferenceExpression)cmie.Method.TargetObject).PropertyName);
Assert.AreEqual (kvp.Key, ((CodePrimitiveExpression)cmie.Parameters [0]).Value);
Assert.AreEqual ("resourceCulture",
((CodeFieldReferenceExpression)cmie.Parameters [1]).FieldName);
CodeCastExpression cce = ((CodeCastExpression)((CodeMethodReturnStatement)cmp.GetStatements [1]).Expression);
Assert.AreEqual (thisType.FullName, ((CodeTypeReference)cce.TargetType).BaseType);
Assert.AreEqual ("obj",
((CodeVariableReferenceExpression)cce.Expression).VariableName);
}
}
[Test]
public void ResourcePropertyForStrings ()
{
// property returns string ResourceManager.GetString(..)
foreach ( KeyValuePair<string,object> kvp in testResources) {
if (!(kvp.Value is String))
continue;
CodeMemberProperty cmp = Get<CodeMemberProperty> (kvp.Key, sampleCcu);
Assert.IsNotNull (cmp);
Assert.AreEqual ("System.String", cmp.Type.BaseType);
Assert.IsInstanceOfType (typeof (CodeMethodReturnStatement), cmp.GetStatements [0]);
CodeMethodInvokeExpression cmie = ((CodeMethodInvokeExpression)((CodeMethodReturnStatement)cmp.GetStatements [0]).Expression);
Assert.AreEqual ("GetString",cmie.Method.MethodName);
Assert.AreEqual (kvp.Key,((CodePrimitiveExpression)cmie.Parameters[0]).Value);
Assert.AreEqual ("ResourceManager",
((CodePropertyReferenceExpression)cmie.Method.TargetObject).PropertyName);
Assert.AreEqual ("resourceCulture",
((CodeFieldReferenceExpression)cmie.Parameters [1]).FieldName);
}
}
[Test]
public void ResourcePropertyForStreams ()
{
// property returns UnmanagedMemoryStream ResourceManager.GetStream(..)
foreach ( KeyValuePair<string,object> kvp in testResources) {
if (!(kvp.Value is Stream))
continue;
CodeMemberProperty cmp = Get<CodeMemberProperty> (kvp.Key, sampleCcu);
Assert.IsNotNull (cmp);
Assert.AreEqual ("System.IO.UnmanagedMemoryStream",cmp.Type.BaseType);
CodeMethodInvokeExpression cmie = ((CodeMethodInvokeExpression)((CodeMethodReturnStatement)cmp.GetStatements [0]).Expression);
Assert.AreEqual ("GetStream", cmie.Method.MethodName);
Assert.AreEqual (kvp.Key, ((CodePrimitiveExpression)cmie.Parameters[0]).Value);
Assert.AreEqual ("ResourceManager",
((CodePropertyReferenceExpression)cmie.Method.TargetObject).PropertyName);
Assert.AreEqual ("resourceCulture",
((CodeFieldReferenceExpression)cmie.Parameters [1]).FieldName);
}
}
}
}
#endif

View File

@@ -0,0 +1,338 @@
//
// StronglyTypedResourceBuilderNamespaceTests.cs - tests the
// generatedCodeNamespace and resourcesNamespace params of the main
// Create overload
//
// Author:
// Gary Barnett (gary.barnett.mono@gmail.com)
//
// Copyright (C) Gary Barnett (2012)
//
//
// 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.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Resources.Tools;
using System.CodeDom;
using Microsoft.CSharp;
using System.Collections.Generic;
namespace MonoTests.System.Resources.Tools {
[TestFixture]
public class StronglyTypedResourceBuilderNamespaceTests {
static string [] keywords = {"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char",
"checked", "class", "const", "continue", "decimal", "default", "delegate",
"do", "double", "else", "enum", "event", "explicit", "extern", "FALSE",
"false", "finally", "fixed", "float", "for", "foreach", "goto", "if",
"implicit", "in", "int", "interface", "internal", "is", "lock", "long",
"namespace", "new", "null", "object", "operator", "out", "override", "params",
"private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed",
"short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this",
"throw", "TRUE", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe",
"ushort", "using", "virtual", "volatile", "void", "while" };
static char [] specialChars = { ' ', '\u00A0', '.', ',', ';', '|', '~', '@', '#', '%', '^', '&',
'*', '+', '-', '/', '\\', '<', '>', '?', '[', ']', '(', ')', '{',
'}', '\"', '\'', ':', '!'};
CSharpCodeProvider provider = new CSharpCodeProvider ();
Dictionary<string, object> testResources;
[SetUp]
public void Setup ()
{
testResources = new Dictionary<string, object> ();
testResources.Add ("akey", String.Empty);
}
[Test]
public void GeneratedCodeNamespaceEmpty ()
{
// empty namespace allowed
string [] unmatchables;
string input, expected;
CodeCompileUnit ccu;
input = String.Empty;
expected = String.Empty;
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
input,
"TestResourceNameSpace",
provider,
true,
out unmatchables);
Assert.AreEqual (expected,ccu.Namespaces [0].Name);
}
[Test]
public void GeneratedCodeNamespaceNull ()
{
// null should be replaced with String.Empty
string [] unmatchables;
string input, expected;
CodeCompileUnit ccu;
input = null;
expected = String.Empty;
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
input,
"TestResourceNameSpace",
provider,
true,
out unmatchables);
Assert.AreEqual (expected,ccu.Namespaces [0].Name);
}
[Test]
public void GeneratedCodeNamespaceProviderInvalidIdentifiersOK ()
{
// identifiers which are still invalid after CreateValidIdentifier called allowed through in .NET framework
string [] unmatchables;
string input, output, expected;
CodeCompileUnit ccu;
input = "te$st";
expected = "te$st";
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
input,
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
output = ccu.Namespaces [0].Name;
Assert.AreEqual (expected,output);
}
[Test]
public void GeneratedCodeNamespaceProviderKeywords ()
{
string expected;
string [] unmatchables;
CodeCompileUnit ccu;
foreach (string input in keywords) {
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
input,
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
expected = provider.CreateValidIdentifier (input);
Assert.AreEqual (expected,ccu.Namespaces [0].Name);
}
}
[Test]
public void GeneratedCodeNamespaceProviderKeywordsMultipart ()
{
// .NET framework does not check individiual elements of multipart namespace
string expected, input;
string [] unmatchables;
CodeCompileUnit ccu;
foreach (string word in keywords) {
input = "Primary." + word;
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
input,
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
expected = provider.CreateValidIdentifier (input);
Assert.AreEqual (expected,ccu.Namespaces [0].Name);
}
}
[Test]
public void GeneratedCodeNamespaceSpecialChars ()
{
// invalid chars replaced with _ noting (. and :) are allowed by .NET framework
string [] unmatchables;
string input, output, expected;
CodeCompileUnit ccu;
foreach (char c in specialChars) {
input = "test" + c.ToString ();
if (c == '.' || c == ':')
expected = input;
else
expected = StronglyTypedResourceBuilder.VerifyResourceName(input, provider);
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
input,
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
output = ccu.Namespaces [0].Name;
Assert.AreEqual (expected,output);
}
}
[Test]
public void ResourcesNamespaceEmpty ()
{
// when ResourcesNamespace is String.Empty no namespace is used
string [] unmatchables;
string input, output, expected;
CodeCompileUnit ccu;
CodeMemberProperty resourceManager;
CodeVariableDeclarationStatement cvds;
input = String.Empty;
expected = "TestClass";
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNameSpace",
input,
provider,
true,
out unmatchables);
resourceManager = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
"ResourceManager", ccu);
cvds = ((CodeVariableDeclarationStatement)((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements [0]);
output = ((CodePrimitiveExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [0]).Value.ToString ();
Assert.AreEqual (expected,output);
}
[Test]
public void ResourcesNamespaceNull ()
{
// when ResourcesNamespace is null generatedCodeNamespace is used in its place
string[] unmatchables;
string input, output, expected;
CodeCompileUnit ccu;
CodeMemberProperty resourceManager;
CodeVariableDeclarationStatement cvds;
input = null;
expected = "TestNameSpace.TestClass";
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNameSpace",
input,
provider,
true,
out unmatchables);
resourceManager = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
"ResourceManager", ccu);
cvds = ((CodeVariableDeclarationStatement)((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements [0]);
output = ((CodePrimitiveExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [0]).Value.ToString ();
Assert.AreEqual (expected,output);
}
[Test]
public void ResourcesNamespaceProviderKeywords ()
{
// not validated against provider keywords in .net framework
string output,expected;
string [] unmatchables;
CodeCompileUnit ccu;
CodeMemberProperty resourceManager;
CodeVariableDeclarationStatement cvds;
foreach (string input in keywords) {
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNamespace",
input,
provider,
true,
out unmatchables);
expected = input + ".TestClass";
resourceManager = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
"ResourceManager", ccu);
cvds = ((CodeVariableDeclarationStatement)((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements [0]);
output = ((CodePrimitiveExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [0]).Value.ToString ();
Assert.AreEqual (expected,output);
}
}
[Test]
public void ResourcesNamespaceSpecialChars ()
{
// ResourcesNamespace doesnt seem to be validated at all in .NET framework
string [] unmatchables;
string input, output, expected;
CodeCompileUnit ccu;
CodeMemberProperty resourceManager;
CodeVariableDeclarationStatement cvds;
foreach (char c in specialChars) {
input = "test" + c.ToString ();
expected = input + ".TestClass";
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNameSpace",
input,
provider,
true,
out unmatchables);
resourceManager = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
"ResourceManager", ccu);
cvds = ((CodeVariableDeclarationStatement)((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements [0]);
output = ((CodePrimitiveExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [0]).Value.ToString ();
Assert.AreEqual (expected,output);
}
}
}
}
#endif

View File

@@ -0,0 +1,231 @@
//
// StronglyTypedResourceBuilderOtherTests.cs - tests the internalClass,
// codeProvider and resourceList params of the main Create overload
//
// Author:
// Gary Barnett (gary.barnett.mono@gmail.com)
//
// Copyright (C) Gary Barnett (2012)
//
//
// 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.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Resources.Tools;
using System.CodeDom;
using Microsoft.CSharp;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Drawing;
namespace MonoTests.System.Resources.Tools {
[TestFixture]
public class StronglyTypedResourceBuilderOtherTests {
CSharpCodeProvider provider = new CSharpCodeProvider ();
Dictionary<string,object> GetAllResourceTypes ()
{
Dictionary<string, object> tempDict = new Dictionary<string, object> ();
Bitmap bmp = new Bitmap (100,100);
MemoryStream wav = new MemoryStream (1000);
tempDict.Add ("astring", "myvalue");
tempDict.Add ("bmp", bmp);
tempDict.Add ("wav", wav);
wav.Close ();
return tempDict;
}
Dictionary<string,object> GetTestResources ()
{
Dictionary<string, object> tempDict = new Dictionary<string, object> ();
tempDict.Add ("akey", string.Empty);
return tempDict;
}
[Test]
public void InternalClassFalse ()
{
// check access modifiers for class, Culture, ResourceManager, string, stream and standard resource properties
Dictionary<string, object> testResources = GetAllResourceTypes ();
string [] unmatchables;
CodeCompileUnit ccu;
CodeMemberProperty cmp;
bool isInternal = false;
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNamespace",
"TestResourcesNameSpace",
provider,
isInternal,
out unmatchables);
CodeTypeDeclaration resType = ccu.Namespaces [0].Types [0];
Assert.IsTrue (resType.TypeAttributes == TypeAttributes.Public);
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
"ResourceManager", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.FamilyAndAssembly
| MemberAttributes.FamilyOrAssembly));
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("Culture", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.FamilyAndAssembly
| MemberAttributes.FamilyOrAssembly));
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("astring", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.FamilyAndAssembly
| MemberAttributes.FamilyOrAssembly));
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("bmp", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.FamilyAndAssembly
| MemberAttributes.FamilyOrAssembly));
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("wav", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.FamilyAndAssembly
| MemberAttributes.FamilyOrAssembly));
}
[Test]
public void InternalClassTrue ()
{
// check access modifiers for class, Culture, ResourceManager, string, stream and standard resource properties
Dictionary<string, object> testResources = GetAllResourceTypes ();
string [] unmatchables;
CodeCompileUnit ccu;
CodeMemberProperty cmp;
bool isInternal = true;
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNamespace",
"TestResourcesNameSpace",
provider,
isInternal,
out unmatchables);
CodeTypeDeclaration resType = ccu.Namespaces [0].Types [0];
Assert.IsTrue (resType.TypeAttributes == TypeAttributes.NotPublic);
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
"ResourceManager", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.Assembly));
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("Culture", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.Assembly));
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("astring", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.Assembly));
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("bmp", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.Assembly));
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("wav", ccu);
Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
| MemberAttributes.Final
| MemberAttributes.Assembly));
}
[Test, ExpectedException (typeof (ArgumentNullException))]
public void ProviderNull ()
{
// should throw exception
Dictionary<string, object> testResources = GetTestResources ();
string [] unmatchables;
StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNamespace",
"TestResourcesNameSpace",
null, //setting provider to null
true,
out unmatchables);
}
[Test]
public void ResourceListEmpty ()
{
//should still create class with default members
Dictionary<string, object> testResources;
string [] unmatchables;
CodeCompileUnit ccu;
testResources = new Dictionary<string, object> ();
ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
Assert.AreEqual(5,ccu.Namespaces [0].Types [0].Members.Count);
}
[Test, ExpectedException (typeof (ArgumentNullException))]
public void ResourceListNull ()
{
// should through exception
Dictionary<string, object> testResources;
string [] unmatchables;
CSharpCodeProvider provider = new CSharpCodeProvider ();
testResources = null;
StronglyTypedResourceBuilder.Create (testResources,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
}
}
#endif

View File

@@ -0,0 +1,302 @@
//
// StronglyTypedResourceBuilderResourceNameTests.cs - tests validation
// of the Resource Names passed in the resourceList param of the main
// Create overload and the unmatatchables returned
//
// Author:
// Gary Barnett (gary.barnett.mono@gmail.com)
//
// Copyright (C) Gary Barnett (2012)
//
//
// 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.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Resources.Tools;
using System.CodeDom;
using Microsoft.CSharp;
using System.Collections.Generic;
namespace MonoTests.System.Resources.Tools {
[TestFixture]
public class StronglyTypedResourceBuilderResourceNameTests {
CSharpCodeProvider provider = new CSharpCodeProvider ();
[Test, ExpectedException (typeof (ArgumentException))]
public void ResourceNamesCaseSensitiveDupes ()
{
// passing in case insensitive dupes of resourcenames throws exception in .NET framework
Dictionary<string, object> testResources = new Dictionary<string, object> ();
string [] unmatchables;
testResources.Add ("FortyTwo", String.Empty);
testResources.Add ("fortytwo", String.Empty);
StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void ResourceNamesCaseSensitiveDupesIgnored ()
{
// passing in case insensitive dupes of resourcenames to be ignored still raises exception
Dictionary<string, object> testResources = new Dictionary<string, object> ();
string [] unmatchables;
testResources.Add (">>FortyTwo", String.Empty);
testResources.Add (">>fortytwo", String.Empty);
StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void ResourceNamesCaseSensitiveDupesInvalid ()
{
// passing in case insensitive dupes of invalid resourcenames still raises exception
Dictionary<string, object> testResources = new Dictionary<string, object> ();
string [] unmatchables;
testResources.Add ("Fo$rtyTwo", String.Empty);
testResources.Add ("fo$rtytwo", String.Empty);
StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
[Test]
public void ResourceNamesCaseSensitiveDupesWithSpecialChars ()
{
// resourcenames with special chars that would result in case insentive dupes go to unmatchables
Dictionary<string, object> testResources = new Dictionary<string, object> ();
string [] unmatchables;
testResources.Add ("Fo&rtyTwo", String.Empty);
testResources.Add ("fo_rtytwo", String.Empty);
StronglyTypedResourceBuilder.Create (testResources,
"TestClass",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
Assert.AreEqual (2,unmatchables.Length);
}
[Test]
public void ResourceNamesDuplicate_NETBUG ()
{
/*
* DUPES CHECK HAPPENS AFTER VerifyResourceName called which changed eg.
* language keywords have _ appended to start
* string.emtpy converted to _
* various chars replaced
* .net inconsistency:
* if keys contain multiple single char names made up of invalid chars
* which are converted to _, sometimes none are used and all are returned
* in unmatchables, while other times one is used, and other times
* none are used but one is still missing from unmatchables like in this case
*/
Dictionary<string, object> testResources = new Dictionary<string, object> ();
string [] unmatchables;
testResources.Add ("for", "1");
testResources.Add ("_for", "2");
testResources.Add (String.Empty, String.Empty);
testResources.Add ("*", String.Empty);
testResources.Add ("_", String.Empty);
testResources.Add (".", String.Empty);
testResources.Add ("/", String.Empty);
testResources.Add ("\\", String.Empty);
testResources.Add ("imok", "2");
CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
int matchedResources = testResources.Count - unmatchables.Length;
int membersExpected = matchedResources + 5; // 5 standard members
Assert.AreEqual (membersExpected,ccu.Namespaces [0].Types [0].Members.Count);
}
[Test]
public void ResourceNamesDuplicate ()
{
/*
* DUPES CHECK HAPPENS AFTER VerifyResourceName called which changed eg.
* language keywords have _ appended to start
* string.emtpy converted to _
* various chars replaced
*/
Dictionary<string, object> testResources = new Dictionary<string, object> ();
string [] unmatchables;
testResources.Add ("for", "1");
testResources.Add ("_for", "2");
testResources.Add ("&", String.Empty);
testResources.Add ("_", String.Empty);
testResources.Add ("imok", "2");
CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
int matchedResources = testResources.Count - unmatchables.Length;
int membersExpected = matchedResources + 5; // 5 standard members
Assert.AreEqual (membersExpected,ccu.Namespaces [0].Types [0].Members.Count);
}
[Test]
public void ResourceNamesIgnored ()
{
// names beginning with the chars "$" and ">>" ignored and not put in unmatchables
Dictionary<string, object> testResources = new Dictionary<string, object>();
string [] unmatchables;
testResources.Add ("$test1", String.Empty);
testResources.Add ("$test2", String.Empty);
testResources.Add (">>test1", String.Empty);
testResources.Add (">>test2", String.Empty);
testResources.Add ("$", String.Empty);
testResources.Add (">>", String.Empty);
testResources.Add (">", String.Empty);
testResources.Add (">test1", String.Empty);
testResources.Add ("test>>", String.Empty);
// resource name with $ somwhere else goes into unmatchables as invalid with csharpprovider
CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
Assert.AreEqual(0,unmatchables.Length);
Assert.AreEqual (8,ccu.Namespaces [0].Types [0].Members.Count); // 3 valid + 5 standard
Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
"_", ccu));
Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
"_test1", ccu));
Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
"test__", ccu));
}
[Test]
public void ResourceNamesInvalid ()
{
// resources named Culture and ResourceManager go into unmatchables (case sensitive)
// if there is a $ in resource name after char 0 it goes into unmatchables with csharpprovider
Dictionary<string, object> testResources = new Dictionary<string, object> ();
string [] unmatchables;
testResources.Add ("ResourceManager", String.Empty);
testResources.Add ("Culture", String.Empty);
testResources.Add ("t$est", String.Empty);
CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
Assert.AreEqual (3,unmatchables.Length);
Assert.AreEqual (5,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard
}
[Test]
public void ResourceNamesInvalidDifferentCase ()
{
// resources named Culture and ResourceManager go into unmatchables (case sensitive)
Dictionary<string, object> testResources = new Dictionary<string, object> ();
string [] unmatchables;
testResources.Add ("resourceManager", String.Empty);
testResources.Add ("culture", String.Empty);
testResources.Add ("t$est", String.Empty);
CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
Assert.AreEqual (1,unmatchables.Length);
Assert.AreEqual (7,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard +2
}
[Test, ExpectedException (typeof (InvalidCastException))]
public void ResourceNamesNotString ()
{
// throws InvalidCastException in .net framework
Dictionary<object, object> testResources = new Dictionary<object, object> ();
string [] unmatchables;
testResources.Add (DateTime.MinValue, "1");
StronglyTypedResourceBuilder.Create (testResources,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
}
}
#endif

View File

@@ -0,0 +1,188 @@
//
// StronglyTypedResourceBuilderResxFileTests.cs - tests overloads of Create
// method that accept resx file names
//
// Author:
// Gary Barnett (gary.barnett.mono@gmail.com)
//
// Copyright (C) Gary Barnett (2012)
//
//
// 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.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Resources.Tools;
using Microsoft.CSharp;
using System.IO;
using System.CodeDom;
using System.Resources;
using System.Drawing;
namespace MonoTests.System.Resources.Tools {
[TestFixture]
public class StronglyTypedResourceBuilderResxFileTests {
CSharpCodeProvider provider = new CSharpCodeProvider ();
[Test, ExpectedException (typeof (ArgumentException))]
public void ResXFilenameEmpty ()
{
// in .NET framework throws exception
string [] unmatchables;
string resx = String.Empty;
StronglyTypedResourceBuilder.Create (resx,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
/* FIXME: need platform dependant check, (file returns not found if invalid anyway)
[Test, ExpectedException (typeof (ArgumentException))]
public void ResXFilenameInvalid ()
{
// in .NET framework throws exception
string [] unmatchables;
string resx = @"C::::\\\\Hello/World";
StronglyTypedResourceBuilder.Create (resx,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
*/
[Test, ExpectedException (typeof (ArgumentNullException))]
public void ResXFilenameNull ()
{
//should throw exception
string [] unmatchables;
string resx = null;
StronglyTypedResourceBuilder.Create (resx,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
[Test, ExpectedException (typeof (FileNotFoundException))]
public void ResXFileNotFound ()
{
// not documented on msdn but throws FileNotFoundException
string [] unmatchables;
//get a valid new filename and then make it not exist
string resx = Path.GetTempFileName ();
File.Delete (resx);
StronglyTypedResourceBuilder.Create (resx,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
}
[Test]
public void ResXFileNotResx ()
{
//***should throw exception but Not using ExpectedException as i want to delete temp file***
string [] unmatchables;
bool exceptionRaised = false;
string resx = Path.GetTempFileName();
try {
StronglyTypedResourceBuilder.Create (resx,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
} catch (Exception ex) {
exceptionRaised = true;
Assert.IsInstanceOfType (typeof (ArgumentException), ex);
} finally {
Assert.IsTrue (exceptionRaised);
File.Delete (resx);
}
}
[Test]
public void ResxFileProcessed ()
{
// resources in resx should be present in codecompileunit with correct property type
string [] unmatchables;
CodeCompileUnit ccu;
Bitmap bmp = new Bitmap (100, 100);
MemoryStream wav = new MemoryStream (1000);
string resxFileName = Path.GetTempFileName();
using (ResXResourceWriter writer = new ResXResourceWriter(resxFileName)) {
writer.AddResource ("astring", "myvalue"); // dont use key of "string" as its a keyword
writer.AddResource ("bmp", bmp);
writer.AddResource ("wav", wav);
writer.Generate ();
}
ccu = StronglyTypedResourceBuilder.Create (resxFileName,
"TestRes",
"TestNamespace",
"TestResourcesNameSpace",
provider,
true,
out unmatchables);
CodeMemberProperty cmp;
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("astring", ccu);
Assert.IsNotNull (cmp);
Assert.AreEqual ("System.String", cmp.Type.BaseType);
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("wav", ccu);
Assert.IsNotNull (cmp);
Assert.AreEqual ("System.IO.UnmanagedMemoryStream", cmp.Type.BaseType);
cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("bmp", ccu);
Assert.IsNotNull (cmp);
Assert.AreEqual ("System.Drawing.Bitmap", cmp.Type.BaseType);
wav.Close ();
File.Delete (resxFileName);
}
}
}
#endif

View File

@@ -0,0 +1,121 @@
//
// StronglyTypedResourceBuilderVerifyResourceNameTests.cs tests
// the VerifyResourceName method
//
// Author:
// Gary Barnett (gary.barnett.mono@gmail.com)
//
// Copyright (C) Gary Barnett (2012)
//
//
// 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.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Resources.Tools;
using Microsoft.CSharp;
namespace MonoTests.System.Resources.Tools {
[TestFixture]
public class StronglyTypedResourceBuilderVerifyResourceNameTests {
static string [] keywords = {"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char",
"checked", "class", "const", "continue", "decimal", "default", "delegate",
"do", "double", "else", "enum", "event", "explicit", "extern", "FALSE",
"false", "finally", "fixed", "float", "for", "foreach", "goto", "if",
"implicit", "in", "int", "interface", "internal", "is", "lock", "long",
"namespace", "new", "null", "object", "operator", "out", "override", "params",
"private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed",
"short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this",
"throw", "TRUE", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe",
"ushort", "using", "virtual", "volatile", "void", "while" };
static char [] specialChars = { ' ', '\u00A0', '.', ',', ';', '|', '~', '@', '#', '%', '^', '&',
'*', '+', '-', '/', '\\', '<', '>', '?', '[', ']', '(', ')', '{',
'}', '\"', '\'', ':', '!'};
CSharpCodeProvider provider = new CSharpCodeProvider();
[Test]
public void VerifyResourceNameEmpty ()
{
// should return _
string output = StronglyTypedResourceBuilder.VerifyResourceName (string.Empty, provider);
Assert.AreEqual ("_", output);
}
[Test, ExpectedException (typeof (ArgumentNullException))]
public void VerifyResourceNameNull () {
// should throw exception
StronglyTypedResourceBuilder.VerifyResourceName (null, provider);
}
[Test]
public void VerifyResourceNameProviderInvalidIdentifiers () {
// function tests by means of provider.IsValidIdentifier after other checks
string output;
output = StronglyTypedResourceBuilder.VerifyResourceName ("tes$t", provider);
Assert.AreEqual (null, output);
}
[Test]
public void VerifyResourceNameProviderKeywords ()
{
// not complete list, doesnt really need to be
string expected, output;
foreach (string input in keywords) {
output = StronglyTypedResourceBuilder.VerifyResourceName (input, provider);
expected = provider.CreateValidIdentifier (input);
Assert.AreEqual (expected, output);
}
}
[Test, ExpectedException (typeof (ArgumentNullException))]
public void VerifyResourceNameProviderNull () {
// should throw exception
StronglyTypedResourceBuilder.VerifyResourceName ("tes$t", null);
}
[Test]
public void VerifyResourceNameSpecialChars ()
{
// should replace with _
string input, expected, output;
foreach (char c in specialChars) {
input = string.Format ("{0}a{0}b{0}", c);
expected = string.Format ("{0}a{0}b{0}", '_');
output = StronglyTypedResourceBuilder.VerifyResourceName (input, provider);
Assert.AreEqual (expected, output);
}
}
}
}
#endif