pijamas.assertion

Pijamas, a BDD assertion library for D.

License

Licensed under the MIT license. See LICENSE for more information.

  • Declaration

    Assertion!T should(T)(auto ref T context);

    The function should it's an helper or syntax sugar to create the assertation. Because of D’s lookup shortcut syntax, one is able to use both should(obj) and obj.should to get an object wrapped around an Assertion instance

  • Declaration

    alias expect = should(T)(auto ref T context);

    The function expect it's an helper or syntax sugar to create the assertation. Because of D’s lookup shortcut syntax, one is able to use both expect(obj) and obj.expect to get an object wrapped around an Assertion instance

  • Declaration

    struct Assertion(T);

    Class returned by should, that it's used to generate the fluent API

    • Declaration

      pure nothrow @safe this(T _context);
      pure nothrow @safe this(ref T _context);

      Creates a instance of Assertion, wrapping a value or object

    • be

      Declaration

      alias be = id;
      alias to = id;
      alias as = id;
      alias of = id;
      alias a = id;
      alias and = id;
      alias have = id;
      alias which = id;
      pure nothrow @nogc @safe Assertion id();

      Identity function. Simply does nothing beyond making assertation more human friendly

    • not

      Declaration

      pure nothrow @nogc @safe Assertion not();

      This function negates the wrapper assertion. With it, one can express fluent assertions without much effort.

      Examples

      1. 10.should.not.equal(2); // OK 10.should.not.equal(10); // Throws an Exception "expected 10 not to be 10"

    • Declaration

      @trusted T equal(U)(auto ref U other, string file = __FILE__, size_t line = __LINE__);

      Asserts for equality between two objects. Returns the value wrapped around the assertion.

      Examples

      1. [1, 2, 3, 4].should.equal([1, 2, 3, 4]); 255.should.equal(10); // Throws an Exception "expected 255 to equal 10"

    • Declaration

      @trusted T approxEqual(U = double)(U other, U maxRelDiff = CommonDefaultFor!(T, U), U maxAbsDiff = 0.0, string file = __FILE__, size_t line = __LINE__) if (is(T : real) && __traits(isFloating, T) && is(U : real) && __traits(isFloating, U));

      Asserts that a float type is aproximated equal. Returns the valued wrapped around the assertion

      Parameters

      U other

      Value to compare to compare.

      U maxRelDiff

      Maximum allowable relative difference. Setting to 0.0 disables this check. Default depends on the type of other and the original valie: It is approximately half the number of decimal digits of precision of the smaller type.

      U maxAbsDiff

      Maximum absolute difference. This is mainly usefull for comparing values to zero. Setting to 0.0 disables this check. Defaults to 0.0.

      string file

      filename

      size_t line

      line number inside of file

      Examples

      1. double d = 0.1; double d2 = d + 1e-10; d.should.not.be.equal(d2); d.should.be.approxEqual(d2);

    • Declaration

      alias close = approxEqual;

      Alias to approxEqual

      Examples

      1. double d = 0.1; double d2 = d + 1e-10; d.should.not.be.close(d2); d.should.be.close(d2);

    • Declaration

      @safe T exist(string file = __FILE__, size_t line = __LINE__);

      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.

      Examples

      1. auto exists = "I exist!"; should(exists).exist; string doesntexist; doesntexist.should.exist; // Throws an Exception "expected null to exist" Object aObject; aClass.should.not.exists;

    • Declaration

      @trusted bool biggerThan(U)(U other, string file = __FILE__, size_t line = __LINE__);

      Asserts if a value is bigger than another value. Returns the result.

      Examples

      1. "z".should.be.biggerThan("a"); 10.should.be.biggerThan(1);

    • Declaration

      @trusted bool biggerOrEqualThan(U)(U other, string file = __FILE__, size_t line = __LINE__);

      Asserts if a value is bigger or equal than another value. Returns the result.

      Examples

      1. "z".should.be.biggerOrEqualThan("a"); 10.should.be.biggerOrEqualThan(10); 20.should.be.biggerOrEqualThan(10);

    • Declaration

      @trusted bool smallerThan(U)(U other, string file = __FILE__, size_t line = __LINE__);

      Asserts if a value is smaller than another value. Returns the result.

      Examples

      1. 10.should.be.smallerThan(100); false.should.be.smallerThan(true);

    • Declaration

      @trusted bool smallerOrEqualThan(U)(U other, string file = __FILE__, size_t line = __LINE__);

      Asserts if a value is smaller or euqal than another value. Returns the result.

      Examples

      1. 10.should.be.smallerOrEqualThan(100); 10.should.be.smallerOrEqualThan(10); false.should.be.smallerOrEqualThan(true);

    • Declaration

      @trusted bool sorted(string file = __FILE__, size_t line = __LINE__);

      Asserts whether a forward range is sorted.

      Examples

      1. [1, 2, 3, 4].should.be.sorted; [1, 2, 0, 4].should.not.be.sorted;

    • key

      Declaration

      @trusted void key(U)(U other, string file = __FILE__, size_t line = __LINE__);

      Asserts for an associative array to have a key equal to other.

      Examples

      1. ["something": 10].should.have.key("something");

    • Declaration

      @nogc @trusted U include(U)(U other, string file = __FILE__, size_t line = __LINE__) if (!isAssociativeArray!T);
      @trusted U include(U)(U other, string file = __FILE__, size_t line = __LINE__) if (isAssociativeArray!T);
      alias value = include;
      alias contain = include;

      Asserts for an input range wrapped around an Assertion to contain/include a value.

      Examples

      1. [1, 2, 3, 4].should.include(3); "something".should.not.include('o'); "something".should.include("th");

    • Declaration

      @trusted U length(U)(U len, string file = __FILE__, size_t line = __LINE__);

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

      Examples

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

    • Declaration

      @trusted bool empty(string file = __FILE__, size_t line = __LINE__);

      Asserts for the .length property or function value to be equal to 0.

      Examples

      1. [].should.be.empty; "".should.be.empty;

    • Declaration

      @safe auto match(RegEx)(RegEx re, string file = __FILE__, size_t line = __LINE__) if (isSomeString!T && isRegexFor!(RegEx, T));
      @safe auto match(U)(U re, string file = __FILE__, size_t line = __LINE__) if (isSomeString!T && isSomeString!U);

      Asserts for a string wrapped around the Assertion to match a regular expression.

      Examples

      1. "something weird".should.match(`[a-z]+`); "something weird".should.match(regex(`[a-z]+`)); "something 2 weird".should.not.match(ctRegex!(`^[a-z]+$`)); "1234numbers".should.match(`[0-9]+[a-z]+`); "1234numbers".should.not.match(`^[a-z]+`);

    • Declaration

      @safe bool True(string file = __FILE__, size_t line = __LINE__);

      Asserts for a boolean value to be equal to true.

      Examples

      1. true.should.be.True; (1 == 1).should.be.True;

    • Declaration

      @safe bool False(string file = __FILE__, size_t line = __LINE__);

      Asserts for a boolean value to be equal to true.

      Examples

      1. false.should.be.False; (1 != 1).should.be.False;

    • Declaration

      @trusted void Throw(T : Throwable = Exception)(string file = __FILE__, size_t line = __LINE__);

      Asserts whether a callable object wrapped around the assertion throws an exception of type T.

      Examples

      1. void throwing() { throw new Exception("I throw with 0!"); } should(&throwing).Throw!Exception; void notThrowing() { return; } should(&notThrowing).not.Throw;