Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1.  
  2. COMP 496
  3. Pie Language Compiler 0.2
  4. Jason Bell
  5. 3078931
  6. February 22, 2016
  7. */
  8.  
  9. import System
  10. import System.IO
  11. import System.CodeDom.Compiler
  12. import System.Reflection
  13.  
  14. namespace Pie:
  15.  
  16. // Provides the entry point of the application.
  17. type Program:
  18. // Entry point
  19. shared act Main(args):
  20. if args == null:
  21. throw NullPointerException()
  22. // If there are fewer than 3 args, something is incorrect in the command line.
  23. if args.Length < 3:
  24. System.Console.WriteLine(@"Arguments must be of the form <output> <test|notest> <source files>")
  25. return
  26.  
  27. // Check that the second argument is valid.
  28. if args[1] != "test" && args[1] != "notest":
  29. System.Console.WriteLine("Second argument must be notest or test")
  30. return
  31.  
  32. // Delete and recreate the sensor repository file if it exists.
  33. if File.Exists("AllSensors.txt"):
  34. File.Delete("AllSensors.txt")
  35. w = File.CreateText("AllSensors.txt")
  36. w.Close()
  37.  
  38. // Setup the compiler options: these are required by the C# Compiler.
  39. parameters = CompilerParameters()
  40. parameters.ReferencedAssemblies.Add(@"C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5.2\\System.Core.dll")
  41. parameters.ReferencedAssemblies.Add(@"C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5.2\\Microsoft.CSharp.dll")
  42. parameters.ReferencedAssemblies.Add(@"C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5.2\\System.dll")
  43. parameters.ReferencedAssemblies.Add(System.IO.Directory.GetCurrentDirectory() + @"\\"+ "Irony.dll")
  44. parameters.GenerateInMemory = false
  45. // Generate an executable if the requested file name ends in exe.
  46. if args[0].EndsWith("exe"):
  47. parameters.GenerateExecutable = true
  48. parameters.IncludeDebugInformation = false
  49. parameters.OutputAssembly = args[0]
  50.  
  51. // Load all source files from file.
  52. sources = list()
  53. for i = 2; i < args.Length; i++:
  54. if !File.Exists(args[i]):
  55. System.Console.WriteLine("File not found: " + args[i])
  56. return
  57. System.Console.WriteLine("Loading: " + args[i])
  58. sources.Add(File.ReadAllText(args[i]))
  59.  
  60. // Create the compiler, and have it track sensor activation if testing is enabled.
  61. compiler = PieCompiler()
  62. if args[1] == "test":
  63. PieCompiler.trackSensors = true
  64.  
  65. // Compile!
  66. results = compiler.CompileAssemblyFromSource(parameters, sources, false)
  67.  
  68. if results != null:
  69. // Output any error messages.
  70. for e in results.Errors:
  71. System.Console.WriteLine(e.Line + " " + e.FileName + ": " + e.ErrorText)
  72.  
  73. // If there's no errors and testing was requested, do test coverage analysis.
  74. if results.Errors.Count == 0 && PieCompiler.trackSensors:
  75. System.Console.WriteLine("Running tests")
  76. if File.Exists("PassedSensors.txt"):
  77. File.Delete("PassedSensors.txt")
  78. w = File.CreateText("PassedSensors.txt")
  79. w.Close()
  80.  
  81. // Get all types in the built assembly, and find all tests.
  82. failedTests = 0.0
  83. passedTests = 0.0
  84. types = results.CompiledAssembly.GetTypes()
  85. for t in types:
  86. methods = t.GetMethods()
  87. for m in methods:
  88. if m.IsStatic && m.Name.StartsWith("PieTestzkyz_"):
  89. name = m.Name.Substring(12)
  90. try: // If an exception is thrown during the test, it fails.
  91. m.Invoke(null, null)
  92. passedTests+=1
  93. catch e:
  94. System.Console.WriteLine("Test " + t.FullName + "." + name + " failed")
  95. System.Console.WriteLine(e.InnerException.Message)
  96. failedTests+=1
  97. // Display the number of tests that passed compared to the total number.
  98. System.Console.WriteLine(passedTests + "/" + (failedTests + passedTests) + " tests passed")
  99.  
  100. // Read all sensors and all activated sensors, to determine coverage.
  101. allSensorGuids = File.ReadAllLines("AllSensors.txt")
  102. allSensors = map()
  103. for s in allSensorGuids:
  104. allSensors.Add(s, false)
  105. allPassedGuids = File.ReadAllLines("PassedSensors.txt")
  106. for s in allPassedGuids:
  107. if allSensors.ContainsKey(s):
  108. allSensors[s] = true
  109. passedCount = 0.0
  110. for v in allSensors.Values:
  111. if v:
  112. passedCount+=1
  113. // Display the test coverage!
  114. System.Console.WriteLine("Test coverage: " + (passedCount / allSensors.Count) * 100.0)
  115.  
  116. System.Console.WriteLine("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement