fluentasserts.core.operations.approximately 35/39(89%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
2249
2349
240
2549
2649
2749
2849
290
3049
310
320
330
34148
35144
3649
370
380
390
400
4149
420
4349
4449
4549
460
4749
480
4949
500
5149
52375
53151
540
550
560
5749
5849
590
6049
610
620
630
640
6591
66144
670
6849
6927
7027
710
720
730
740
7549
7620
778
780
790
808
818
820
830
840
850
8629
877
880
890
900
910
9249
930
module fluentasserts.core.operations.approximately; import fluentasserts.core.results; import fluentasserts.core.evaluation; import fluentasserts.core.array; import fluentasserts.core.serializers; import fluentasserts.core.operations.contain; import fluentasserts.core.lifecycle; import std.algorithm; import std.array; import std.conv; import std.math; version(unittest) { import fluentasserts.core.expect; } /// IResult[] approximately(ref Evaluation evaluation) @trusted nothrow { Lifecycle.instance.addValue("±" ~ evaluation.expectedValue.meta["1"]); Lifecycle.instance.addText("."); double maxRelDiff; real[] testData; real[] expectedPieces; bool usingArrays; try usingArrays = evaluation.currentValue.typeName.canFind('['); catch(Exception) usingArrays = true; try { testData = evaluation.currentValue.strValue.parseList.cleanString.map!(a => a.to!real).array; expectedPieces = evaluation.expectedValue.strValue.parseList.cleanString.map!(a => a.to!real).array; maxRelDiff = evaluation.expectedValue.meta["1"].to!double; } catch(Exception e) { return [ new MessageResult("Can not perform the assert.") ]; } auto comparison = ListComparison!real(testData, expectedPieces, maxRelDiff); auto missing = comparison.missing; auto extra = comparison.extra; auto common = comparison.common; IResult[] results = []; bool allEqual = testData.length == expectedPieces.length; if(allEqual) { foreach(i; 0..testData.length) { allEqual = allEqual && approxEqual(testData[i], expectedPieces[i], maxRelDiff); } } string strExpected; string strMissing; if(maxRelDiff == 0) { strExpected = evaluation.expectedValue.strValue; try strMissing = missing.length == 0 ? "" : missing.to!string; catch(Exception) {} } else try { strMissing = missing.map!(a => a.to!string ~ "±" ~ maxRelDiff.to!string).join(", "); strExpected = expectedPieces.map!(a => a.to!string ~ "±" ~ maxRelDiff.to!string).join(", "); if(usingArrays) { strMissing = "[" ~ strMissing ~ "]"; strExpected = "[" ~ strExpected ~ "]"; } } catch(Exception) {} if(!evaluation.isNegated) { if(!allEqual) { try results ~= new ExpectedActualResult(strExpected, evaluation.currentValue.strValue); catch(Exception) {} if(usingArrays) { try results ~= new ExtraMissingResult(extra.length == 0 ? "" : extra.to!string, strMissing); catch(Exception) {} } } } else { if(allEqual) { try results ~= new ExpectedActualResult("not " ~ strExpected, evaluation.currentValue.strValue); catch(Exception) {} } } return results; }