示例#1
0
    public object Create(object request, ISpecimenContext context)
    {
        var speciment = _enumGenerator.Create(request, context);

        if (speciment.ToString() == "Unknown" ||
            speciment.ToString() == "Uninitialized")
        {
            speciment = _enumGenerator.Create(request, context);
        }
        return(speciment);
    }
示例#2
0
        public void SutCanCorrectlyInterleaveDifferentEnumTypes(Type enumType, object expectedResult)
        {
            // Arrange
            var sut          = new EnumGenerator();
            var dummyContext = new DelegatingSpecimenContext();

            sut.Create(typeof(TriState), dummyContext);
            sut.Create(typeof(TriState), dummyContext);
            sut.Create(typeof(DayOfWeek), dummyContext);
            // Act
            var result = sut.Create(enumType, dummyContext);

            // Assert
            Assert.Equal(expectedResult, result);
        }
示例#3
0
        public void SutCanCorrectlyInterleaveDifferentEnumTypes(Type enumType, object expectedResult)
        {
            // Fixture setup
            var sut          = new EnumGenerator();
            var dummyContext = new DelegatingSpecimenContext();

            sut.Create(typeof(TriState), dummyContext);
            sut.Create(typeof(TriState), dummyContext);
            sut.Create(typeof(DayOfWeek), dummyContext);
            // Exercise system
            var result = sut.Create(enumType, dummyContext);

            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }
    public object Create(object request, ISpecimenContext context)
    {
        var enumType = request as Type;

        if (enumType == null || !enumType.IsEnum)
        {
            return(new NoSpecimen());
        }
        var namesEnumerator = Enum.GetNames(enumType).GetEnumerator();

        while (namesEnumerator.MoveNext())
        {
            var specimen = _enumGenerator.Create(request, context);
            if (specimen.ToString() != "Unknown" &&
                specimen.ToString() != "Uninitialized")
            {
                return(specimen);
            }
        }
        throw new ObjectCreationException(
                  "AutoFixture was unable to create a value for " +
                  enumType.FullName +
                  " since it is an enum containing either no values or " +
                  "ignored values only ('Unknown' and 'Uninitialized'). " +
                  "Please add at least one valid value to the enum.");
    }
示例#5
0
 public void Enum_WhenCreatingEnumWithAttribute_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("[Test]enumMyEnum{MyValue,MyOtherValue}", EnumGenerator.Create("MyEnum", new List <EnumMember> {
         new EnumMember("MyValue"), new EnumMember("MyOtherValue")
     }, attributes: new List <Attribute> {
         new Attribute("Test")
     }).ToString());
 }
示例#6
0
 public void Enum_WhenCreatingEnumWithModifiers_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("publicenumMyEnum{MyValue,MyOtherValue}", EnumGenerator.Create("MyEnum", new List <EnumMember> {
         new EnumMember("MyValue"), new EnumMember("MyOtherValue")
     }, modifiers: new List <Modifiers>()
     {
         Modifiers.Public
     }).ToString());
 }
示例#7
0
        public void RequestForEnumWithNoValuesThrowsObjectCreationException()
        {
            // Arrange
            var sut = new EnumGenerator();
            // Act & assert
            var dummyContext = new DelegatingSpecimenContext();

            Assert.Throws <ObjectCreationException>(() => sut.Create(typeof(EmptyEnum), dummyContext));
        }
示例#8
0
        public void RequestForEnumWithNoValuesThrowsObjectCreationException()
        {
            // Fixture setup
            var sut = new EnumGenerator();
            // Exercise system and Verify outcome
            var dummyContext = new DelegatingSpecimenContext();

            Assert.Throws <ObjectCreationException>(() => sut.Create(typeof(EmptyEnum), dummyContext));
            // Teardown
        }
示例#9
0
        public void RequestForFlagEnumTypeReturnsCorrectResult(Type enumType, int requestCount, object expectedResult)
        {
            // Arrange
            var sut = new EnumGenerator();
            // Act
            var dummyContext = new DelegatingSpecimenContext();
            var result       = Enumerable.Repeat <Func <object> >(() => sut.Create(enumType, dummyContext), requestCount).Select(f => f()).ToArray().Last();

            // Assert
            Assert.Equal(expectedResult, result);
        }
示例#10
0
        public void RequestForEnumTypeReturnsCorrectResult(Type enumType, int requestCount, object expectedResult)
        {
            // Fixture setup
            var sut = new EnumGenerator();
            // Exercise system
            var dummyContext = new DelegatingSpecimenContext();
            var result       = Enumerable.Repeat <Func <object> >(() => sut.Create(enumType, dummyContext), requestCount).Select(f => f()).Last();

            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }
示例#11
0
        public void RequestNonEnumReturnsCorrectResult(object request)
        {
            // Arrange
            var sut = new EnumGenerator();
            // Act
            var dummyContext = new DelegatingSpecimenContext();
            var result       = sut.Create(request, dummyContext);
            // Assert
            var expectedResult = new NoSpecimen();

            Assert.Equal(expectedResult, result);
        }
示例#12
0
 public void Enum_WhenCreatingEnumWithValueOnMember_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("[Test]publicenumMyEnum{MyValue=2,MyOtherValue}",
                     EnumGenerator.Create("MyEnum", new List <EnumMember> {
         new EnumMember("MyValue", 2), new EnumMember("MyOtherValue")
     }, new List <Modifiers>()
     {
         Modifiers.Public
     },
                                          new List <Attribute> {
         new Attribute("Test")
     }).ToString());
 }
示例#13
0
        public void RequestNonEnumReturnsCorrectResult(object request)
        {
            // Fixture setup
            var sut = new EnumGenerator();
            // Exercise system
            var dummyContext = new DelegatingSpecimenContext();
            var result       = sut.Create(request, dummyContext);
            // Verify outcome
            var expectedResult = new NoSpecimen(request);

            Assert.Equal(expectedResult, result);
            // Teardown
        }
示例#14
0
 public void Enum_WhenCreatingEnum_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("enumMyEnum{MyValue,MyOtherValue}", EnumGenerator.Create("MyEnum", new List <EnumMember> {
         new EnumMember("MyValue"), new EnumMember("MyOtherValue")
     }).ToString());
 }
示例#15
0
 public SyntaxList <MemberDeclarationSyntax> AddMember(SyntaxList <MemberDeclarationSyntax> members)
 {
     return(members.Add(EnumGenerator.Create(_name, _members, _modifiers, _attributes)));
 }