Skip to content

Commit

Permalink
Code moved from private repo
Browse files Browse the repository at this point in the history
  • Loading branch information
alhugone committed Dec 16, 2018
1 parent 3b2e8ff commit 4b42336
Show file tree
Hide file tree
Showing 41 changed files with 1,528 additions and 0 deletions.
150 changes: 150 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
###############################
# Core EditorConfig Options #
###############################

root = true

# All files
[*]
indent_style = space

# Code files
[*.{cs,csx,vb,vbx}]
indent_size = 2
insert_final_newline = true
charset = utf-8-bom

###############################
# .NET Coding Conventions #
###############################

[*.{cs,vb}]
# Organize usings
dotnet_sort_system_directives_first = true

# this. preferences
dotnet_style_qualification_for_field = false:none
dotnet_style_qualification_for_property = false:none
dotnet_style_qualification_for_method = false:none
dotnet_style_qualification_for_event = false:none

# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:none
dotnet_style_predefined_type_for_member_access = true:none

# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent

# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members:none
dotnet_style_readonly_field = true:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent

###############################
# Naming Conventions #
###############################

# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const

###############################
# C# Code Style Rules #
###############################

[*.cs]
# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none

# Expression-bodied members
csharp_style_expression_bodied_methods = true:none
csharp_style_expression_bodied_constructors = true:none
csharp_style_expression_bodied_operators = true:none
csharp_style_expression_bodied_properties = true:none
csharp_style_expression_bodied_indexers = true:none
csharp_style_expression_bodied_accessors = true:none

# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion

# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

# Expression-level preferences
csharp_prefer_braces = true:none
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion

###############################
# C# Formatting Rules #
###############################

# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left

# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false

# Wrapping preferences
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true

##################################
# Visual Basic Code Style Rules #
##################################

[*.vb]
# Modifier preferences
visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace BindToConfig.UnitTests.AddBoundToConfig
{
public class AddBoundToConfig_Binds
{
private const string AnyKey = "_";
private static readonly IConfiguration EmptyConfiguration = new ConfigurationBuilder().Build();
private readonly IServiceCollection _serviceCollection = new ServiceCollection();

[Fact]
public void Private_Setters()
{
// arrange
const string key = nameof(SampleConfiguration);
var expectedSampleConfiguration = new SampleConfiguration(100, "My string value");
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>(
key + ":" + nameof(expectedSampleConfiguration.IntValue),
expectedSampleConfiguration.IntValue.ToString()),
new KeyValuePair<string, string>(
key + ":" + nameof(expectedSampleConfiguration.StringValue),
expectedSampleConfiguration.StringValue)
});
var configuration = configurationBuilder.Build();
// act
_serviceCollection.AddBoundToConfig<SampleConfiguration>(configuration, key);
// assert
var provider = _serviceCollection.BuildServiceProvider(true);
var myOptions1 = provider.CreateScope().ServiceProvider.GetRequiredService<SampleConfiguration>();
Assert.Equal(expectedSampleConfiguration.IntValue, myOptions1.IntValue);
Assert.Equal(expectedSampleConfiguration.StringValue, myOptions1.StringValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using BindToConfig.Exceptions;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace BindToConfig.UnitTests.AddBoundToConfig
{
public class AddBoundToConfig_FastFails
{
private const string AnyKey = "_";
private static readonly IConfiguration EmptyConfiguration = new ConfigurationBuilder().Build();
private readonly IServiceCollection _serviceCollection = new ServiceCollection();

[Fact]
public void By_Throwing_ConfigurationObjectCreationFailed_If_Instantiation_Of_Object_Is_Not_Possible()
{
// act
var exception = Assert.Throws<ObjectCreationFailed>(
() =>
new ServiceCollection().AddBoundToConfig<NotInstantiationClass>(
EmptyConfiguration,
AnyKey,
BindOptions.DontThrowIfSectionIsMissingOrEmpty));
// assert
exception.CorrespondingType.Should().Be<NotInstantiationClass>();
exception.InnerException.Should().NotBeNull("Runtime exception was catched.");
}

[Fact]
public void
By_Throwing_ConfigurationSectionIsMissingOrEmpty_When_Section_With_Given_Key_Does_Not_Exist()
{
// arrange
const string expectedNotExistingSectionKey = "NonExistingSectionKey";
// act
var exception = Assert.Throws<SectionIsMissingOrEmpty>(
() =>
new ServiceCollection().AddBoundToConfig<NotInstantiationClass>(
EmptyConfiguration,
expectedNotExistingSectionKey));
// assert
Assert.Equal(expectedNotExistingSectionKey, exception.Key);
exception.CorrespondingType.Should().Be<NotInstantiationClass>();
}

public class NotInstantiationClass
{
public NotInstantiationClass(int i)
{
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.Threading;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace BindToConfig.UnitTests.AddBoundToConfig
{
public class AddBoundToConfig_Registers_Class_So_That
{
private const string AnyKey = "_";
private static readonly IConfiguration EmptyConfiguration = new ConfigurationBuilder().Build();
private readonly IServiceCollection _serviceCollection = new ServiceCollection();

[Fact]
public void When_Configuration_Has_Been_Changed_A_New_Object_Is_Returned_For_A_New_Scope()
{
// arrange
var expected = new SampleConfiguration(1001, Guid.NewGuid().ToString());
var configurationProvider = new TesTConfigClassProvider().SetSection(100, "any");
var configuration = new ConfigurationRoot(new List<IConfigurationProvider> {configurationProvider});
_serviceCollection.AddBoundToConfig<SampleConfiguration>(configuration, nameof(SampleConfiguration));
var provider = _serviceCollection.BuildServiceProvider(true);
var myOptions1 = provider.CreateScope().ServiceProvider.GetRequiredService<SampleConfiguration>();
// act
configurationProvider.SetSection(expected.IntValue, expected.StringValue);
configurationProvider.Reload();
// assert
var myOptions2 = provider.CreateScope().ServiceProvider.GetRequiredService<SampleConfiguration>();
Assert.NotSame(myOptions1, myOptions2);
myOptions2.Should().BeEquivalentTo(expected);
}

[Fact]
public void When_Configuration_Has_Been_Changed_And_Causes_Instatnion_Errors_Registered_Handler_Is_Called()
{
// arrange
var configurationProvider = new TesTConfigClassProvider().SetSection(100, "any");
var configuration = new ConfigurationRoot(new List<IConfigurationProvider> {configurationProvider});
var resetEvent = new ManualResetEventSlim(false);
_serviceCollection.AddBoundToConfig<SampleConfiguration>(
configuration,
nameof(SampleConfiguration),
(ex, type) => { resetEvent.Set(); });
var provider = _serviceCollection.BuildServiceProvider(true);
// act
configurationProvider.Clear();
configurationProvider.Reload();
provider.CreateScope().ServiceProvider.GetRequiredService<SampleConfiguration>();
// assert
resetEvent.Wait(TimeSpan.FromMilliseconds(1000)).Should().BeTrue("Error handler should be called.");
}

[Fact]
public void
When_Configuration_Has_Been_Changed_And_Now_Causes_Instatnion_Errors_The_Last_Object_Is_Returned_For_A_New_Scope()
{
// arrange
var configurationProvider = new TesTConfigClassProvider().SetSection(100, "any");
var configuration = new ConfigurationRoot(new List<IConfigurationProvider> {configurationProvider});
_serviceCollection.AddBoundToConfig<SampleConfiguration>(configuration, nameof(SampleConfiguration));
var provider = _serviceCollection.BuildServiceProvider(true);
var myOptions1 = provider.CreateScope().ServiceProvider.GetRequiredService<SampleConfiguration>();
// act
configurationProvider.Clear();
configurationProvider.Reload();
var myOptions2 = provider.CreateScope().ServiceProvider.GetRequiredService<SampleConfiguration>();
// assert
Assert.Same(myOptions1, myOptions2);
}

[Fact]
public void
When_Configuration_Has_Been_Changed_It_Does_Not_Affect_Existing_Scope_And_The_Same_Object_Is_Always_Returned_For_The_Same_Scope()
{
// arrange
var configurationProvider = new TesTConfigClassProvider();
var configuration = new ConfigurationRoot(new List<IConfigurationProvider> {configurationProvider});
_serviceCollection.AddBoundToConfig<SampleConfiguration>(
configuration,
AnyKey,
BindOptions.DontThrowIfSectionIsMissingOrEmpty);
var provider = _serviceCollection.BuildServiceProvider(true);
var myOptions1 = provider.CreateScope().ServiceProvider.GetRequiredService<SampleConfiguration>();
// act
configurationProvider.Reload();
// assert
var myOptions2 = provider.CreateScope().ServiceProvider.GetRequiredService<SampleConfiguration>();
Assert.NotSame(myOptions1, myOptions2);
}

[Fact]
public void
When_Configuration_Has_Not_Been_Changed_The_Same_Object_Is_Always_Returned_For_The_Same_Scope()
{
// act
_serviceCollection.AddBoundToConfig<SampleConfiguration>(
EmptyConfiguration,
AnyKey,
BindOptions.DontThrowIfSectionIsMissingOrEmpty);
// assert
var provider = _serviceCollection.BuildServiceProvider(true).CreateScope().ServiceProvider;
var myOptions1 = provider.GetRequiredService<SampleConfiguration>();
var myOptions2 = provider.GetRequiredService<SampleConfiguration>();
Assert.Same(myOptions1, myOptions2);
}

[Fact]
public void
When_Configuration_Has_Not_Been_Changed_The_Same_Object_Is_Returned_For_Each_New_Scope()
{
// act
_serviceCollection.AddBoundToConfig<SampleConfiguration>(
EmptyConfiguration,
AnyKey,
BindOptions.DontThrowIfSectionIsMissingOrEmpty);
var provider = _serviceCollection.BuildServiceProvider(true);
Func<SampleConfiguration> GetObjectFromNewScope = () =>
provider.CreateScope().ServiceProvider.GetRequiredService<SampleConfiguration>();
var expected = GetObjectFromNewScope();
var list = new List<SampleConfiguration>();
// act
for (var i = 0; i < 10; i++)
{
list.Add(GetObjectFromNewScope());
}

// assert
list.TrueForAll(x => ReferenceEquals(expected, x)).Should().BeTrue();
}
}
}

0 comments on commit 4b42336

Please sign in to comment.