Permalink
Browse files

Added configure --macro to specify which features are compiled in

  • Loading branch information...
misprintt committed Feb 6, 2012
1 parent 3530a5f commit bb92c4829db5c2bee4f693bfb487aeeb3e05ee05
@@ -5,6 +5,7 @@ import m.cover.coverage.client.PrintClient;
import m.cover.coverage.client.TraceClient;
import m.cover.coverage.CoverageException;
import m.cover.coverage.CoverageLogger;
+import m.cover.coverage.CoverageMacro;
import m.cover.coverage.CoverageReportClient;
import m.cover.coverage.data.AbstractBlock;
import m.cover.coverage.data.AbstractNode;
@@ -21,6 +22,7 @@ import m.cover.coverage.data.Package;
import m.cover.coverage.data.Statement;
import m.cover.coverage.DataTypes;
import m.cover.coverage.macro.CoverageBuildMacro;
+import m.cover.coverage.MCoverage;
import m.cover.coverage.munit.client.MCoverPrintClient;
import m.cover.coverage.util.Timer;
import m.cover.Exception;
@@ -31,12 +33,16 @@ import m.cover.logger.data.LogRecording;
import m.cover.logger.LogException;
import m.cover.logger.Logger;
import m.cover.logger.LoggerImpl;
+import m.cover.logger.LoggerMacro;
import m.cover.logger.macro.LoggerBuildMacro;
+import m.cover.logger.MLogger;
import m.cover.logger.Utils;
import m.cover.macro.BuildMacro;
+import m.cover.macro.BuildMacroParser;
+import m.cover.macro.IncludeMacro;
+import m.cover.macro.MacroUtil;
import m.cover.macro.PackageHelper;
import m.cover.MCover;
-import m.cover.MLogger;
@IgnoreCover
class AllClasses
View
@@ -4,35 +4,48 @@ package m.cover;
import haxe.macro.Expr;
import haxe.macro.Context;
import haxe.macro.Compiler;
-
-import m.cover.coverage.macro.CoverageBuildMacro;
+import m.cover.macro.BuildMacro;
+import m.cover.coverage.CoverageMacro;
+import m.cover.logger.LoggerMacro;
import m.cover.macro.PackageHelper;
+import m.cover.macro.IncludeMacro;
+
+
/**
-* Main Coverage class containing macro and runtime methods for creating,
-* logging and reporting code coverage.
+* MCover provides a collection of macro based tools for measuring code quality and behavior.
*
* MACRO USAGE:
*
-* Use --macro m.cover.MCover.include(['package.name'],['sourcePath'], ['ignored patterns'])
-* to specifiy which packages/src directories to cover
-*
-*
-* RUNTIME USAGE:
+* To configure which tools to include, use:
+* --macro m.cover.MCover.configure(...);
*
-* See detailed documentation below for the following:
-* getLogger();
+* To specify which class paths/packages to include, use:
+* --macro m.cover.MCover.include(['package.name'],['sourcePath'], ['ignored patterns'])
+*
*/
@:keep class MCover
{
- static public var RESOURCE_DATA:String = "MCoverData";
-
- static public var classPathHash:IntHash<String> = new IntHash();
- //--------------- MACROS --------------..
+ public static var includes:Array<IncludeMacro> = [];
+
+ /**
+ * Configures MCover features for compilation
+ * A
+ * From the command line/hxml add a macro reference:
+ * --macro m.cover.MCover.configure();
+ *
+ * @param coverage include code coverage macros
+ * @param logging include function logging macros
+ **/
+ public static function configure(?coverage:Bool=true, ?logging:Bool=false):Void
+ {
+ if(coverage) includes.push(new CoverageMacro());
+ if(logging) includes.push(new LoggerMacro());
+ }
/**
- * Includes classes/packages logging.
- * Adds @:build(macro.BuildMacro.build()) to included classes
+ * Includes classes/packages for MCover.
+ * Adds @:build(m.cover.macro.BuildMacro.build()) to included classes
*
* From the command line/hxml add a macro reference:
* --macro m.cover.MCover.include(['package.name'], ['src'], null)
@@ -41,22 +54,27 @@ import m.cover.macro.PackageHelper;
* @param classPaths - array of classpaths to search in (defaults to local scope only (''))
* @param exclusions - array of qualified class names to exclude (supports '*' wildcard patterns)
**/
- public static function include(?packages : Array<String>=null, ?classPaths : Array<String>=null, ?exclusions : Array<String>=null)
+ static function include(?packages : Array<String>=null, ?classPaths : Array<String>=null, ?exclusions : Array<String>=null)
{
- if(packages == null || packages.length == 0) packages = [""];
- var helper = new PackageHelper();
- helper.ignoreClassMeta = "@IgnoreCover";
- var classes = helper.include(classPaths, packages, exclusions);
+ var classes:Array<String> = [];
- for(cp in classPaths)
+ if(includes.length == 0)
+ {
+ var pos = haxe.macro.Context.makePosition({file:"m.cover.MCover", min:0, max:0});//haxe.macro.Context.currentPos()
+ haxe.macro.Context.error("MCover not configured. Please ensure to set --macro m.cover.MCover.configure() before calling --macro m.cover.MCover.include", pos);
+ }
+
+ for(item in includes)
{
- classPathHash.set(Lambda.count(classPathHash), cp);
+ item.initialize();
+ var a = item.getClasses(packages, classPaths, exclusions);
+ classes = appendClasses(a, classes);
}
for(cl in classes)
{
//trace(cl);
- Compiler.addMetadata("@:build(m.cover.coverage.macro.CoverageBuildMacro.build())", cl);
+ Compiler.addMetadata("@:build(m.cover.macro.BuildMacro.build())", cl);
//Compiler.keep(cl, null, true);
}
@@ -65,53 +83,28 @@ import m.cover.macro.PackageHelper;
Compiler.include(pack, true, exclusions, classPaths);
}
- haxe.macro.Context.onGenerate(CoverageBuildMacro.onGenerate);
- }
-
- public static function onGenerate(types:Array<haxe.macro.Type>):Void
- {
-
+ haxe.macro.Context.onGenerate(onGenerate);
}
-}
-#else
-
-#if neko
-import neko.vm.Deque;
-#end
-
-import m.cover.coverage.CoverageLogger;
-
-class MCover
-{
- static public var RESOURCE_DATA:String = "MCoverData";
- #if neko
- static public var mutex:neko.vm.Mutex;
- #end
-
- static public var logger(default, null):CoverageLogger;
-
-
- @IgnoreCover
- public static function getLogger():CoverageLogger
+ static function appendClasses(a1:Array<String>, a2:Array<String>):Array<String>
{
- #if neko
- if(mutex == null) mutex = new neko.vm.Mutex();
- mutex.acquire();
- #end
- if(logger == null)
+ for(a in a1)
{
- logger = new CoverageLoggerImpl();
+ a2.remove(a);
}
- #if neko mutex.release(); #end
- return logger;
+
+ return a2.concat(a1);
}
- @IgnoreCover
- function new()
- {
+ static function onGenerate(types:Array<haxe.macro.Type>):Void
+ {
+ for(item in includes)
+ {
+ item.onGenerate(types);
+ }
}
}
+
#end
View
@@ -1,86 +0,0 @@
-package m.cover;
-
-#if macro
-import haxe.macro.Expr;
-import haxe.macro.Context;
-import haxe.macro.Compiler;
-
-import m.cover.macro.PackageHelper;
-
-
-@:keep class MLogger
-{
- //--------------- MACROS --------------..
-
- /**
- * Includes classes/packages logging.
- * Adds @:build(macro.BuildMacro.build()) to included classes
- *
- * From the command line/hxml add a macro reference:
- * --macro MLog.include(['package.name'], ['src'], null)
- *
- * @param packages - array of packages to include (e.g. "com.example"). Defaults to all ([""])
- * @param classPaths - array of classpaths to search in (defaults to local scope only (''))
- * @param exclusions - array of qualified class names to exclude (supports '*' wildcard patterns)
- **/
- public static function include(?packages : Array<String>, ?classPaths : Array<String>, ?exclusions : Array<String> )
- {
- if(packages == null || packages.length == 0) packages = [""];
- var helper = new PackageHelper();
- var classes = helper.include(classPaths, packages, exclusions);
-
- for(cl in classes)
- {
- Compiler.addMetadata("@:build(m.cover.logger.macro.LoggerBuildMacro.build())", cl);
- //Compiler.keep(cl, null, true);
- }
-
- for(pack in packages)
- {
- Compiler.include(pack, true, exclusions, classPaths);
- }
-
- haxe.macro.Context.onGenerate(MLogger.onGenerate);
- }
-
- public static function onGenerate(types:Array<haxe.macro.Type>):Void
- {
-
- }
-}
-#else
-
-#if neko
-import neko.vm.Deque;
-#end
-import m.cover.logger.Logger;
-import m.cover.logger.LoggerImpl;
-
-class MLogger
-{
- #if neko
- static public var mutex:neko.vm.Mutex;
- #end
-
- static public var logger(default, null):Logger;
-
- public static function getLogger():Logger
- {
- #if neko
- if(mutex == null) mutex = new neko.vm.Mutex();
- mutex.acquire();
- #end
- if(logger == null)
- {
- logger = new LoggerImpl();
- }
- #if neko mutex.release(); #end
- return logger;
- }
-
- function new()
- {
-
- }
-}
-#end
@@ -75,7 +75,7 @@ interface CoverageLogger
class CoverageLoggerImpl implements CoverageLogger
{
#if neko
- static public var mutex:neko.vm.Mutex = new neko.vm.Mutex();
+ static public var mutex:neko.vm.Mutex;
#end
/**
@@ -189,7 +189,7 @@ class CoverageLoggerImpl implements CoverageLogger
public function initializeCoverage(?resourceName:String = null)
{
- if(resourceName == null) resourceName = MCover.RESOURCE_DATA;
+ if(resourceName == null) resourceName = MCoverage.RESOURCE_DATA;
var serializedData:String = haxe.Resource.getString(resourceName);
if(serializedData == null) throw new CoverageException("No generated coverage data found in haxe Resource '" + resourceName + "'");
try
@@ -209,7 +209,10 @@ class CoverageLoggerImpl implements CoverageLogger
@IgnoreCover
public function logStatement(id:Int)
{
- #if neko mutex.acquire(); #end
+ #if neko
+ if(mutex == null) mutex = new neko.vm.Mutex();
+ mutex.acquire();
+ #end
updateStatementHash(allStatementResultsById, id);
@@ -242,7 +245,10 @@ class CoverageLoggerImpl implements CoverageLogger
@IgnoreCover
public function logBranch(id:Int, value:Dynamic, ?compareValue:Dynamic=null):Dynamic
{
- #if neko mutex.acquire(); #end
+ #if neko
+ if(mutex == null) mutex = new neko.vm.Mutex();
+ mutex.acquire();
+ #end
var bool = false;
@@ -0,0 +1,56 @@
+package m.cover.coverage;
+
+#if macro
+import haxe.macro.Context;
+import m.cover.MCover;
+import m.cover.macro.PackageHelper;
+import m.cover.macro.BuildMacro;
+import m.cover.macro.IncludeMacro;
+import m.cover.coverage.data.Coverage;
+import m.cover.coverage.macro.CoverageBuildMacro;
+
+class CoverageMacro implements IncludeMacro
+{
+ static public var coverage = new Coverage();
+ static public var classPathHash:IntHash<String> = new IntHash();
+
+ public function new()
+ {
+
+ }
+
+ public function initialize()
+ {
+ BuildMacro.addParserClass(CoverageBuildMacro);
+ }
+
+ public function getClasses(?packages : Array<String>=null, ?classPaths : Array<String>=null, ?exclusions : Array<String>=null):Array<String>
+ {
+ if(packages == null || packages.length == 0) packages = [""];
+ var helper = new PackageHelper();
+ helper.ignoreClassMeta = "IgnoreCover";
+
+ var classes = helper.include(classPaths, packages, exclusions);
+
+ for(cp in classPaths)
+ {
+ classPathHash.set(Lambda.count(classPathHash), cp);
+ }
+
+
+ return classes;
+
+ }
+
+ /**
+ * Inserts reference to all identified code coverage blocks into a haxe.Resource file called 'MCover'.
+ * This resource is used by MCoverRunner to determine code coverage results
+ */
+ public function onGenerate(types:Array<haxe.macro.Type>):Void
+ {
+ var serializedData = haxe.Serializer.run(CoverageMacro.coverage);
+ Context.addResource(MCoverage.RESOURCE_DATA, haxe.io.Bytes.ofString(serializedData));
+ }
+}
+
+#end
Oops, something went wrong.

0 comments on commit bb92c48

Please sign in to comment.