#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2017 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.Reflection;
using NUnit.Framework;
namespace Nuclex.Support {
/// Unit Test for the strign segment class
[TestFixture]
internal class TypeHelperTest {
#region class NoDefaultConstructor
/// Test class that doesn't have a default constructor
private class NoDefaultConstructor {
/// Initializes a new instance of the test class
/// Dummy argument so this is no default constructor
public NoDefaultConstructor(int dummy) { }
}
#endregion // class NoDefaultConstructor
#region class NonPublicDefaultConstructor
/// Test class that has a non-public default constructor
private class NonPublicDefaultConstructor {
/// Initializes a new instance of the test class
protected NonPublicDefaultConstructor() { }
}
#endregion // class NonPublicDefaultConstructor
#region class PublicDefaultConstructor
/// Test class that has a public default constructor
private class PublicDefaultConstructor {
/// Initializes a new instance of the test class
public PublicDefaultConstructor() { }
}
#endregion // class PublicDefaultConstructor
#region class Base
/// Base class used to test the helper methods
private class Base {
/// A simple public field
public int PublicBaseField;
/// An automatic property with a hidden backing field
public int PublicBaseProperty { get; set; }
}
#endregion // class Base
#region class Derived
/// Derived class used to test the helper methods
private class Derived : Base {
/// A simple public field
public int PublicDerivedField;
/// An automatic property with a hidden backing field
public int PublicDerivedProperty { get; set; }
}
#endregion // class Derived
#region class HasIgnoreAttribute
/// Class that carries an IgnoreAttribute
[Ignore]
private class HasIgnoreAttribute { }
#endregion // class HasIgnoreAttribute
///
/// Verifies that the type helper can determine whether a class is carrying an attribute
///
[Test]
public void CanDetermineIfTypeHasAttribute() {
Assert.IsTrue(typeof(HasIgnoreAttribute).HasAttribute());
Assert.IsFalse(typeof(HasIgnoreAttribute).HasAttribute());
}
///
/// Verifies that the GetFieldInfosIncludingBaseClasses() will include the backing
/// fields of automatically implemented properties in base classes
///
[Test]
public void CanGetBackingFieldsForPropertiesInBaseClasses() {
FieldInfo[] fieldInfos = typeof(Derived).GetFieldInfosIncludingBaseClasses(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
);
Assert.AreEqual(4, fieldInfos.Length);
}
///
/// Useless test that avoids a compile warning about unused fields
///
[Test]
public void AvoidCompilerWarnings() {
var derived = new Derived() {
PublicBaseField = 123,
PublicBaseProperty = 321,
PublicDerivedField = 456
};
}
/// Tests whether the default constructor detection works as expected
[Test]
public void TestDefaultConstructorDetection() {
Assert.IsFalse(typeof(NoDefaultConstructor).HasDefaultConstructor());
Assert.IsFalse(typeof(NonPublicDefaultConstructor).HasDefaultConstructor());
Assert.IsTrue(typeof(PublicDefaultConstructor).HasDefaultConstructor());
}
}
} // namespace Nuclex.Support
#endif // UNITTEST