|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using Microsoft.VisualStudio.TestTools.UnitTesting; |
|
|
|
namespace Expressions.Test.Unit |
|
{ |
|
[TestClass] |
|
public class CoalescingExpressionVisitorTests |
|
{ |
|
private class Class |
|
{ |
|
public string Property { get; set; } |
|
|
|
public IEnumerable<string> Collection { get; set; } |
|
} |
|
|
|
private class First |
|
{ |
|
public Second Second { get; set; } |
|
} |
|
|
|
private class Second |
|
{ |
|
public Third Third { get; set; } |
|
} |
|
|
|
private class Third |
|
{ |
|
public string Value { get; set; } |
|
} |
|
|
|
[TestMethod] |
|
public void ChainedPropertyAccess() |
|
{ |
|
var coalesceValue = "foo"; |
|
|
|
var lambda = Coalesce.CreateDelegate<First, string>(f => f.Second.Third.Value, coalesceValue); |
|
|
|
var returnValue = lambda(null); |
|
Assert.AreEqual(coalesceValue, returnValue); |
|
|
|
var first = new First(); |
|
returnValue = lambda(first); |
|
Assert.AreEqual(coalesceValue, returnValue); |
|
|
|
first.Second = new Second(); |
|
returnValue = lambda(first); |
|
Assert.AreEqual(coalesceValue, returnValue); |
|
|
|
first.Second.Third = new Third(); |
|
returnValue = lambda(first); |
|
Assert.AreEqual(first.Second.Third.Value, returnValue); |
|
|
|
first.Second.Third.Value = "bar"; |
|
returnValue = lambda(first); |
|
Assert.AreEqual(first.Second.Third.Value, returnValue); |
|
} |
|
|
|
[TestMethod] |
|
public void NonNullRootValue_ReturnsPropertyValue() |
|
{ |
|
var coalesceValue = "foo"; |
|
|
|
var lambda = Coalesce.CreateDelegate<Class, string>(o => o.Property, coalesceValue); |
|
|
|
var root = new Class() { Property = "notFoo" }; |
|
var returnValue = lambda(root); |
|
|
|
Assert.AreEqual(root.Property, returnValue); |
|
} |
|
|
|
[TestMethod] |
|
public void NullRootValue_ReturnsCoalesceValue() |
|
{ |
|
var coalesceValue = "foo"; |
|
|
|
var lambda = Coalesce.CreateDelegate<Class, string>(o => o.Property, coalesceValue); |
|
|
|
var returnValue = lambda(null); |
|
|
|
Assert.AreEqual(coalesceValue, returnValue); |
|
} |
|
|
|
[TestMethod] |
|
public void CollectionIsNull_ToString_ReturnsCoalesceValue() |
|
{ |
|
var coalesceValue = "foo"; |
|
|
|
var lambda = Coalesce.CreateDelegate<Class, string>(o => o.Collection.ToString(), coalesceValue); |
|
|
|
var root = new Class(); |
|
var returnValue = lambda(root); |
|
|
|
Assert.AreEqual(coalesceValue, returnValue); |
|
} |
|
|
|
[TestMethod] |
|
public void CollectionIsNull_FirstOrDefault_ReturnsCoalesceValue() |
|
{ |
|
var coalesceValue = "foo"; |
|
|
|
var lambda = Coalesce.CreateDelegate<Class, string>(o => o.Collection.FirstOrDefault(), coalesceValue); |
|
|
|
var root = new Class(); |
|
var returnValue = lambda(root); |
|
|
|
Assert.AreEqual(coalesceValue, returnValue); |
|
} |
|
|
|
[TestMethod] |
|
public void CollectionIsEmpty_FirstOrDefault_ReturnsNull() |
|
{ |
|
var defaultValue = "foo"; |
|
|
|
var lambda = Coalesce.CreateDelegate<Class, string>(o => o.Collection.FirstOrDefault(), defaultValue); |
|
|
|
var root = new Class() { Collection = new string[] { } }; |
|
var returnValue = lambda(root); |
|
|
|
Assert.AreEqual(null, returnValue); |
|
} |
|
|
|
[TestMethod] |
|
public void CollectionIsNotEmpty_FirstOrDefault_ReturnsFirst() |
|
{ |
|
var defaultValue = "foo"; |
|
|
|
var lambda = Coalesce.CreateDelegate<Class, string>(o => o.Collection.FirstOrDefault(), defaultValue); |
|
|
|
var root = new Class() { Collection = new string[] { "bar" } }; |
|
var returnValue = lambda(root); |
|
|
|
Assert.AreEqual(root.Collection.First(), returnValue); |
|
} |
|
|
|
[TestMethod] |
|
public void CollectionIsNotEmpty_ExtensionMethod_ReturnsFirstToMatchFilter() |
|
{ |
|
var defaultValue = "foo"; |
|
|
|
// this test case is for constant in filter |
|
var lambda = Coalesce.CreateDelegate<Class, string>(o => o.Collection.FirstOrDefault(s => s == "blah"), defaultValue); |
|
|
|
var root = new Class() { Collection = new string[] { "bar", "blah" } }; |
|
var returnValue = lambda(root); |
|
|
|
Assert.AreEqual("blah", returnValue); |
|
} |
|
|
|
[TestMethod] |
|
public void CollectionIsNotEmpty_ExtensionMethodClosure_ReturnsFirstToMatchFilter() |
|
{ |
|
var defaultValue = "foo"; |
|
var closureVariable = "blah"; |
|
|
|
var lambda = Coalesce.CreateDelegate<Class, string>(o => o.Collection.FirstOrDefault(s => s == closureVariable), defaultValue); |
|
|
|
var root = new Class() { Collection = new string[] { "bar", "blah" } }; |
|
var returnValue = lambda(root); |
|
|
|
Assert.AreEqual(closureVariable, returnValue); |
|
} |
|
} |
|
} |