Ejemplo n.º 1
0
    public void Should_return_derived_type()
    {
        EnumWithDerivedType.Get(2).Should().Be(EnumWithDerivedType.ItemOfDerivedType);

        AbstractEnum.Get(1).Should().Be(AbstractEnum.Item);

        ExtensibleTestEnum.Get("DerivedItem").Should().Be(ExtensibleTestEnum.DerivedItem);
        ExtendedTestEnum.Get("DerivedItem").Should().Be(ExtendedTestEnum.DerivedItem);
        DifferentAssemblyExtendedTestEnum.Get("DerivedItem").Should().Be(DifferentAssemblyExtendedTestEnum.DerivedItem);
    }
    public void Should_throw_if_item_is_invalid()
    {
        TestEnum.Get("invalid").Invoking(e => e.EnsureValid())
        .Should().Throw <InvalidOperationException>().WithMessage($"The current enumeration item of type \"{nameof(TestEnum)}\" with identifier \"invalid\" is not valid.");

        StructStringEnum.Get("invalid").Invoking(e => e.EnsureValid())
        .Should().Throw <InvalidOperationException>().WithMessage($"The current enumeration item of type \"{nameof(StructStringEnum)}\" with identifier \"invalid\" is not valid.");

        AbstractEnum.Get(42).Invoking(e => e.EnsureValid())
        .Should().Throw <InvalidOperationException>().WithMessage($"The current enumeration item of type \"{nameof(AbstractEnum)}\" with identifier \"42\" is not valid.");

        StructIntegerEnum.Get(42).Invoking(e => e.EnsureValid())
        .Should().Throw <InvalidOperationException>().WithMessage($"The current enumeration item of type \"{nameof(StructIntegerEnum)}\" with identifier \"42\" is not valid.");

        // we cannot prevent construction of a struct
        new StructIntegerEnumWithZero().Invoking(e => e.EnsureValid())
        .Should().Throw <InvalidOperationException>().WithMessage($"The current enumeration item of type \"{nameof(StructIntegerEnumWithZero)}\" with identifier \"0\" is not valid.");

        ExtensibleTestValidatableEnum.Get("invalid").Invoking(e => e.EnsureValid())
        .Should().Throw <InvalidOperationException>().WithMessage($"The current enumeration item of type \"{nameof(ExtensibleTestValidatableEnum)}\" with identifier \"invalid\" is not valid.");

        ExtendedTestValidatableEnum.Get("invalid").Invoking(e => e.EnsureValid())
        .Should().Throw <InvalidOperationException>().WithMessage($"The current enumeration item of type \"{nameof(ExtendedTestValidatableEnum)}\" with identifier \"invalid\" is not valid.");
    }