Skip to content

Commit

Permalink
General cleanup, fixing naming issues (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethWhite authored and Keboo committed Oct 4, 2019
1 parent 480a845 commit 5a9a49c
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 291 deletions.
10 changes: 10 additions & 0 deletions AutoDI.AssemblyGenerator/AssemblyInvocationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace AutoDI. AssemblyGenerator
{
public class AssemblyInvocationException : Exception
{
public AssemblyInvocationException(string message) : base(message)
{ }
}
}
12 changes: 6 additions & 6 deletions AutoDI.AssemblyGenerator/AssemblyMixins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public static object InvokeStatic<TContainingType>(this Assembly assembly, strin

Type type = assembly.GetType(typeof(TContainingType).FullName);
if (type == null)
throw new AssemblyInvocationExcetion($"Could not find '{typeof(TContainingType).FullName}' in '{assembly.FullName}'");
throw new AssemblyInvocationException($"Could not find '{typeof(TContainingType).FullName}' in '{assembly.FullName}'");

MethodInfo method = type.GetMethod(methodName);
if (method == null)
throw new AssemblyInvocationExcetion($"Could not find method '{methodName}' on type '{type.FullName}'");
throw new AssemblyInvocationException($"Could not find method '{methodName}' on type '{type.FullName}'");

if (!method.IsStatic)
throw new AssemblyInvocationExcetion($"Method '{type.FullName}.{methodName}' is not static");
throw new AssemblyInvocationException($"Method '{type.FullName}.{methodName}' is not static");

return method.Invoke(null, parameters);
}
Expand All @@ -61,7 +61,7 @@ public static object InvokeGeneric<TGeneric>(this Assembly assembly, object targ
{
Type genericType = assembly.GetType(typeof(TGeneric).FullName);
if (genericType == null)
throw new AssemblyInvocationExcetion($"Could not find generic parameter type '{typeof(TGeneric).FullName}' in '{assembly.FullName}'");
throw new AssemblyInvocationException($"Could not find generic parameter type '{typeof(TGeneric).FullName}' in '{assembly.FullName}'");

return InvokeGeneric(assembly, genericType, target, methodName, parameters);
}
Expand All @@ -81,7 +81,7 @@ public static object InvokeGeneric(this Assembly assembly, Type genericType, obj

MethodInfo method = methods.FirstOrDefault(m => !m.IsStatic && m.IsGenericMethodDefinition);
if (method == null)
throw new AssemblyInvocationExcetion($"Could not find method '{methodName}' on type '{targetType.FullName}'");
throw new AssemblyInvocationException($"Could not find method '{methodName}' on type '{targetType.FullName}'");

MethodInfo genericMethod = method.MakeGenericMethod(genericType);

Expand Down Expand Up @@ -123,7 +123,7 @@ public static object Resolve<T>(this Assembly assembly, Type containerType = nul
if (provider == null)
{
//TODO: Better exception
throw new AssemblyInvocationExcetion($"Could not find service provider for '{assembly.FullName}'");
throw new AssemblyInvocationException($"Could not find service provider for '{assembly.FullName}'");
}

return provider.GetService(type);
Expand Down
5 changes: 0 additions & 5 deletions AutoDI.AssemblyGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ MetadataReference GetReference(string name)
{
return MetadataReference.CreateFromFile(filePath);
}
//Assembly loadedAssembly = Assembly.Load(new AssemblyName(name));
//if (loadedAssembly != null)
//{
// return MetadataReference.CreateFromFile(loadedAssembly.Location);
//}
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions AutoDI.Build.Tests/CanResolveFromNonGenericTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void CanResolveServiceWithGenericMethod()

var method = typeof(ServiceProviderServiceExtensions)
.GetMethod(nameof(ServiceProviderServiceExtensions.GetService))
.MakeGenericMethod(serviceType);
Assert.IsTrue(method.Invoke(null, new object[]{provider}).Is<Service>(GetType()));
?.MakeGenericMethod(serviceType);
Assert.IsTrue(method != null && method.Invoke(null, new object[]{provider}).Is<Service>(GetType()));
}
}

Expand Down
8 changes: 0 additions & 8 deletions AutoDI.Build.Tests/DependentAssemblyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,9 @@ public class DependentAssemblyTests
public static async Task Initialize(TestContext context)
{
var gen = new Generator();
//gen.WeaverAdded += (sender, args) =>
//{
// if (args.Weaver.Name == "AutoDI")
// {
// args.Weaver.Instance.Config = XElement.Parse($@"<AutoDI {nameof(Settings.GenerateRegistrations)}=""False"" />");
// }
//};

var testAssemblies = await gen.Execute();

//_sharedAssembly = testAssemblies["shared"].Assembly;
_mainAssembly = testAssemblies["main"].Assembly;

}
Expand Down
253 changes: 0 additions & 253 deletions AutoDI.Build.Tests/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,265 +13,12 @@ namespace AutoDI.Build.Tests
[TestClass]
public class SettingsTests
{
//[TestMethod]
//public void CanLoadBasicSettings()
//{
// var xml = XElement.Parse(@"<AutoDI />");
//
// var settings = Settings.Parse(new Settings(), xml);
//
// Assert.AreEqual((int)Behaviors.Default, (int)settings.Behavior);
//}
//
//[TestMethod]
//public void CanLoadIndividualBehavior()
//{
// var xml = XElement.Parse($@"<AutoDI behavior=""{Behaviors.SingleInterfaceImplementation}""/>");
//
// var settings = Settings.Parse(new Settings(), xml);
//
// Assert.AreEqual((int)Behaviors.SingleInterfaceImplementation, (int)settings.Behavior);
//}
//
//[TestMethod]
//public void CanLoadCompositeBehavior()
//{
// var xml = XElement.Parse($@"<AutoDI behavior=""{Behaviors.SingleInterfaceImplementation},{Behaviors.IncludeClasses}""/>");
//
// var settings = Settings.Parse(new Settings(), xml);
//
// Assert.AreEqual((int)(Behaviors.SingleInterfaceImplementation | Behaviors.IncludeClasses), (int)settings.Behavior);
//}
//
//[TestMethod]
//public void CanLoadSettingsWithDeclaredType()
//{
// var xml = XElement.Parse(@"
// <AutoDI>
// <type name=""NS.MyType*"" lifetime=""Transient"" />
// </AutoDI>");
//
// var settings = Settings.Parse(new Settings(), xml);
//
// Assert.AreEqual(1, settings.Types.Count);
// Assert.IsTrue(settings.Types[0].Matches("NS.MyType2"));
// Assert.AreEqual((int)Lifetime.Transient, (int)settings.Types[0].Lifetime);
//}
//
//[TestMethod]
//public void CanLoadSettingsWithSimpleMap()
//{
// var xml = XElement.Parse(@"
// <AutoDI>
// <map from=""IService"" to=""Service"" />
// </AutoDI>");
//
// var settings = Settings.Parse(new Settings(), xml);
//
// Assert.AreEqual(1, settings.Maps.Count);
// var type = new TypeDefinition("", "IService", TypeAttributes.Interface);
// Assert.IsTrue(settings.Maps[0].TryGetMap(type, out string mappedType));
// Assert.AreEqual("Service", mappedType);
//}
//
//[TestMethod]
//public void CanLoadSettingsWithRegexMap()
//{
// var xml = XElement.Parse(@"
// <AutoDI>
// <map from=""regex:ViewModels.(.*)"" to=""Views.$1"" />
// </AutoDI>");
//
// var settings = Settings.Parse(new Settings(), xml);
//
// Assert.AreEqual(1, settings.Maps.Count);
// var type = new TypeDefinition("ViewModels", "Test", TypeAttributes.Class);
// Assert.IsTrue(settings.Maps[0].TryGetMap(type, out var mappedType));
// Assert.AreEqual("Views.Test", mappedType);
//}
//
//[TestMethod]
//public void CanLoadSettingsWithIncludedAssembly()
//{
// var xml = XElement.Parse(@"
// <AutoDI>
// <assembly name=""MyAssembly.*"" />
// </AutoDI>");
//
// var settings = Settings.Parse(new Settings(), xml);
//
// Assert.AreEqual(1, settings.Assemblies.Count);
// var assembly = AssemblyDefinition.CreateAssembly(new AssemblyNameDefinition("MyAssembly.Test", new Version(1, 0, 0)), "<Module>", ModuleKind.Dll);
// Assert.IsTrue(settings.Assemblies[0].Matches(assembly));
//}
//
//[TestMethod]
//public void CanSkipContainerGeneration()
//{
// var xml = XElement.Parse(@"<AutoDI GenerateRegistrations=""False"" />");
//
// var settings = Settings.Parse(new Settings(), xml);
//
// Assert.IsFalse(settings.GenerateRegistrations);
//}
//
//[TestMethod]
//public void ErrorWhenRootElementContainsInvalidAttribute()
//{
// var xml = XElement.Parse(@"<AutoDI InvalidAttribute=""True"" />");
//
// TextExpectedParseException(xml, "'InvalidAttribute' is not a valid attribute for AutoDI");
//}
//
//[TestMethod]
//public void ErrorWhenBehaviorIsEmpty()
//{
// var xml = XElement.Parse(@"<AutoDI Behavior="""" />");
//
// TextExpectedParseException(xml, "'' is not a valid value for 'Behavior'");
//}
//
//[TestMethod]
//public void ErrorWhenBehaviorContainsAnInvalidValue()
//{
// var xml = XElement.Parse(@"<AutoDI Behavior=""InvalidValue"" />");
//
// TextExpectedParseException(xml, "'InvalidValue' is not a valid value for 'Behavior'");
//}
//
//[TestMethod]
//public void ErrorWhenAutoInitContainsAnInvalidValue()
//{
// var xml = XElement.Parse(@"<AutoDI AutoInit=""Foo"" />");
//
// TextExpectedParseException(xml, "'Foo' is not a valid value for 'AutoInit'");
//}
//
//[TestMethod]
//public void ErrorWhenGenerateRegistrationsContainsAnInvalidValue()
//{
// var xml = XElement.Parse(@"<AutoDI GenerateRegistrations=""Foo"" />");
//
// TextExpectedParseException(xml, "'Foo' is not a valid value for 'GenerateRegistrations'");
//}
//
//[TestMethod]
//public void ErrorWhenDebugLogLevelContainsAnInvalidValue()
//{
// var xml = XElement.Parse(@"<AutoDI DebugLogLevel=""Foo"" />");
//
// TextExpectedParseException(xml, "'Foo' is not a valid value for 'DebugLogLevel'");
//}
//
//[TestMethod]
//public void ErrorWhenChildNodeIsUnknown()
//{
// var xml = XElement.Parse(@"<AutoDI><Unknown /></AutoDI>");
//
// TextExpectedParseException(xml, "'Unknown' is not a valid child node of AutoDI");
//}
//
//[TestMethod]
//public void ErrorWhenAssemblyNodeDoesNotHaveName()
//{
// var xml = XElement.Parse(@"<AutoDI><Assembly /></AutoDI>");
//
// TextExpectedParseException(xml, "'Assembly' requires a value for 'Name'");
//}
//
//[TestMethod]
//public void ErrorWhenAssemblyNodeContainsInvalidAttribute()
//{
// var xml = XElement.Parse(@"<AutoDI><Assembly InvalidAttribute=""True""/></AutoDI>");
//
// TextExpectedParseException(xml, "'InvalidAttribute' is not a valid attribute for Assembly");
//}
//
//[TestMethod]
//public void ErrorWhenTypeNodeDoesNotHaveName()
//{
// var xml = XElement.Parse(@"<AutoDI><Type Lifetime=""Singleton"" /></AutoDI>");
//
// TextExpectedParseException(xml, "'Type' requires a value for 'Name'");
//}
//
//[TestMethod]
//public void ErrorWhenTypeNodeDoesNotHaveLifetime()
//{
// var xml = XElement.Parse(@"<AutoDI><Type Name=""MyClass"" /></AutoDI>");
//
// TextExpectedParseException(xml, "'Type' requires a value for 'Lifetime'");
//}
//
//[TestMethod]
//public void ErrorWhenTypeNodeDoesNotHaveValidLifetime()
//{
// var xml = XElement.Parse(@"<AutoDI><Type Name=""MyClass"" Lifetime=""Foo"" /></AutoDI>");
//
// TextExpectedParseException(xml, "'Foo' is not a valid value for 'Lifetime'");
//}
//
//[TestMethod]
//public void ErrorWhenTypeNodeContainsInvalidAttribute()
//{
// var xml = XElement.Parse(@"<AutoDI><Type Name=""MyClass"" Lifetime=""Singleton"" InvalidAttribute=""True""/></AutoDI>");
//
// TextExpectedParseException(xml, "'InvalidAttribute' is not a valid attribute for Type");
//}
//
//[TestMethod]
//public void ErrorWhenMapNodeDoesNotHaveFrom()
//{
// var xml = XElement.Parse(@"<AutoDI><Map To=""MyClass"" /></AutoDI>");
//
// TextExpectedParseException(xml, "'Map' requires a value for 'From'");
//}
//
//[TestMethod]
//public void ErrorWhenMapNodeDoesNotHaveTo()
//{
// var xml = XElement.Parse(@"<AutoDI><Map From=""MyClass"" /></AutoDI>");
//
// TextExpectedParseException(xml, "'Map' requires a value for 'To'");
//}
//
//[TestMethod]
//public void ErrorWhenMapNodeContainsInvalidAttribute()
//{
// var xml = XElement.Parse(@"<AutoDI><Map From=""IClass"" To=""MyClass"" InvalidAttribute=""True""/></AutoDI>");
//
// TextExpectedParseException(xml, "'InvalidAttribute' is not a valid attribute for Map");
//}

[TestMethod]
public void DebugCodeGenerationDefaultsOff()
{
var settings = new Settings();

Assert.AreEqual((int)CodeLanguage.None, (int)settings.DebugCodeGeneration);
}

//[TestMethod]
//public void CanEnableCSharpCodeGeneration()
//{
// var xml = XElement.Parse($@"<AutoDI {nameof(Settings.DebugCodeGeneration)}=""{CodeLanguage.CSharp}"" />");
//
// var settings = Settings.Parse(new Settings(), xml);
//
// Assert.AreEqual((int)CodeLanguage.CSharp, (int)settings.DebugCodeGeneration);
//}
//
//private static void TextExpectedParseException(XElement xml, string expectedMessage)
//{
// try
// {
// Settings.Parse(new Settings(), xml);
// }
// catch (SettingsParseException e) when (e.Message == expectedMessage)
// {
// return;
// }
// Assert.Fail("Failed to get expected exception");
//}
}
}
Loading

0 comments on commit 5a9a49c

Please sign in to comment.