Skip to content

Commit

Permalink
.empty #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Zardoz89 committed May 15, 2020
1 parent 363a63d commit 0a9c332
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v0.3.0

* Rename module to pijamas.
* .empty for arrays, associative arrays and strings.

# v0.2.2

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,22 @@ value.
#### `U length(U)(U length, string file = __FILE__, size_t line = __LINE__);`

Asserts for the `.length` property or function value to equal some value.

```d
[1, 2, 3, 4].should.have.length(4);
"abcdefg".should.have.length(0);
// ^^ - Throws an Exception "expected 'abcdefg' to have length of 0"
```

#### `bool empty(string file = __FILE__, size_t line = __LINE__);`

Asserts that the .lenght property or function value is equal to 0;

```d
[].should.be.empty;
"".should.be.empty;
```

#### `auto match(RegEx)(RegEx re, string file = __FILE__, size_t line = __LINE__);`

Asserts for a string wrapped around the Assertion to match a regular expression.
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ title: Changelog
# v0.3.0

* Rename module to pijamas.
* .empty for arrays, associative arrays and strings.

# v0.2.2

Expand Down
10 changes: 10 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,22 @@ value.
#### `U length(U)(U length, string file = __FILE__, size_t line = __LINE__);`

Asserts for the `.length` property or function value to equal some value.

```d
[1, 2, 3, 4].should.have.length(4);
"abcdefg".should.have.length(0);
// ^^ - Throws an Exception "expected 'abcdefg' to have length of 0"
```

#### `bool empty(string file = __FILE__, size_t line = __LINE__);`

Asserts that the .lenght property or function value is equal to 0;

```d
[].should.be.empty;
"".should.be.empty;
```

#### `auto match(RegEx)(RegEx re, string file = __FILE__, size_t line = __LINE__);`

Asserts for a string wrapped around the Assertion to match a regular expression.
Expand Down
50 changes: 50 additions & 0 deletions docs/pijamas.html
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,56 @@ <h4>Examples</h4>

</div>

</li><li class="ddoc_member">
<div class="ddoc_member_header">
<div class="ddoc_header_anchor">
<a href="#Assertion.empty" id="Assertion.empty"><code class="code">empty</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.empty"></span>bool <code class="code">empty</code>(string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__);

</code>
</p>
</div>
</div>
</section>
</div>
<div class="ddoc_decl">
<section class="section ddoc_sections">
<div class="ddoc_summary">
<p class="para">
Asserts for the .length property or function value to be equal to 0.

</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">[].should.be.<span class="psymbol">empty</span>;
<span class="string_literal">""</span>.should.be.<span class="psymbol">empty</span>;
</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
15 changes: 15 additions & 0 deletions source/pijamas.d
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,21 @@ class Assertion(T)
ok(context.length == len, message(len), file, line);
return len;
}

/**
* Asserts for the .length property or function value to be equal to 0.
*
* Examples:
* ```
* [].should.be.empty;
* "".should.be.empty;
* ```
*/
bool empty(string file = __FILE__, size_t line = __LINE__)
{
operator = "is empty";
return ok(context.length == 0, message(), file, line);
}
}

import std.regex : Regex, isRegexFor;
Expand Down
32 changes: 32 additions & 0 deletions tests/pijamas_spec.d
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,38 @@ unittest
}
}

@("Should Assertion.empty")
unittest
{
// it("asserts that a string is empty"
{
auto str = "1234567";
str.should.not.be.empty;
assertThrown!Exception(str.should.be.empty);
"".should.be.empty;
}

// it("asserts that an array is empty"
{
auto a = [1, 2, 3, 4, 5, 6];
a.should.not.be.empty;
assertThrown!Exception(a.should.be.empty);
int[] emptyArr;
emptyArr.should.be.empty;
}

// it("asserts that an associative array is empty",
{
string[string] aa = [
"something" : "here", "what" : "is", "this" : "stuff", "we're" : "doing"
];
aa.should.not.be.empty;
assertThrown!Exception(aa.should.be.empty);
int[string] emptyAa;
emptyAa.should.be.empty;
}
}

@("Should Assertion.Throw")
unittest
{
Expand Down

0 comments on commit 0a9c332

Please sign in to comment.