Skip to content

Instantly share code, notes, and snippets.

@cardinal252 cardinal252/LazyString
Last active Aug 10, 2016

Embed
What would you like to do?
Basic LazyString to help with Glass codegen + usage
public class GlassLazy<T> : Lazy<T>
{
public GlassLazy(Func<object> lazy) : base(() => (T)lazy())
{
}
public override string ToString()
{
return Value.ToString();
}
public static implicit operator T(GlassLazy<T> lazy)
{
return lazy != null ? lazy.Value : default(T);
}
public static implicit operator GlassLazy<T>(T value)
{
return new GlassLazy<T>(() => value);
}
}
[TestFixture]
public class LazyStringTests
{
[Test]
public void LazyString_implicitly_converts_to_string()
{
// Arrange
GlassLazy<string> lazyString = new GlassLazy<string>(() => "Test String");
// Assert
Assert.AreEqual("Test String", lazyString.ToString());
Assert.IsTrue(lazyString == "Test String");
}
[Test]
public void LazyString_implicitly_converts_from_string()
{
// Arrange
string target = "Test String";
// Act
GlassLazy<string> lazyString = target;
// Assert
Assert.AreEqual("Test String", lazyString.ToString());
Assert.IsTrue(lazyString == "Test String");
}
[Test]
public void LazyString_evaluates_when_null()
{
// Arrange
GlassLazy<string> lazyString = null;
// Act
string assigned = lazyString;
// Assert
Assert.IsNull(assigned);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.