Skip to content

Commit

Permalink
feat: Added ByAdding and BySubtracting
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Aug 17, 2023
1 parent f0cbe70 commit 24d59ed
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 38 deletions.
66 changes: 66 additions & 0 deletions ValueTransformers-Test/ByAdding.cs
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 ) );
}
}
}
66 changes: 66 additions & 0 deletions ValueTransformers-Test/BySubtracting.cs
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 ) );
}
}
}
27 changes: 21 additions & 6 deletions ValueTransformers-Test/DividedBy.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
/*******************************************************************************
* Copyright (c) 2019, DigiDNA
* All rights reserved
* The MIT License (MIT)
*
* Unauthorised copying of this copyrighted work, via any medium is strictly
* prohibited.
* Proprietary and confidential.
* 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 UnitTests
namespace ValueTransformers_Test
{
[TestClass]
public class DividedBy
Expand Down
27 changes: 21 additions & 6 deletions ValueTransformers-Test/MultipliedBy.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
/*******************************************************************************
* Copyright (c) 2019, DigiDNA
* All rights reserved
* The MIT License (MIT)
*
* Unauthorised copying of this copyrighted work, via any medium is strictly
* prohibited.
* Proprietary and confidential.
* 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 UnitTests
namespace ValueTransformers_Test
{
[TestClass]
public class MultipliedBy
Expand Down
133 changes: 133 additions & 0 deletions ValueTransformers/Converters/ByAdding.cs
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;
}
}
}
Loading

0 comments on commit 24d59ed

Please sign in to comment.