Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 2.95 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Imports System.Reflection
  2. Imports System.Linq.Expressions
  3. Imports System.Runtime.CompilerServices
  4. Imports Rhino.Mocks
  5.  
  6. Public Module VisualBasicTestExtensions
  7.     Private Const DefaultFlags As BindingFlags = BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic
  8.  
  9.     <Extension()> _
  10.    Public Function [Call](Of T)(ByVal source As T, ByVal method As Action) As T
  11.         method()
  12.         Return source
  13.     End Function
  14.  
  15.     <Extension()> _
  16.     Public Function [Call](Of T, T1)(ByVal source As T, ByVal method As Action(Of T1), ByVal arg As T1) As T
  17.         method(arg)
  18.         Return source
  19.     End Function
  20.  
  21.     <Extension()> _
  22.     Public Function [Call](Of T, T1, T2)(ByVal source As T, ByVal method As Action(Of T1, T2), ByVal arg1 As T1, ByVal arg2 As T2) As T
  23.         method(arg1, arg2)
  24.         Return source
  25.     End Function
  26.  
  27.     <Extension()> _
  28.     Public Function [Call](Of T, T1, T2, T3)(ByVal source As T, ByVal method As Action(Of T1, T2, T3), ByVal arg1 As T1, ByVal arg2 As T2, ByVal arg3 As T3) As T
  29.         method(arg1, arg2, arg3)
  30.         Return source
  31.     End Function
  32.  
  33.     <Extension()> _
  34.     Public Function [Call](Of T, T1, T2, T3, T4)(ByVal source As T, ByVal method As Action(Of T1, T2, T3, T4), ByVal arg1 As T1, ByVal arg2 As T2, ByVal arg3 As T3, ByVal arg4 As T4) As T
  35.         method(arg1, arg2, arg3, arg4)
  36.         Return source
  37.     End Function
  38.  
  39.     <Extension()> _
  40.     Public Function AssertWasSet(Of T As Class, TValue)(ByVal source As T, ByVal prop As Expression(Of Func(Of T, TValue)), ByVal value As TValue) As T
  41.         source.AssertWasCalled(Function(c) c.Set(Of TValue)(prop, value))
  42.         Return source
  43.     End Function
  44.  
  45.     <Extension()> _
  46.     Public Function [Set](Of T As Class, TValue)(ByVal source As T, ByVal expression As Expression(Of Func(Of T, TValue)), ByVal value As TValue) As T
  47.         Dim memberName = CType(expression.Body, MemberExpression).Member.Name
  48.         If (SetProperty(source, memberName, value)) Then Return source
  49.         If (SetField(source, memberName, value)) Then Return source
  50.         Return source
  51.     End Function
  52.  
  53.     Private Function SetProperty(Of T As Class, TValue)(ByVal source As T, ByVal memberName As String, ByVal value As TValue) As Boolean
  54.         Dim prop = source.GetType().GetProperty(memberName, DefaultFlags)
  55.         If (prop Is Nothing) Then Return False
  56.         If (prop.GetSetMethod Is Nothing) Then Return False
  57.  
  58.         prop.SetValue(source, value, Nothing)
  59.         Return True
  60.     End Function
  61.  
  62.     Private Function SetField(Of T As Class, TValue)(ByVal source As T, ByVal memberName As String, ByVal value As TValue) As Boolean
  63.         Dim field = source.GetType().GetField("_" + memberName, DefaultFlags)
  64.         If (field Is Nothing) Then
  65.             field = source.GetType().GetField("_" & Char.ToLower(memberName(0)) & memberName.Substring(1), DefaultFlags)
  66.         End If
  67.         If (field Is Nothing) Then Return False
  68.         field.SetValue(source, value)
  69.         Return True
  70.     End Function
  71.  
  72. End Module