Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zardoz89 committed May 24, 2020
2 parents f39881a + f501054 commit a87c03d
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v0.3.2

* .equals and others, must be @trusted to allow to call @system opEquals
* .approxEquals to do approximated equality of float types

# v0.3.1

* Make Pijamas @safe
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ assertion.
255.should.equal(10); // Throws an Exception "expected 255 to equal 10"
```

#### `T approxEqual(U)(U other, U maxRelDiff = 1e-2, U maxAbsDiff = 1e-05, string file = __FILE__, size_t line = __LINE__);`

Asserts for aproximated equality of float types. Returns the value wrapped around the
assertion.
```d
(1.0f).should.be.approxEqual(1.00000001);
(1.0f).should.not.be.approxEqual(1.001);
```

#### `T exist(string file = __FILE__, size_t line = __LINE__);`

Asserts whether a value exists - currently simply compares it with `null`, if it
Expand Down
66 changes: 59 additions & 7 deletions docs/pijamas.html
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ <h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.equal"></span>@safe T <code class="code">equal</code>(U)(U <code class="code">other</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);
<span class="ddoc_anchor" id="Assertion.equal"></span>@trusted T <code class="code">equal</code>(U)(U <code class="code">other</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);

</code>
</p>
Expand Down Expand Up @@ -743,6 +743,58 @@ <h4>Examples</h4>

</div>

</li><li class="ddoc_member">
<div class="ddoc_member_header">
<div class="ddoc_header_anchor">
<a href="#Assertion.approxEqual" id="Assertion.approxEqual"><code class="code">approxEqual</code></a>
</div>
</div><div class="ddoc_decl">
<section class="section">
<div class="declaration">
<h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.approxEqual"></span>@trusted T <code class="code">approxEqual</code>(U = double)(U <code class="code">other</code>, U <code class="code">maxRelDiff</code> = 0.01, U <code class="code">maxAbsDiff</code> = 1e-05, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__) if (is(T : real) &amp;&amp; __traits(isFloating, T) &amp;&amp; is(U : real) &amp;&amp; __traits(isFloating, U));

</code>
</p>
</div>
</div>
</section>
</div>
<div class="ddoc_decl">
<section class="section ddoc_sections">
<div class="ddoc_summary">
<p class="para">
Asserts that a float type is aproximated equal. Returns the valued wrapped around the assertion

</p>
</div>
<div class="ddoc_examples">
<h4>Examples</h4>
<p class="para">

<section class="code_listing">
<div class="code_sample">
<div class="dlang">
<ol class="code_lines">
<li><code class="code"><span class="keyword">double</span> d = 0.1;
<span class="keyword">double</span> d2 = d + 1e-05;
d.should.not.be.equal(d2);
d.should.be.<span class="psymbol">approxEqual</span>(d2);
</code></li>
</ol>
</div>
</div>
</section>

</p>
</div>
</section>

</div>

</li><li class="ddoc_member">
<div class="ddoc_member_header">
<div class="ddoc_header_anchor">
Expand Down Expand Up @@ -810,7 +862,7 @@ <h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.biggerThan"></span>@safe bool <code class="code">biggerThan</code>(U)(U <code class="code">other</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);
<span class="ddoc_anchor" id="Assertion.biggerThan"></span>@trusted bool <code class="code">biggerThan</code>(U)(U <code class="code">other</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);

</code>
</p>
Expand Down Expand Up @@ -860,7 +912,7 @@ <h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.smallerThan"></span>@safe bool <code class="code">smallerThan</code>(U)(U <code class="code">other</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);
<span class="ddoc_anchor" id="Assertion.smallerThan"></span>@trusted bool <code class="code">smallerThan</code>(U)(U <code class="code">other</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);

</code>
</p>
Expand Down Expand Up @@ -910,7 +962,7 @@ <h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.sorted"></span>@safe bool <code class="code">sorted</code>(string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);
<span class="ddoc_anchor" id="Assertion.sorted"></span>@trusted bool <code class="code">sorted</code>(string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);

</code>
</p>
Expand Down Expand Up @@ -960,7 +1012,7 @@ <h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.key"></span>@safe void <code class="code">key</code>(U)(U <code class="code">other</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);
<span class="ddoc_anchor" id="Assertion.key"></span>@trusted void <code class="code">key</code>(U)(U <code class="code">other</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);

</code>
</p>
Expand Down Expand Up @@ -1064,7 +1116,7 @@ <h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.length"></span>@safe U <code class="code">length</code>(U)(U <code class="code">len</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);
<span class="ddoc_anchor" id="Assertion.length"></span>@trusted U <code class="code">length</code>(U)(U <code class="code">len</code>, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);

</code>
</p>
Expand Down Expand Up @@ -1115,7 +1167,7 @@ <h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.empty"></span>@safe bool <code class="code">empty</code>(string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);
<span class="ddoc_anchor" id="Assertion.empty"></span>@trusted bool <code class="code">empty</code>(string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);

</code>
</p>
Expand Down
35 changes: 28 additions & 7 deletions source/pijamas.d
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,33 @@ class Assertion(T)
* 255.should.equal(10); // Throws an Exception "expected 255 to equal 10"
* ```
*/
T equal(U)(U other, string file = __FILE__, size_t line = __LINE__) @safe
T equal(U)(U other, string file = __FILE__, size_t line = __LINE__) @trusted
{
this.ok(context == other, this.message(other), file, line);
return context;
}

/**
* Asserts that a float type is aproximated equal. Returns the valued wrapped around the assertion
*
* Examples:
* ```
* double d = 0.1;
* double d2 = d + 1e-05;
* d.should.not.be.equal(d2);
* d.should.be.approxEqual(d2);
* ```
*/
T approxEqual(U = double)(U other, U maxRelDiff = 1e-2, U maxAbsDiff = 1e-05,
string file = __FILE__, size_t line = __LINE__) @trusted
if (is(T : real) && __traits(isFloating, T) && is(U : real) && __traits(isFloating, U))
{
import std.math : approxEqual;
operator = "be approximated equal than";
this.ok(approxEqual(context, other, maxRelDiff, maxAbsDiff), this.message(other), file, line);
return context;
}

/**
* Asserts whether a value exists - currently simply compares it with null, if it is a pointer, a class or a string.
* Returns the value wrapped around the assertion.
Expand Down Expand Up @@ -157,7 +178,7 @@ class Assertion(T)
* 10.should.be.biggerThan(1);
* ```
*/
bool biggerThan(U)(U other, string file = __FILE__, size_t line = __LINE__) @safe
bool biggerThan(U)(U other, string file = __FILE__, size_t line = __LINE__) @trusted
{
operator = "be bigger than";
return this.ok(context > other, this.message(other), file, line);
Expand All @@ -172,7 +193,7 @@ class Assertion(T)
* false.should.be.smallerThan(true);
* ```
*/
bool smallerThan(U)(U other, string file = __FILE__, size_t line = __LINE__) @safe
bool smallerThan(U)(U other, string file = __FILE__, size_t line = __LINE__) @trusted
{
operator = "be smaller than";
return this.ok(context < other, this.message(other), file, line);
Expand All @@ -189,7 +210,7 @@ class Assertion(T)
* [1, 2, 0, 4].should.not.be.sorted;
* ```
*/
bool sorted(string file = __FILE__, size_t line = __LINE__) @safe
bool sorted(string file = __FILE__, size_t line = __LINE__) @trusted
{
operator = "be sorted";
return this.ok(context.isSorted, this.message, file, line);
Expand All @@ -206,7 +227,7 @@ class Assertion(T)
* ["something": 10].should.have.key("something");
* ```
*/
void key(U)(U other, string file = __FILE__, size_t line = __LINE__) @safe
void key(U)(U other, string file = __FILE__, size_t line = __LINE__) @trusted
{
operator = "have key";
this.ok(!(other !in context), this.message(other), file, line);
Expand Down Expand Up @@ -256,7 +277,7 @@ class Assertion(T)
* // ^^ - Throws an Exception "expected 'abcdefg' to have length of 0"
* ```
*/
U length(U)(U len, string file = __FILE__, size_t line = __LINE__) @safe
U length(U)(U len, string file = __FILE__, size_t line = __LINE__) @trusted
{
operator = "have length of";
this.ok(context.length == len, this.message(len), file, line);
Expand All @@ -272,7 +293,7 @@ class Assertion(T)
* "".should.be.empty;
* ```
*/
bool empty(string file = __FILE__, size_t line = __LINE__) @safe
bool empty(string file = __FILE__, size_t line = __LINE__) @trusted
{
operator = "is empty";
return this.ok(context.length == 0, this.message(), file, line);
Expand Down
81 changes: 76 additions & 5 deletions tests/pijamas_spec.d
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,87 @@ import pijamas;

// it("works for structs",
{
struct Example
struct ExampleS
{
bool a = false;
string f = "something";
}

auto e = Example(true, "here");
e.should.be.equal(Example(true, "here"));
e.should.be.not.equal(Example(true, "asdf"));
assertThrown!Exception(e.should.be.equal(Example(true, "asdf")));
auto e = ExampleS(true, "here");
e.should.be.equal(ExampleS(true, "here"));
e.should.be.not.equal(ExampleS(true, "asdf"));
assertThrown!Exception(e.should.be.equal(ExampleS(true, "asdf")));
}

// it("works for classes",
{
class ExampleC
{
int x;
this (int x)
{
this.x = x;
}

override bool opEquals(Object o) const// @trusted
{
if (ExampleC rhs = cast(ExampleC)o) {
return this.x == rhs.x;
}
return false;
}
}

auto e = new ExampleC(33);
e.should.be.equal(new ExampleC(33));
e.should.be.not.equal(new ExampleC(1));
assertThrown!Exception(e.should.be.equal(new ExampleC(1)));

}
}

@("Should Assertion.approxEqual")
@safe unittest
{
// it("asserts that the identical value are identical")
{
float f = 0.01;
f.should.be.approxEqual(f);

double d = 0.01;
d.should.be.approxEqual(d);

real r = 0.01;
r.should.be.approxEqual(r);
}

// it("handles comparing diferent float types")
{
float f = 0.01;
double d = 0.01;
real r = 0.01;
f.should.be.approxEqual(d);
f.should.be.approxEqual(r);

d.should.be.approxEqual(f);
d.should.be.approxEqual(r);

r.should.be.approxEqual(f);
r.should.be.approxEqual(d);
}

// it("asserts that two nearly identical float values are approximated equal")
{
double d = 0.1;
double d2 = d + 1e-05;
d.should.not.be.equal(d2);
d.should.be.approxEqual(d2);

// and("when increase the difference, it must not be approximated equals")
d2 += 1e-2;
d.should.not.be.equal(d2);
d.should.not.be.approxEqual(d2);
assertThrown!Exception(d.should.be.approxEqual(d2));
}
}

Expand Down

0 comments on commit a87c03d

Please sign in to comment.