Skip to the content.

Objects API

up

Here are the examples of how you can use the should template with objects.

Summary

Examples

Be null

Success expectations

    Object o = null;

    o.should.beNull;
    (new Object).should.not.beNull;

    /// or using the Assert utility
    Assert.beNull(o);
    Assert.notNull(new Object);

Failing expectations

    Object o = null;

    o.should.not.beNull;
    (new Object).should.beNull;

Instance of

  class BaseClass { }
  class ExtendedClass : BaseClass { }
  class SomeClass { }
  class OtherClass { }

  auto someObject = new SomeClass;
  auto otherObject = new OtherClass;
  auto extendedObject = new ExtendedClass;

Success expectations

  someObject.should.be.instanceOf!SomeClass;
  extendedObject.should.be.instanceOf!BaseClass;

  someObject.should.not.be.instanceOf!OtherClass;
  someObject.should.not.be.instanceOf!BaseClass;

Failing expectations

  otherObject.should.be.instanceOf!SomeClass;
  otherObject.should.not.be.instanceOf!OtherClass;

Equal

  class TestEqual {
    private int value;

    this(int value) {
      this.value = value;
    }
  }

  auto instance = new TestEqual(1);

Success expectations

  instance.should.equal(instance);
  instance.should.not.equal(new TestEqual(1));

Failing expectations

  instance.should.not.equal(instance);
  instance.should.equal(new TestEqual(1));