Пример #1
0
    public void Should_return_default_if_struct_is_default()
    {
        StructIntegerEnum item = default;
        int key = item;

        key.Should().Be(0);
    }
    public void Should_return_true_for_invalid_structs_on_equality()
    {
        // ReSharper disable once EqualExpressionComparison
        (StructIntegerEnum.Get(42) == StructIntegerEnum.Get(42)).Should().BeTrue();

        // ReSharper disable once EqualExpressionComparison
        (new StructIntegerEnumWithZero() == new StructIntegerEnumWithZero()).Should().BeTrue();
    }
    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.");
    }
 public void Should_return_false_if_struct_dont_have_any_items()
 {
     StructIntegerEnum.TryGet(42, out var item).Should().BeFalse();
     item.Should().Be(new StructIntegerEnum());
 }
 public void Should_return_false_for_invalid_structs_on_inequality()
 {
     // ReSharper disable once EqualExpressionComparison
     (StructIntegerEnum.Get(42) == StructIntegerEnum.Get(43)).Should().BeFalse();
 }