Last active
August 10, 2016 18:21
-
-
Save cardinal252/52b70d64b7f039aa5862086cf98d0d58 to your computer and use it in GitHub Desktop.
Basic LazyString to help with Glass codegen + usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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