Guest User

Untitled

a guest
Jul 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. interface IValid
  2. {
  3. bool IsValid();
  4. }
  5.  
  6. public abstract class BaseClass : IValid
  7. {
  8. public bool Validity { get; protected set; } = true;
  9.  
  10. public bool IsValid()
  11. {
  12. return this.Validity;
  13. }
  14. }
  15.  
  16. public class ChildOfTrue : BaseClass
  17. {
  18. public ChildOfTrue() => this.Validity = true;
  19. }
  20.  
  21. public class ChildOfFalse : BaseClass
  22. {
  23. public ChildOfFalse() => this.Validity = false;
  24. }
  25.  
  26. public class TestCase
  27. {
  28. public TestCase()
  29. {
  30. List<BaseClass> testSubjects = new List<BaseClass>
  31. {
  32. new ChildOfTrue(),
  33. new ChildOfFalse(),
  34. null
  35. };
  36. foreach (var testSubject in testSubjects)
  37. {
  38. if (testSubject != null && testSubject.IsValid())
  39. {
  40. /* Perform tests. */
  41. }
  42. }
  43. }
  44. }
  45.  
  46. public static class BaseClassExtensionHelper
  47. {
  48. public static bool IsValid(this BaseClass o)
  49. {
  50. if (ReferenceEquals(o, null)) return false;
  51. return o.Validity;
  52. }
  53.  
  54. public static bool IsNotNullAndValid(this BaseClass o)
  55. {
  56. if (ReferenceEquals(o, null)) return false;
  57. return o.IsValid();
  58. }
  59. }
  60.  
  61. if (BaseClassExtensionHelper.IsValid(testSubject)) { /* Perform tests. */ }
Add Comment
Please, Sign In to add comment