public void ModelWithDuplicateCustomAttribute_DoesNotThrowException() { ConfigurationContext.Current.CustomAttributes = new[] { new CustomAttributeDescriptor(typeof(FancyHelpTextAttribute)) }; var sut = new TypeDiscoveryHelper(); var resources = sut.ScanResources(typeof(ModelWithCustomAttributesDuplicates)); Assert.NotNull(resources); }
public LocalizedModelsDiscoveryTests() { var types = new[] { typeof(SampleViewModel), typeof(SubViewModel) }; var sut = new TypeDiscoveryHelper(); Assert.NotEmpty(types); _properties = types.SelectMany(t => sut.ScanResources(t)); }
public void ModelWithCustomAttribute_NullTranslation_DiscoversResource() { ConfigurationContext.Current.CustomAttributes = new[] { new CustomAttributeDescriptor(typeof(HelpTextAttribute), false) }; var sut = new TypeDiscoveryHelper(); var resources = sut.ScanResources(typeof(ModelWithCustomAttributes)); var helpTextResource = resources.First(r => r.PropertyName == "UserName-HelpText"); Assert.Equal(string.Empty, helpTextResource.Translation); }
public void SameModel_MultipleDefinitions_DoesNotThrowException() { var sut = new TypeDiscoveryHelper(); var resources = sut.ScanResources(typeof(ViewModelWithDuplicateSubModels)); Assert.NotNull(resources); var count = resources.Count(r => r.Key == "DbLocalizationProvider.Tests.SubModel.MyProperty-StringLength"); Assert.Equal(1, count); }
public void SingleLevel_ScalarProperties() { var sut = new TypeDiscoveryHelper(); var type = _types.First(t => t.FullName == "DbLocalizationProvider.Tests.ResourceKeys"); var properties = sut.ScanResources(type); var staticField = properties.First(p => p.Key == "DbLocalizationProvider.Tests.ResourceKeys.ThisIsConstant"); Assert.True(LocalizedTypeScannerBase.IsStringProperty(staticField.ReturnType)); Assert.Equal("Default value for constant", staticField.Translation); }
public void Test() { var sut = new TypeDiscoveryHelper(); var properties = new[] { typeof(SampleViewModelWithBaseNotInherit), typeof(BaseLocalizedViewModel) } .Select(t => sut.ScanResources(t)) .ToList(); var childModel = new SampleViewModelWithBaseNotInherit(); var basePropertyKey = ExpressionHelper.GetFullMemberName(() => childModel.BaseProperty); Assert.Equal("DbLocalizationProvider.Tests.InheritedModels.BaseLocalizedViewModel.BaseProperty", basePropertyKey); }
private void RegisterDiscoveredResources(IEnumerable<Type> types) { var helper = new TypeDiscoveryHelper(); var properties = types.SelectMany(type => helper.ScanResources(type)).DistinctBy(r => r.Key); using (var db = new LanguageEntities()) { foreach (var property in properties) RegisterIfNotExist(db, property.Key, property.Translation); db.SaveChanges(); } }
public void NotInheritedModel_ContainsOnlyDeclaredProperties() { var sut = new TypeDiscoveryHelper(); var properties = sut.ScanResources(typeof(SampleViewModelWithBase)).ToList(); var keys = properties.Select(p => p.Key).ToList(); var stringLengthResource = properties.FirstOrDefault(r => r.Key == "DbLocalizationProvider.Tests.DataAnnotations.SampleViewModelWithBase.ChildProperty-StringLength"); Assert.Contains("DbLocalizationProvider.Tests.DataAnnotations.SampleViewModelWithBase.ChildProperty-Description", keys); Assert.NotNull(stringLengthResource); Assert.Contains("StringLength", stringLengthResource.Translation); Assert.DoesNotContain("DbLocalizationProvider.Tests.DataAnnotations.SampleViewModelWithBase.BaseProperty", keys); Assert.DoesNotContain("DbLocalizationProvider.Tests.DataAnnotations.SampleViewModelWithBase.BaseProperty-Required", keys); Assert.DoesNotContain("DbLocalizationProvider.Tests.DataAnnotations.SampleViewModelWithBase.ChildProperty-Description-Required", keys); }
public void ModelWith2ChildModelAsProperties_ReturnsDuplicates() { ConfigurationContext.Current.CustomAttributes = new[] { new CustomAttributeDescriptor(typeof(HelpTextAttribute), false), new CustomAttributeDescriptor(typeof(FancyHelpTextAttribute), false) }; var sut = new TypeDiscoveryHelper(); var types = new[] { typeof(ModelWithTwoChildModelPropertiesCustomAttributes), typeof(AnotherModelWithTwoChildModelPropertiesCustomAttributes) }; var resources = types.SelectMany(t => sut.ScanResources(t)).DistinctBy(r => r.Key); var containsDuplicates = resources.GroupBy(r => r.Key).Any(g => g.Count() > 1); Assert.False(containsDuplicates); }
public LocalizedResourceDiscoveryTests() { _sut = new TypeDiscoveryHelper(); _types = TypeDiscoveryHelper.GetTypesWithAttribute <LocalizedResourceAttribute>().ToList(); Assert.NotEmpty(_types); }
public void SpecficAssemblyFilter_ShouldIncludeInternal() { var assemblies = TypeDiscoveryHelper.GetAssemblies(a => a.FullName.StartsWith("NonExisting"), false); Assert.NotEmpty(assemblies); }
public LocalizedResourceDiscoveryTests() { _sut = new TypeDiscoveryHelper(); _types = TypeDiscoveryHelper.GetTypesWithAttribute<LocalizedResourceAttribute>().ToList(); Assert.NotEmpty(_types); }
/// <summary>Registers the discovered resources.</summary> /// <param name="types"> The types. </param> /// <param name="allResources"> all resources. </param> private void RegisterDiscoveredResources(IEnumerable <Type> types, IList <LocalizationResource> allResources) { var helper = new TypeDiscoveryHelper(); var props = types.SelectMany(type => helper.ScanResources(type)).DistinctBy(r => r.Key); // split work queue by 400 resources each var groupedProperties = props.SplitByCount(400); // loop through all groups foreach (var group in groupedProperties) { using (var uow = _dataService.StartUnitOfWork()) { var resourceRepository = uow.ResourceRepository; var translationRepository = uow.TranslationRepository; // enumerate beforehand var properties = group.ToList(); // loop through all refactored properties and change their key foreach (var refactoredResource in properties.Where(r => !string.IsNullOrEmpty(r.OldResourceKey))) { if (resourceRepository.RefactorKey(refactoredResource.OldResourceKey, refactoredResource.Key)) { allResources.Single(r => r.ResourceKey == refactoredResource.OldResourceKey).ResourceKey = refactoredResource.Key; } } // loop through all properties foreach (var property in properties) { var existingResource = allResources.FirstOrDefault(r => r.ResourceKey == property.Key); if (existingResource == null) { // add the new resource var newResource = resourceRepository.Add(new ResourceEntity { Author = "Code", FromCode = true, IsHidden = property.IsHidden, IsModified = false, ResourceKey = property.Key, ModificationDate = DateTime.Now }); //we don't have to respect existing translations - just insert them all translationRepository.AddRange(property.Translations.Select(t => new TranslationEntity { ResourceId = newResource.Id, Language = t.Culture, Value = t.Translation })); } else { // update the existing resource var resourceEntity = resourceRepository.GetByKey(existingResource.ResourceKey); resourceEntity.FromCode = true; resourceEntity.IsHidden = property.IsHidden; resourceRepository.Update(resourceEntity); // add/update languages var byResource = translationRepository.ByResource(resourceEntity); var existingTranslations = byResource.ToDictionary(t => t.Language ?? string.Empty); foreach (var translation in property.Translations) { // if it exists - update it if it was modified or the invariant translations if (existingTranslations.ContainsKey(translation.Culture)) { // skip if modified - or culture isnt invariant if (translation.Culture != string.Empty && (!existingResource.IsModified.HasValue || existingResource.IsModified.Value)) { continue; } var entity = existingTranslations[translation.Culture]; entity.Value = translation.Translation; translationRepository.Update(entity); } // if it doesnt - add it else { translationRepository.Add(new TranslationEntity { Language = translation.Culture, ResourceId = existingResource.Id, Value = translation.Translation }); } } } } uow.Commit(); } } }
public NamedModelsTests() { _sut = new TypeDiscoveryHelper(); }
public NamedResourcesTests() { _sut = new TypeDiscoveryHelper(); ConfigurationContext.Current.TypeFactory.ForQuery <DetermineDefaultCulture.Query>().SetHandler <DetermineDefaultCulture.Handler>(); }
public void ThrowOnDuplicateCultures_FromOrdinaryResource() { var sut = new TypeDiscoveryHelper(); Assert.Throws <DuplicateResourceTranslationsException>(() => sut.ScanResources(typeof(SomeResourcesWithDuplicateCultures))); }
public NamedResourcesTests() { _sut = new TypeDiscoveryHelper(); }
public ViewModelWithIncludedOnlyTests() { _sut = new TypeDiscoveryHelper(); }
public NamedResourcesTests() { _sut = new TypeDiscoveryHelper(); }
public GenericModelTests() { _sut = new TypeDiscoveryHelper(); }
public void ScanResource_BadTranslationLanguage() { var sut = new TypeDiscoveryHelper(); Assert.Throws <ArgumentException>(() => sut.ScanResources(typeof(BadResourceWithNoExistingLanguageCode))); }