public void VarPattern_ChangeType_Variable() { var t = Expression.Variable(typeof(int)); var p = CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(int), typeof(int)), variable: t)); AssertEx.Throws <ArgumentException>(() => p.ChangeType(typeof(long))); }
public void VarPattern_Reduce_Variable() { var t = Expression.Variable(typeof(int)); var res = CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(int), typeof(int)), variable: t)); AssertCompile((log, append) => { var check = Expression.Variable(typeof(bool)); var obj = Expression.Block(log("O"), Expression.Constant(42)); return(Expression.Block( new[] { check, t }, Expression.Assign(check, res.Reduce(obj)), Expression.Invoke( append, CSharpExpression.InterpolatedString( CSharpExpression.InterpolationStringLiteral("check = "), CSharpExpression.InterpolationStringInsert(check), CSharpExpression.InterpolationStringLiteral("; value = "), CSharpExpression.InterpolationStringInsert(t) ) ), check )); }, new LogAndResult <bool> { Value = true, Log = { "O", "check = True; value = 42" } }); }
public void TypePattern_Reduce() { var res = CSharpPattern.Type(CSharpPattern.PatternInfo(typeof(object), typeof(int)), typeof(int)); AssertCompile((log, append) => { var obj = Expression.Block(log("O"), Expression.Constant(42, typeof(object))); return(res.Reduce(obj)); }, new LogAndResult <bool> { Value = true, Log = { "O" } }); AssertCompile((log, append) => { var obj = Expression.Block(log("O"), Expression.Constant(42L, typeof(object))); return(res.Reduce(obj)); }, new LogAndResult <bool> { Value = false, Log = { "O" } }); AssertCompile((log, append) => { var obj = Expression.Block(log("O"), Expression.Constant(null, typeof(object))); return(res.Reduce(obj)); }, new LogAndResult <bool> { Value = false, Log = { "O" } }); }
public void TypePattern_Properties_Object() { var p = CSharpPattern.Type(typeof(int)); Assert.AreEqual(typeof(object), p.InputType); Assert.AreEqual(typeof(int), p.NarrowedType); }
public void VarPattern_Update_Invalid() { var t = Expression.Variable(typeof(int)); var p = CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(int), typeof(int)), variable: t)); var u = Expression.Variable(typeof(long)); AssertEx.Throws <ArgumentException>(() => p.Update(u)); }
public void VarPattern_Properties() { var t = Expression.Variable(typeof(int)); var p = CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(int), typeof(int)), variable: t)); Assert.AreEqual(typeof(int), p.InputType); Assert.AreEqual(typeof(int), p.NarrowedType); Assert.AreSame(t, p.Variable); }
public void TypePattern_Visitor() { var p = CSharpPattern.Type(CSharpPattern.PatternInfo(typeof(object), typeof(int)), typeof(int)); var visitor = new V(); visitor.VisitPattern(p); Assert.IsTrue(visitor.Visited); }
public void DiscardPattern_Visitor() { var p = CSharpPattern.Discard(CSharpPattern.PatternInfo(typeof(int), typeof(int))); var visitor = new V(); visitor.VisitPattern(p); Assert.IsTrue(visitor.Visited); }
public void VarPattern_Visitor() { var t = Expression.Variable(typeof(int)); var p = CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(int), typeof(int)), variable: t)); var visitor = new V(); visitor.VisitPattern(p); Assert.IsTrue(visitor.Visited); }
public void DiscardPattern_ChangeType_Triggered() { var discard = CSharpPattern.Discard(); var length = typeof(string).GetProperty(nameof(string.Length)); var property = CSharpPattern.PropertySubpattern(discard, length); Assert.AreNotSame(discard, property.Pattern); Assert.AreEqual(typeof(int), property.Pattern.InputType); Assert.AreEqual(typeof(int), property.Pattern.NarrowedType); }
public void VarPattern_Reduce_NoVariable() { var res = CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(int), typeof(int)), variable: null)); AssertCompile((log, append) => { var obj = Expression.Block(log("O"), Expression.Constant(42)); return(res.Reduce(obj)); }, new LogAndResult <bool> { Value = true, Log = { "O" } }); }
public void DiscardPattern_ChangeType() { var p = CSharpPattern.Discard(CSharpPattern.PatternInfo(typeof(int), typeof(int))); Assert.AreSame(p, p.ChangeType(typeof(int))); var q = (DiscardCSharpPattern)p.ChangeType(typeof(long)); Assert.AreNotSame(p, q); Assert.AreEqual(typeof(long), q.InputType); Assert.AreEqual(typeof(long), q.NarrowedType); }
public void TypePattern_ChangeType() { var p = CSharpPattern.Type(CSharpPattern.PatternInfo(typeof(object), typeof(int)), typeof(int)); Assert.AreSame(p, p.ChangeType(typeof(object))); var q = (TypeCSharpPattern)p.ChangeType(typeof(ValueType)); Assert.AreNotSame(p, q); Assert.AreEqual(typeof(ValueType), q.InputType); Assert.AreEqual(typeof(int), q.NarrowedType); }
public void VarPattern_Update() { var t = Expression.Variable(typeof(int)); var p = CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(int), typeof(int)), variable: t)); Assert.AreSame(p, p.Update(t)); var u = Expression.Variable(typeof(int)); var q = p.Update(u); Assert.AreNotSame(p, q); Assert.AreSame(u, q.Variable); }
public void VarPattern_ChangeType_NoVariable() { var p = CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(int), typeof(int)), variable: null)); Assert.AreSame(p, p.ChangeType(typeof(int))); var q = (VarCSharpPattern)p.ChangeType(typeof(long)); Assert.AreNotSame(p, q); Assert.AreEqual(typeof(long), q.InputType); Assert.AreEqual(typeof(long), q.NarrowedType); Assert.IsNull(q.Variable); }
public void VarPattern_ArgumentChecking() { // null AssertEx.Throws <ArgumentNullException>(() => CSharpPattern.Var(info: null)); AssertEx.Throws <ArgumentNullException>(() => CSharpPattern.Var(variable: null)); // by-ref type AssertEx.Throws <ArgumentException>(() => CSharpPattern.Var(variable: Expression.Variable(typeof(int).MakeByRefType()))); AssertEx.Throws <ArgumentException>(() => CSharpPattern.Var(info: null, variable: Expression.Variable(typeof(int).MakeByRefType()))); // invalid typing AssertEx.Throws <ArgumentException>(() => CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(object), typeof(int)), variable: null))); AssertEx.Throws <ArgumentException>(() => CSharpPattern.Var(CSharpPattern.ObjectPatternInfo(CSharpPattern.PatternInfo(typeof(int), typeof(int)), Expression.Variable(typeof(long))))); }
public void TypePattern_ChangeType_Triggered() { var p = CSharpPattern.Type(typeof(int)); Assert.AreEqual(typeof(object), p.InputType); Assert.AreEqual(typeof(int), p.NarrowedType); var value = typeof(StrongBox <IFormattable>).GetField(nameof(StrongBox <IFormattable> .Value)); var property = CSharpPattern.PropertySubpattern(p, value); Assert.AreNotSame(p, property.Pattern); Assert.AreEqual(typeof(IFormattable), property.Pattern.InputType); Assert.AreEqual(typeof(int), property.Pattern.NarrowedType); }
public void TypePattern_ArgumentChecking() { // null checks AssertEx.Throws <ArgumentNullException>(() => CSharpPattern.Type(type: null)); AssertEx.Throws <ArgumentNullException>(() => CSharpPattern.Type(CSharpPattern.PatternInfo(typeof(object), typeof(object)), type: null)); // invalid type AssertEx.Throws <ArgumentException>(() => CSharpPattern.Type(typeof(void))); AssertEx.Throws <ArgumentException>(() => CSharpPattern.Type(typeof(int).MakeByRefType())); AssertEx.Throws <ArgumentException>(() => CSharpPattern.Type(typeof(int).MakePointerType())); // incompatible types AssertEx.Throws <ArgumentException>(() => CSharpPattern.Type(CSharpPattern.PatternInfo(typeof(object), typeof(int)), typeof(long))); }
public void DiscardPattern_Reduce_IncompatibleType() { var res = CSharpPattern.Discard(typeof(int)); AssertEx.Throws <ArgumentException>(() => res.Reduce(Expression.Default(typeof(long)))); }
public void DiscardPattern_ArgumentChecking() { // invalid typing AssertEx.Throws <ArgumentException>(() => CSharpPattern.Discard(CSharpPattern.PatternInfo(typeof(object), typeof(int)))); }
internal CSharpSubpattern(CSharpPattern pattern) => Pattern = pattern;
protected internal virtual CSharpPattern VisitPattern(CSharpPattern node) => node?.Accept(this);