Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3.  
  4. public class Program
  5. {
  6. public static void Main()
  7. {
  8. TestRunner.Run(new Program());
  9. }
  10. public void test1(){
  11. Assert.AreEqual(1,2);
  12. }
  13. }
  14.  
  15.  
  16. public static class TestRunner{
  17. public static void Run(object target){
  18. var mi = target.GetType().GetMethods(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
  19. int passed=0,failed=0;
  20. foreach(var m in mi){
  21. var argCount = m.GetParameters().Length;
  22. var isVoid=m.ReturnType == typeof(void);
  23. if (argCount==0 && isVoid){
  24. Console.WriteLine("> "+m.Name);
  25. try{
  26. m.Invoke(target, null);
  27. Console.WriteLine("\tPassed");
  28. passed++;
  29. }catch(TargetInvocationException tex) {
  30. if (tex.InnerException is Failed){
  31. var f = tex.InnerException as Failed;
  32. Console.WriteLine("Failed "+f.Message);
  33. failed++;
  34. }else{throw;}
  35. }
  36. }
  37. }
  38. Console.WriteLine("Total: "+(passed+failed)+", Passed: "+passed+", Failed: "+failed);
  39. }
  40. }
  41. class Failed:Exception{
  42. public Failed(string message):base(message){}
  43. }
  44. public static class Assert{
  45. public static void AreEqual(object expected, object actual){
  46. if (expected.GetType() != actual.GetType()){
  47. throw new Failed("Types are not equal");
  48. }
  49. if (!expected.Equals(actual)){
  50. var q=(actual is string)?"\"":"";
  51. throw new Failed("Values are not equal,\n\texpected "+q+expected+q+"\n\tactual "+q+actual+q);
  52. }
  53. }
  54. public static void IsNull(object expected){
  55. if (expected!=null){
  56. throw new Failed("Expected null");
  57. }
  58. }
  59. public static void NotNull(object expected){
  60. if (expected==null){
  61. throw new Failed("Expected not null");
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement