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
220
230
2428
250
2628
2728
2828
290
3028
3128
3228
330
340
3528
3628
3728
380
390
400
410
420
430
4428
4528
460
4728
480
4928
5015
510
520
5328
5420
550
560
578
588
598
600
618
624
630
644
650
660
678
688
690
700
7116
720
738
740
750
760
770
7833
7933
800
8133
8233
8333
840
850
86116
87112
8833
890
900
910
920
9333
940
9533
9633
9733
980
9933
1000
10133
1020
10333
104279
105157
1060
1070
1080
10933
11033
1110
11233
1130
1140
1150
1160
11763
118112
1190
1200
12133
12213
1234
1240
1250
1268
1270
1280
1290
13020
1313
1320
1330
1340
1350
13633
1370
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; } static immutable approximatelyDescription = "Asserts that the target is a number that's within a given +/- `delta` range of the given number expected. However, it's often best to assert that the target is equal to its expected value."; /// 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; }