Skip to content

Commit

Permalink
StubGeneratorDefaultValueAttribute and StubGeneratorIgnoreAttribute a…
Browse files Browse the repository at this point in the history
…re added.

Some memory optimization
  • Loading branch information
MCKanpolat committed Mar 5, 2018
1 parent d3087c9 commit afd89d6
Show file tree
Hide file tree
Showing 18 changed files with 295 additions and 39 deletions.
30 changes: 30 additions & 0 deletions src/StubGenerator.Test.Models/DefaultValueTestModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using StubGenerator.Core;
using StubGenerator.Core.Attributes;

namespace StubGenerator.Test.Models
{
public class DefaultValueTestModel
{
public const string defaultEmail = "[email protected]";
public const EnTestEnum defaultEnum = EnTestEnum.Option2;
public const int defaultInt = 123456;


public string FirstName { get; set; }
public string LastName { get; set; }
[StubGeneratorDefaultValue(defaultEmail)]
public string Email { get; set; }

[StubGeneratorDefaultValue(defaultEnum)]
public EnTestEnum TestEnum { get; set; }

[StubGeneratorDefaultValue(defaultInt)]
public int TestInt { get; set; }

[StubGeneratorIgnore]
public int TestIntIgnored { get; set; }

[StubGeneratorIgnore]
public string Country { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/StubGenerator.Test.Models/ModelWithComplexTypeProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ public ModelConstructorMixed(string firstName)

public string FirstName { get; set; }
}

public class ModelOfType<T> where T : struct
{
public T Value { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\StubMiddleware.Core\StubGenerator.csproj" />
</ItemGroup>

</Project>
14 changes: 6 additions & 8 deletions src/StubGenerator.Test/CacheManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public CacheManagerTests()
_stubTypeMemoryCache = new MemoryStubTypeCache(_cacheKeyGenerator);
}

[Fact(DisplayName = "Cache Key Generator Test")]
public void CacheKeyGeneratorTest()
[Fact(DisplayName = "Should Generate Cache Key")]
public void Should_Generate_Cache_Key()
{
var cacheKey = _cacheKeyGenerator.GenerateKey<PersonDto>();
Assert.NotEmpty(cacheKey);
Expand All @@ -32,19 +32,17 @@ public void Should_Add_PropertyInfos_To_Cache_Successfully()
Assert.Equal(cachedPropertyInfos.Length, personData.GetType().GetProperties().Length);
}


[Fact(DisplayName = "Ensure Memory Cache Manager Empty")]
public void CacheManagerEmptyTest()
[Fact(DisplayName = "Should Memory Cache Is Empty")]
public void Should_Memory_Cache_Is_Empty()
{
var personData = new PersonDto();
_stubTypeMemoryCache.Set(personData, personData.GetType().GetProperties());
_stubTypeMemoryCache.Clear();
Assert.True(_stubTypeMemoryCache.IsEmpty());
}


[Fact(DisplayName = "Memory Cache Manager Concurrency")]
public void CacheManagerConcurrencyTest()
[Fact(DisplayName = "Should Cache Manager Is Concurrent")]
public void Should_Cache_Manager_Is_Concurrent()
{
var personData = new PersonDto();
var mainTask = Task.Run(async () =>
Expand Down
110 changes: 107 additions & 3 deletions src/StubGenerator.Test/StubManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,90 @@ public void Should_Generate_Data_For_Guid_Type()
Assert.True(Guid.TryParse(generatedStubData.GuidProperty.ToString(), out _));
}


[Fact(DisplayName = "Should Generate Data For Byte Type In Range")]
public void Should_Generate_Data_For_Byte_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<Byte>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, byte.MinValue, byte.MaxValue);
}

[Fact(DisplayName = "Should Generate Data For Single Type In Range")]
public void Should_Generate_Data_For_Single_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<Single>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, Single.MinValue, Single.MaxValue);
}


[Fact(DisplayName = "Should Generate Data For Int16 Type In Range")]
public void Should_Generate_Data_For_Int16_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<Int16>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, Int16.MinValue, Int16.MaxValue);
}


[Fact(DisplayName = "Should Generate Data For Int32 Type In Range")]
public void Should_Generate_Data_For_Int32_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<Int32>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, Int32.MinValue, Int32.MaxValue);
}

[Fact(DisplayName = "Should Generate Data For Int64 Type In Range")]
public void Should_Generate_Data_For_Int64_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<Int64>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, Int64.MinValue, Int64.MaxValue);
}

[Fact(DisplayName = "Should Generate Data For Double Type In Range")]
public void Should_Generate_Data_For_Double_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<Double>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, Double.MinValue, Double.MaxValue);
}


[Fact(DisplayName = "Should Generate Data For Decimal Type In Range")]
public void Should_Generate_Data_For_Decimal_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<Decimal>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, Decimal.MinValue, Decimal.MaxValue);
}

[Fact(DisplayName = "Should Generate Data For UInt16 Type In Range")]
public void Should_Generate_Data_For_UInt16_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<UInt16>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, UInt16.MinValue, UInt16.MaxValue);
}

[Fact(DisplayName = "Should Generate Data For UInt32 Type In Range")]
public void Should_Generate_Data_For_UInt32_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<UInt32>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, UInt32.MinValue, UInt32.MaxValue);
}

[Fact(DisplayName = "Should Generate Data For UInt64 Type In Range")]
public void Should_Generate_Data_For_UInt64_Type_In_Range()
{
var generatedStubData = _stubManager.CreateNew<ModelOfType<UInt64>>();
Assert.NotNull(generatedStubData);
Assert.InRange(generatedStubData.Value, UInt64.MinValue, UInt64.MaxValue);
}

[Fact(DisplayName = "Should Generate Data For Char Type")]
public void Should_Generate_Data_For_Chard_Type()
{
Expand Down Expand Up @@ -135,7 +219,7 @@ public void Should_Generate_Data_For_Collection_Types()
Assert.True(generatedStubData.CollectionTypeComplex[0].IntegerProperty != default(int));
}

[Fact(DisplayName = "Should Set Given values while generating data")]
[Fact(DisplayName = "Should Set Given Values While Generating Data")]
public void Should_Set_Given_Values_While_Generating_Data()
{
var givenDataTime = new DateTime(2017, 1, 1);
Expand All @@ -157,8 +241,8 @@ public void Should_Generate_Data_For_Complex_Type_Properties_Check_Parameterless
}


[Fact(DisplayName = "Check Unsupported Types")]
public void Check_Unsupported_Types()
[Fact(DisplayName = "Should Unsupported Types Are Null")]
public void Should_Unsupported_Types_Are_Null()
{
var generatedStubData = _stubManager.CreateNew<UnsupportedTypes>();
Assert.NotNull(generatedStubData);
Expand Down Expand Up @@ -194,5 +278,25 @@ public void Should_Generate_Generic_Class_With_2_Argument_Count_Specified()
var generatedStubData = _stubManager.InvokeCreateNew("StubGenerator.Test.Models.GenericModel`2<StubGenerator.Test.Models.ModelWithComplexTypeProperty, StubGenerator.Test.Models;StubGenerator.Test.Models.ComplexModel, StubGenerator.Test.Models>, StubGenerator.Test.Models");
Assert.NotNull(generatedStubData);
}


[Fact(DisplayName = "Should Set Given Value Property Has Default Value Attribute")]
public void Should_Set_Given_Value_Property_Has_Default_Value_Attribute()
{
var generatedStubData = _stubManager.CreateNew<DefaultValueTestModel>();
Assert.NotNull(generatedStubData);
Assert.Equal(DefaultValueTestModel.defaultEmail, generatedStubData.Email);
Assert.Equal(DefaultValueTestModel.defaultEnum, generatedStubData.TestEnum);
Assert.Equal(DefaultValueTestModel.defaultInt, generatedStubData.TestInt);
}


[Fact(DisplayName = "Should Set Null Value If Property Has Ignore Attribute")]
public void Should_Set_Null_Value_If_Property_Has_Ignore_Attribute()
{
var generatedStubData = _stubManager.CreateNew<DefaultValueTestModel>();
Assert.NotNull(generatedStubData);
Assert.Null(generatedStubData.Country);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace StubGenerator.Core.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public sealed class StubGeneratorDefaultValueAttribute : Attribute
{
private object _defaultValue;

public StubGeneratorDefaultValueAttribute(object defaultValue)
{
DefaultValue = defaultValue;
}

public object DefaultValue { get => _defaultValue; private set => _defaultValue = value; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace StubGenerator.Core.Attributes
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
public sealed class StubGeneratorIgnoreAttribute : Attribute
{

}
}
File renamed without changes.
20 changes: 16 additions & 4 deletions src/StubMiddleware.Core/Core/FakeDataFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using StubGenerator.Defaults;
using System.Linq;
using StubGenerator.Core.FakeDataGenerators;
using StubGenerator.Core.Attributes;

namespace StubGenerator.Core.FakeDataProvider
{
Expand All @@ -29,6 +30,17 @@ public object ProvideValue(PropertyInfo propertyInfo)
return null;
}

if (propertyInfo.GetCustomAttribute<StubGeneratorIgnoreAttribute>() != null)
{
return null;
}

var stubGeneratorDefaultValueAttribute = propertyInfo.GetCustomAttribute<StubGeneratorDefaultValueAttribute>();
if (stubGeneratorDefaultValueAttribute != null)
{
return stubGeneratorDefaultValueAttribute.DefaultValue;
}

var matchingConvetion = _stubDataMappingProfile.Conventions.FirstOrDefault(c => c.Condition(propertyInfo));
if (matchingConvetion != null)
{
Expand Down Expand Up @@ -64,15 +76,15 @@ private static object GenerateValueByType(Type propertyType)
case TypeCode.Int32:
return new IntegerValueGeneratorBase<Int32>().Generate();
case TypeCode.Int64:
return new IntegerValueGeneratorBase<Int64>().Generate();
return new FloatValueGeneratorBase<Int64>().Generate();
case TypeCode.UInt16:
return new IntegerValueGeneratorBase<UInt16>().Generate();
case TypeCode.UInt32:
return new IntegerValueGeneratorBase<UInt32>().Generate();
return new UInt32ValueGenerator().Generate();
case TypeCode.UInt64:
return new IntegerValueGeneratorBase<UInt64>().Generate();
return new FloatValueGeneratorBase<UInt64>().Generate();
case TypeCode.Single:
return new IntegerValueGeneratorBase<Single>().Generate();
return new FloatValueGeneratorBase<Single>().Generate();
case TypeCode.Byte:
return new IntegerValueGeneratorBase<Byte>().Generate();
case TypeCode.Double:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ namespace StubGenerator.Core.FakeDataGenerators
{
public class BoolValueGenerator : IValueGenerator
{
private static readonly Random _random;
static BoolValueGenerator() => _random = new Random();
private static readonly Lazy<Random> _random;
static BoolValueGenerator()
{
_random = new Lazy<Random>(() => new Random());
}

public object Generate()
{
return _random.NextDouble() >= 0.5;
return _random.Value.NextDouble() >= 0.5;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ namespace StubGenerator.Core.FakeDataGenerators
{
public class CharValueGenerator : IValueGenerator
{
private static char[] charValues;
private static int charsize;
private static readonly Lazy<Random> _random;
private static char[] _charValues;
private static int _charsize;
static CharValueGenerator()
{
charValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
charsize = charValues.Length;
_random = new Lazy<Random>(() => new Random());
_charValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
_charsize = _charValues.Length;
}

public object Generate()
{
return charValues[new Random().Next(0, charsize - 1)];
return _charValues[_random.Value.Next(0, _charsize - 1)];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ namespace StubGenerator.Core.FakeDataGenerators
{
public class DateTimeValueGenerator : IValueGenerator
{
private static readonly Random _random;
static DateTimeValueGenerator() => _random = new Random();
private static readonly Lazy<Random> _random;
static DateTimeValueGenerator()
{
_random = new Lazy<Random>(() => new Random());
}

public object Generate()
{
var startDate = new DateTime(1995, 1, 1);
return startDate.AddDays(_random.Next(0, (DateTime.Today - startDate).Days));
var startDate = new DateTime(1900, 1, 1);
return startDate.AddDays(_random.Value.Next(0, (DateTime.Today - startDate).Days));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ namespace StubGenerator.Core.FakeDataGenerators
{
public class DecimalValueGenerator : IValueGenerator
{
private static readonly Random _random;
static DecimalValueGenerator() => _random = new Random();
private static readonly Lazy<Random> _random;
static DecimalValueGenerator()
{
_random = new Lazy<Random>(() => new Random());
}

public object Generate()
{
return _random.NextDecimal();
return _random.Value.NextDecimal();
}
}
}
Loading

0 comments on commit afd89d6

Please sign in to comment.