-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added ByAdding and BySubtracting
- Loading branch information
Showing
10 changed files
with
459 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/******************************************************************************* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 DigiDNA - www.imazing.com | ||
* | ||
* 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: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* 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. | ||
******************************************************************************/ | ||
|
||
using System; | ||
using System.Windows; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace ValueTransformers_Test | ||
{ | ||
[TestClass] | ||
public class ByAdding | ||
{ | ||
private readonly ValueTransformers.ByAdding Converter = new ValueTransformers.ByAdding(); | ||
|
||
[TestMethod] | ||
public void TestType() | ||
{ | ||
Assert.IsTrue( this.Converter.Convert( 1.0, typeof( int ), 1.0, null ) is int ); | ||
Assert.IsTrue( this.Converter.Convert( 1, typeof( float ), 1, null ) is float ); | ||
} | ||
|
||
[TestMethod] | ||
public void Test() | ||
{ | ||
Assert.AreEqual( 42, this.Converter.Convert( 42, typeof( int ), 0, null ) ); | ||
Assert.AreEqual( 43, this.Converter.Convert( 42, typeof( int ), 1, null ) ); | ||
Assert.AreEqual( 44, this.Converter.Convert( 42, typeof( int ), 2, null ) ); | ||
Assert.AreEqual( 41, this.Converter.Convert( 42, typeof( int ), -1, null ) ); | ||
Assert.AreEqual( 40, this.Converter.Convert( 42, typeof( int ), -2, null ) ); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNullValue() | ||
{ | ||
Assert.AreEqual( null, this.Converter.Convert( null, typeof( int ), 1, null ) ); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNullParameter() | ||
{ | ||
Assert.AreEqual( 42, this.Converter.Convert( 42, typeof( int ), null, null ) ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/******************************************************************************* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 DigiDNA - www.imazing.com | ||
* | ||
* 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: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* 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. | ||
******************************************************************************/ | ||
|
||
using System; | ||
using System.Windows; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace ValueTransformers_Test | ||
{ | ||
[TestClass] | ||
public class BySubtracting | ||
{ | ||
private readonly ValueTransformers.BySubtracting Converter = new ValueTransformers.BySubtracting(); | ||
|
||
[TestMethod] | ||
public void TestType() | ||
{ | ||
Assert.IsTrue( this.Converter.Convert( 1.0, typeof( int ), 1.0, null ) is int ); | ||
Assert.IsTrue( this.Converter.Convert( 1, typeof( float ), 1, null ) is float ); | ||
} | ||
|
||
[TestMethod] | ||
public void Test() | ||
{ | ||
Assert.AreEqual( 42, this.Converter.Convert( 42, typeof( int ), 0, null ) ); | ||
Assert.AreEqual( 41, this.Converter.Convert( 42, typeof( int ), 1, null ) ); | ||
Assert.AreEqual( 40, this.Converter.Convert( 42, typeof( int ), 2, null ) ); | ||
Assert.AreEqual( 43, this.Converter.Convert( 42, typeof( int ), -1, null ) ); | ||
Assert.AreEqual( 44, this.Converter.Convert( 42, typeof( int ), -2, null ) ); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNullValue() | ||
{ | ||
Assert.AreEqual( null, this.Converter.Convert( null, typeof( int ), 1, null ) ); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNullParameter() | ||
{ | ||
Assert.AreEqual( 42, this.Converter.Convert( 42, typeof( int ), null, null ) ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/******************************************************************************* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 DigiDNA - www.imazing.com | ||
* | ||
* 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: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* 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. | ||
******************************************************************************/ | ||
|
||
using System; | ||
using System.Windows.Data; | ||
using System.Windows.Markup; | ||
|
||
namespace ValueTransformers | ||
{ | ||
[MarkupExtensionReturnType( typeof( IValueConverter ) )] | ||
[ValueConversion( typeof( object ), typeof( object ) )] | ||
public class ByAdding: MarkupExtension, IValueConverter | ||
{ | ||
public object? Convert( object? value, Type targetType, object? parameter, System.Globalization.CultureInfo? culture ) | ||
{ | ||
if( value == null ) | ||
{ | ||
return null; | ||
} | ||
|
||
if( parameter == null ) | ||
{ | ||
parameter = 0; | ||
} | ||
|
||
double n = System.Convert.ToDouble( value ) + System.Convert.ToDouble( parameter ); | ||
|
||
if( targetType == typeof( char ) ) | ||
{ | ||
return ( char )n; | ||
} | ||
|
||
if( targetType == typeof( sbyte ) ) | ||
{ | ||
return ( sbyte )n; | ||
} | ||
|
||
if( targetType == typeof( byte ) ) | ||
{ | ||
return ( byte )n; | ||
} | ||
|
||
if( targetType == typeof( short ) ) | ||
{ | ||
return ( short )n; | ||
} | ||
|
||
if( targetType == typeof( ushort ) ) | ||
{ | ||
return ( ushort )n; | ||
} | ||
|
||
if( targetType == typeof( int ) ) | ||
{ | ||
return ( int )n; | ||
} | ||
|
||
if( targetType == typeof( uint ) ) | ||
{ | ||
return ( uint )n; | ||
} | ||
|
||
if( targetType == typeof( long ) ) | ||
{ | ||
return ( long )n; | ||
} | ||
|
||
if( targetType == typeof( long ) ) | ||
{ | ||
return ( long )n; | ||
} | ||
|
||
if( targetType == typeof( float ) ) | ||
{ | ||
return ( float )n; | ||
} | ||
|
||
if( targetType == typeof( double ) ) | ||
{ | ||
return ( double )n; | ||
} | ||
|
||
if( targetType == typeof( decimal ) ) | ||
{ | ||
return ( decimal )n; | ||
} | ||
|
||
throw new ArgumentException( "Unsupported type", nameof( value ) ); | ||
} | ||
|
||
public object? ConvertBack( object? value, Type targetType, object? parameter, System.Globalization.CultureInfo? culture ) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
private static ByAdding? Converter | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
public override object ProvideValue( IServiceProvider serviceProvider ) | ||
{ | ||
if( Converter == null ) | ||
{ | ||
Converter = new ByAdding(); | ||
} | ||
|
||
return Converter; | ||
} | ||
} | ||
} |
Oops, something went wrong.