Skip to content

Commit

Permalink
feat(TestMode): Add basic TestMode
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-R2 committed Jan 14, 2023
1 parent 1175b2e commit 23bf1c2
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 69 deletions.
19 changes: 19 additions & 0 deletions lib/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,32 @@ class Logger {
logMessage(Message.warning(title: 'Init Logger', log: false));
}

/// Enable testMode for Logger and reset testModeCounter to 0
static void test() {
Logger();
_testModeCounter = 0;
_testMode = true;
Message.warning(title: 'Enter Testmode');
}

static final Logger _logger = Logger._internal();

/// Message queue
static final Map<int, Message> messages = {};

static bool _activeLogging = true;

static bool _testMode = false;

static bool get testMode => _testMode;

static int _testModeCounter = 0;

static int get testModeCounter {
if (!testMode) return 0;
return _testModeCounter += 1;
}

final _controller = StreamController<Message>.broadcast();

late final _logIt = MessageTemplate(klasse: this);
Expand Down
14 changes: 12 additions & 2 deletions lib/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class Message {
/// Usually the DateTime from the creation of the message
final DateTime time;

int get timeMC => time.microsecondsSinceEpoch;

/// The higher the worse
final int level;

Expand All @@ -158,6 +160,8 @@ class Message {
/// Can provide stacktrace information when available
StackTrace? stackTrace;

late final int testModeCount = Logger.testModeCounter;

/// A marker used to stop coloring
static const _endColorMarker = '\x1B[0m';

Expand Down Expand Up @@ -226,7 +230,7 @@ class Message {
/// Removes all curly brackets from a given [text]
static String removeCurlyBrackets(String? text) {
if (text == null) return '';
return text.replaceAll('{', '').replaceAll('}', '');
return text.replaceAll('{', '').replaceAll( '','}');
}

/// Replaces the templates in a given [text]
Expand Down Expand Up @@ -289,7 +293,13 @@ class Message {
}) {
final str = StringBuffer();

if (time) str.write('${this.time}');
if (time) {
if (!Logger.testMode) {
str.write('${this.time}');
} else {
str.write('TestMode: $testModeCount');
}
}

if (type) {
if (str.isNotEmpty) str.write(' ');
Expand Down
151 changes: 151 additions & 0 deletions mutation_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="UTF-8"?>
<mutations version="1.0">
<!-- The rules element describes all mutations done during a mutation test -->
<!-- The following children are parsed: literal and regex -->
<!-- A literal element matches the literal text -->
<!-- A regex element mutates source code if the regular expression matches -->
<!-- Each of them must have at least one mutation child -->
<rules>
<!-- A literal element matches the literal text and replaces it with the list of mutations -->
<literal text="&amp;&amp;">
<mutation text="||"/>
</literal>
<literal text="||">
<mutation text="&amp;&amp;"/>
</literal>
<literal text="+">
<mutation text="-"/>
<mutation text="*"/>
</literal>
<literal text="-">
<mutation text="+"/>
<mutation text="*"/>
</literal>
<literal text="*">
<mutation text="+"/>
<mutation text="-"/>
</literal>
<literal text="/">
<mutation text="*"/>
<mutation text="+"/>
</literal>
<literal text="==">
<mutation text="!="/>
</literal>
<literal text="&lt;=">
<mutation text="=="/>
<mutation text="&lt;"/>
</literal>
<literal text="&gt;=">
<mutation text="=="/>
<mutation text="&gt;"/>
</literal>
<literal text="!=">
<mutation text="=="/>
</literal>
<!-- It is also possible to match a regular expression with capture groups. -->
<!-- If the optional attribute dotAll is set to true, then the . will also match newlines. -->
<!-- If not present, the default value for dotAll is false. -->
<!-- Here, we capture everything inside of the braces of "if ()" -->
<regex pattern="[\s]if[\s]*\((.*?)\)[\s]*{" dotAll="true">
<!-- You can access groups via $1. -->
<!-- If your string contains a $ followed by a number that should not be replaced, escape the dollar \$ -->
<!-- If your string contains a \$ followed by a number that should not be replaced, escape the slash \\$ -->
<!-- Tabs and newlines should also be escaped. -->
<mutation text=" if (!($1)) {"/>
</regex>
<!-- Matches long chains of && -->
<regex pattern="&amp;([^&amp;()]+?)&amp;" dotAll="true">
<mutation text="&amp;!($1)&amp;"/>
</regex>
<!-- Matches long chains of || -->
<regex pattern="\|([^|()]+?)\|" dotAll="true">
<mutation text="|!($1)|"/>
</regex>
<regex pattern="\(([^$(]*?)&amp;&amp;([^$()]*?)\)">
<mutation text="(!($1)&amp;&amp;$2)"/>
<mutation text="($1&amp;&amp;!($2))"/>
</regex>
<regex pattern="\(([^|(]*?)\|\|([^()|]*?)\)">
<mutation text="(!($1)||$2)"/>
<mutation text="($1||!($2))"/>
</regex>
<!-- Replace start of conditional block -->
<regex pattern="if\s*\(([^|&amp;\)]*?)([|&amp;][|&amp;])">
<mutation text="if (!($1)$2"/>
</regex>
<!-- Replace end of conditional block -->
<regex pattern="([|&amp;][|&amp;])([^|&amp;]*?)\)">
<mutation text="$1!($2))"/>
</regex>
<regex pattern="([|&amp;][|&amp;])[\s]*?\(" dotAll="true">
<mutation text="$1!("/>
</regex>
<!-- Replaces numbers with negative values -->
<regex pattern="([\s=\(])([1-9\.]+[0-9]+|0\.0*[1-9])">
<mutation text="$1-$2"/>
</regex>
<!-- switch function call arguments. Matches 2 args -->
<regex pattern="([\s][a-zA-z]*?[^\(;\s]*?)\s*\(([^,]*),([^,]*)\)\s*;">
<mutation text="$1($3,$2);"/>
</regex>
<!-- switch function call arguments. Matches 3 args -->
<regex pattern="([\s][a-zA-z]*?[^\(;\s{}]*?)\s*\(([^,\n\(]*),([^,\n\(]*),([^,\n\(]*)\)\s*;">
<mutation text="$1($3,$2,$4);"/>
<mutation text="$1($2,$4,$3);"/>
</regex>
<!-- switch function call arguments. Matches 4 args -->
<regex pattern="([\s][a-zA-z]*?[^\(;\s{}]*?)\s*\(([^,\n\(]*),([^,\n\(]*),([^,\n\(]*),([^,\n\(]*)\)\s*;">
<mutation text="$1($3,$2,$4,$5);"/>
<mutation text="$1($2,$4,$3,$5);"/>
<mutation text="$1($2,$3,$5,$4);"/>
</regex>
</rules>
<!-- This element creates a blacklist, allowing you to exclude parts from the mutations -->
<exclude>
<!-- excludes anything between two tokens -->
<!-- single line comments -->
<token begin="//" end="\n"/>
<!-- exclude dart exports and imports -->
<token begin="export &apos;" end="&apos;;"/>
<token begin="import &apos;" end="&apos;;"/>
<token begin="export &quot;" end="&quot;;"/>
<token begin="import &quot;" end="&quot;;"/>

<!-- exclude dart part and part of -->
<token begin="part &apos;" end="&apos;;"/>
<token begin="part of &apos;" end="&apos;;"/>
<token begin="part &quot;" end="&quot;;"/>
<token begin="part of &quot;" end="&quot;;"/>

<!-- excludes anything that matches a pattern -->
<!-- multi line comments -->
<regex pattern="/[*].*?[*]/" dotAll="true"/>

<!-- exclude increment and decrement operators. Produces mostly false positives. -->
<regex pattern="\+\+"/>
<regex pattern="--"/>

<!-- exclude loops to prevent infinte tests -->
<regex pattern="[\s]for[\s]*\(.*?\)[\s]*{" dotAll="true"/>
<regex pattern="[\s]while[\s]*\(.*?\)[\s]*{.*?}" dotAll="true"/>

<!-- lines can also be globally excluded -->
<!-- line index starts at 1 -->
<!-- lines begin="1" end="2"/-->
</exclude>

<!-- Configures the reporting thresholds as percentage of detected mutations -->
<!-- Attribute failure is required and must be a floating point number. -->
<!-- Note: There can only be one threshold element in all input files! -->
<!-- If no threshold element is found, these values will be used. -->
<threshold failure="80">
<!-- Provides reliability rating levels. Attributes are required. -->
<rating over="100" name="A"/>
<rating over="80" name="B"/>
<rating over="60" name="C"/>
<rating over="40" name="D"/>
<rating over="20" name="E"/>
<rating over="0" name="F"/>
</threshold>
</mutations>
Loading

0 comments on commit 23bf1c2

Please sign in to comment.