Skip to content

Commit

Permalink
Merge pull request #43 from jamesmontemagno/async-comand
Browse files Browse the repository at this point in the history
Add AsyncCommand from JT
  • Loading branch information
jamesmontemagno authored Aug 19, 2019
2 parents a0b4c34 + b147088 commit a37c6ce
Show file tree
Hide file tree
Showing 26 changed files with 2,006 additions and 795 deletions.
97 changes: 97 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Suppress: EC112
# top-most EditorConfig file
root = true

# Don't use tabs for indentation.
[*]
indent_style = tab
# (Please don't specify an indent_size here; that has too many unintended consequences.)

# Code files
[*.{cs,csx,vb,vbx}]
indent_size = 4

# Xml project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2

# Xml config files
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
indent_size = 2

# JSON files
[*.json]
indent_size = 2

# Dotnet code style settings:
[*.{cs,vb}]
# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = false
# Avoid "this." and "Me." if not necessary
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Use language keywords instead of framework type names for type references
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# Suggest more modern language features when available
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion

# Naming Conventions:
# Pascal Casing
dotnet_naming_symbols.method_and_property_symbols.applicable_kinds= method,property,enum
dotnet_naming_symbols.method_and_property_symbols.applicable_accessibilities = *
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

dotnet_naming_rule.methods_and_properties_must_be_pascal_case.severity = warning
dotnet_naming_rule.methods_and_properties_must_be_pascal_case.symbols = method_and_property_symbols
dotnet_naming_rule.methods_and_properties_must_be_pascal_case.style = pascal_case_style

# Non-public members must be lower-case
dotnet_naming_symbols.non_public_symbols.applicable_kinds = property,field,event,delegate
dotnet_naming_symbols.non_public_symbols.applicable_accessibilities = private
dotnet_naming_style.all_lower_case_style.capitalization = camel_case

dotnet_naming_rule.non_public_members_must_be_lower_case.severity = warning
dotnet_naming_rule.non_public_members_must_be_lower_case.symbols = non_public_symbols
dotnet_naming_rule.non_public_members_must_be_lower_case.style = all_lower_case_style

# CSharp code style settings:
[*.cs]
# Do not prefer "var" everywhere
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion

# Prefer method-like constructs to have a block body
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:none
csharp_style_expression_bodied_operators = true:none

# Prefer property-like constructs to have an expression-body
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:none
csharp_style_expression_bodied_accessors = true:none

# Suggest more modern language features when available
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion

# Newline settings
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

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

### [1.5.0]
* Add AsyncCommand & AsyncCommand<T>
* Add Command and Command<T>
* Add WeakEventManager
* Don't raise event when IEnumerable<T> is empty. Thanks @Berrysoft!

### [1.4.0]
* OnPropertyChanges is now virtual
* Streamlined CI/CD
Expand Down
179 changes: 179 additions & 0 deletions MvvmHelpers.UnitTests/AsyncCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvvmHelpers.Commands;
using System;
using System.Threading.Tasks;

namespace MvvmHelpers.UnitTests
{
[TestClass]
public class AsyncCommandTests
{
#region Events
protected event EventHandler TestEvent
{
add => TestWeakEventManager.AddEventHandler(value);
remove => TestWeakEventManager.RemoveEventHandler(value);
}
#endregion

#region Properties
protected const int Delay = 500;
protected WeakEventManager TestWeakEventManager { get; } = new WeakEventManager();
#endregion

#region Methods
protected Task NoParameterTask() => Task.Delay(Delay);
protected Task IntParameterTask(int delay) => Task.Delay(delay);
protected Task StringParameterTask(string text) => Task.Delay(Delay);
protected Task NoParameterImmediateNullReferenceExceptionTask() => throw new NullReferenceException();
protected Task ParameterImmediateNullReferenceExceptionTask(int delay) => throw new NullReferenceException();

protected async Task NoParameterDelayedNullReferenceExceptionTask()
{
await Task.Delay(Delay);
throw new NullReferenceException();
}

protected async Task IntParameterDelayedNullReferenceExceptionTask(int delay)
{
await Task.Delay(delay);
throw new NullReferenceException();
}

protected bool CanExecuteTrue(object parameter) => true;
protected bool CanExecuteFalse(object parameter) => false;
protected bool CanExecuteDynamic(object booleanParameter) => (bool)booleanParameter;
#endregion

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void AsyncCommand_NullExecuteParameter()
{
//Arrange

//Act

//Assert
new AsyncCommand(null);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void AsyncCommandT_NullExecuteParameter()
{
//Arrange

//Act

//Assert
new AsyncCommand<object>(null);
}

[TestMethod]
public async Task AsyncCommand_ExecuteAsync_IntParameter_Test()
{
//Arrange
var command = new AsyncCommand<int>(IntParameterTask);

//Act
await command.ExecuteAsync(500);
await command.ExecuteAsync(default);

//Assert

}

[TestMethod]
public async Task AsyncCommand_ExecuteAsync_StringParameter_Test()
{
//Arrange
var command = new AsyncCommand<string>(StringParameterTask);

//Act
await command.ExecuteAsync("Hello");
await command.ExecuteAsync(default);

//Assert

}

[TestMethod]
public void AsyncCommand_Parameter_CanExecuteTrue_Test()
{
//Arrange
var command = new AsyncCommand<int>(IntParameterTask, CanExecuteTrue);

//Act

//Assert
Assert.IsTrue(command.CanExecute(null));
}

[TestMethod]
public void AsyncCommand_Parameter_CanExecuteFalse_Test()
{
//Arrange
var command = new AsyncCommand<int>(IntParameterTask, CanExecuteFalse);

//Act

//Assert
Assert.IsFalse(command.CanExecute(null));
}

[TestMethod]
public void AsyncCommand_NoParameter_CanExecuteTrue_Test()
{
//Arrange
var command = new AsyncCommand(NoParameterTask, CanExecuteTrue);

//Act

//Assert
Assert.IsTrue(command.CanExecute(null));
}

[TestMethod]
public void AsyncCommand_NoParameter_CanExecuteFalse_Test()
{
//Arrange
var command = new AsyncCommand(NoParameterTask, CanExecuteFalse);

//Act

//Assert
Assert.IsFalse(command.CanExecute(null));
}


[TestMethod]
public void AsyncCommand_CanExecuteChanged_Test()
{
//Arrange
var canCommandExecute = false;
var didCanExecuteChangeFire = false;

var command = new AsyncCommand(NoParameterTask, commandCanExecute);
command.CanExecuteChanged += handleCanExecuteChanged;

void handleCanExecuteChanged(object sender, EventArgs e) => didCanExecuteChangeFire = true;
bool commandCanExecute(object parameter) => canCommandExecute;

Assert.IsFalse(command.CanExecute(null));

//Act
canCommandExecute = true;

//Assert
Assert.IsTrue(command.CanExecute(null));
Assert.IsFalse(didCanExecuteChangeFire);

//Act
command.RaiseCanExecuteChanged();

//Assert
Assert.IsTrue(didCanExecuteChangeFire);
Assert.IsTrue(command.CanExecute(null));
}
}
}
Loading

0 comments on commit a37c6ce

Please sign in to comment.