fluentasserts.core.operations.approximately 56/61(91%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
2228
230
2428
2528
2628
270
2828
2928
3028
310
320
3328
3428
3528
360
370
380
390
400
410
4228
4328
440
4528
460
4728
4815
490
500
5128
5220
530
540
558
568
578
580
598
604
610
624
630
640
658
668
670
680
6916
700
718
720
730
740
750
7633
7733
780
7933
8033
8133
820
830
84116
85112
8633
870
880
890
900
9133
920
9333
9433
9533
960
9733
980
9933
1000
10133
102279
103157
1040
1050
1060
10733
10833
1090
11033
1110
1120
1130
1140
11563
116112
1170
1180
11933
12013
1214
1220
1230
1248
1250
1260
1270
12820
1293
1300
1310
1320
1330
13433
1350
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 { IResult[] results = []; evaluation.message.addValue("±"); evaluation.message.addValue(evaluation.expectedValue.meta["1"]); evaluation.message.addText("."); real current; real expected; real delta; try { current = evaluation.currentValue.strValue.to!real; expected = evaluation.expectedValue.strValue.to!real; delta = evaluation.expectedValue.meta["1"].to!real; } catch(Exception e) { results ~= new MessageResult("Can't parse the provided arguments!"); return results; } string strExpected = evaluation.expectedValue.strValue ~ "±" ~ evaluation.expectedValue.meta["1"]; string strCurrent = evaluation.currentValue.strValue; auto result = isClose(current, expected, 0, delta); if(evaluation.isNegated) { result = !result; } if(result) { return []; } if(evaluation.currentValue.typeName != "bool") { evaluation.message.addText(" "); evaluation.message.addValue(strCurrent); if(evaluation.isNegated) { evaluation.message.addText(" is approximately "); } else { evaluation.message.addText(" is not approximately "); } evaluation.message.addValue(strExpected); evaluation.message.addText("."); } try results ~= new ExpectedActualResult((evaluation.isNegated ? "not " : "") ~ strExpected, strCurrent); catch(Exception) {} return results; } /// IResult[] approximatelyList(ref Evaluation evaluation) @trusted nothrow { evaluation.message.addValue("±" ~ evaluation.expectedValue.meta["1"]); evaluation.message.addText("."); double maxRelDiff; real[] testData; real[] expectedPieces; 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 && isClose(testData[i], expectedPieces[i], 0, maxRelDiff) && true; } } 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(", ") ~ "]"; } catch(Exception) {} if(!evaluation.isNegated) { if(!allEqual) { try results ~= new ExpectedActualResult(strExpected, evaluation.currentValue.strValue); catch(Exception) {} 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; }