├── src ├── SimpleFixture.snk ├── SimpleFixture │ ├── Attributes │ │ ├── LocateAttribute.cs │ │ ├── IParameterInfoAware.cs │ │ ├── GenerateAttribute.cs │ │ ├── IComplexAttribute.cs │ │ ├── IMethodInfoAware.cs │ │ ├── FreezeAttribute.cs │ │ ├── DataConfigurationAttribute.cs │ │ ├── FixtureCreationAttribute.cs │ │ ├── FixtureInitializationAttribute.cs │ │ ├── FixtureCustomizationAttribute.cs │ │ ├── ExportValueAttribute.cs │ │ └── ExportAttribute.cs │ ├── Impl │ │ ├── IConventionProvider.cs │ │ ├── FieldSetter.cs │ │ ├── PropertySetter.cs │ │ ├── ModelService.cs │ │ ├── CircularReferenceHandler.cs │ │ ├── TypeFieldSelector.cs │ │ ├── ConventionList.cs │ │ ├── ConstructorSelector.cs │ │ ├── TypedConventions.cs │ │ ├── ConventionProvider.cs │ │ └── TypePropertySelector.cs │ ├── IFixtureCustomization.cs │ ├── ITypedConvention.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── DI │ │ ├── IGContainer.cs │ │ └── GContainer.cs │ ├── Conventions │ │ ├── BoolConvention.cs │ │ ├── NullableConvention.cs │ │ ├── TypeConvention.cs │ │ ├── UriConvention.cs │ │ ├── SimpleTypeConvention.cs │ │ ├── IComparerConvention.cs │ │ ├── IEqualityComparerConvention.cs │ │ ├── ArrayConvention.cs │ │ ├── EnumConvention.cs │ │ ├── LongConvention.cs │ │ ├── ULongConvention.cs │ │ ├── CharConvention.cs │ │ ├── IntConvention.cs │ │ ├── UIntConvention.cs │ │ ├── SByteConvention.cs │ │ ├── ShortConvention.cs │ │ ├── ByteConvention.cs │ │ ├── UShortConvention.cs │ │ ├── DecimalConvention.cs │ │ ├── DoubleConvention.cs │ │ ├── TimeSpanConvention.cs │ │ ├── ReadOnlyCollectionConvention.cs │ │ ├── DateTimeConvention.cs │ │ ├── DictionaryConvention.cs │ │ ├── FilteredConvention.cs │ │ ├── DateTimeOffSetConvention.cs │ │ └── ComplexConvention.cs │ ├── SimpleFixture.csproj │ ├── IConvention.cs │ ├── ExportAs.cs │ ├── IFixtureConfiguration.cs │ └── FromConfiguration.cs ├── SimpleFixture.FakeItEasy │ ├── LanguageExtensions.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── FakeFixture.cs │ ├── SimpleFixture.FakeItEasy.csproj │ └── FakeConvention.cs ├── SimpleFixture.Moq │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── MoqFixtureInitializeAttribute.cs │ ├── LanguageExtensions.cs │ ├── MoqFixture.cs │ └── SimpleFixture.Moq.csproj ├── SimpleFixture.xUnit │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── AutoDataAttribute.cs │ └── SimpleFixture.xUnit.csproj └── SimpleFixture.NSubstitute │ ├── Properties │ └── AssemblyInfo.cs │ ├── LanguageExtensions.cs │ ├── SubFixtureInitializeAttribute.cs │ ├── SubFixture.cs │ ├── SimpleFixture.NSubstitute.csproj │ └── SubstituteConvention.cs ├── tests └── SimpleFixture.Tests │ ├── Classes │ ├── ISomeInterface.cs │ ├── ImportPropertyClass.cs │ ├── FieldClass.cs │ ├── SomeClass.cs │ ├── ImportSomeClass.cs │ ├── ImportEnumerableClass.cs │ ├── NestedNamespace │ │ └── NestedBulkExports.cs │ ├── DependentClass.cs │ ├── StringDependentClass.cs │ ├── ImportSomeInterface.cs │ ├── ExampleComplexAttribute.cs │ ├── ImplementInterfaceClass.cs │ ├── RangedClass.cs │ ├── IBulkExportInterface.cs │ ├── PropertiesClass.cs │ ├── BulkExportClasses.cs │ └── CircularClasses.cs │ ├── FixtureTests │ ├── Complex │ │ ├── ComplexConstraintTests.cs │ │ ├── FieldTests.cs │ │ ├── DictionaryTests.cs │ │ ├── ComparerConventionTests.cs │ │ ├── ReadOnlyCollectionTests.cs │ │ ├── DelegateFixtureTests.cs │ │ └── ListTests.cs │ ├── Primitives │ │ ├── TypeFixtureTests.cs │ │ ├── UriFixtureTests.cs │ │ ├── TimeSpanTests.cs │ │ ├── SimpleConventionTests.cs │ │ ├── EnumFixtureTests.cs │ │ ├── BoolFixtureTests.cs │ │ ├── StringFixtureTests.cs │ │ ├── ByteFixtureTests.cs │ │ ├── DoubleFixtureTests.cs │ │ ├── SByteFixtureTests.cs │ │ ├── ShortFixtureTests.cs │ │ └── LongFixtureTests.cs │ ├── PopulateTests.cs │ ├── ExportValueTests.cs │ ├── Conventions │ │ └── IEqualityComparerConventionTests.cs │ ├── RandomDataGeneratorServiceTests.cs │ ├── ConstraintTests.cs │ ├── FreezeTests.cs │ ├── BehaviorTests.cs │ ├── StandardFixtureTests.cs │ └── NamedConventions │ │ └── AddressStringConventionTests.cs │ ├── xUnitTests │ ├── ComplexAttributeTests.cs │ ├── FixtureCustomizationAttributeTests.cs │ └── AutoDataAttributeTests.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── CodeCoverageAppVeyor.cmd │ ├── SimpleFixture.Tests.csproj │ └── MockTests │ └── FakeTests.cs ├── appveyor.yml ├── SimpleFixture.sln.DotSettings.user ├── License.md └── .gitignore /src/SimpleFixture.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ipjohnson/SimpleFixture/HEAD/src/SimpleFixture.snk -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/ISomeInterface.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public interface ISomeInterface 4 | { 5 | int SomeIntMethod(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/ImportPropertyClass.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public class ImportPropertyClass 4 | { 5 | public SomeClass SomeClass { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/FieldClass.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public class FieldClass 4 | { 5 | public int IntField; 6 | 7 | public string FirstName; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/LocateAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.Attributes 4 | { 5 | public class LocateAttribute : Attribute 6 | { 7 | public virtual object Value { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/IParameterInfoAware.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace SimpleFixture.Attributes 4 | { 5 | public interface IParameterInfoAware 6 | { 7 | ParameterInfo Parameter { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/SomeClass.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public class SomeClass 4 | { 5 | public int IntValue { get; set; } 6 | 7 | public string StringValue { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/IConventionProvider.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace SimpleFixture.Impl 4 | { 5 | public interface IConventionProvider 6 | { 7 | IEnumerable ProvideConventions(IFixtureConfiguration configuration); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/GenerateAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.Attributes 4 | { 5 | public class GenerateAttribute : Attribute 6 | { 7 | public virtual object Min { get; set; } 8 | 9 | public virtual object Max { get; set; } 10 | 11 | public virtual string ConstraintName { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/IComplexAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace SimpleFixture.Attributes 6 | { 7 | /// 8 | /// Attributes that implement this will be scanned for other attributes 9 | /// 10 | public interface IComplexAttribute 11 | { 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/IMethodInfoAware.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace SimpleFixture.Attributes 4 | { 5 | /// 6 | /// Attributes that implement this interface are aware of being run in context of a method 7 | /// 8 | public interface IMethodInfoAware 9 | { 10 | MethodInfo Method { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/ImportSomeClass.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public class ImportSomeClass 4 | { 5 | private readonly SomeClass _someClass; 6 | 7 | public ImportSomeClass(SomeClass someClass) 8 | { 9 | _someClass = someClass; 10 | } 11 | 12 | public SomeClass SomeClass => _someClass; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/ImportEnumerableClass.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace SimpleFixture.Tests.Classes 4 | { 5 | public class ImportEnumerableClass 6 | { 7 | public ImportEnumerableClass(IEnumerable intValues) 8 | { 9 | IntValues = intValues; 10 | } 11 | 12 | public IEnumerable IntValues { get; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/NestedNamespace/NestedBulkExports.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes.NestedNamespace 2 | { 3 | public interface INestedBulkExportInterface 4 | { 5 | int Compute(); 6 | } 7 | 8 | public class NestedBulkExports : INestedBulkExportInterface 9 | { 10 | public int Compute() 11 | { 12 | return 10; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/DependentClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace SimpleFixture.Tests.Classes 7 | { 8 | public class DependentClass 9 | { 10 | public DependentClass(T value) 11 | { 12 | Value = value; 13 | } 14 | 15 | public T Value { get; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/StringDependentClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace SimpleFixture.Tests.Classes 6 | { 7 | public class StringDependentClass 8 | { 9 | public StringDependentClass(string someString) 10 | { 11 | SomeString = someString; 12 | } 13 | 14 | public string SomeString { get; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/ImportSomeInterface.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public class ImportSomeInterface 4 | { 5 | private readonly ISomeInterface _someInterface; 6 | 7 | public ImportSomeInterface(ISomeInterface someInterface) 8 | { 9 | _someInterface = someInterface; 10 | } 11 | 12 | public int SomeValue => _someInterface.SomeIntMethod(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/ExampleComplexAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using SimpleFixture.Attributes; 7 | 8 | namespace SimpleFixture.Tests.Classes 9 | { 10 | [ExportValue("testing1",1)] 11 | [ExportValue("testing2", 2)] 12 | public class ExampleComplexAttribute : Attribute, IComplexAttribute 13 | { 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/ImplementInterfaceClass.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public interface IImplementInterfaceClass 4 | { 5 | int SomeValue { get; } 6 | 7 | string SomeString { get; } 8 | } 9 | 10 | public class ImplementInterfaceClass : IImplementInterfaceClass 11 | { 12 | public int SomeValue { get; set; } 13 | 14 | public string SomeString { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/SimpleFixture/IFixtureCustomization.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture 2 | { 3 | /// 4 | /// This interface allows you to package a set of customization for reuse. 5 | /// 6 | public interface IFixtureCustomization 7 | { 8 | /// 9 | /// Customize the fixture 10 | /// 11 | /// fixture to customize 12 | void Customize(Fixture fixture); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/RangedClass.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace SimpleFixture.Tests.Classes 4 | { 5 | public class RangedClass 6 | { 7 | [Range(100, 200)] 8 | public int IntValue { get; set; } 9 | 10 | [StringLength(100, MinimumLength = 50)] 11 | public string TestString { get; set; } 12 | 13 | [StringLength(100)] 14 | public string FirstName { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/FreezeAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.Attributes 4 | { 5 | public class FreezeAttribute : Attribute 6 | { 7 | public virtual object Min { get; set; } 8 | 9 | public virtual object Max { get; set; } 10 | 11 | public virtual string ConstraintName { get; set; } 12 | 13 | public virtual Type For { get; set; } 14 | 15 | public virtual object Value { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/SimpleFixture/ITypedConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace SimpleFixture 5 | { 6 | /// 7 | /// Conventions implementing this interface support a specific set of types 8 | /// 9 | public interface ITypedConvention : IConvention 10 | { 11 | /// 12 | /// Types the convention supports 13 | /// 14 | IEnumerable SupportedTypes { get; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/FieldSetter.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace SimpleFixture.Impl 4 | { 5 | public interface IFieldSetter 6 | { 7 | void SetField(FieldInfo fieldInfo, object instance, object value); 8 | } 9 | 10 | public class FieldSetter : IFieldSetter 11 | { 12 | public void SetField(FieldInfo fieldInfo, object instance, object value) 13 | { 14 | fieldInfo.SetValue(instance, value); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/DataConfigurationAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace SimpleFixture.Attributes 5 | { 6 | public interface IFixtureConfigurationAttribute 7 | { 8 | IFixtureConfiguration ProvideConfiguration(MethodInfo method); 9 | } 10 | 11 | public abstract class DataConfigurationAttribute : Attribute, IFixtureConfigurationAttribute 12 | { 13 | public abstract IFixtureConfiguration ProvideConfiguration(MethodInfo method); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/PropertySetter.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace SimpleFixture.Impl 4 | { 5 | public interface IPropertySetter 6 | { 7 | void SetProperty(PropertyInfo propertyInfo, object instance, object value); 8 | } 9 | 10 | public class PropertySetter : IPropertySetter 11 | { 12 | public void SetProperty(PropertyInfo propertyInfo, object instance, object value) 13 | { 14 | if (propertyInfo.CanWrite) 15 | { 16 | propertyInfo.SetValue(instance, value); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Complex/ComplexConstraintTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Tests.Classes; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.Complex 5 | { 6 | public class ComplexConstraintTests 7 | { 8 | [Fact] 9 | public void Fixture_Generate_IntConstraintSeed() 10 | { 11 | var fixture = new Fixture(); 12 | 13 | var someClass = fixture.Generate(constraints: new { IntValue = 8 }); 14 | 15 | Assert.NotNull(someClass); 16 | Assert.Equal(8, someClass.IntValue); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/IBulkExportInterface.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public interface IBulkExportInterface 4 | { 5 | int Compute(); 6 | } 7 | 8 | 9 | public interface IBulkExportInterface2 10 | { 11 | int Compute(); 12 | } 13 | 14 | public interface IBulkExportInterface3 15 | { 16 | int Compute(); 17 | } 18 | 19 | public interface IGenericBulkInterface 20 | { 21 | T GetT(); 22 | } 23 | 24 | public interface IMultipleTypeGenericBulkInterface 25 | { 26 | T1 Get(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/PropertiesClass.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public class PropertiesClass 4 | { 5 | public int IntValue1 { get; set; } 6 | 7 | public int IntValue2 { get; set; } 8 | 9 | public string StringValue1 { get; set; } 10 | 11 | public string StringValue2 { get; set; } 12 | } 13 | 14 | public class PropertiesClass2 15 | { 16 | public int IntValue1 { get; set; } 17 | 18 | public int IntValue2 { get; set; } 19 | 20 | public string StringValue1 { get; set; } 21 | 22 | public string StringValue2 { get; set; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/xUnitTests/ComplexAttributeTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using SimpleFixture.Tests.Classes; 7 | using SimpleFixture.xUnit; 8 | using Xunit; 9 | 10 | namespace SimpleFixture.Tests.xUnitTests 11 | { 12 | public class ComplexAttributeTests 13 | { 14 | [Theory] 15 | [AutoData] 16 | [ExampleComplex] 17 | public void ComplexAttributeTest(int testing1, int testing2) 18 | { 19 | Assert.Equal(1, testing1); 20 | Assert.Equal(2, testing2); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/SimpleFixture.FakeItEasy/LanguageExtensions.cs: -------------------------------------------------------------------------------- 1 | using FakeItEasy; 2 | using System; 3 | 4 | namespace SimpleFixture.FakeItEasy 5 | { 6 | public static class LanguageExtensions 7 | { 8 | public static T Fake(this Fixture fixture, Action arrange = null, Action> options = null, bool? singleton = null) where T : class 9 | { 10 | T returnValue = fixture.Generate(constraints: new 11 | { 12 | fakeSingleton = singleton, 13 | builderOptions = options 14 | }); 15 | 16 | arrange?.Invoke(returnValue); 17 | 18 | return returnValue; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Complex/FieldTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using SimpleFixture.Tests.Classes; 3 | using System.Linq; 4 | using Xunit; 5 | 6 | namespace SimpleFixture.Tests.FixtureTests.Complex 7 | { 8 | public class FieldTests 9 | { 10 | [Fact] 11 | public void Fixture_PopulateFields_ReturnsValue() 12 | { 13 | var fixture = new Fixture(new DefaultFixtureConfiguration { PopulateFields = true }); 14 | 15 | var fieldClass = fixture.Generate(); 16 | 17 | fieldClass.IntField.Should().NotBe(0); 18 | fieldClass.FirstName.Should().Match(s => s.All(char.IsLetter)); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/SimpleFixture/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany("")] 9 | [assembly: AssemblyProduct("SimpleFixture")] 10 | [assembly: AssemblyTrademark("")] 11 | 12 | // Setting ComVisible to false makes the types in this assembly not visible 13 | // to COM components. If you need to access a type in this assembly from 14 | // COM, set the ComVisible attribute to true on that type. 15 | [assembly: ComVisible(false)] 16 | 17 | -------------------------------------------------------------------------------- /src/SimpleFixture.Moq/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany("")] 9 | [assembly: AssemblyProduct("SimpleFixture.Moq")] 10 | [assembly: AssemblyTrademark("")] 11 | 12 | // Setting ComVisible to false makes the types in this assembly not visible 13 | // to COM components. If you need to access a type in this assembly from 14 | // COM, set the ComVisible attribute to true on that type. 15 | [assembly: ComVisible(false)] 16 | 17 | -------------------------------------------------------------------------------- /src/SimpleFixture.xUnit/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany("")] 9 | [assembly: AssemblyProduct("SimpleFixture.xUnit")] 10 | [assembly: AssemblyTrademark("")] 11 | 12 | // Setting ComVisible to false makes the types in this assembly not visible 13 | // to COM components. If you need to access a type in this assembly from 14 | // COM, set the ComVisible attribute to true on that type. 15 | [assembly: ComVisible(false)] 16 | 17 | -------------------------------------------------------------------------------- /src/SimpleFixture.FakeItEasy/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany("")] 9 | [assembly: AssemblyProduct("SimpleFixture.FakeItEasy")] 10 | [assembly: AssemblyTrademark("")] 11 | 12 | // Setting ComVisible to false makes the types in this assembly not visible 13 | // to COM components. If you need to access a type in this assembly from 14 | // COM, set the ComVisible attribute to true on that type. 15 | [assembly: ComVisible(false)] 16 | 17 | -------------------------------------------------------------------------------- /src/SimpleFixture.NSubstitute/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany("")] 9 | [assembly: AssemblyProduct("SimpleFixture.NSubstitute")] 10 | [assembly: AssemblyTrademark("")] 11 | 12 | // Setting ComVisible to false makes the types in this assembly not visible 13 | // to COM components. If you need to access a type in this assembly from 14 | // COM, set the ComVisible attribute to true on that type. 15 | [assembly: ComVisible(false)] 16 | 17 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | build_version: 4.0.0 3 | Version: $(build_version)-RC%APPVEYOR_BUILD_NUMBER% 4 | COVERALLS_REPO_TOKEN: 5 | secure: y0rSDEqwr4qPrtlqBPN+nuJ7XqeO2fvwjx88kYMriIiXmS/m3cBYBWYQ99SsWeQH 6 | version: $(build_version)-{build} 7 | configuration: Release 8 | assembly_info: 9 | patch: true 10 | file: '**\AssemblyInfo.*' 11 | assembly_version: '$(build_version).0' 12 | assembly_file_version: '$(build_version).{build}' 13 | assembly_informational_version: '$(build_version)' 14 | before_build: 15 | - cmd: nuget restore SimpleFixture.sln 16 | build: 17 | project: SimpleFixture.sln 18 | publish_nuget: true 19 | verbosity: minimal 20 | artifacts: 21 | - path: SimpleFixture*.nupkg 22 | name: SimpleFixture 23 | image: 24 | - Visual Studio 2022 25 | -------------------------------------------------------------------------------- /src/SimpleFixture/DI/IGContainer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.DI 4 | { 5 | /// 6 | /// Simple dependency injection container interface 7 | /// 8 | public interface IGContainer 9 | { 10 | /// 11 | /// Export a particular type 12 | /// 13 | /// type being exported 14 | /// export function 15 | void Export(Func exportFunc); 16 | 17 | /// 18 | /// Locate instance of T 19 | /// 20 | /// type to locate 21 | /// instance of T 22 | T Locate(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/FixtureCreationAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.Attributes 4 | { 5 | /// 6 | /// 7 | /// 8 | public interface IFixtureCreationAttribute 9 | { 10 | /// 11 | /// Create fixture 12 | /// 13 | /// 14 | Fixture CreateFixture(); 15 | } 16 | 17 | 18 | /// 19 | /// Attribute for creating fixture 20 | /// 21 | public abstract class FixtureCreationAttribute : Attribute, IFixtureCreationAttribute 22 | { 23 | /// 24 | /// Create fixture 25 | /// 26 | /// 27 | public abstract Fixture CreateFixture(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/ModelService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace SimpleFixture.Impl 5 | { 6 | public interface IModelService 7 | { 8 | ComplexModel GetModel(Type modelType); 9 | } 10 | 11 | public class ModelService : IModelService 12 | { 13 | private readonly Dictionary _models = new Dictionary(); 14 | 15 | public ComplexModel GetModel(Type modelType) 16 | { 17 | ComplexModel model; 18 | 19 | if (!_models.TryGetValue(modelType, out model)) 20 | { 21 | model = new ComplexModel(); 22 | 23 | _models[modelType] = model; 24 | } 25 | 26 | return model; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/TypeFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SimpleFixture.Conventions; 3 | using Xunit; 4 | 5 | namespace SimpleFixture.Tests.FixtureTests.Primitives 6 | { 7 | public class TypeFixtureTests 8 | { 9 | [Fact] 10 | public void Fixture_Locate_Type() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var type = fixture.Locate(); 15 | 16 | Assert.NotNull(type); 17 | Assert.Equal(TypeConvention.LocateType, type); 18 | } 19 | 20 | [Fact] 21 | public void Fixture_Generate_Type() 22 | { 23 | var fixture = new Fixture(); 24 | 25 | var type = fixture.Generate(); 26 | 27 | Assert.NotNull(type); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany("")] 9 | [assembly: AssemblyProduct("SimpleFixture.Tests")] 10 | [assembly: AssemblyTrademark("")] 11 | 12 | // Setting ComVisible to false makes the types in this assembly not visible 13 | // to COM components. If you need to access a type in this assembly from 14 | // COM, set the ComVisible attribute to true on that type. 15 | [assembly: ComVisible(false)] 16 | 17 | // The following GUID is for the ID of the typelib if this project is exposed to COM 18 | [assembly: Guid("5b0d6a47-081b-40c5-98d5-438c69028dd9")] 19 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/FixtureInitializationAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.Attributes 4 | { 5 | /// 6 | /// attributes that implement this interface will be called to initialize fixture 7 | /// 8 | public interface IFixtureInitializationAttribute 9 | { 10 | /// 11 | /// 12 | /// 13 | /// 14 | void Initialize(Fixture fixture); 15 | } 16 | 17 | /// 18 | /// Attribute for initializing fixture 19 | /// 20 | public abstract class FixtureInitializationAttribute : Attribute, IFixtureInitializationAttribute 21 | { 22 | /// 23 | /// Initialize fixture 24 | /// 25 | /// fixture 26 | public abstract void Initialize(Fixture fixture); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/PopulateTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests 5 | { 6 | public class PopulateTests 7 | { 8 | public class Person 9 | { 10 | public string FirstName { get; set; } 11 | 12 | public string LastName { get; set; } 13 | } 14 | 15 | [Fact] 16 | public void Populate_Existing_Object() 17 | { 18 | var fixture = new Fixture(); 19 | 20 | var person = new Person(); 21 | 22 | fixture.Populate(person); 23 | 24 | Assert.False(string.IsNullOrEmpty(person.FirstName)); 25 | Assert.True(person.FirstName.All(char.IsLetter)); 26 | 27 | Assert.False(string.IsNullOrEmpty(person.LastName)); 28 | Assert.True(person.LastName.All(char.IsLetter)); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/ExportValueTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using SimpleFixture.Attributes; 5 | using SimpleFixture.Tests.Classes; 6 | using SimpleFixture.xUnit; 7 | using Xunit; 8 | 9 | namespace SimpleFixture.Tests.FixtureTests 10 | { 11 | public class ExportValueTests 12 | { 13 | [Theory] 14 | [AutoData] 15 | [ExportValue("someString", "123")] 16 | public void ExportStringValueTest(StringDependentClass stringDependentClass) 17 | { 18 | Assert.Equal("123", stringDependentClass.SomeString); 19 | } 20 | 21 | [Theory] 22 | [AutoData] 23 | [ExportValue("otherString", "123")] 24 | public void ExportStringValueNotMatchTest(StringDependentClass stringDependentClass) 25 | { 26 | Assert.Equal("String", stringDependentClass.SomeString); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Complex/DictionaryTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests 5 | { 6 | public class DictionaryTests 7 | { 8 | [Fact] 9 | public void Fixture_GenerateDictionaryIntString_ReturnsPopulatedDictionary() 10 | { 11 | var fixture = new Fixture(); 12 | 13 | var dictionary = fixture.Generate>(); 14 | 15 | Assert.NotNull(dictionary); 16 | Assert.True(dictionary.Count > 0); 17 | } 18 | 19 | [Fact] 20 | public void Fixture_LocateDictionaryIntString_ReturnsPopulatedDictionary() 21 | { 22 | var fixture = new Fixture(); 23 | 24 | var dictionary = fixture.Locate>(); 25 | 26 | Assert.NotNull(dictionary); 27 | Assert.True(dictionary.Count == 0); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/SimpleFixture.FakeItEasy/FakeFixture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using FakeItEasy; 6 | using FakeItEasy.Creation; 7 | 8 | namespace SimpleFixture.FakeItEasy 9 | { 10 | public class FakeFixture : Fixture 11 | { 12 | public FakeFixture(IFixtureConfiguration configuration = null, bool defaultSingleton = true) : base(configuration) 13 | { 14 | Add(new FakeConvention(defaultSingleton)); 15 | } 16 | 17 | public T Fake(Action arrange = null, Action> options = null, bool? singleton = null) where T : class 18 | { 19 | T returnValue = Generate(constraints: new 20 | { 21 | fakeSingleton = singleton, 22 | builderOptions = options 23 | }); 24 | 25 | arrange?.Invoke(returnValue); 26 | 27 | return returnValue; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/SimpleFixture.Moq/MoqFixtureInitializeAttribute.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Attributes; 2 | 3 | namespace SimpleFixture.Moq 4 | { 5 | /// 6 | /// Initialize attribute for Moq 7 | /// 8 | public class MoqFixtureInitializeAttribute : FixtureInitializationAttribute 9 | { 10 | /// 11 | /// Default constructor 12 | /// 13 | public MoqFixtureInitializeAttribute() 14 | { 15 | DefaultSingleton = true; 16 | } 17 | 18 | /// 19 | /// Default singleton 20 | /// 21 | public bool DefaultSingleton { get; set; } 22 | 23 | 24 | /// 25 | /// Initialize fixture 26 | /// 27 | /// fixture 28 | public override void Initialize(Fixture fixture) 29 | { 30 | fixture.Add(new MoqConvention(DefaultSingleton)); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/SimpleFixture.NSubstitute/LanguageExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.NSubstitute 4 | { 5 | /// 6 | /// Language extensions 7 | /// 8 | public static class LanguageExtensions 9 | { 10 | /// 11 | /// Substitute interface 12 | /// 13 | /// 14 | /// 15 | /// 16 | /// 17 | /// 18 | public static T Substitute(this Fixture fixture, Action substituteAction = null, bool? singleton = null) 19 | { 20 | var returnValue = fixture.Generate(constraints: new 21 | { 22 | fakeSingleton = singleton 23 | }); 24 | 25 | substituteAction?.Invoke(returnValue); 26 | 27 | return returnValue; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/FixtureCustomizationAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.Attributes 4 | { 5 | public class FixtureCustomizationAttribute : FixtureInitializationAttribute 6 | { 7 | private readonly Type[] _types; 8 | 9 | public FixtureCustomizationAttribute(params Type[] types) 10 | { 11 | _types = types; 12 | } 13 | 14 | public override void Initialize(Fixture fixture) 15 | { 16 | foreach (var type in _types) 17 | { 18 | var initializer = fixture.Locate(type); 19 | 20 | if (initializer is IConvention) 21 | { 22 | fixture.Add(initializer as IConvention); 23 | } 24 | else if (initializer is IFixtureCustomization) 25 | { 26 | fixture.Add(initializer as IFixtureCustomization); 27 | } 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/BulkExportClasses.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture.Tests.Classes 2 | { 3 | public class BulkExport : IBulkExportInterface 4 | { 5 | public int Compute() 6 | { 7 | return 5; 8 | } 9 | } 10 | 11 | public class BulkExport2 : IBulkExportInterface2 12 | { 13 | public int Compute() 14 | { 15 | return 10; 16 | } 17 | } 18 | 19 | 20 | public class BulkExport3 : IBulkExportInterface3 21 | { 22 | public int Compute() 23 | { 24 | return 10; 25 | } 26 | } 27 | 28 | public class OpenGeneric : IGenericBulkInterface 29 | { 30 | public T GetT() 31 | { 32 | return default(T); 33 | } 34 | } 35 | 36 | public class MultipleTypeGenericBulk : IMultipleTypeGenericBulkInterface 37 | { 38 | public T1 Get() 39 | { 40 | return default(T1); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/CodeCoverageAppVeyor.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | SET dotnet="C:/Program Files/dotnet/dotnet.exe" 4 | SET opencover=..\..\tools\OpenCover.4.6.519\tools\OpenCover.Console.exe 5 | SET coveralls=..\..\tools\coveralls.net.0.412\tools\csmacnz.Coveralls.exe 6 | 7 | SET targetargs="test" 8 | SET filter="+[*]SimpleFixture.* -[SimpleFixture.Tests]* -[xunit.*]* " 9 | SET coveragefile=Coverage.xml 10 | SET coveragedir=Coverage 11 | 12 | nuget install OpenCover -Version 4.6.519 -OutputDirectory ..\..\tools 13 | nuget install coveralls.net -Version 0.412.0 -OutputDirectory ..\..\tools 14 | 15 | REM Run code coverage analysis 16 | %opencover% -oldStyle -returntargetcode -register:user -target:%dotnet% -output:%coveragefile% -targetargs:%targetargs% -filter:%filter% -skipautoprops -hideskipped:All 17 | 18 | IF %ERRORLEVEL% NEQ 0 ( 19 | Echo Unit tests failed 20 | EXIT /B %ERRORLEVEL% 21 | ) 22 | 23 | REM publish 24 | IF NOT "%COVERALLS_REPO_TOKEN%"=="" %coveralls% --serviceName appveyor --opencover -i .\Coverage.xml -------------------------------------------------------------------------------- /SimpleFixture.sln.DotSettings.user: -------------------------------------------------------------------------------- 1 | 2 | <SessionState ContinuousTestingMode="0" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> 3 | <Solution /> 4 | </SessionState> 5 | <SessionState ContinuousTestingMode="0" IsActive="True" Name="ExportValueTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> 6 | <Solution /> 7 | </SessionState> -------------------------------------------------------------------------------- /src/SimpleFixture.NSubstitute/SubFixtureInitializeAttribute.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Attributes; 2 | 3 | namespace SimpleFixture.NSubstitute 4 | { 5 | /// 6 | /// Attribute to initialize a fixture, used by SimpleFixture.xUnit 7 | /// 8 | public class SubFixtureInitializeAttribute : FixtureInitializationAttribute 9 | { 10 | /// 11 | /// Default constructor 12 | /// 13 | public SubFixtureInitializeAttribute() 14 | { 15 | DefaultSingleton = true; 16 | } 17 | 18 | /// 19 | /// Default interfaces to singleton 20 | /// 21 | public bool DefaultSingleton { get; set; } 22 | 23 | /// 24 | /// Initialize fixture 25 | /// 26 | /// 27 | public override void Initialize(Fixture fixture) 28 | { 29 | fixture.Add(new SubstituteConvention(DefaultSingleton)); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Conventions/IEqualityComparerConventionTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Conventions; 2 | using SimpleFixture.Tests.Classes; 3 | using Xunit; 4 | 5 | namespace SimpleFixture.Tests.FixtureTests.Conventions 6 | { 7 | // ReSharper disable once InconsistentNaming 8 | public class IEqualityComparerConventionTests 9 | { 10 | [Fact] 11 | public void IEqualityComparerConvention_Priority() 12 | { 13 | var instance = new IEqualityComparerConvention(); 14 | 15 | Assert.Equal(ConventionPriority.Last, instance.Priority); 16 | } 17 | 18 | [Fact] 19 | public void IEqualityComparerConvention_GenerateType() 20 | { 21 | var instance = new IEqualityComparerConvention(); 22 | var fixture = new Fixture(); 23 | 24 | var request = new DataRequest(null, fixture, typeof(ISomeInterface), DependencyType.Unknown, null, true, null, null); 25 | 26 | Assert.Equal(Convention.NoValue, instance.GenerateData(request)); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Ian Johnson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /src/SimpleFixture.Moq/LanguageExtensions.cs: -------------------------------------------------------------------------------- 1 | using Moq; 2 | using System; 3 | 4 | namespace SimpleFixture.Moq 5 | { 6 | /// 7 | /// Language extensions 8 | /// 9 | public static class LanguageExtensions 10 | { 11 | /// 12 | /// Mock a specific object, by default mocks are treated as singletons 13 | /// 14 | /// type to mock 15 | /// 16 | /// action to apply to the mock 17 | /// should it be a singleton 18 | /// new mock 19 | public static Mock Mock(this Fixture fixture, Action> mockAction = null, bool? singleton = null) where T : class 20 | { 21 | var returnValue = fixture.Generate>(constraints: new 22 | { 23 | moqSingleton = singleton 24 | }); 25 | 26 | mockAction?.Invoke(returnValue); 27 | 28 | return returnValue; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/SimpleFixture.xUnit/AutoDataAttribute.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Attributes; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using Xunit.Sdk; 6 | using System; 7 | 8 | namespace SimpleFixture.xUnit 9 | { 10 | /// 11 | /// Provide autogenerated data for xUnit 12 | /// 13 | public class AutoDataAttribute : DataAttribute 14 | { 15 | private readonly object[] _parameters; 16 | 17 | /// 18 | /// Create random data for XUnit tests 19 | /// 20 | /// 21 | public AutoDataAttribute(params object[] parameters) 22 | { 23 | _parameters = parameters; 24 | } 25 | 26 | /// 27 | /// 28 | /// 29 | /// 30 | /// 31 | public override IEnumerable GetData(MethodInfo testMethod) 32 | { 33 | yield return AttributeHelper.GetData(testMethod, _parameters); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/CircularReferenceHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.Impl 4 | { 5 | /// 6 | /// interface to handle circular references 7 | /// 8 | public interface ICircularReferenceHandler 9 | { 10 | /// 11 | /// Handle circular reference 12 | /// 13 | /// data request that has caused the circular reference 14 | /// data request value 15 | object HandleCircularReference(DataRequest request); 16 | } 17 | 18 | /// 19 | /// Default implementation of ICircularReferenceHandler, throws an exception 20 | /// 21 | public class CircularReferenceHandler : ICircularReferenceHandler 22 | { 23 | /// 24 | /// Handle circular reference 25 | /// 26 | /// data request that has caused the circular reference 27 | /// data request value 28 | public object HandleCircularReference(DataRequest request) 29 | { 30 | throw new Exception("Circular reference detected creating " + request.RequestedType.FullName); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/xUnitTests/FixtureCustomizationAttributeTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Attributes; 2 | using SimpleFixture.Conventions; 3 | using SimpleFixture.xUnit; 4 | using Xunit; 5 | 6 | namespace SimpleFixture.Tests.xUnitTests 7 | { 8 | 9 | public class FixtureCustomizationAttributeTests 10 | { 11 | [Theory] 12 | [AutoData] 13 | [FixtureCustomization(typeof(Customization))] 14 | public void FixtureCustomizationAttribute_Customization_Test(int intValue) 15 | { 16 | Assert.Equal(100, intValue); 17 | } 18 | 19 | [Theory] 20 | [AutoData] 21 | [FixtureCustomization(typeof(IntConvention))] 22 | public void FixtureCustomizationAttribute_Convention_Test(int intValue) 23 | { 24 | Assert.Equal(500, intValue); 25 | } 26 | } 27 | 28 | public class Customization : IFixtureCustomization 29 | { 30 | public void Customize(Fixture fixture) 31 | { 32 | fixture.Return(100); 33 | } 34 | } 35 | 36 | public class IntConvention : SimpleTypeConvention 37 | { 38 | public override object GenerateData(DataRequest request) 39 | { 40 | return 500; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/UriFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SimpleFixture.Conventions; 3 | using Xunit; 4 | 5 | namespace SimpleFixture.Tests.FixtureTests.Primitives 6 | { 7 | public class UriFixtureTests 8 | { 9 | [Fact] 10 | public void Fixture_GenerateUri_ReturnsPopulatedValue() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var uri = fixture.Generate(); 15 | 16 | Assert.NotNull(uri); 17 | } 18 | 19 | [Fact] 20 | public void Fixture_LocateUri_ReturnsLocateValue() 21 | { 22 | var fixture = new Fixture(); 23 | 24 | var uri = fixture.Locate(); 25 | 26 | Assert.NotNull(uri); 27 | Assert.Equal(UriConvention.LocateValue, uri); 28 | } 29 | 30 | [Fact] 31 | public void Fixture_GenerateUriWithConstraints_ReturnsPopulatedValue() 32 | { 33 | var fixture = new Fixture(); 34 | 35 | var uri = fixture.Generate(constraints: new { uriDomain = "apple.com", uriScheme = "ftp" }); 36 | 37 | Assert.NotNull(uri); 38 | Assert.Equal("ftp", uri.Scheme); 39 | Assert.Equal("apple.com", uri.Host); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/BoolConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// convention for creating bool 7 | /// 8 | public class BoolConvention : SimpleTypeConvention 9 | { 10 | private readonly IRandomDataGeneratorService _dataGenerator; 11 | 12 | /// 13 | /// Value return for locate 14 | /// 15 | public static bool LocateValue = true; 16 | 17 | /// 18 | /// Default constructor 19 | /// 20 | /// 21 | public BoolConvention(IRandomDataGeneratorService dataGenerator) 22 | { 23 | _dataGenerator = dataGenerator; 24 | } 25 | 26 | /// 27 | /// Generate data for the request, return Constrain.NoValue instead of null 28 | /// 29 | /// data request 30 | /// generated data 31 | public override object GenerateData(DataRequest request) 32 | { 33 | if (!request.Populate) 34 | { 35 | return LocateValue; 36 | } 37 | 38 | return _dataGenerator.NextBool(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/RandomDataGeneratorServiceTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests 5 | { 6 | public class RandomDataGeneratorServiceTests 7 | { 8 | public enum RandomEnum 9 | { 10 | Kinda, 11 | Sorta, 12 | Maybe, 13 | Yes, 14 | No 15 | } 16 | 17 | [Fact] 18 | public void RandomDataGeneratorService_Generate_Decimal() 19 | { 20 | var service = new RandomDataGeneratorService(); 21 | 22 | service.NextDecimal(); 23 | } 24 | 25 | [Fact] 26 | public void RandomDataGeneratorService_Generate_Enum() 27 | { 28 | var service = new RandomDataGeneratorService(); 29 | 30 | var enumValue = service.NextEnum(); 31 | } 32 | 33 | [Fact] 34 | public void RandomDataGeneratorService_Generate_DateTime() 35 | { 36 | var service = new RandomDataGeneratorService(); 37 | 38 | var dateTime = service.NextDateTime(); 39 | } 40 | 41 | [Fact] 42 | public void RandomDataGeneratorService_Generate_Timespan() 43 | { 44 | var service = new RandomDataGeneratorService(); 45 | 46 | var value = service.NextTimeSpan(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/Classes/CircularClasses.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace SimpleFixture.Tests.Classes 4 | { 5 | /// 6 | /// THese classes are for issue #16 7 | /// 8 | public partial class Order 9 | { 10 | public int Id { get; set; } 11 | 12 | public System.Guid AdministrationId { get; set; } 13 | 14 | public virtual Administration Administration { get; set; } 15 | } 16 | 17 | public partial class Administration 18 | { 19 | public System.Guid Id { get; set; } 20 | 21 | public virtual ICollection Order { get; set; } 22 | } 23 | 24 | public partial class ParentClass 25 | { 26 | public ChildClass ChildClass { get; set; } 27 | } 28 | 29 | public class ChildClass 30 | { 31 | public ParentClass ParentClass { get; set; } 32 | } 33 | 34 | public interface IParentInterfaceClass 35 | { 36 | ChildInterfaceClass Child { get; } 37 | } 38 | 39 | public class ParentInterfaceClass : IParentInterfaceClass 40 | { 41 | public ChildInterfaceClass Child { get; set; } 42 | } 43 | 44 | public class ChildInterfaceClass 45 | { 46 | public ChildInterfaceClass(IParentInterfaceClass parent) 47 | { 48 | Parent = parent; 49 | } 50 | 51 | public IParentInterfaceClass Parent { get; } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/NullableConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating nullable 7 | /// 8 | public class NullableConvention : IConvention 9 | { 10 | /// 11 | /// Prioirity the convention should be looked at 12 | /// 13 | public ConventionPriority Priority => ConventionPriority.Low; 14 | 15 | /// 16 | /// Priorit changed event 17 | /// 18 | public event EventHandler PriorityChanged; 19 | 20 | /// 21 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 22 | /// 23 | /// data request 24 | /// generated data value 25 | public object GenerateData(DataRequest request) 26 | { 27 | if (request.RequestedType.IsConstructedGenericType && 28 | request.RequestedType.GetGenericTypeDefinition() == typeof(Nullable<>)) 29 | { 30 | var newRequest = new DataRequest(request, request.RequestedType.GenericTypeArguments[0]); 31 | 32 | return newRequest.Fixture.Generate(newRequest); 33 | } 34 | 35 | return Convention.NoValue; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/TypeFieldSelector.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Reflection; 4 | 5 | namespace SimpleFixture.Impl 6 | { 7 | public interface ITypeFieldSelector 8 | { 9 | IEnumerable SelectFields(object instance, DataRequest request, ComplexModel model); 10 | } 11 | 12 | public class TypeFieldSelector : ITypeFieldSelector 13 | { 14 | private readonly IConstraintHelper _helper; 15 | 16 | public TypeFieldSelector(IConstraintHelper helper) 17 | { 18 | _helper = helper; 19 | } 20 | 21 | public IEnumerable SelectFields(object instance, DataRequest request, ComplexModel model) 22 | { 23 | var skipFields = new List(); 24 | 25 | var skipFieldsEnumerable = _helper.GetValue>(request.Constraints, 26 | null, 27 | "_skipFields"); 28 | 29 | if (skipFieldsEnumerable != null) 30 | { 31 | skipFields.AddRange(skipFieldsEnumerable); 32 | } 33 | 34 | return instance.GetType() 35 | .GetRuntimeFields() 36 | .Where(f => f.IsPublic && !skipFields.Contains(f.Name)); 37 | 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/ConstraintTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using SimpleFixture.Tests.Classes; 3 | using Xunit; 4 | 5 | namespace SimpleFixture.Tests.FixtureTests 6 | { 7 | public class ConstraintTests 8 | { 9 | [Fact] 10 | public void Fixture_NamedPropertyConstrain_ReturnsCorrectly() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var importClass = 15 | fixture.Generate(constraints: new { SomeClass = new SomeClass { IntValue = 50, StringValue = "Test" } }); 16 | 17 | importClass.Should().NotBeNull(); 18 | importClass.SomeClass.Should().NotBeNull(); 19 | importClass.SomeClass.IntValue.Should().Be(50); 20 | importClass.SomeClass.StringValue.Should().Be("Test"); 21 | } 22 | 23 | [Fact] 24 | public void Fixture_TypedValues_ReturnsCorrectly() 25 | { 26 | var fixture = new Fixture(); 27 | 28 | var importClass = 29 | fixture.Generate(constraints: new { _Values = new[] { new SomeClass { IntValue = 50, StringValue = "Test" } } }); 30 | 31 | importClass.Should().NotBeNull(); 32 | importClass.SomeClass.Should().NotBeNull(); 33 | importClass.SomeClass.IntValue.Should().Be(50); 34 | importClass.SomeClass.StringValue.Should().Be("Test"); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/TypeConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SimpleFixture.Impl; 3 | 4 | namespace SimpleFixture.Conventions 5 | { 6 | /// 7 | /// Generate random type 8 | /// 9 | public class TypeConvention : SimpleTypeConvention 10 | { 11 | private readonly IRandomDataGeneratorService _dataGenerator; 12 | private readonly Type[] _types = { typeof(bool), typeof(short), typeof(int), typeof(double), typeof(string), typeof(Fixture) }; 13 | 14 | /// 15 | /// Type returned for locate 16 | /// 17 | public static readonly Type LocateType = typeof(int); 18 | 19 | /// 20 | /// Default constructor 21 | /// 22 | /// 23 | public TypeConvention(IRandomDataGeneratorService dataGenerator) 24 | { 25 | _dataGenerator = dataGenerator; 26 | } 27 | 28 | /// 29 | /// Generate data for the request, return Constrain.NoValue instead of null 30 | /// 31 | /// data request 32 | /// generated data 33 | public override object GenerateData(DataRequest request) 34 | { 35 | if (!request.Populate) 36 | { 37 | return LocateType; 38 | } 39 | 40 | return _dataGenerator.NextInSet(_types); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/TimeSpanTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.Primitives 5 | { 6 | public class TimeSpanTests 7 | { 8 | [Fact] 9 | public void Fixture_LocateTimeSpan_ReturnsRandomTimeSpan() 10 | { 11 | var fixture = new Fixture(); 12 | 13 | var timestamp = fixture.Generate(); 14 | 15 | Assert.NotNull(timestamp); 16 | 17 | var timestamp2 = fixture.Generate(); 18 | 19 | Assert.NotEqual(timestamp, timestamp2); 20 | } 21 | 22 | [Fact] 23 | public void Fixture_LocateTimeSpan_ConstraintValue() 24 | { 25 | var fixture = new Fixture(); 26 | var constraint = new { min = new TimeSpan(10000), max = new TimeSpan(11000) }; 27 | var timestamp = fixture.Generate(constraints: constraint); 28 | 29 | Assert.NotNull(timestamp); 30 | Assert.InRange(timestamp, constraint.min, constraint.max); 31 | } 32 | 33 | 34 | [Fact] 35 | public void Fixture_LocateTimeSpan_Min_Greater_Than_Max() 36 | { 37 | var fixture = new Fixture(); 38 | var constraint = new { min = new TimeSpan(11000), max = new TimeSpan(10000) }; 39 | var timestamp = fixture.Generate(constraints: constraint); 40 | 41 | Assert.Equal(new TimeSpan(10000), timestamp); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/SimpleFixture.Moq/MoqFixture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Moq; 3 | 4 | namespace SimpleFixture.Moq 5 | { 6 | /// 7 | /// Fixture that uses Moq 8 | /// 9 | public class MoqFixture : Fixture 10 | { 11 | /// 12 | /// Default constructor 13 | /// 14 | /// 15 | /// 16 | public MoqFixture(IFixtureConfiguration configuration = null, bool defaultSingleton = true) 17 | : base(configuration) 18 | { 19 | Add(new MoqConvention(defaultSingleton)); 20 | } 21 | 22 | /// 23 | /// Mock a specific object, by default mocks are treated as singletons 24 | /// 25 | /// type to mock 26 | /// action to apply to the mock 27 | /// should it be a singleton 28 | /// new mock 29 | public Mock Mock(Action> mockAction = null, bool? singleton = null) where T : class 30 | { 31 | var returnValue = Generate>(constraints: new 32 | { 33 | moqSingleton = singleton 34 | }); 35 | 36 | if (mockAction != null) 37 | { 38 | mockAction(returnValue); 39 | } 40 | 41 | return returnValue; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/SimpleFixture.NSubstitute/SubFixture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture.NSubstitute 4 | { 5 | /// 6 | /// Fixture using NSubstitute 7 | /// 8 | public class SubFixture : Fixture 9 | { 10 | /// 11 | /// Default constructor 12 | /// 13 | /// 14 | /// 15 | public SubFixture(IFixtureConfiguration configuration = null, bool defaultSingleton = true) 16 | : base(configuration) 17 | { 18 | Add(new SubstituteConvention(defaultSingleton)); 19 | } 20 | 21 | /// 22 | /// Substitute for a particular type, by default substitute types are treated as singletons 23 | /// 24 | /// Type to substitute 25 | /// arrange 26 | /// singleton 27 | /// new substituted type 28 | public T Substitute(Action substituteAction = null, bool? singleton = null) 29 | { 30 | var returnValue = Generate(constraints: new 31 | { 32 | fakeSingleton = singleton 33 | }); 34 | 35 | substituteAction?.Invoke(returnValue); 36 | 37 | return returnValue; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/UriConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SimpleFixture.Impl; 3 | 4 | namespace SimpleFixture.Conventions 5 | { 6 | /// 7 | /// Convention for creating uri 8 | /// 9 | public class UriConvention : SimpleTypeConvention 10 | { 11 | private readonly IConstraintHelper _constraintHelper; 12 | 13 | /// 14 | /// Value returned for locate 15 | /// 16 | public static Uri LocateValue = new Uri("http://google.com"); 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | public UriConvention(IConstraintHelper constraintHelper) 23 | { 24 | _constraintHelper = constraintHelper; 25 | } 26 | 27 | /// 28 | /// Generate data for the request, return Constrain.NoValue instead of null 29 | /// 30 | /// data request 31 | /// generated data 32 | public override object GenerateData(DataRequest request) 33 | { 34 | if (!request.Populate) 35 | { 36 | return LocateValue; 37 | } 38 | 39 | var domain = _constraintHelper.GetValue(request.Constraints, "google.com", "uriDomain"); 40 | var scheme = _constraintHelper.GetValue(request.Constraints, "http", "uriScheme"); 41 | 42 | return new Uri(string.Format("{0}://{1}", scheme, domain)); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/FreezeTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Tests.Classes; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests 5 | { 6 | public class FreezeTests 7 | { 8 | [Fact] 9 | public void Fixture_FreezeInt_ReturnsInt() 10 | { 11 | var fixture = new Fixture(); 12 | 13 | var froozen = fixture.Freeze(); 14 | 15 | Assert.Equal(froozen, fixture.Generate()); 16 | } 17 | 18 | [Fact] 19 | public void Fixture_FreezeIntForType_ReturnsFroozenInt() 20 | { 21 | var fixture = new Fixture(); 22 | 23 | var froozen = fixture.Freeze(value: i => i.For()); 24 | 25 | var instance = fixture.Generate(); 26 | 27 | Assert.NotEqual(froozen, fixture.Generate()); 28 | 29 | Assert.Equal(froozen, instance.IntValue); 30 | } 31 | 32 | [Fact] 33 | public void Fixture_FreezeIntWhenNamed_ReturnsFroozenInt() 34 | { 35 | var fixture = new Fixture(); 36 | 37 | var froozen = fixture.Freeze(value: i => i.WhenNamed(name => name.EndsWith("1"))); 38 | 39 | var instance1 = fixture.Generate(); 40 | var instance2 = fixture.Generate(); 41 | 42 | Assert.Equal(froozen, instance1.IntValue1); 43 | Assert.Equal(froozen, instance2.IntValue1); 44 | 45 | Assert.NotEqual(froozen,instance1.IntValue2); 46 | Assert.NotEqual(froozen,instance2.IntValue2); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Complex/ComparerConventionTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.Complex 5 | { 6 | public class ComparerConventionTests 7 | { 8 | [Fact] 9 | public void Fixture_GenerateIComparerInt_ReturnsWorkingComparer() 10 | { 11 | var fixture = new Fixture(); 12 | 13 | var comparer = fixture.Generate>(); 14 | 15 | Assert.NotNull(comparer); 16 | Assert.True(comparer.Compare(1, 2) < 0); 17 | } 18 | 19 | [Fact] 20 | public void Fixture_GenerateIEqualityComparerInt_ReturnsWorkingComparer() 21 | { 22 | var fixture = new Fixture(); 23 | 24 | var comparer = fixture.Locate>(); 25 | 26 | Assert.NotNull(comparer); 27 | Assert.False(comparer.Equals(1, 2)); 28 | } 29 | 30 | [Fact] 31 | public void Fixture_LocateIComparerInt_ReturnsWorkingComparer() 32 | { 33 | var fixture = new Fixture(); 34 | 35 | var comparer = fixture.Locate>(); 36 | 37 | Assert.NotNull(comparer); 38 | Assert.True(comparer.Compare(1,2) < 0); 39 | } 40 | 41 | [Fact] 42 | public void Fixture_LocateIEqualityComparerInt_ReturnsWorkingComparer() 43 | { 44 | var fixture = new Fixture(); 45 | 46 | var comparer = fixture.Locate>(); 47 | 48 | Assert.NotNull(comparer); 49 | Assert.False(comparer.Equals(1, 2)); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/SimpleTypeConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace SimpleFixture.Conventions 5 | { 6 | /// 7 | /// Abstract simple type convention 8 | /// 9 | /// 10 | public abstract class SimpleTypeConvention : ITypedConvention 11 | { 12 | /// 13 | /// Priority for the convention, last by default 14 | /// 15 | public virtual ConventionPriority Priority => ConventionPriority.Last; 16 | 17 | /// 18 | /// Priorit changed event 19 | /// 20 | public event EventHandler PriorityChanged; 21 | 22 | /// 23 | /// Generate data for the request, return Constrain.NoValue instead of null 24 | /// 25 | /// data request 26 | /// generated data 27 | public abstract object GenerateData(DataRequest request); 28 | 29 | /// 30 | /// Supported types 31 | /// 32 | public virtual IEnumerable SupportedTypes 33 | { 34 | get { yield return typeof(T); } 35 | } 36 | 37 | /// 38 | /// Raise priority changed event 39 | /// 40 | /// 41 | protected void RaisePriorityChanged(ConventionPriority priority) 42 | { 43 | if (PriorityChanged != null) 44 | { 45 | PriorityChanged(this,new PriorityChangedEventArgs{ Priority = priority}); 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/ExportValueAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | 7 | namespace SimpleFixture.Attributes 8 | { 9 | /// 10 | /// Set a particular simple value through attribute 11 | /// 12 | [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] 13 | public class ExportValueAttribute : FixtureInitializationAttribute, IConvention 14 | { 15 | private readonly string _key; 16 | private readonly object _value; 17 | 18 | /// 19 | /// default constructor 20 | /// 21 | /// 22 | /// 23 | public ExportValueAttribute(string key, object value) 24 | { 25 | _key = key; 26 | _value = value; 27 | } 28 | 29 | /// Initialize fixture 30 | /// fixture 31 | public override void Initialize(Fixture fixture) 32 | { 33 | fixture.Add(this); 34 | } 35 | 36 | /// 37 | public ConventionPriority Priority => ConventionPriority.High; 38 | 39 | /// 40 | public event EventHandler PriorityChanged; 41 | 42 | /// 43 | public object GenerateData(DataRequest request) 44 | { 45 | return request.RequestName == _key && 46 | request.RequestedType.IsAssignableFrom(_value.GetType()) ? 47 | _value : 48 | Convention.NoValue; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/IComparerConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace SimpleFixture.Conventions 6 | { 7 | /// 8 | /// Convention for creating IComparer 9 | /// 10 | // ReSharper disable once InconsistentNaming 11 | public class IComparerConvention : ITypedConvention 12 | { 13 | /// 14 | /// Prioirity the convention should be looked at 15 | /// 16 | public ConventionPriority Priority => ConventionPriority.Last; 17 | 18 | /// 19 | /// Priorit changed event 20 | /// 21 | public event EventHandler PriorityChanged; 22 | 23 | /// 24 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 25 | /// 26 | /// data request 27 | /// generated data value 28 | public object GenerateData(DataRequest request) 29 | { 30 | if (request.RequestedType.IsConstructedGenericType) 31 | { 32 | var closedType = typeof(Comparer<>).MakeGenericType(request.RequestedType.GenericTypeArguments[0]); 33 | 34 | var defaultProperty = closedType.GetRuntimeProperty("Default"); 35 | 36 | return defaultProperty.GetValue(null); 37 | } 38 | 39 | return Convention.NoValue; 40 | } 41 | 42 | /// 43 | /// Types the convention supports 44 | /// 45 | public IEnumerable SupportedTypes 46 | { 47 | get { yield return typeof(IComparer<>); } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/IEqualityComparerConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace SimpleFixture.Conventions 6 | { 7 | /// 8 | /// Convention for IEqualityComparer 9 | /// 10 | // ReSharper disable once InconsistentNaming 11 | public class IEqualityComparerConvention : ITypedConvention 12 | { 13 | /// 14 | /// Prioirity the convention should be looked at 15 | /// 16 | public ConventionPriority Priority => ConventionPriority.Last; 17 | 18 | /// 19 | /// Priorit changed event 20 | /// 21 | public event EventHandler PriorityChanged; 22 | 23 | /// 24 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 25 | /// 26 | /// data request 27 | /// generated data value 28 | public object GenerateData(DataRequest request) 29 | { 30 | if (request.RequestedType.IsConstructedGenericType) 31 | { 32 | var closedType = typeof(EqualityComparer<>).MakeGenericType(request.RequestedType.GenericTypeArguments[0]); 33 | 34 | var defualtProperty = closedType.GetRuntimeProperty("Default"); 35 | 36 | return defualtProperty.GetValue(null); 37 | } 38 | 39 | return Convention.NoValue; 40 | } 41 | 42 | /// 43 | /// Types the convention supports 44 | /// 45 | public IEnumerable SupportedTypes 46 | { 47 | get { yield return typeof(IEqualityComparer<>); } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/SimpleFixture/Attributes/ExportAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Threading.Tasks; 6 | 7 | namespace SimpleFixture.Attributes 8 | { 9 | /// 10 | /// Export specific type to be used in Fixture 11 | /// 12 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Assembly, AllowMultiple = true)] 13 | public class ExportAttribute : FixtureInitializationAttribute 14 | { 15 | private readonly Type _type; 16 | 17 | /// 18 | /// Default constuctor 19 | /// 20 | /// 21 | public ExportAttribute(Type type) 22 | { 23 | _type = type; 24 | } 25 | 26 | /// 27 | /// Singleton 28 | /// 29 | public bool Singleton { get; set; } 30 | 31 | /// Initialize fixture 32 | /// fixture 33 | public override void Initialize(Fixture fixture) 34 | { 35 | foreach (var implementedInterface in _type.GetTypeInfo().ImplementedInterfaces) 36 | { 37 | var closedMethod = _exportInterface.MakeGenericMethod(_type, implementedInterface); 38 | 39 | closedMethod.Invoke(this, new object[] { fixture }); 40 | } 41 | } 42 | 43 | private void ExportInterface(Fixture fixture) where T : class, TInterface 44 | { 45 | fixture.ExportAs(); 46 | } 47 | 48 | private static readonly MethodInfo _exportInterface = 49 | typeof(ExportAttribute).GetTypeInfo().DeclaredMethods.First(m => m.Name == "ExportInterface"); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/ArrayConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | 6 | namespace SimpleFixture.Conventions 7 | { 8 | /// 9 | /// Convention for creating array 10 | /// 11 | public class ArrayConvention : IConvention 12 | { 13 | /// 14 | /// Prioirity the convention should be looked at 15 | /// 16 | public ConventionPriority Priority => ConventionPriority.Low; 17 | 18 | /// 19 | /// Priorit changed event 20 | /// 21 | public event EventHandler PriorityChanged; 22 | 23 | /// 24 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 25 | /// 26 | /// data request 27 | /// generated data value 28 | public object GenerateData(DataRequest request) 29 | { 30 | if (request.RequestedType.IsArray) 31 | { 32 | var method = GetType().GetTypeInfo().DeclaredMethods.First(m => m.Name == "GetArray"); 33 | 34 | var closedMethod = method.MakeGenericMethod(request.RequestedType.GetElementType()); 35 | 36 | return closedMethod.Invoke(this, new object[] { request }); 37 | } 38 | 39 | return Convention.NoValue; 40 | } 41 | 42 | private T[] GetArray(DataRequest request) 43 | { 44 | var newRequest = new DataRequest(request, typeof(IEnumerable)); 45 | 46 | var enumerable = (IEnumerable) request.Fixture.Generate(newRequest); 47 | 48 | return enumerable.ToArray(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/EnumConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | using SimpleFixture.Impl; 5 | 6 | namespace SimpleFixture.Conventions 7 | { 8 | /// 9 | /// Convention for creating enum 10 | /// 11 | public class EnumConvention : IConvention 12 | { 13 | private readonly IRandomDataGeneratorService _dataGenerator; 14 | 15 | /// 16 | /// DEfault constructor 17 | /// 18 | /// 19 | /// 20 | public EnumConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 21 | { 22 | _dataGenerator = dataGenerator; 23 | } 24 | 25 | /// 26 | /// Prioirity the convention should be looked at 27 | /// 28 | public ConventionPriority Priority => ConventionPriority.Low; 29 | 30 | /// 31 | /// Priorit changed event 32 | /// 33 | public event EventHandler PriorityChanged; 34 | 35 | /// 36 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 37 | /// 38 | /// data request 39 | /// generated data value 40 | public object GenerateData(DataRequest request) 41 | { 42 | if(request.RequestedType.GetTypeInfo().IsEnum) 43 | { 44 | if(!request.Populate) 45 | { 46 | return Enum.GetValues(request.RequestedType) 47 | .Cast() 48 | .First(); 49 | } 50 | 51 | return _dataGenerator.NextEnum(request.RequestedType); 52 | } 53 | 54 | return Convention.NoValue; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/LongConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating long 7 | /// 8 | public class LongConvention : SimpleTypeConvention 9 | { 10 | private readonly IConstraintHelper _constraintHelper; 11 | private readonly IRandomDataGeneratorService _dataGenerator; 12 | 13 | /// 14 | /// Value returned for locate 15 | /// 16 | public static long LocateValue = 5; 17 | 18 | /// 19 | /// Default construtor 20 | /// 21 | /// 22 | /// 23 | public LongConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, long.MinValue, long.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextLong(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/ULongConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating ulong 7 | /// 8 | public class ULongConvention : SimpleTypeConvention 9 | { 10 | private readonly IConstraintHelper _constraintHelper; 11 | private readonly IRandomDataGeneratorService _dataGenerator; 12 | 13 | /// 14 | /// value returned for locate 15 | /// 16 | public static ulong LocateValue = 5; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public ULongConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, ulong.MinValue, ulong.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextULong(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/CharConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating random char 7 | /// 8 | public class CharConvention : SimpleTypeConvention 9 | { 10 | private readonly IRandomDataGeneratorService _dataGenerator; 11 | private readonly IConstraintHelper _constraintHelper; 12 | 13 | /// 14 | /// Value that is returned for locate 15 | /// 16 | public static char LocateValue = 'C'; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public CharConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate char data 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, char.MinValue, char.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextChar(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/IntConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating int 7 | /// 8 | public class IntConvention : SimpleTypeConvention 9 | { 10 | private readonly IConstraintHelper _constraintHelper; 11 | private readonly IRandomDataGeneratorService _dataGenerator; 12 | 13 | /// 14 | /// Locate value for int 15 | /// 16 | public static int LocateValue = 5; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public IntConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, int.MinValue, int.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextInt(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/UIntConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating uint 7 | /// 8 | public class UIntConvention : SimpleTypeConvention 9 | { 10 | private readonly IConstraintHelper _constraintHelper; 11 | private readonly IRandomDataGeneratorService _dataGenerator; 12 | 13 | /// 14 | /// Value returned for locate 15 | /// 16 | public static uint LocateValue = 5; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public UIntConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, uint.MinValue, uint.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextUInt(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/ConventionList.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace SimpleFixture.Impl 4 | { 5 | public interface IConventionList 6 | { 7 | void AddConvention(IConvention convention); 8 | 9 | bool TryGetValue(DataRequest dataRequest, out object value); 10 | } 11 | 12 | public class ConventionList : IConventionList 13 | { 14 | private readonly List _conventions = new List(); 15 | 16 | public void AddConvention(IConvention convention) 17 | { 18 | InternalAddConvention(convention); 19 | 20 | convention.PriorityChanged += 21 | (sender, args) => 22 | { 23 | _conventions.Remove(convention); 24 | 25 | InternalAddConvention(convention); 26 | }; 27 | } 28 | 29 | public bool TryGetValue(DataRequest dataRequest, out object value) 30 | { 31 | var returnValue = false; 32 | 33 | value = null; 34 | 35 | foreach (var convention in _conventions) 36 | { 37 | var newValue = convention.GenerateData(dataRequest); 38 | 39 | if (newValue == Convention.NoValue) 40 | { 41 | continue; 42 | } 43 | 44 | value = newValue; 45 | returnValue = true; 46 | break; 47 | } 48 | 49 | return returnValue; 50 | } 51 | 52 | private void InternalAddConvention(IConvention convention) 53 | { 54 | for (var i = 0; i < _conventions.Count; i++) 55 | { 56 | if (convention.Priority > _conventions[i].Priority) 57 | { 58 | continue; 59 | } 60 | 61 | _conventions.Insert(i, convention); 62 | return; 63 | } 64 | 65 | _conventions.Add(convention); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/SByteConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating sbyte 7 | /// 8 | public class SByteConvention : SimpleTypeConvention 9 | { 10 | private readonly IRandomDataGeneratorService _dataGenerator; 11 | private readonly IConstraintHelper _constraintHelper; 12 | 13 | /// 14 | /// value returned for locate 15 | /// 16 | public static sbyte LocateValue = 5; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public SByteConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, sbyte.MinValue, sbyte.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextSByte(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/ShortConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating short 7 | /// 8 | public class ShortConvention : SimpleTypeConvention 9 | { 10 | private readonly IConstraintHelper _constraintHelper; 11 | private readonly IRandomDataGeneratorService _dataGenerator; 12 | 13 | /// 14 | /// Value returned for locate 15 | /// 16 | public static short LocateValue = 5; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public ShortConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, short.MinValue, short.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextShort(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/ByteConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating byte 7 | /// 8 | public class ByteConvention : SimpleTypeConvention 9 | { 10 | private readonly IRandomDataGeneratorService _dataGenerator; 11 | private readonly IConstraintHelper _constraintHelper; 12 | 13 | /// 14 | /// value returned for locate 15 | /// 16 | public static byte LocateValue = 5; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public ByteConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, byte.MinValue, byte.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextByte(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/SimpleConventionTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using SimpleFixture.Conventions; 4 | using SimpleFixture.Impl; 5 | using Xunit; 6 | 7 | namespace SimpleFixture.Tests.FixtureTests.Primitives 8 | { 9 | public class SimpleConventionTests 10 | { 11 | [Theory] 12 | [InlineData(typeof(ByteConvention))] 13 | [InlineData(typeof(SByteConvention))] 14 | [InlineData(typeof(ShortConvention))] 15 | [InlineData(typeof(UShortConvention))] 16 | [InlineData(typeof(IntConvention))] 17 | [InlineData(typeof(UIntConvention))] 18 | [InlineData(typeof(LongConvention))] 19 | [InlineData(typeof(ULongConvention))] 20 | [InlineData(typeof(DoubleConvention))] 21 | [InlineData(typeof(DecimalConvention))] 22 | public void SimpleConvention_Min_Max_Test(Type conventionType) 23 | { 24 | var fixture = new Fixture(); 25 | 26 | var convention = CreateTypedConvention(conventionType); 27 | 28 | var value = convention.GenerateData(new DataRequest(null, fixture, GetPrimitiveType(conventionType), DependencyType.Unknown, null, true, new {min = 3, max = 2}, null)); 29 | 30 | Assert.Equal(2, Convert.ToInt32(value)); 31 | } 32 | 33 | private Type GetPrimitiveType(Type type) 34 | { 35 | var baseType = type.GetTypeInfo().BaseType; 36 | 37 | if (!baseType.IsConstructedGenericType || 38 | baseType.GetGenericTypeDefinition() != typeof(SimpleTypeConvention<>)) 39 | { 40 | throw new Exception("Must be SimpleTypeConvention"); 41 | } 42 | 43 | return baseType.GenericTypeArguments[0]; 44 | } 45 | 46 | private ITypedConvention CreateTypedConvention(Type conventionType) 47 | { 48 | return (ITypedConvention)Activator.CreateInstance(conventionType, new RandomDataGeneratorService(), new ConstraintHelper()); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/UShortConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention returned for ushort 7 | /// 8 | public class UShortConvention : SimpleTypeConvention 9 | { 10 | private readonly IConstraintHelper _constraintHelper; 11 | private readonly IRandomDataGeneratorService _dataGenerator; 12 | 13 | /// 14 | /// value returned for locate 15 | /// 16 | public static ushort LocateValue = 5; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public UShortConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, ushort.MinValue, ushort.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextUShort(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/DecimalConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// Convention for creating decimal 7 | /// 8 | public class DecimalConvention : SimpleTypeConvention 9 | { 10 | private readonly IRandomDataGeneratorService _dataGenerator; 11 | private readonly IConstraintHelper _constraintHelper; 12 | 13 | /// 14 | /// value returned for locate 15 | /// 16 | public static decimal LocateValue = 5; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public DecimalConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, decimal.MinValue, decimal.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextDecimal(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/DoubleConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | 3 | namespace SimpleFixture.Conventions 4 | { 5 | /// 6 | /// convention for creating double 7 | /// 8 | public class DoubleConvention : SimpleTypeConvention 9 | { 10 | private readonly IRandomDataGeneratorService _dataGenerator; 11 | private readonly IConstraintHelper _constraintHelper; 12 | 13 | /// 14 | /// Value returned for locate 15 | /// 16 | public static double LocateValue = 5; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | /// 23 | public DoubleConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 24 | { 25 | _dataGenerator = dataGenerator; 26 | _constraintHelper = constraintHelper; 27 | } 28 | 29 | /// 30 | /// Generate data for the request, return Constrain.NoValue instead of null 31 | /// 32 | /// data request 33 | /// generated data 34 | public override object GenerateData(DataRequest request) 35 | { 36 | if (!request.Populate) 37 | { 38 | return LocateValue; 39 | } 40 | 41 | var minMax = _constraintHelper.GetMinMax(request, double.MinValue, double.MaxValue); 42 | 43 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 44 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 45 | 46 | if (minMax.Min.CompareTo(minMax.Max) > 0) 47 | { 48 | minMax.Min = minMax.Max; 49 | } 50 | 51 | return _dataGenerator.NextDouble(minMax.Min, minMax.Max); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/SimpleFixture/SimpleFixture.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test fixture framework for unit and integration testing. 5 | Ian Johnson 6 | netstandard2.0;net6.0 7 | netstandard2.0; 8 | true 9 | SimpleFixture 10 | SimpleFixture 11 | Test;Fixture;Data;Generation 12 | https://github.com/ipjohnson/SimpleFixture/wiki/Release-Notes 13 | https://github.com/ipjohnson/SimpleFixture 14 | https://github.com/ipjohnson/SimpleFixture/blob/master/License.md 15 | git 16 | https://github.com/ipjohnson/SimpleFixture 17 | false 18 | false 19 | false 20 | full 21 | True 22 | true 23 | ..\SimpleFixture.snk 24 | 25 | 26 | 27 | 28 | true 29 | true 30 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb 31 | 32 | 33 | 34 | 1701;1702;1591 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/TimeSpanConvention.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | using System; 3 | 4 | namespace SimpleFixture.Conventions 5 | { 6 | /// 7 | /// Convention for creating timespan 8 | /// 9 | public class TimeSpanConvention : SimpleTypeConvention 10 | { 11 | private readonly IRandomDataGeneratorService _dataGenerator; 12 | private readonly IConstraintHelper _constraintHelper; 13 | 14 | /// 15 | /// value returned for Locate 16 | /// 17 | public static TimeSpan LocateValue = new TimeSpan(1,1,1,1); 18 | 19 | /// 20 | /// Default constructor 21 | /// 22 | /// 23 | /// 24 | public TimeSpanConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 25 | { 26 | _dataGenerator = dataGenerator; 27 | _constraintHelper = constraintHelper; 28 | } 29 | 30 | /// 31 | /// Generate data for the request, return Constrain.NoValue instead of null 32 | /// 33 | /// data request 34 | /// generated data 35 | public override object GenerateData(DataRequest request) 36 | { 37 | if (!request.Populate) 38 | { 39 | return LocateValue; 40 | } 41 | 42 | var minMax = _constraintHelper.GetMinMax(request, TimeSpan.MinValue, TimeSpan.MaxValue); 43 | 44 | minMax.Min = _constraintHelper.GetValue(request.Constraints, minMax.Min, "min", "minValue"); 45 | minMax.Max = _constraintHelper.GetValue(request.Constraints, minMax.Max, "max", "maxValue"); 46 | 47 | if (minMax.Min.CompareTo(minMax.Max) > 0) 48 | { 49 | minMax.Min = minMax.Max; 50 | } 51 | 52 | return _dataGenerator.NextTimeSpan(minMax.Min, minMax.Max); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/EnumFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using Xunit; 2 | 3 | namespace SimpleFixture.Tests.FixtureTests.Primitives 4 | { 5 | public class EnumFixtureTests 6 | { 7 | public enum TestEnum 8 | { 9 | Value1, 10 | Value2, 11 | Value3 12 | } 13 | 14 | #region Generate Tests 15 | [Fact] 16 | public void Fixture_GenerateEnum_ReturnsPopulatedValue() 17 | { 18 | var fixture = new Fixture(); 19 | 20 | var value = fixture.Generate(); 21 | } 22 | 23 | [Fact] 24 | public void Fixture_GenerateNullableEnum_ReturnsPopulatedValue() 25 | { 26 | var fixture = new Fixture(); 27 | 28 | var nullable = fixture.Generate(); 29 | 30 | Assert.True(nullable.HasValue); 31 | } 32 | 33 | [Fact] 34 | public void Fixture_GenerateEnumArray_ReturnsPopulateValue() 35 | { 36 | var fixture = new Fixture(); 37 | 38 | var array = fixture.Generate(); 39 | 40 | Assert.NotNull(array); 41 | Assert.True(array.Length > 0); 42 | } 43 | #endregion 44 | 45 | #region Locate Tests 46 | [Fact] 47 | public void Fixture_LocateEnum_ReturnLocateValue() 48 | { 49 | var fixture = new Fixture(); 50 | 51 | var value = fixture.Locate(); 52 | } 53 | 54 | [Fact] 55 | public void Fixture_LocateNullableEnum_ReturnsLocateValue() 56 | { 57 | var fixture = new Fixture(); 58 | 59 | var nullable = fixture.Locate(); 60 | 61 | Assert.True(nullable.HasValue); 62 | } 63 | 64 | [Fact] 65 | public void Fixture_LocateBoolArray_ReturnsEmptyArray() 66 | { 67 | var fixture = new Fixture(); 68 | 69 | var array = fixture.Locate(); 70 | 71 | Assert.NotNull(array); 72 | Assert.True(array.Length == 0); 73 | } 74 | #endregion 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/SimpleFixture.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 2.0.0 5 | net6.0 6 | SimpleFixture.Tests 7 | SimpleFixture.Tests 8 | true 9 | false 10 | false 11 | false 12 | full 13 | false 14 | 15 | 16 | 17 | 18 | 19 | all 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/BoolFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Conventions; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.Primitives 5 | { 6 | public class BoolFixtureTests 7 | { 8 | #region Generate Tests 9 | [Fact] 10 | public void Fixture_GenerateBool_ReturnsPopulatedValue() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var value = fixture.Generate(); 15 | } 16 | 17 | [Fact] 18 | public void Fixture_GenerateNullableBool_ReturnsPopulatedValue() 19 | { 20 | var fixture = new Fixture(); 21 | 22 | var nullable = fixture.Generate(); 23 | 24 | Assert.True(nullable.HasValue); 25 | } 26 | 27 | [Fact] 28 | public void Fixture_GenerateBoolArray_ReturnsPopulateValue() 29 | { 30 | var fixture = new Fixture(); 31 | 32 | var array = fixture.Generate(); 33 | 34 | Assert.NotNull(array); 35 | Assert.True(array.Length > 0); 36 | } 37 | #endregion 38 | 39 | #region Locate Tests 40 | [Fact] 41 | public void Fixture_LocateBool_ReturnLocateValue() 42 | { 43 | var fixture = new Fixture(); 44 | 45 | var value = fixture.Locate(); 46 | 47 | Assert.Equal(BoolConvention.LocateValue, value); 48 | } 49 | 50 | [Fact] 51 | public void Fixture_LocateNullableBool_ReturnsLocateValue() 52 | { 53 | var fixture = new Fixture(); 54 | 55 | var nullable = fixture.Locate(); 56 | 57 | Assert.True(nullable.HasValue); 58 | Assert.Equal(BoolConvention.LocateValue, nullable.Value); 59 | } 60 | 61 | [Fact] 62 | public void Fixture_LocateBoolArray_ReturnsEmptyArray() 63 | { 64 | var fixture = new Fixture(); 65 | 66 | var array = fixture.Locate(); 67 | 68 | Assert.NotNull(array); 69 | Assert.True(array.Length == 0); 70 | } 71 | #endregion 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /src/SimpleFixture/IConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleFixture 4 | { 5 | /// 6 | /// class with special values 7 | /// 8 | public class Convention 9 | { 10 | /// 11 | /// Value to return instead of null 12 | /// 13 | public static readonly object NoValue = new object(); 14 | } 15 | 16 | /// 17 | /// Convention priority 18 | /// 19 | public enum ConventionPriority 20 | { 21 | /// 22 | /// First convention to try 23 | /// 24 | First, 25 | /// 26 | /// High priority 27 | /// 28 | High, 29 | 30 | /// 31 | /// Normal priority 32 | /// 33 | Normal, 34 | 35 | /// 36 | /// Low priority 37 | /// 38 | Low, 39 | 40 | /// 41 | /// Last convention tried 42 | /// 43 | Last 44 | } 45 | 46 | /// 47 | /// Event args for when priority changes 48 | /// 49 | public class PriorityChangedEventArgs : EventArgs 50 | { 51 | /// 52 | /// Priority 53 | /// 54 | public ConventionPriority Priority; 55 | } 56 | 57 | /// 58 | /// Convention for satisifying a data request 59 | /// 60 | public interface IConvention 61 | { 62 | /// 63 | /// Prioirity the convention should be looked at 64 | /// 65 | ConventionPriority Priority { get; } 66 | 67 | /// 68 | /// Priorit changed event 69 | /// 70 | event EventHandler PriorityChanged; 71 | 72 | /// 73 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 74 | /// 75 | /// data request 76 | /// generated data value 77 | object GenerateData(DataRequest request); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/SimpleFixture/ExportAs.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFixture 2 | { 3 | /// 4 | /// Fluent helper class 5 | /// 6 | /// 7 | public class ExportAs 8 | { 9 | private readonly Fixture _fixture; 10 | private readonly bool _isSingleton; 11 | private object _singleton; 12 | 13 | /// 14 | /// Default constructor 15 | /// 16 | /// 17 | /// 18 | public ExportAs(Fixture fixture, bool isSingleton) 19 | { 20 | _fixture = fixture; 21 | _isSingleton = isSingleton; 22 | } 23 | 24 | /// 25 | /// As a specific implementation 26 | /// 27 | /// 28 | /// 29 | public ReturnConfiguration As() 30 | { 31 | return _fixture.Return(r => 32 | { 33 | if (_singleton != null) 34 | { 35 | return (TExport)_singleton; 36 | } 37 | 38 | var newRequest = new DataRequest(r.ParentRequest, 39 | _fixture, 40 | typeof(T), 41 | r.DependencyType, 42 | r.RequestName, 43 | r.Populate, 44 | r.Constraints, 45 | r.ExtraInfo); 46 | 47 | var returnValue = (TExport)r.Fixture.Generate(newRequest); 48 | 49 | if (_isSingleton) 50 | { 51 | _singleton = returnValue; 52 | } 53 | 54 | return returnValue; 55 | }); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/SimpleFixture/DI/GContainer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace SimpleFixture.DI 5 | { 6 | /// 7 | /// Simple dependency injection container 8 | /// 9 | public class GContainer : IGContainer 10 | { 11 | private readonly Dictionary _exports = new Dictionary(); 12 | 13 | /// 14 | /// Export a particular type 15 | /// 16 | /// type being exported 17 | /// export function 18 | public void Export(Func exportFunc) 19 | { 20 | _exports[typeof(T)] = exportFunc; 21 | } 22 | 23 | /// 24 | /// Export a type as a singleton 25 | /// 26 | /// type to export 27 | /// export func 28 | public void ExportSingleton(Func exportFunc) 29 | { 30 | var tValue = default (T); 31 | 32 | Func singletonFunc = 33 | g => 34 | { 35 | if (Equals(tValue, default(T))) 36 | { 37 | tValue = exportFunc(g); 38 | } 39 | 40 | return tValue; 41 | }; 42 | 43 | _exports[typeof(T)] = singletonFunc; 44 | } 45 | 46 | /// 47 | /// Locate an instance of T 48 | /// 49 | /// type to locate 50 | /// new instance of T 51 | public T Locate() 52 | { 53 | object objectFunc; 54 | 55 | if (!_exports.TryGetValue(typeof(T), out objectFunc)) 56 | { 57 | throw new Exception("Could not locate type: " + typeof(T).FullName); 58 | } 59 | 60 | var exportFunc = objectFunc as Func; 61 | 62 | if (exportFunc == null) 63 | { 64 | throw new Exception("Could not cast func to proper type: " + objectFunc.GetType()); 65 | } 66 | 67 | return exportFunc(this); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/SimpleFixture/IFixtureConfiguration.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.DI; 2 | 3 | namespace SimpleFixture 4 | { 5 | /// 6 | /// 7 | /// 8 | public enum CircularReferenceHandlingAlgorithm 9 | { 10 | /// 11 | /// Throws an exception when a max depth is reached, this is the original algorithm 12 | /// 13 | MaxDepth, 14 | 15 | /// 16 | /// Omit circular properties, skips properties that are circular and returns empty collections when circular 17 | /// 18 | OmitCircularReferences, 19 | 20 | /// 21 | /// Autowire circular references, if a parent in the object graph can be used it will be 22 | /// 23 | AutoWire, 24 | } 25 | 26 | /// 27 | /// Configuration interface for SimpleFixture. Only implement this interface if you want to change the internal worksings of SimpleFixture 28 | /// 29 | public interface IFixtureConfiguration : IGContainer 30 | { 31 | /// 32 | /// Use default conventions for primitive types as well as string 33 | /// 34 | bool UseDefaultConventions { get; } 35 | 36 | /// 37 | /// Use conventions that try and assign values based on parameter or property names 38 | /// 39 | bool UseNamedConventions { get; } 40 | 41 | /// 42 | /// If set this will indicate how many items to generate for IEnumerables and other colletions. 43 | /// If null then the convention is free to do what it wants 44 | /// 45 | int? ItemCount { get; } 46 | 47 | /// 48 | /// Populate public properties 49 | /// 50 | bool PopulateProperties { get; } 51 | 52 | /// 53 | /// Populate public fields 54 | /// 55 | bool PopulateFields { get; } 56 | 57 | /// 58 | /// How to handle circular references 59 | /// 60 | CircularReferenceHandlingAlgorithm CircularReferenceHandling { get; } 61 | 62 | /// 63 | /// Use non pubic constructors, false by default 64 | /// 65 | bool UseNonPublicConstructors { get; } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/SimpleFixture.Moq/SimpleFixture.Moq.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Ian Johnson 5 | netstandard2.0;net6.0 6 | netstandard2.0; 7 | true 8 | SimpleFixture.Moq 9 | SimpleFixture.Moq 10 | Test;Fixture;Data;Generation 11 | https://github.com/ipjohnson/SimpleFixture/wiki/Release-Notes 12 | https://github.com/ipjohnson/SimpleFixture 13 | https://github.com/ipjohnson/SimpleFixture/blob/master/License.md 14 | git 15 | https://github.com/ipjohnson/SimpleFixture 16 | false 17 | false 18 | false 19 | full 20 | True 21 | true 22 | ..\SimpleFixture.snk 23 | 24 | 25 | 26 | 27 | true 28 | true 29 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 1701;1702;1591 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/BehaviorTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Tests.Classes; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests 5 | { 6 | public class BehaviorTests 7 | { 8 | #region Behavior Locate Tests 9 | [Fact] 10 | public void Fixture_AddBehavior_AppliesToLocateInt() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var behaviorCalled = 0; 15 | 16 | fixture.Behavior.Add((r, o) => { behaviorCalled++; return o; }); 17 | 18 | var value = fixture.Locate(); 19 | 20 | Assert.Equal(1, behaviorCalled); 21 | } 22 | 23 | [Fact] 24 | public void Fixture_AddBehavior_AppliesToLocateComplex() 25 | { 26 | var fixture = new Fixture(); 27 | 28 | var behaviorCalled = 0; 29 | 30 | fixture.Behavior.Add((r, o) => { behaviorCalled++; return o; }); 31 | 32 | var value = fixture.Locate(); 33 | 34 | Assert.Equal(2, behaviorCalled); 35 | } 36 | 37 | 38 | [Fact] 39 | public void Fixture_AddBehaviorGeneric_AppliesToLocateComplex() 40 | { 41 | var fixture = new Fixture(); 42 | 43 | var behaviorCalled = 0; 44 | 45 | fixture.Behavior.Add((r, o) => { behaviorCalled++; return o; }); 46 | 47 | var value = fixture.Locate(); 48 | 49 | Assert.Equal(1, behaviorCalled); 50 | } 51 | 52 | [Fact] 53 | public void Fixture_BehaviorWhen_AppliesCorrectly() 54 | { 55 | var fixture = new Fixture(); 56 | 57 | var apply = false; 58 | var behaviorCalled = 0; 59 | 60 | fixture.Behavior.Add((r, o) => 61 | { 62 | behaviorCalled++; 63 | return o; 64 | }) 65 | .When((r,o) => apply); 66 | 67 | var instance = fixture.Generate(); 68 | 69 | Assert.Equal(0, behaviorCalled); 70 | 71 | apply = true; 72 | 73 | instance = fixture.Generate(); 74 | 75 | Assert.Equal(1, behaviorCalled); 76 | } 77 | #endregion 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/SimpleFixture.FakeItEasy/SimpleFixture.FakeItEasy.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Ian Johnson 5 | netstandard2.0;net6.0 6 | netstandard2.0; 7 | true 8 | SimpleFixture.FakeItEasy 9 | SimpleFixture.FakeItEasy 10 | Test;Fixture;Data;Generation 11 | https://github.com/ipjohnson/SimpleFixture/wiki/Release-Notes 12 | https://github.com/ipjohnson/SimpleFixture 13 | https://github.com/ipjohnson/SimpleFixture/blob/master/License.md 14 | git 15 | https://github.com/ipjohnson/SimpleFixture 16 | false 17 | false 18 | false 19 | full 20 | True 21 | true 22 | ..\SimpleFixture.snk 23 | 24 | 25 | 26 | 27 | true 28 | true 29 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 1701;1702;1591 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/SimpleFixture.NSubstitute/SimpleFixture.NSubstitute.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Ian Johnson 5 | netstandard2.0;net6.0 6 | netstandard2.0; 7 | true 8 | SimpleFixture.NSubstitute 9 | SimpleFixture.NSubstitute 10 | Test;Fixture;Data;Generation 11 | https://github.com/ipjohnson/SimpleFixture/wiki/Release-Notes 12 | https://github.com/ipjohnson/SimpleFixture 13 | https://github.com/ipjohnson/SimpleFixture/blob/master/License.md 14 | git 15 | https://github.com/ipjohnson/SimpleFixture 16 | false 17 | false 18 | false 19 | full 20 | True 21 | true 22 | ..\SimpleFixture.snk 23 | 24 | 25 | 26 | 27 | true 28 | true 29 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 1701;1702;1591 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/ReadOnlyCollectionConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Linq; 5 | using System.Reflection; 6 | 7 | namespace SimpleFixture.Conventions 8 | { 9 | /// 10 | /// Convention for creating readonly collection 11 | /// 12 | public class ReadOnlyCollectionConvention : ITypedConvention 13 | { 14 | /// 15 | /// Prioirity the convention should be looked at 16 | /// 17 | public ConventionPriority Priority => ConventionPriority.Last; 18 | 19 | /// 20 | /// Priorit changed event 21 | /// 22 | public event EventHandler PriorityChanged; 23 | 24 | /// 25 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 26 | /// 27 | /// data request 28 | /// generated data value 29 | public object GenerateData(DataRequest request) 30 | { 31 | if (request.RequestedType.IsConstructedGenericType) 32 | { 33 | var methodInfo = 34 | GetType().GetTypeInfo().DeclaredMethods.First(m => m.Name == "GetReadOnlyList"); 35 | 36 | var closedMethod = methodInfo.MakeGenericMethod(request.RequestedType.GenericTypeArguments); 37 | 38 | return closedMethod.Invoke(this, new object[] { request }); 39 | } 40 | 41 | return Convention.NoValue; 42 | } 43 | 44 | /// 45 | /// Types the convention supports 46 | /// 47 | public IEnumerable SupportedTypes 48 | { 49 | get 50 | { 51 | yield return typeof(IReadOnlyCollection<>); 52 | yield return typeof(IReadOnlyList<>); 53 | yield return typeof(ReadOnlyCollection<>); 54 | } 55 | } 56 | 57 | private object GetReadOnlyList(DataRequest request) 58 | { 59 | var newRequest = new DataRequest(request, typeof(List)); 60 | 61 | var returnValue = newRequest.Fixture.Generate(newRequest) as List; 62 | 63 | return new ReadOnlyCollection(returnValue); 64 | } 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/SimpleFixture.xUnit/SimpleFixture.xUnit.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Ian Johnson 5 | netstandard2.0;net6.0 6 | netstandard2.0; 7 | true 8 | SimpleFixture.xUnit 9 | SimpleFixture.xUnit 10 | Test;Fixture;Data;Generation 11 | https://github.com/ipjohnson/SimpleFixture/wiki/Release-Notes 12 | https://github.com/ipjohnson/SimpleFixture 13 | https://github.com/ipjohnson/SimpleFixture/blob/master/License.md 14 | git 15 | https://github.com/ipjohnson/SimpleFixture 16 | false 17 | false 18 | false 19 | full 20 | True 21 | True 22 | true 23 | ..\SimpleFixture.snk 24 | 25 | 26 | 27 | 28 | true 29 | true 30 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 1701;1702;1591 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Complex/ReadOnlyCollectionTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Collections.ObjectModel; 3 | using System.Linq; 4 | using Xunit; 5 | 6 | namespace SimpleFixture.Tests.FixtureTests.Complex 7 | { 8 | public class ReadOnlyCollectionTests 9 | { 10 | #region Generate Tests 11 | [Fact] 12 | public void Fixture_GenerateIReadOnlyCollectionInt_ReturnsPopulatedValue() 13 | { 14 | var fixture = new Fixture(); 15 | 16 | var enumerable = fixture.Generate>(); 17 | 18 | Assert.NotNull(enumerable); 19 | Assert.True(enumerable.Any()); 20 | } 21 | 22 | [Fact] 23 | public void Fixture_GenerateIReadOnlyListInt_ReturnsPopulatedValue() 24 | { 25 | var fixture = new Fixture(); 26 | 27 | var enumerable = fixture.Generate>(); 28 | 29 | Assert.NotNull(enumerable); 30 | Assert.True(enumerable.Any()); 31 | } 32 | 33 | [Fact] 34 | public void Fixture_GenerateReadOnlyCollectionInt_ReturnsPopulatedValue() 35 | { 36 | var fixture = new Fixture(); 37 | 38 | var list = fixture.Generate>(); 39 | 40 | Assert.NotNull(list); 41 | Assert.True(list.Any()); 42 | } 43 | #endregion 44 | 45 | #region Locate Tests 46 | [Fact] 47 | public void Fixture_LocateIReadOnlyCollectionInt_ReturnsPopulatedValue() 48 | { 49 | var fixture = new Fixture(); 50 | 51 | var enumerable = fixture.Locate>(); 52 | 53 | Assert.NotNull(enumerable); 54 | Assert.False(enumerable.Any()); 55 | } 56 | 57 | [Fact] 58 | public void Fixture_LocateIReadOnlyListInt_ReturnsPopulatedValue() 59 | { 60 | var fixture = new Fixture(); 61 | 62 | var enumerable = fixture.Locate>(); 63 | 64 | Assert.NotNull(enumerable); 65 | Assert.False(enumerable.Any()); 66 | } 67 | 68 | [Fact] 69 | public void Fixture_LocateReadOnlyCollectionInt_ReturnsPopulatedValue() 70 | { 71 | var fixture = new Fixture(); 72 | 73 | var list = fixture.Locate>(); 74 | 75 | Assert.NotNull(list); 76 | Assert.False(list.Any()); 77 | } 78 | #endregion 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/SimpleFixture.FakeItEasy/FakeConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using FakeItEasy; 5 | using FakeItEasy.Creation; 6 | using SimpleFixture.Impl; 7 | 8 | namespace SimpleFixture.FakeItEasy 9 | { 10 | public class FakeConvention : IConvention 11 | { 12 | private readonly Dictionary _singletons; 13 | private readonly bool _defaultSingletons = true; 14 | 15 | public FakeConvention(bool fakeSingleton = true) 16 | { 17 | _defaultSingletons = fakeSingleton; 18 | _singletons = new Dictionary(); 19 | } 20 | 21 | public ConventionPriority Priority 22 | { 23 | get { return ConventionPriority.Last; } 24 | } 25 | 26 | public event EventHandler PriorityChanged; 27 | 28 | public object GenerateData(DataRequest request) 29 | { 30 | if (!request.RequestedType.GetTypeInfo().IsInterface && 31 | !request.RequestedType.GetTypeInfo().IsAbstract) 32 | { 33 | return Convention.NoValue; 34 | } 35 | 36 | object returnValue = null; 37 | var helper = request.Fixture.Configuration.Locate(); 38 | 39 | bool? singleton = helper.GetValue(request.Constraints, null, "fakeSingleton"); 40 | 41 | if (!singleton.HasValue) 42 | { 43 | singleton = _defaultSingletons; 44 | } 45 | 46 | if (singleton.Value) 47 | { 48 | if (_singletons.TryGetValue(request.RequestedType, out returnValue)) 49 | { 50 | return returnValue; 51 | } 52 | } 53 | 54 | var method = GetType().GetMethod("GenerateClosedFake", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(request.RequestedType); 55 | 56 | returnValue = method.Invoke(this, new object[] { request, helper }); 57 | 58 | if (singleton.Value) 59 | { 60 | _singletons[request.RequestedType] = returnValue; 61 | } 62 | 63 | return returnValue; 64 | } 65 | 66 | private T GenerateClosedFake(DataRequest request, IConstraintHelper helper) where T : class 67 | { 68 | Action> options = helper.GetValue>>(request.Constraints, null, "builderOptions"); 69 | 70 | return options != null ? A.Fake(options) : A.Fake(); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/DateTimeConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SimpleFixture.Impl; 3 | 4 | namespace SimpleFixture.Conventions 5 | { 6 | /// 7 | /// Convention for creating datetime 8 | /// 9 | public class DateTimeConvention : SimpleTypeConvention 10 | { 11 | private readonly IRandomDataGeneratorService _dataGenerator; 12 | private readonly IConstraintHelper _helper; 13 | 14 | /// 15 | /// Value returned for locate 16 | /// 17 | public static DateTime LocateValue = new DateTime(1970,1,1); 18 | 19 | /// 20 | /// Default constructor 21 | /// 22 | /// 23 | /// 24 | public DateTimeConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 25 | { 26 | _dataGenerator = dataGenerator; 27 | _helper = constraintHelper; 28 | } 29 | 30 | /// 31 | /// Generate data for the request, return Constrain.NoValue instead of null 32 | /// 33 | /// data request 34 | /// generated data 35 | public override object GenerateData(DataRequest request) 36 | { 37 | if (!request.Populate) 38 | { 39 | return LocateValue; 40 | } 41 | 42 | var min = _helper.GetValue(request.Constraints, null, "min", "minDate"); 43 | var max = _helper.GetValue(request.Constraints, null, "max", "maxDate"); 44 | 45 | if (!min.HasValue) 46 | { 47 | if (!max.HasValue) 48 | { 49 | min = DateTime.Today.AddYears(-5); 50 | max = min.Value.AddYears(10); 51 | } 52 | else 53 | { 54 | min = max.Value.AddYears(-100); 55 | } 56 | } 57 | else if(!max.HasValue) 58 | { 59 | max = min.Value.AddYears(100); 60 | } 61 | 62 | if (min.Value.CompareTo(max.Value) > 0) 63 | { 64 | min = max; 65 | } 66 | 67 | var minMax = _helper.GetMinMax(request, min.Value, max.Value); 68 | 69 | var timeSpan = minMax.Max.Subtract(minMax.Min); 70 | 71 | return minMax.Min.AddSeconds(_dataGenerator.NextDouble(0, timeSpan.TotalSeconds)); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/StringFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using SimpleFixture.Tests.Classes; 3 | using System.Linq; 4 | using SimpleFixture.Conventions; 5 | using SimpleFixture.Impl; 6 | using Xunit; 7 | 8 | namespace SimpleFixture.Tests.FixtureTests.Primitives 9 | { 10 | public class StringFixtureTests 11 | { 12 | [Fact] 13 | public void Fixture_RangedClass_StringCorrectLength() 14 | { 15 | var fixture = new Fixture(); 16 | 17 | var ranged = fixture.Generate(); 18 | 19 | ranged.TestString.Length.Should().BeInRange(50, 100); 20 | ranged.FirstName.All(char.IsLetter).Should().BeTrue(); 21 | } 22 | 23 | [Fact] 24 | public void Fixture_Locate_String() 25 | { 26 | var fixture = new Fixture(); 27 | 28 | var value = fixture.Locate(); 29 | 30 | Assert.Equal(StringConvention.LocateValue, value); 31 | } 32 | 33 | [Fact] 34 | public void Generate_String_With_Prefix() 35 | { 36 | var fixture = new Fixture(); 37 | 38 | var stringValue = fixture.Generate(constraints: new { preFix = "Hello" }); 39 | 40 | Assert.StartsWith("Hello",stringValue); 41 | } 42 | 43 | [Fact] 44 | public void Generate_String_With_Postfix() 45 | { 46 | var fixture = new Fixture(); 47 | 48 | var stringValue = fixture.Generate(constraints: new { postFix = "World" }); 49 | 50 | Assert.EndsWith("World", stringValue); 51 | } 52 | 53 | [Fact] 54 | public void Generate_String_Min_Greater_Than_Max() 55 | { 56 | var fixture = new Fixture(); 57 | 58 | var stringValue = fixture.Generate(constraints: new { min = 11, max = 10 }); 59 | 60 | Assert.Equal(10, stringValue.Length); 61 | } 62 | 63 | [Fact] 64 | public void Generate_String_Alpha() 65 | { 66 | var fixture = new Fixture(); 67 | 68 | var stringValue = fixture.Generate(constraints: new { stringType = StringType.Alpha }); 69 | 70 | Assert.True(stringValue.All(char.IsLetter)); 71 | } 72 | 73 | [Fact] 74 | public void Generate_String_LoremIpsum() 75 | { 76 | var fixture = new Fixture(); 77 | 78 | var stringValue = fixture.Generate(constraints: new { stringType = StringType.LoremIpsum }); 79 | 80 | Assert.StartsWith(stringValue, RandomDataGeneratorService.LoremIpsum); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/StandardFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using SimpleFixture.Tests.Classes; 4 | using Xunit; 5 | 6 | namespace SimpleFixture.Tests.FixtureTests 7 | { 8 | public class StandardFixtureTests 9 | { 10 | public class PrivateClass 11 | { 12 | private PrivateClass() 13 | { 14 | 15 | } 16 | } 17 | 18 | [Fact] 19 | public void Fixture_Throws_Exception_When_No_Constructor_Found() 20 | { 21 | var fixture = new Fixture(); 22 | 23 | Assert.Throws(() => fixture.Generate()); 24 | } 25 | 26 | [Fact] 27 | public void Fixture_UseNonPublicConstructor() 28 | { 29 | var fixture = new Fixture(new DefaultFixtureConfiguration { UseNonPublicConstructors = true }); 30 | 31 | var instance = fixture.Generate(); 32 | 33 | Assert.NotNull(instance); 34 | Assert.IsType(instance); 35 | } 36 | 37 | public struct MyStruct 38 | { 39 | public int IntValue { get; set; } 40 | } 41 | 42 | [Fact] 43 | public void Fixture_Creates_Value_Type() 44 | { 45 | var fixture = new Fixture(); 46 | 47 | var instance = fixture.Generate(); 48 | 49 | Assert.NotNull(instance); 50 | Assert.NotEqual(0, instance.IntValue); 51 | } 52 | 53 | [Fact] 54 | public void Fixture_Null_Reference_Test() 55 | { 56 | var fixture = new Fixture(); 57 | 58 | Assert.Throws(() => fixture.Generate(null)); 59 | Assert.Throws(() => fixture.Generate((Type)null)); 60 | Assert.Throws(() => fixture.Locate((Type)null)); 61 | Assert.Throws(() => fixture.Populate(null)); 62 | Assert.Throws(() => fixture.Return((Func)null)); 63 | Assert.Throws(() => fixture.ReturnIEnumerable((ISomeInterface[])null)); 64 | Assert.Throws(() => fixture.Add((IConvention)null)); 65 | Assert.Throws(() => fixture.Add((IFixtureCustomization)null)); 66 | } 67 | 68 | [Fact] 69 | public void Fixture_IEnumerable() 70 | { 71 | var fixture = new Fixture(); 72 | 73 | foreach (var variable in (IEnumerable)fixture) 74 | { 75 | 76 | } 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/DictionaryConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | 6 | namespace SimpleFixture.Conventions 7 | { 8 | /// 9 | /// Convention for creating dictionary 10 | /// 11 | public class DictionaryConvention : ITypedConvention 12 | { 13 | /// 14 | /// Prioirity the convention should be looked at 15 | /// 16 | public ConventionPriority Priority => ConventionPriority.Low; 17 | 18 | /// 19 | /// Priorit changed event 20 | /// 21 | public event EventHandler PriorityChanged; 22 | 23 | /// 24 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 25 | /// 26 | /// data request 27 | /// generated data value 28 | public object GenerateData(DataRequest request) 29 | { 30 | if (request.RequestedType.IsConstructedGenericType) 31 | { 32 | var openType = request.RequestedType.GetGenericTypeDefinition(); 33 | 34 | if (openType == typeof(IDictionary<,>) || openType == typeof(Dictionary<,>)) 35 | { 36 | var methodInfo = 37 | GetType().GetRuntimeMethods().First(m => m.Name == "GetDictionary"); 38 | 39 | var closedMethod = methodInfo.MakeGenericMethod(request.RequestedType.GenericTypeArguments); 40 | 41 | return closedMethod.Invoke(this, new object[] { request }); 42 | } 43 | } 44 | 45 | return Convention.NoValue; 46 | } 47 | 48 | private object GetDictionary(DataRequest request) 49 | { 50 | var newRequest = new DataRequest(request, typeof(IEnumerable>)); 51 | 52 | var values = 53 | (IEnumerable>)request.Fixture.Generate(newRequest); 54 | 55 | var returnValues = new Dictionary(); 56 | 57 | foreach (var keyValuePair in values) 58 | { 59 | returnValues[keyValuePair.Key] = keyValuePair.Value; 60 | } 61 | 62 | return returnValues; 63 | } 64 | 65 | public IEnumerable SupportedTypes 66 | { 67 | get 68 | { 69 | yield return typeof(Dictionary<,>); 70 | yield return typeof(IDictionary<,>); 71 | } 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/FilteredConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace SimpleFixture.Conventions 5 | { 6 | /// 7 | /// Convention for filtering 8 | /// 9 | /// 10 | public class FilteredConvention : SimpleTypeConvention 11 | { 12 | private readonly List> _filters = new List>(); 13 | private readonly Func _valueFunc; 14 | private ConventionPriority _priority; 15 | 16 | /// 17 | /// Default constructor 18 | /// 19 | /// 20 | /// 21 | public FilteredConvention(Func valueFunc, ConventionPriority priority = ConventionPriority.Low) 22 | { 23 | _valueFunc = valueFunc; 24 | _priority = priority; 25 | } 26 | 27 | /// 28 | /// Priority for the convention, last by default 29 | /// 30 | public override ConventionPriority Priority => _priority; 31 | 32 | /// 33 | /// Add filter to convention 34 | /// 35 | /// 36 | public void AddFilter(Func matchingFilter) 37 | { 38 | _filters.Add(matchingFilter); 39 | 40 | CalculatePriority(); 41 | } 42 | 43 | /// 44 | /// Generate data for the request, return Constrain.NoValue instead of null 45 | /// 46 | /// data request 47 | /// generated data 48 | public override object GenerateData(DataRequest request) 49 | { 50 | foreach (var filter in _filters) 51 | { 52 | if (!filter(request)) 53 | { 54 | return Convention.NoValue; 55 | } 56 | } 57 | 58 | return _valueFunc(request); 59 | } 60 | 61 | private void CalculatePriority() 62 | { 63 | switch (_filters.Count) 64 | { 65 | case 0: 66 | _priority = ConventionPriority.Low; 67 | break; 68 | case 1: 69 | _priority = ConventionPriority.Normal; 70 | break; 71 | default: 72 | _priority = ConventionPriority.High; 73 | break; 74 | } 75 | 76 | RaisePriorityChanged(_priority); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/SimpleFixture.NSubstitute/SubstituteConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using SimpleFixture.Impl; 5 | using NSub = NSubstitute; 6 | 7 | namespace SimpleFixture.NSubstitute 8 | { 9 | /// 10 | /// Convention that uses NSubstitute 11 | /// 12 | public class SubstituteConvention : IConvention 13 | { 14 | private readonly bool _defaultSingleton; 15 | private readonly Dictionary _substituted = new Dictionary(); 16 | 17 | /// 18 | /// Default constructor 19 | /// 20 | /// 21 | public SubstituteConvention(bool defaultSingleton) 22 | { 23 | _defaultSingleton = defaultSingleton; 24 | } 25 | 26 | /// 27 | /// Prioirity the convention should be looked at 28 | /// 29 | public ConventionPriority Priority => ConventionPriority.Last; 30 | 31 | /// 32 | /// Priorit changed event 33 | /// 34 | public event EventHandler PriorityChanged; 35 | 36 | /// 37 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 38 | /// 39 | /// data request 40 | /// generated data value 41 | public object GenerateData(DataRequest request) 42 | { 43 | if (!(request.RequestedType.GetTypeInfo().IsInterface || 44 | request.RequestedType.GetTypeInfo().IsAbstract)) 45 | { 46 | return Convention.NoValue; 47 | } 48 | 49 | object returnValue = null; 50 | var helper = request.Fixture.Configuration.Locate(); 51 | 52 | var singleton = helper.GetValue(request.Constraints, null, "fakeSingleton"); 53 | 54 | if (!singleton.HasValue) 55 | { 56 | singleton = _defaultSingleton; 57 | } 58 | 59 | if (singleton.Value) 60 | { 61 | if (_substituted.TryGetValue(request.RequestedType, out returnValue)) 62 | { 63 | return returnValue; 64 | } 65 | } 66 | 67 | returnValue = NSub.Substitute.For(new[] { request.RequestedType }, new object[0]); 68 | 69 | if(singleton.Value) 70 | { 71 | _substituted[request.RequestedType] = returnValue; 72 | } 73 | 74 | return returnValue; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/DateTimeOffSetConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using SimpleFixture.Impl; 5 | 6 | namespace SimpleFixture.Conventions 7 | { 8 | /// 9 | /// Convention for creating DateTimeOffset 10 | /// 11 | public class DateTimeOffsetConvention : SimpleTypeConvention 12 | { 13 | private readonly IRandomDataGeneratorService _dataGenerator; 14 | private readonly IConstraintHelper _helper; 15 | 16 | /// 17 | /// Value returned for locate 18 | /// 19 | public static DateTimeOffset LocateValue = new DateTime(1970, 1, 1); 20 | 21 | /// 22 | /// Default constructor 23 | /// 24 | /// 25 | /// 26 | public DateTimeOffsetConvention(IRandomDataGeneratorService dataGenerator, IConstraintHelper constraintHelper) 27 | { 28 | _dataGenerator = dataGenerator; 29 | _helper = constraintHelper; 30 | } 31 | 32 | /// 33 | /// Generate data for the request, return Constrain.NoValue instead of null 34 | /// 35 | /// data request 36 | /// generated data 37 | public override object GenerateData(DataRequest request) 38 | { 39 | if (!request.Populate) 40 | { 41 | return LocateValue; 42 | } 43 | 44 | var min = _helper.GetValue(request.Constraints, null, "min", "minDateTimeOffset"); 45 | var max = _helper.GetValue(request.Constraints, null, "max", "maxDateTimeOffset"); 46 | 47 | if (!min.HasValue) 48 | { 49 | if (!max.HasValue) 50 | { 51 | min = DateTimeOffset.Now.AddYears(-5); 52 | max = min.Value.AddYears(10); 53 | } 54 | else 55 | { 56 | min = max.Value.AddYears(-100); 57 | } 58 | } 59 | else if (!max.HasValue) 60 | { 61 | max = min.Value.AddYears(100); 62 | } 63 | 64 | if (min.Value.CompareTo(max.Value) > 0) 65 | { 66 | min = max; 67 | } 68 | 69 | var minMax = _helper.GetMinMax(request, min.Value, max.Value); 70 | 71 | var timeSpan = minMax.Max.Subtract(minMax.Min); 72 | 73 | return minMax.Min.AddSeconds(_dataGenerator.NextDouble(0, timeSpan.TotalSeconds)); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/ConstructorSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | 6 | namespace SimpleFixture.Impl 7 | { 8 | public interface IConstructorSelector 9 | { 10 | ConstructorInfo SelectConstructor(DataRequest request, Type type); 11 | } 12 | 13 | public class ConstructorSelector : IConstructorSelector 14 | { 15 | 16 | public ConstructorInfo SelectConstructor(DataRequest request, Type type) 17 | { 18 | var constructors = new List(); 19 | 20 | var maxParameters = -1; 21 | 22 | var allConstructors = new List(type.GetTypeInfo() 23 | .DeclaredConstructors 24 | .Where(c => c.IsPublic && !c.IsStatic)); 25 | 26 | var constructor = PickConstructorInfo(allConstructors, maxParameters, constructors); 27 | 28 | if (constructor != null) 29 | { 30 | return constructor; 31 | } 32 | 33 | if (request.Fixture.Configuration.UseNonPublicConstructors) 34 | { 35 | allConstructors.AddRange(type.GetTypeInfo() 36 | .DeclaredConstructors 37 | .Where(c => !c.IsPublic && !c.IsStatic)); 38 | 39 | return PickConstructorInfo(allConstructors, maxParameters, constructors); 40 | } 41 | 42 | return null; 43 | } 44 | 45 | private static ConstructorInfo PickConstructorInfo(List allConstructors, int maxParameters, List constructors) 46 | { 47 | allConstructors.Sort((x, y) => Comparer.Default.Compare(y.GetParameters().Length, 48 | x.GetParameters().Length)); 49 | 50 | 51 | foreach (var info in allConstructors) 52 | { 53 | if (info.GetParameters().Count() < maxParameters) 54 | { 55 | continue; 56 | } 57 | 58 | constructors.Add(info); 59 | maxParameters = info.GetParameters().Count(); 60 | } 61 | 62 | if (constructors.Count == 1) 63 | { 64 | return constructors[0]; 65 | } 66 | 67 | constructors.Sort( 68 | (x, y) => 69 | Comparer.Default.Compare(y.GetParameters().Count(p => !p.ParameterType.GetTypeInfo().IsPrimitive), 70 | x.GetParameters().Count(p => !p.ParameterType.GetTypeInfo().IsPrimitive))); 71 | 72 | return constructors.FirstOrDefault(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Complex/DelegateFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SimpleFixture.Conventions; 3 | using SimpleFixture.Tests.Classes; 4 | using Xunit; 5 | 6 | namespace SimpleFixture.Tests.FixtureTests.Complex 7 | { 8 | public class DelegateFixtureTests 9 | { 10 | #region Generate Tests 11 | [Fact] 12 | public void Fixture_GenerateFuncInt_ReturnsWorkingFunc() 13 | { 14 | var fixture = new Fixture(); 15 | 16 | var func = fixture.Generate>(); 17 | 18 | Assert.NotNull(func); 19 | 20 | var intValue = func(); 21 | } 22 | 23 | [Fact] 24 | public void Fixture_GenerateActionInt_ReturnsWorkingFunc() 25 | { 26 | var fixture = new Fixture(); 27 | 28 | var func = fixture.Generate>(); 29 | 30 | Assert.NotNull(func); 31 | 32 | func(5); 33 | } 34 | #endregion 35 | 36 | #region Locate Tests 37 | [Fact] 38 | public void Fixture_LocateFuncInt_ReturnsWorkingAction() 39 | { 40 | var fixture = new Fixture(); 41 | 42 | var func = fixture.Locate>(); 43 | 44 | Assert.NotNull(func); 45 | 46 | Assert.Equal(IntConvention.LocateValue,func()); 47 | } 48 | 49 | [Fact] 50 | public void Fixture_LocateActionInt_ReturnsWorkingAction() 51 | { 52 | var fixture = new Fixture(); 53 | 54 | var func = fixture.Locate>(); 55 | 56 | Assert.NotNull(func); 57 | 58 | func(5); 59 | } 60 | 61 | public delegate ImportSomeClass ImportSomeClassFunc(string stringValue, int intValue); 62 | 63 | [Fact] 64 | public void Fixture_Locate_Delegate() 65 | { 66 | var fixture = new Fixture(); 67 | 68 | var func = fixture.Generate(); 69 | 70 | var instance = func("Hello", 15); 71 | 72 | Assert.NotNull(instance); 73 | Assert.Equal("Hello", instance.SomeClass.StringValue); 74 | Assert.Equal(15, instance.SomeClass.IntValue); 75 | } 76 | 77 | public delegate ImportSomeClass ImportSomeClass2Func(string stringValue); 78 | 79 | [Fact] 80 | public void Fixture_Locate_Delegate_One_Arg() 81 | { 82 | var fixture = new Fixture(); 83 | 84 | var func = fixture.Generate(constraints: new { intValue = 20}); 85 | 86 | var instance = func("Hello"); 87 | 88 | Assert.NotNull(instance); 89 | Assert.Equal("Hello", instance.SomeClass.StringValue); 90 | Assert.Equal(20, instance.SomeClass.IntValue); 91 | } 92 | 93 | #endregion 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/xUnitTests/AutoDataAttributeTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentAssertions; 3 | using SimpleFixture.Attributes; 4 | using SimpleFixture.Tests.Classes; 5 | //using SimpleFixture.xUnit; 6 | using System.Linq; 7 | using SimpleFixture.Conventions; 8 | using SimpleFixture.xUnit; 9 | using Xunit; 10 | 11 | namespace SimpleFixture.Tests.xUnitTests 12 | { 13 | public class AutoDataAttributeTests 14 | { 15 | [Theory] 16 | [AutoData] 17 | public void AutoData_ProvidesFixture(Fixture fixture) 18 | { 19 | fixture.Should().NotBeNull(); 20 | } 21 | 22 | [Theory] 23 | [AutoData] 24 | public void AutoData_ProvidesData(string someString, int value) 25 | { 26 | someString.Should().Be(StringConvention.LocateValue); 27 | value.Should().Be(SimpleFixture.Conventions.IntConvention.LocateValue); 28 | } 29 | 30 | [Theory] 31 | [AutoData] 32 | public void AutoData_ProvidesGeneratedData([Generate]string firstName,[Generate] int value) 33 | { 34 | firstName.All(Char.IsLetter).Should().BeTrue(); 35 | firstName.Should().NotBe(StringConvention.LocateValue); 36 | } 37 | 38 | [Theory] 39 | [AutoData] 40 | public void AutoData_Freeze(Fixture fixture, [Freeze]int froozen) 41 | { 42 | fixture.Generate().Should().Be(froozen); 43 | } 44 | 45 | [Theory] 46 | [AutoData] 47 | public void AutoData_FreezeValue(Fixture fixture, [Freeze(Value = 8)]int froozen) 48 | { 49 | froozen.Should().Be(8); 50 | fixture.Generate().Should().Be(froozen); 51 | } 52 | 53 | [Theory] 54 | [AutoData] 55 | public void AutoData_LocateData(Fixture fixture, [Locate]int locate) 56 | { 57 | fixture.Locate().Should().Be(locate); 58 | } 59 | 60 | [Theory] 61 | [AutoData] 62 | public void AutoData_LocateValue(Fixture fixture, [Locate(Value = 8)]int locate) 63 | { 64 | locate.Should().Be(8); 65 | } 66 | 67 | [Theory] 68 | [AutoData(8)] 69 | public void AutoData_MixInData(SomeClass someClass, int value) 70 | { 71 | someClass.Should().NotBeNull(); 72 | value.Should().Be(8); 73 | } 74 | 75 | public class SomeClass : ISomeInterface 76 | { 77 | public int SomeIntMethod() 78 | { 79 | return 5; 80 | } 81 | } 82 | 83 | [Theory] 84 | [AutoData(typeof(SomeClass))] 85 | public void AutoData_MixInType(ISomeInterface interfaceInstance) 86 | { 87 | Assert.NotNull(interfaceInstance); 88 | Assert.IsType(interfaceInstance); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/SimpleFixture/Conventions/ComplexConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using SimpleFixture.Impl; 4 | 5 | namespace SimpleFixture.Conventions 6 | { 7 | /// 8 | /// Convention for creating an populating objects 9 | /// 10 | public class ComplexConvention : IConvention 11 | { 12 | private readonly IFixtureConfiguration _configuration; 13 | private readonly ITypeCreator _typeCreator; 14 | private readonly ITypePopulator _typePopulator; 15 | private readonly ICircularReferenceHandler _circularReferenceHandler; 16 | private readonly IModelService _modelService; 17 | 18 | /// 19 | /// Default constructor 20 | /// 21 | /// 22 | public ComplexConvention(IFixtureConfiguration configuration) 23 | { 24 | _configuration = configuration; 25 | _typeCreator = configuration.Locate(); 26 | _typePopulator = configuration.Locate(); 27 | _circularReferenceHandler = configuration.Locate(); 28 | _modelService = configuration.Locate(); 29 | } 30 | 31 | /// 32 | /// Prioirity the convention should be looked at 33 | /// 34 | public ConventionPriority Priority => ConventionPriority.Last; 35 | 36 | /// 37 | /// Priorit changed event 38 | /// 39 | public event EventHandler PriorityChanged; 40 | 41 | /// 42 | /// Generate data for the request, return Convention.NoValue if the convention has no value to provide 43 | /// 44 | /// data request 45 | /// generated data value 46 | public object GenerateData(DataRequest request) 47 | { 48 | if (_configuration.CircularReferenceHandling == CircularReferenceHandlingAlgorithm.MaxDepth && 49 | request.RequestDepth > 100) 50 | { 51 | return _circularReferenceHandler.HandleCircularReference(request); 52 | } 53 | 54 | if (request.RequestedType.GetTypeInfo().IsInterface || 55 | request.RequestedType.GetTypeInfo().IsAbstract) 56 | { 57 | return Convention.NoValue; 58 | } 59 | 60 | var model = _modelService.GetModel(request.RequestedType); 61 | 62 | var returnValue = _typeCreator.CreateType(request, model); 63 | 64 | request.Instance = returnValue; 65 | 66 | if (request.Populate) 67 | { 68 | _typePopulator.Populate(returnValue, request, model); 69 | } 70 | 71 | model.Apply(returnValue); 72 | 73 | return returnValue; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Complex/ListTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using Xunit; 4 | 5 | namespace SimpleFixture.Tests.FixtureTests 6 | { 7 | public class ListTests 8 | { 9 | #region Generate Tests 10 | [Fact] 11 | public void Fixture_GenerateIEnumerableInt_ReturnsPopulatedValue() 12 | { 13 | var fixture = new Fixture(); 14 | 15 | var enumerable = fixture.Generate>(); 16 | 17 | Assert.NotNull(enumerable); 18 | Assert.True(enumerable.Any()); 19 | } 20 | 21 | [Fact] 22 | public void Fixture_GenerateICollectionInt_ReturnsPopulatedValue() 23 | { 24 | var fixture = new Fixture(); 25 | 26 | var collection = fixture.Generate>(); 27 | 28 | Assert.NotNull(collection); 29 | Assert.True(collection.Any()); 30 | } 31 | 32 | [Fact] 33 | public void Fixture_GenerateIListInt_ReturnsPopulatedValue() 34 | { 35 | var fixture = new Fixture(); 36 | 37 | var list = fixture.Generate>(); 38 | 39 | Assert.NotNull(list); 40 | Assert.True(list.Any()); 41 | } 42 | 43 | [Fact] 44 | public void Fixture_GenerateListInt_ReturnsPopulatedValue() 45 | { 46 | var fixture = new Fixture(); 47 | 48 | var list = fixture.Generate>(); 49 | 50 | Assert.NotNull(list); 51 | Assert.True(list.Any()); 52 | } 53 | #endregion 54 | 55 | #region Locate Tests 56 | [Fact] 57 | public void Fixture_LocateIEnumerableInt_ReturnsPopulatedValue() 58 | { 59 | var fixture = new Fixture(); 60 | 61 | var enumerable = fixture.Locate>(); 62 | 63 | Assert.NotNull(enumerable); 64 | Assert.False(enumerable.Any()); 65 | } 66 | 67 | [Fact] 68 | public void Fixture_LocateICollectionInt_ReturnsPopulatedValue() 69 | { 70 | var fixture = new Fixture(); 71 | 72 | var collection = fixture.Locate>(); 73 | 74 | Assert.NotNull(collection); 75 | Assert.False(collection.Any()); 76 | } 77 | 78 | [Fact] 79 | public void Fixture_LocateIListInt_ReturnsPopulatedValue() 80 | { 81 | var fixture = new Fixture(); 82 | 83 | var list = fixture.Locate>(); 84 | 85 | Assert.NotNull(list); 86 | Assert.False(list.Any()); 87 | } 88 | 89 | [Fact] 90 | public void Fixture_LocateListInt_ReturnsPopulatedValue() 91 | { 92 | var fixture = new Fixture(); 93 | 94 | var list = fixture.Locate>(); 95 | 96 | Assert.NotNull(list); 97 | Assert.False(list.Any()); 98 | } 99 | #endregion 100 | 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/NamedConventions/AddressStringConventionTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.NamedConventions 5 | { 6 | public class AddressStringConventionTests 7 | { 8 | [Theory, InlineData("PostalCode"), InlineData("ZipCode"),InlineData("Zip")] 9 | public void Fixture_GeneratePostalCode_ReturnsGoodValue(string name) 10 | { 11 | var fixture = new Fixture(); 12 | 13 | var value = fixture.Generate(name); 14 | 15 | Assert.NotNull(value); 16 | Assert.True(value.All(char.IsDigit)); 17 | } 18 | 19 | [Theory] 20 | [InlineData("AddressLine")] 21 | [InlineData("AddressLine1")] 22 | [InlineData("AddressLineOne")] 23 | public void Fixture_GenerateAddressLineOne_ReturnsGoodValue(string name) 24 | { 25 | var fixture = new Fixture(); 26 | 27 | var value = fixture.Generate(name); 28 | 29 | Assert.NotNull(value); 30 | Assert.True(value.All(c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c))); 31 | } 32 | 33 | [Theory] 34 | [InlineData("AddressLineTwo")] 35 | [InlineData("AddressLine2")] 36 | public void Fixture_GenerateAddressLineTwo_ReturnsGoodValue(string name) 37 | { 38 | var fixture = new Fixture(); 39 | 40 | var value = fixture.Generate(name); 41 | 42 | Assert.NotNull(value); 43 | Assert.True(value.All(c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c))); 44 | } 45 | 46 | [Fact] 47 | public void Fixture_GenerateState_ReturnsGoodValue() 48 | { 49 | var fixture = new Fixture(); 50 | 51 | var value = fixture.Generate("State"); 52 | 53 | Assert.NotNull(value); 54 | Assert.True(value.All(c => char.IsLetter(c) || char.IsWhiteSpace(c))); 55 | } 56 | 57 | [Fact] 58 | public void Fixture_GenerateStateAbbreviation_ReturnsGoodValue() 59 | { 60 | var fixture = new Fixture(); 61 | 62 | var value = fixture.Generate("StateAbbreviation"); 63 | 64 | Assert.NotNull(value); 65 | Assert.Equal(2, value.Length); 66 | Assert.True(value.All(char.IsLetter)); 67 | } 68 | 69 | [Fact] 70 | public void Fixture_GenerateCountry_ReturnsGoodValue() 71 | { 72 | var fixture = new Fixture(); 73 | 74 | var value = fixture.Generate("Country"); 75 | 76 | Assert.NotNull(value); 77 | Assert.True(value.All(c => char.IsLetter(c) || char.IsWhiteSpace(c))); 78 | } 79 | [Fact] 80 | public void Fixture_GenerateCity_ReturnsGoodValue() 81 | { 82 | var fixture = new Fixture(); 83 | 84 | var value = fixture.Generate("City"); 85 | 86 | Assert.NotNull(value); 87 | Assert.True(value.All(c => char.IsLetter(c) || char.IsWhiteSpace(c))); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # This .gitignore file was automatically created by Microsoft(R) Visual Studio. 3 | ################################################################################ 4 | 5 | /Output/Tests 6 | *.suo 7 | /src/packages 8 | /src/SimpleFixture/bin/Debug 9 | /src/SimpleFixture/obj/Debug 10 | /src/SimpleFixture.FakeItEasy/bin/Debug 11 | /src/SimpleFixture.FakeItEasy/obj/Debug 12 | /src/SimpleFixture.Moq/bin/Debug 13 | /src/SimpleFixture.Moq/obj/Debug 14 | /src/SimpleFixture.NSubstitute/bin/Debug 15 | /src/SimpleFixture.NSubstitute/obj/Debug 16 | /src/SimpleFixture.xUnit/bin/Debug 17 | /src/SimpleFixture.xUnit/obj/Debug 18 | /tests/SimpleFixture.Tests/obj/Debug 19 | /tests/SimpleFixture.Tests/bin/Debug 20 | 21 | /.vs 22 | /src/SimpleFixture.xUnit/project.lock.json 23 | /src/SimpleFixture.xUnit 24 | /src/SimpleFixture.NSubstitute/project.lock.json 25 | /src/SimpleFixture.NSubstitute 26 | /src/SimpleFixture.Moq/project.lock.json 27 | /src/SimpleFixture.Moq 28 | /src/SimpleFixture.FakeItEasy/project.lock.json 29 | /src/SimpleFixture.FakeItEasy 30 | /src/SimpleFixture/project.lock.json 31 | /Backup 32 | /src/SimpleFixture/obj 33 | /src/SimpleFixture/obj/SimpleFixture.csproj.nuget.g.targets 34 | /src/SimpleFixture/obj/SimpleFixture.csproj.nuget.g.props 35 | /src/SimpleFixture/obj/project.assets.json 36 | /tests/SimpleFixture.Tests/obj/SimpleFixture.Tests.csproj.nuget.g.targets 37 | /tests/SimpleFixture.Tests/obj/SimpleFixture.Tests.csproj.nuget.g.props 38 | /tests/SimpleFixture.Tests/obj/project.assets.json 39 | /src/SimpleFixture.MSTest/bin/Debug/uap10.0 40 | /src/SimpleFixture.MSTest/bin/Debug/netstandard1.0 41 | /src/SimpleFixture.MSTest/bin/Debug/net45 42 | /src/SimpleFixture.MSTest/obj/Debug/net45 43 | /src/SimpleFixture.MSTest/obj/Debug/uap10.0 44 | /src/SimpleFixture.MSTest/obj 45 | /tests/SimpleFixture.Tests.MSTest/bin/Debug/netcoreapp1.1 46 | /tests/SimpleFixture.Tests/obj 47 | /tests/SimpleFixture.Tests.MSTest/obj/SimpleFixture.Tests.MSTest.csproj.nuget.g.targets 48 | /tests/SimpleFixture.Tests.MSTest/obj/SimpleFixture.Tests.MSTest.csproj.nuget.g.props 49 | /tests/SimpleFixture.Tests.MSTest/obj/SimpleFixture.Tests.MSTest.csproj.nuget.cache 50 | /tests/SimpleFixture.Tests.MSTest/obj/project.assets.json 51 | /tests/SimpleFixture.Tests.MSTest/obj/Debug/netcoreapp1.1/SimpleFixture.Tests.MSTest.Program.cs 52 | /tests/SimpleFixture.Tests.MSTest/obj/Debug/netcoreapp1.1/SimpleFixture.Tests.MSTest.pdb 53 | /tests/SimpleFixture.Tests.MSTest/obj/Debug/netcoreapp1.1/SimpleFixture.Tests.MSTest.dll 54 | /tests/SimpleFixture.Tests.MSTest/obj/Debug/netcoreapp1.1/SimpleFixture.Tests.MSTest.csprojResolveAssemblyReference.cache 55 | /tests/SimpleFixture.Tests.MSTest/obj/Debug/netcoreapp1.1/SimpleFixture.Tests.MSTest.csproj.FileListAbsolute.txt 56 | /tests/SimpleFixture.Tests.MSTest/obj/Debug/netcoreapp1.1/SimpleFixture.Tests.MSTest.csproj.CoreCompileInputs.cache 57 | /tests/SimpleFixture.Tests.MSTest/obj/Debug/netcoreapp1.1/SimpleFixture.Tests.MSTest.csproj.CopyComplete 58 | /tests/SimpleFixture.Tests.MSTest/obj/Debug/netcoreapp1.1/SimpleFixture.Tests.MSTest.AssemblyInfoInputs.cache 59 | /tests/SimpleFixture.Tests.MSTest/obj/Debug/netcoreapp1.1 60 | /tests/SimpleFixture.Tests.MSTest/obj 61 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/TypedConventions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace SimpleFixture.Impl 5 | { 6 | /// 7 | /// Convention that handles ITypeConvention 8 | /// 9 | public class TypedConventions : IConvention 10 | { 11 | private Dictionary _typedConventions; 12 | private readonly IFixtureConfiguration _configuration; 13 | 14 | /// 15 | /// Default constructor 16 | /// 17 | /// 18 | /// 19 | public TypedConventions(IFixtureConfiguration configuration, ConventionPriority priority = ConventionPriority.High) 20 | { 21 | _configuration = configuration; 22 | Priority = priority; 23 | } 24 | 25 | /// 26 | /// Add typed convention 27 | /// 28 | /// convention 29 | public void AddConvention(ITypedConvention typedConvention) 30 | { 31 | if (_typedConventions == null) 32 | { 33 | _typedConventions = new Dictionary(); 34 | } 35 | 36 | IConventionList conventionList; 37 | 38 | foreach (var supportedType in typedConvention.SupportedTypes) 39 | { 40 | if (!_typedConventions.TryGetValue(supportedType, out conventionList)) 41 | { 42 | conventionList = _configuration.Locate(); 43 | _typedConventions[supportedType] = conventionList; 44 | } 45 | 46 | conventionList.AddConvention(typedConvention); 47 | } 48 | } 49 | 50 | /// 51 | /// Priority for convention 52 | /// 53 | public ConventionPriority Priority { get; protected set; } 54 | 55 | /// 56 | /// Priorit changed event 57 | /// 58 | public event EventHandler PriorityChanged; 59 | 60 | /// 61 | /// Generate data for the request 62 | /// 63 | /// data request 64 | /// 65 | public object GenerateData(DataRequest request) 66 | { 67 | if (_typedConventions == null) 68 | { 69 | return Convention.NoValue; 70 | } 71 | 72 | object returnValue = null; 73 | IConventionList conventionList; 74 | 75 | if (_typedConventions.TryGetValue(request.RequestedType, out conventionList)) 76 | { 77 | conventionList.TryGetValue(request, out returnValue); 78 | } 79 | 80 | if (returnValue == null && request.RequestedType.IsConstructedGenericType) 81 | { 82 | var openType = request.RequestedType.GetGenericTypeDefinition(); 83 | 84 | if (_typedConventions.TryGetValue(openType, out conventionList)) 85 | { 86 | conventionList.TryGetValue(request, out returnValue); 87 | } 88 | } 89 | 90 | return returnValue ?? Convention.NoValue; 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/MockTests/FakeTests.cs: -------------------------------------------------------------------------------- 1 | using FakeItEasy; 2 | using SimpleFixture.FakeItEasy; 3 | using SimpleFixture.Tests.Classes; 4 | using Xunit; 5 | 6 | namespace SimpleFixture.Tests.MockTests 7 | { 8 | public class FakeTests 9 | { 10 | [Fact] 11 | public void FakeFixture_LocateTypeWithInterface_ReturnsInstance() 12 | { 13 | var fixture = new FakeFixture(); 14 | 15 | var instance = fixture.Locate(); 16 | 17 | Assert.NotNull(instance); 18 | Assert.Equal(0, instance.SomeValue); 19 | } 20 | 21 | [Fact] 22 | public void FakeFixture_FakeAndLocateTypeWithInterface_ReturnsInstance() 23 | { 24 | var fixture = new FakeFixture(); 25 | 26 | fixture.Fake(f => A.CallTo(() => f.SomeIntMethod()).Returns(10)); 27 | 28 | var instance = fixture.Locate(); 29 | 30 | Assert.NotNull(instance); 31 | Assert.Equal(10, instance.SomeValue); 32 | } 33 | 34 | [Fact] 35 | public void FakeFixture_DefaultSingleton_ReturnsCorrectInstance() 36 | { 37 | var fixture = new FakeFixture(defaultSingleton: true); 38 | 39 | var instance1 = fixture.Fake(x => A.CallTo(() => x.SomeIntMethod()).Returns(10), singleton: false); 40 | 41 | var instance2 = fixture.Fake(x => A.CallTo(() => x.SomeIntMethod()).Returns(15), singleton: false); 42 | 43 | fixture.Fake(x => A.CallTo(() => x.SomeIntMethod()).Returns(20)); 44 | 45 | Assert.Equal(20, fixture.Locate().SomeIntMethod()); 46 | 47 | Assert.Equal(10, instance1.SomeIntMethod()); 48 | 49 | Assert.Equal(15, instance2.SomeIntMethod()); 50 | 51 | Assert.Equal(20, fixture.Locate().SomeIntMethod()); 52 | } 53 | 54 | [Fact] 55 | public void FakeFixture_DefaultSingletonFalse_ReturnsCorrectInstance() 56 | { 57 | var fixture = new FakeFixture(defaultSingleton: false); 58 | 59 | var instance1 = fixture.Fake(x => A.CallTo(() => x.SomeIntMethod()).Returns(10)); 60 | 61 | var instance2 = fixture.Fake(x => A.CallTo(() => x.SomeIntMethod()).Returns(15)); 62 | 63 | Assert.Equal(0, fixture.Locate().SomeIntMethod()); 64 | 65 | Assert.Equal(10, instance1.SomeIntMethod()); 66 | 67 | Assert.Equal(15, instance2.SomeIntMethod()); 68 | 69 | Assert.Equal(0, fixture.Locate().SomeIntMethod()); 70 | } 71 | 72 | [Fact] 73 | public void Fakeixture_DefaultSingletonFalseAndSingleton_ReturnsSingleton() 74 | { 75 | var fixture = new FakeFixture(defaultSingleton: false); 76 | 77 | fixture.Fake(x => A.CallTo(() => x.SomeIntMethod()).Returns(20), singleton: true); 78 | 79 | var instance1 = fixture.Fake(x => A.CallTo(() => x.SomeIntMethod()).Returns(10)); 80 | 81 | Assert.Equal(20, fixture.Fake(singleton: true).SomeIntMethod()); 82 | 83 | Assert.Equal(10, instance1.SomeIntMethod()); 84 | 85 | Assert.Equal(20, fixture.Fake(singleton: true).SomeIntMethod()); 86 | 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/ConventionProvider.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using SimpleFixture.Conventions; 3 | using SimpleFixture.Conventions.Named; 4 | 5 | namespace SimpleFixture.Impl 6 | { 7 | public class ConventionProvider : IConventionProvider 8 | { 9 | public IEnumerable ProvideConventions(IFixtureConfiguration configuration) 10 | { 11 | var conventions = new List(); 12 | 13 | if (configuration.UseDefaultConventions) 14 | { 15 | conventions.AddRange(ProvideDefaultConventions(configuration)); 16 | } 17 | 18 | if (configuration.UseNamedConventions) 19 | { 20 | conventions.AddRange(ProvideNamedConventions(configuration)); 21 | } 22 | 23 | return conventions; 24 | } 25 | 26 | private IEnumerable ProvideDefaultConventions(IFixtureConfiguration configuration) 27 | { 28 | var dataGenerator = configuration.Locate(); 29 | var helper = configuration.Locate(); 30 | 31 | yield return new BoolConvention(dataGenerator); 32 | yield return new ByteConvention(dataGenerator, helper); 33 | yield return new CharConvention(dataGenerator, helper); 34 | yield return new DateTimeConvention(dataGenerator, helper); 35 | yield return new DateTimeOffsetConvention(dataGenerator, helper); 36 | yield return new DecimalConvention(dataGenerator, helper); 37 | yield return new DelegateConvention(helper); 38 | yield return new DoubleConvention(dataGenerator, helper); 39 | yield return new EnumConvention(dataGenerator, helper); 40 | yield return new IntConvention(dataGenerator, helper); 41 | yield return new LongConvention(dataGenerator, helper); 42 | yield return new SByteConvention(dataGenerator, helper); 43 | yield return new ShortConvention(dataGenerator, helper); 44 | yield return new StringConvention(dataGenerator, helper); 45 | yield return new TimeSpanConvention(dataGenerator, helper); 46 | yield return new TypeConvention(dataGenerator); 47 | yield return new UIntConvention(dataGenerator, helper); 48 | yield return new ULongConvention(dataGenerator, helper); 49 | yield return new UriConvention(helper); 50 | yield return new UShortConvention(dataGenerator, helper); 51 | 52 | yield return new ComplexConvention(configuration); 53 | 54 | yield return new ArrayConvention(); 55 | yield return new DictionaryConvention(); 56 | yield return new ListConvention(configuration); 57 | yield return new ReadOnlyCollectionConvention(); 58 | yield return new IComparerConvention(); 59 | yield return new IEqualityComparerConvention(); 60 | 61 | yield return new NullableConvention(); 62 | } 63 | 64 | private IEnumerable ProvideNamedConventions(IFixtureConfiguration configuration) 65 | { 66 | var dataGenerator = configuration.Locate(); 67 | var helper = configuration.Locate(); 68 | 69 | yield return new StringNamedConvention(dataGenerator, helper); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/SimpleFixture/Impl/TypePropertySelector.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Reflection; 4 | 5 | namespace SimpleFixture.Impl 6 | { 7 | public interface ITypePropertySelector 8 | { 9 | IEnumerable SelectProperties(object instance, DataRequest request, ComplexModel model); 10 | } 11 | 12 | public class TypePropertySelector : ITypePropertySelector 13 | { 14 | private readonly IFixtureConfiguration _configuration; 15 | private readonly IConstraintHelper _helper; 16 | 17 | public TypePropertySelector(IFixtureConfiguration configuration, IConstraintHelper helper) 18 | { 19 | _configuration = configuration; 20 | _helper = helper; 21 | } 22 | 23 | public virtual IEnumerable SelectProperties(object instance, DataRequest request, ComplexModel model) 24 | { 25 | var skipProperties = new List(); 26 | 27 | var skipPropertiesEnumerable = _helper.GetValue>(request.Constraints, 28 | null, 29 | "_skipProps", 30 | "_skipProperties"); 31 | 32 | if (skipPropertiesEnumerable != null) 33 | { 34 | skipProperties.AddRange(skipPropertiesEnumerable); 35 | } 36 | 37 | var returnProperties = instance.GetType() 38 | .GetRuntimeProperties() 39 | .Where(p => p.CanWrite && 40 | p.SetMethod.IsPublic && 41 | !p.SetMethod.IsStatic && 42 | p.SetMethod.GetParameters().Count() == 1 && 43 | !skipProperties.Contains(p.Name)); 44 | 45 | if(request.ParentRequest != null && 46 | _configuration.CircularReferenceHandling == CircularReferenceHandlingAlgorithm.OmitCircularReferences) 47 | { 48 | returnProperties = CheckForCircularProperties(request, returnProperties); 49 | } 50 | 51 | return returnProperties; 52 | } 53 | 54 | private IEnumerable CheckForCircularProperties(DataRequest request, IEnumerable returnProperties) 55 | { 56 | foreach (var propertyInfo in returnProperties) 57 | { 58 | var propertyInfoTypeInfo = propertyInfo.PropertyType.GetTypeInfo(); 59 | var circular = false; 60 | var currentRequest = request; 61 | 62 | while (currentRequest != null) 63 | { 64 | if (currentRequest.Instance != null && 65 | propertyInfoTypeInfo.IsAssignableFrom(currentRequest.RequestedType.GetTypeInfo())) 66 | { 67 | circular = true; 68 | break; 69 | } 70 | 71 | currentRequest = currentRequest.ParentRequest; 72 | } 73 | 74 | if (!circular) 75 | { 76 | yield return propertyInfo; 77 | } 78 | } 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/SimpleFixture/FromConfiguration.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Impl; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Reflection; 5 | 6 | namespace SimpleFixture 7 | { 8 | public class FromConfiguration 9 | { 10 | private readonly Fixture _fixture; 11 | 12 | public FromConfiguration(Fixture fixture) 13 | { 14 | _fixture = fixture; 15 | } 16 | 17 | public TypeMatchingConfiguration From(IEnumerable types) 18 | { 19 | var convention = new ExportAllByInterfaceConvention(types); 20 | 21 | _fixture.Add(convention); 22 | 23 | return new TypeMatchingConfiguration(convention); 24 | } 25 | 26 | public TypeMatchingConfiguration FromAssembly(Assembly assembly) 27 | { 28 | return From(assembly.ExportedTypes); 29 | } 30 | 31 | public TypeMatchingConfiguration FromAssemblyContaining() 32 | { 33 | return From(typeof(T).GetTypeInfo().Assembly.ExportedTypes); 34 | } 35 | } 36 | 37 | public class TypeMatchingConfiguration 38 | { 39 | private readonly ExportAllByInterfaceConvention _convention; 40 | private readonly List> _filters; 41 | 42 | public TypeMatchingConfiguration(ExportAllByInterfaceConvention convention) 43 | { 44 | _convention = convention; 45 | _convention.TypeFilter = TypeFilter; 46 | _filters = new List>(); 47 | } 48 | 49 | private bool TypeFilter(Type implementationType, Type interfaceType) 50 | { 51 | foreach(var filter in _filters) 52 | { 53 | if(!filter(implementationType,interfaceType)) 54 | { 55 | return false; 56 | } 57 | } 58 | 59 | return true; 60 | } 61 | 62 | public TypeMatchingConfiguration Matching(Func matchingDelegate) 63 | { 64 | _filters.Add(matchingDelegate); 65 | 66 | return this; 67 | } 68 | 69 | public TypeMatchingConfiguration InterfaceMatching() 70 | { 71 | _filters.Add((c, i) => typeof(T).GetTypeInfo().IsAssignableFrom(i.GetTypeInfo())); 72 | 73 | return this; 74 | } 75 | 76 | public TypeMatchingConfiguration InNamespace(string namespaceStr, bool inherit = true) 77 | { 78 | if (!inherit) 79 | { 80 | _filters.Add((c, i) => c.Namespace == namespaceStr); 81 | } 82 | else 83 | { 84 | _filters.Add((c, i) => c.Namespace == namespaceStr || c.Namespace.StartsWith(namespaceStr + ".")); 85 | } 86 | 87 | return this; 88 | } 89 | 90 | public TypeMatchingConfiguration EndsWith(string name) 91 | { 92 | _filters.Add((c, i) => c.Name.EndsWith(name)); 93 | 94 | return this; 95 | } 96 | 97 | public TypeMatchingConfiguration AsSingleton(Func match = null) 98 | { 99 | if(match == null) 100 | { 101 | match = (c, t) => true; 102 | } 103 | 104 | _convention.AsSingleton = match; 105 | 106 | return this; 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/ByteFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Conventions; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.Primitives 5 | { 6 | public class ByteFixtureTests 7 | { 8 | #region Generate Tests 9 | [Fact] 10 | public void Fixture_GenerateByte_ReturnsPopulatedValue() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var value = fixture.Generate(); 15 | } 16 | 17 | [Fact] 18 | public void Fixture_GenerateNullableByte_ReturnsPopulatedValue() 19 | { 20 | var fixture = new Fixture(); 21 | 22 | var nullable = fixture.Generate(); 23 | 24 | Assert.True(nullable.HasValue); 25 | } 26 | 27 | [Fact] 28 | public void Fixture_GenerateByteArray_ReturnsPopulateValue() 29 | { 30 | var fixture = new Fixture(); 31 | 32 | var array = fixture.Generate(); 33 | 34 | Assert.NotNull(array); 35 | Assert.True(array.Length > 0); 36 | } 37 | #endregion 38 | 39 | #region Locate Tests 40 | [Fact] 41 | public void Fixture_LocateByte_ReturnLocateValue() 42 | { 43 | var fixture = new Fixture(); 44 | 45 | var value = fixture.Locate(); 46 | 47 | Assert.Equal(ByteConvention.LocateValue, value); 48 | } 49 | 50 | [Fact] 51 | public void Fixture_LocateNullableByte_ReturnsLocateValue() 52 | { 53 | var fixture = new Fixture(); 54 | 55 | var nullable = fixture.Locate(); 56 | 57 | Assert.True(nullable.HasValue); 58 | Assert.Equal(ByteConvention.LocateValue, nullable.Value); 59 | } 60 | 61 | [Fact] 62 | public void Fixture_LocateByteArray_ReturnsEmptyArray() 63 | { 64 | var fixture = new Fixture(); 65 | 66 | var array = fixture.Locate(); 67 | 68 | Assert.NotNull(array); 69 | Assert.True(array.Length == 0); 70 | } 71 | #endregion 72 | 73 | #region Min/Max Tests 74 | 75 | [Fact] 76 | public void Fixture_GenerateByteWithMin_ReturnsValueGreaterThanMin() 77 | { 78 | var fixture = new Fixture(); 79 | 80 | for (var i = 0; i < 100; i++) 81 | { 82 | var min = (byte)i; 83 | 84 | var value = fixture.Generate(constraints: new { min }); 85 | 86 | Assert.True(value >= min); 87 | } 88 | } 89 | 90 | [Fact] 91 | public void Fixture_GenerateByteWithMax_ReturnsValueLessThanMax() 92 | { 93 | var fixture = new Fixture(); 94 | 95 | for (var i = 0; i < 100; i++) 96 | { 97 | var max = (byte)(i + 10); 98 | 99 | var value = fixture.Generate(constraints: new { max }); 100 | 101 | Assert.True(value <= max); 102 | } 103 | } 104 | 105 | [Fact] 106 | public void Fixture_GenerateByteWithMinMax_ReturnsValueInRange() 107 | { 108 | var fixture = new Fixture(); 109 | 110 | for (var i = 0; i < 100; i++) 111 | { 112 | var min = (byte)i; 113 | var max = (byte)(min + 10); 114 | 115 | var value = fixture.Generate(constraints: new { min, max }); 116 | 117 | Assert.True(value >= min); 118 | Assert.True(value <= max); 119 | } 120 | } 121 | #endregion 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/DoubleFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Conventions; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.Primitives 5 | { 6 | public class DoubleFixtureTests 7 | { 8 | #region Generate Tests 9 | [Fact] 10 | public void Fixture_GenerateDouble_ReturnsPopulatedValue() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var value = fixture.Generate(); 15 | } 16 | 17 | [Fact] 18 | public void Fixture_GenerateNullableDouble_ReturnsPopulatedValue() 19 | { 20 | var fixture = new Fixture(); 21 | 22 | var nullable = fixture.Generate(); 23 | 24 | Assert.True(nullable.HasValue); 25 | } 26 | 27 | [Fact] 28 | public void Fixture_GenerateDoubleArray_ReturnsPopulateValue() 29 | { 30 | var fixture = new Fixture(); 31 | 32 | var array = fixture.Generate(); 33 | 34 | Assert.NotNull(array); 35 | Assert.True(array.Length > 0); 36 | } 37 | #endregion 38 | 39 | #region Locate Tests 40 | [Fact] 41 | public void Fixture_LocateDouble_ReturnLocateValue() 42 | { 43 | var fixture = new Fixture(); 44 | 45 | var value = fixture.Locate(); 46 | 47 | Assert.Equal(DoubleConvention.LocateValue, value); 48 | } 49 | 50 | [Fact] 51 | public void Fixture_LocateNullableDouble_ReturnsLocateValue() 52 | { 53 | var fixture = new Fixture(); 54 | 55 | var nullable = fixture.Locate(); 56 | 57 | Assert.True(nullable.HasValue); 58 | Assert.Equal(DoubleConvention.LocateValue, nullable.Value); 59 | } 60 | 61 | [Fact] 62 | public void Fixture_LocateDoubleArray_ReturnsEmptyArray() 63 | { 64 | var fixture = new Fixture(); 65 | 66 | var array = fixture.Locate(); 67 | 68 | Assert.NotNull(array); 69 | Assert.True(array.Length == 0); 70 | } 71 | #endregion 72 | 73 | #region Min/Max Tests 74 | 75 | [Fact] 76 | public void Fixture_GenerateDoubleWithMin_ReturnsValueGreaterThanMin() 77 | { 78 | var fixture = new Fixture(); 79 | 80 | for (var i = 0; i < 100; i++) 81 | { 82 | double min = i; 83 | 84 | var value = fixture.Generate(constraints: new { min }); 85 | 86 | Assert.True(value >= min); 87 | } 88 | } 89 | 90 | [Fact] 91 | public void Fixture_GenerateDoubleWithMax_ReturnsValueLessThanMax() 92 | { 93 | var fixture = new Fixture(); 94 | 95 | for (var i = 0; i < 100; i++) 96 | { 97 | double max = i + 10; 98 | 99 | var value = fixture.Generate(constraints: new { max }); 100 | 101 | Assert.True(value <= max); 102 | } 103 | } 104 | 105 | [Fact] 106 | public void Fixture_GenerateDoubleWithMinMax_ReturnsValueInRange() 107 | { 108 | var fixture = new Fixture(); 109 | 110 | for (var i = 0; i < 100; i++) 111 | { 112 | double min = i; 113 | var max = min + 10; 114 | 115 | var value = fixture.Generate(constraints: new { min, max }); 116 | 117 | Assert.True(value >= min); 118 | Assert.True(value <= max); 119 | } 120 | } 121 | #endregion 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/SByteFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Conventions; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.Primitives 5 | { 6 | public class SByteFixtureTests 7 | { 8 | #region Generate Tests 9 | [Fact] 10 | public void Fixture_GenerateSByte_ReturnsPopulatedValue() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var value = fixture.Generate(); 15 | } 16 | 17 | [Fact] 18 | public void Fixture_GenerateNullableSByte_ReturnsPopulatedValue() 19 | { 20 | var fixture = new Fixture(); 21 | 22 | var nullable = fixture.Generate(); 23 | 24 | Assert.True(nullable.HasValue); 25 | } 26 | 27 | [Fact] 28 | public void Fixture_GenerateSByteArray_ReturnsPopulateValue() 29 | { 30 | var fixture = new Fixture(); 31 | 32 | var array = fixture.Generate(); 33 | 34 | Assert.NotNull(array); 35 | Assert.True(array.Length > 0); 36 | } 37 | #endregion 38 | 39 | #region Locate Tests 40 | [Fact] 41 | public void Fixture_LocateSByte_ReturnLocateValue() 42 | { 43 | var fixture = new Fixture(); 44 | 45 | var value = fixture.Locate(); 46 | 47 | Assert.Equal(SByteConvention.LocateValue, value); 48 | } 49 | 50 | [Fact] 51 | public void Fixture_LocateNullableSByte_ReturnsLocateValue() 52 | { 53 | var fixture = new Fixture(); 54 | 55 | var nullable = fixture.Locate(); 56 | 57 | Assert.True(nullable.HasValue); 58 | Assert.Equal(SByteConvention.LocateValue, nullable.Value); 59 | } 60 | 61 | [Fact] 62 | public void Fixture_LocateSByteArray_ReturnsEmptyArray() 63 | { 64 | var fixture = new Fixture(); 65 | 66 | var array = fixture.Locate(); 67 | 68 | Assert.NotNull(array); 69 | Assert.True(array.Length == 0); 70 | } 71 | #endregion 72 | 73 | #region Min/Max Tests 74 | 75 | [Fact] 76 | public void Fixture_GenerateSByteWithMin_ReturnsValueGreaterThanMin() 77 | { 78 | var fixture = new Fixture(); 79 | 80 | for (var i = 0; i < 100; i++) 81 | { 82 | var min = (sbyte)i; 83 | 84 | var value = fixture.Generate(constraints: new { min }); 85 | 86 | Assert.True(value >= min); 87 | } 88 | } 89 | 90 | [Fact] 91 | public void Fixture_GenerateSByteWithMax_ReturnsValueLessThanMax() 92 | { 93 | var fixture = new Fixture(); 94 | 95 | for (var i = 0; i < 100; i++) 96 | { 97 | var max = (sbyte)(i + 10); 98 | 99 | var value = fixture.Generate(constraints: new { max }); 100 | 101 | Assert.True(value <= max); 102 | } 103 | } 104 | 105 | [Fact] 106 | public void Fixture_GenerateSByteWithMinMax_ReturnsValueInRange() 107 | { 108 | var fixture = new Fixture(); 109 | 110 | for (var i = 0; i < 100; i++) 111 | { 112 | var min = (sbyte)i; 113 | var max = (sbyte)(min + 10); 114 | 115 | var value = fixture.Generate(constraints: new { min, max }); 116 | 117 | Assert.True(value >= min); 118 | Assert.True(value <= max); 119 | } 120 | } 121 | #endregion 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/ShortFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Conventions; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.Primitives 5 | { 6 | public class ShortFixtureTests 7 | { 8 | #region Generate Tests 9 | [Fact] 10 | public void Fixture_GenerateShort_ReturnsPopulatedValue() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var value = fixture.Generate(); 15 | } 16 | 17 | [Fact] 18 | public void Fixture_GenerateNullableShort_ReturnsPopulatedValue() 19 | { 20 | var fixture = new Fixture(); 21 | 22 | var nullable = fixture.Generate(); 23 | 24 | Assert.True(nullable.HasValue); 25 | } 26 | 27 | [Fact] 28 | public void Fixture_GenerateShortArray_ReturnsPopulateValue() 29 | { 30 | var fixture = new Fixture(); 31 | 32 | var array = fixture.Generate(); 33 | 34 | Assert.NotNull(array); 35 | Assert.True(array.Length > 0); 36 | } 37 | #endregion 38 | 39 | #region Locate Tests 40 | [Fact] 41 | public void Fixture_LocateShort_ReturnLocateValue() 42 | { 43 | var fixture = new Fixture(); 44 | 45 | var value = fixture.Locate(); 46 | 47 | Assert.Equal(ShortConvention.LocateValue, value); 48 | } 49 | 50 | [Fact] 51 | public void Fixture_LocateNullableShort_ReturnsLocateValue() 52 | { 53 | var fixture = new Fixture(); 54 | 55 | var nullable = fixture.Locate(); 56 | 57 | Assert.True(nullable.HasValue); 58 | Assert.Equal(ShortConvention.LocateValue, nullable.Value); 59 | } 60 | 61 | [Fact] 62 | public void Fixture_LocateShortArray_ReturnsEmptyArray() 63 | { 64 | var fixture = new Fixture(); 65 | 66 | var array = fixture.Locate(); 67 | 68 | Assert.NotNull(array); 69 | Assert.True(array.Length == 0); 70 | } 71 | #endregion 72 | 73 | #region Min/Max Tests 74 | 75 | [Fact] 76 | public void Fixture_GenerateShortWithMin_ReturnsValueGreaterThanMin() 77 | { 78 | var fixture = new Fixture(); 79 | 80 | for (var i = 0; i < 100; i++) 81 | { 82 | var min = (short)i; 83 | 84 | var value = fixture.Generate(constraints: new { min }); 85 | 86 | Assert.True(value >= min); 87 | } 88 | } 89 | 90 | [Fact] 91 | public void Fixture_GenerateShortWithMax_ReturnsValueLessThanMax() 92 | { 93 | var fixture = new Fixture(); 94 | 95 | for (var i = 0; i < 100; i++) 96 | { 97 | var max = (short)(i + 10); 98 | 99 | var value = fixture.Generate(constraints: new { max }); 100 | 101 | Assert.True(value <= max); 102 | } 103 | } 104 | 105 | [Fact] 106 | public void Fixture_GenerateShortWithMinMax_ReturnsValueInRange() 107 | { 108 | var fixture = new Fixture(); 109 | 110 | for (var i = 0; i < 100; i++) 111 | { 112 | var min = (short)i; 113 | var max = (short)(min + 10); 114 | 115 | var value = fixture.Generate(constraints: new { min, max }); 116 | 117 | Assert.True(value >= min); 118 | Assert.True(value <= max); 119 | } 120 | } 121 | #endregion 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /tests/SimpleFixture.Tests/FixtureTests/Primitives/LongFixtureTests.cs: -------------------------------------------------------------------------------- 1 | using SimpleFixture.Conventions; 2 | using Xunit; 3 | 4 | namespace SimpleFixture.Tests.FixtureTests.Primitives 5 | { 6 | public class LongFixtureTests 7 | { 8 | #region Generate Tests 9 | [Fact] 10 | public void Fixture_GenerateLong_ReturnsPopulatedValue() 11 | { 12 | var fixture = new Fixture(); 13 | 14 | var value = fixture.Generate(); 15 | 16 | Assert.True(value != 0); 17 | } 18 | 19 | [Fact] 20 | public void Fixture_GenerateNullableLong_ReturnsPopulatedValue() 21 | { 22 | var fixture = new Fixture(); 23 | 24 | var nullable = fixture.Generate(); 25 | 26 | Assert.True(nullable.HasValue); 27 | } 28 | 29 | [Fact] 30 | public void Fixture_GenerateLongArray_ReturnsPopulateValue() 31 | { 32 | var fixture = new Fixture(); 33 | 34 | var array = fixture.Generate(); 35 | 36 | Assert.NotNull(array); 37 | Assert.True(array.Length > 0); 38 | } 39 | #endregion 40 | 41 | #region Locate Tests 42 | [Fact] 43 | public void Fixture_LocateLong_ReturnLocateValue() 44 | { 45 | var fixture = new Fixture(); 46 | 47 | var value = fixture.Locate(); 48 | 49 | Assert.Equal(LongConvention.LocateValue, value); 50 | } 51 | 52 | [Fact] 53 | public void Fixture_LocateNullableLong_ReturnsLocateValue() 54 | { 55 | var fixture = new Fixture(); 56 | 57 | var nullable = fixture.Locate(); 58 | 59 | Assert.True(nullable.HasValue); 60 | Assert.Equal(LongConvention.LocateValue, nullable.Value); 61 | } 62 | 63 | [Fact] 64 | public void Fixture_LocateLongArray_ReturnsEmptyArray() 65 | { 66 | var fixture = new Fixture(); 67 | 68 | var array = fixture.Locate(); 69 | 70 | Assert.NotNull(array); 71 | Assert.True(array.Length == 0); 72 | } 73 | #endregion 74 | 75 | #region Min/Max Tests 76 | 77 | [Fact] 78 | public void Fixture_GenerateLongWithMin_ReturnsValueGreaterThanMin() 79 | { 80 | var fixture = new Fixture(); 81 | 82 | for (var i = 0; i < 100; i++) 83 | { 84 | long min = 1000 * i; 85 | 86 | var value = fixture.Generate(constraints: new { min }); 87 | 88 | Assert.True(value >= min); 89 | } 90 | } 91 | 92 | [Fact] 93 | public void Fixture_GenerateLongWithMax_ReturnsValueLessThanMax() 94 | { 95 | var fixture = new Fixture(); 96 | 97 | for (var i = 0; i < 100; i++) 98 | { 99 | long max = 1000 * i; 100 | 101 | var value = fixture.Generate(constraints: new { max }); 102 | 103 | Assert.True(value <= max); 104 | } 105 | } 106 | 107 | [Fact] 108 | public void Fixture_GenerateLongWithMinMax_ReturnsValueInRange() 109 | { 110 | var fixture = new Fixture(); 111 | 112 | for (var i = 0; i < 100; i++) 113 | { 114 | long min = 1000 * i; 115 | var max = min + 10; 116 | 117 | var value = fixture.Generate(constraints: new { min, max }); 118 | 119 | Assert.True(value >= min); 120 | Assert.True(value <= max); 121 | } 122 | } 123 | #endregion 124 | } 125 | } 126 | --------------------------------------------------------------------------------