调整测试

This commit is contained in:
sanchime 2023-01-15 21:50:01 +08:00
parent 79402fa1f8
commit 27978f8d32
11 changed files with 145 additions and 85 deletions

View File

@ -0,0 +1,33 @@
using Sanchime.Functional.Products;
using Sanchime.Toolkits;
using static Sanchime.Functional.Products.Optional;
namespace Sanchime.Functional.Test;
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void OptionNone()
{
var foo = (Option<int> o) => o;
var result = foo(Optional.None);
Assert.AreEqual(result, Optional.None);
}
[Test]
[TestCase(-1)]
[TestCase(0)]
[TestCase(1)]
[TestCase(null)]
public void OptionMap(int? value)
{
var result = Some(value).Map(x => x * 10);
Assert.AreEqual(result, Some(value * 10));
}
}

View File

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<RootNamespace>Sanchime.Funtional.Test</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Sanchime.Functional\Sanchime.Functional.csproj" />
<ProjectReference Include="..\Sanchime.Toolkits\Sanchime.Toolkits.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1 @@
global using NUnit.Framework;

View File

@ -3,7 +3,7 @@ namespace Sanchime.Functional.Extensions;
public static class EnumExtension public static class EnumExtension
{ {
/// <summary> /// <summary>
/// 将字符串转换为相应的枚举值的<see cref="Optional"/ /// 将字符串转换为相应的枚举值的<see cref="Optional"/>
/// </summary> /// </summary>
public static Option<T> Parse<T>(this string @this) where T : struct public static Option<T> Parse<T>(this string @this) where T : struct
=> Enum.TryParse(@this, out T val) ? Optional.Some(val) : Optional.None; => Enum.TryParse(@this, out T val) ? Optional.Some(val) : Optional.None;

View File

@ -1,3 +1 @@
global using Sanchime.Functional.Products; global using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;

View File

@ -1,14 +1,6 @@
namespace Sanchime.Functional.Products; namespace Sanchime.Functional.Products;
public readonly struct Coyo<V, T> public record struct Coyo<V, T>(V Value, Func<object, T> Func);
{
public V Value {get;}
public Func<object, T> Func { get; }
public Coyo(V value, Func<object, T> func)
=> (Value, Func) = (value, func);
}
public static class Coyo public static class Coyo
{ {

View File

@ -5,11 +5,9 @@ namespace Sanchime.Functional.Products;
/// </summary> /// </summary>
public readonly partial struct None : IOption<Unit> public readonly partial struct None : IOption<Unit>
{ {
public None() private static readonly Unit _value = Unit.Value;
{ static None() { }
}
public bool IsSome => false; public bool IsSome => false;
internal static readonly None Default = new(); internal static readonly None Default = new();
public Unit Value { get; } = Unit.Value; public Unit Value => _value;
} }

View File

@ -1,6 +1,6 @@
namespace Sanchime.Functional.Products; namespace Sanchime.Functional.Products;
public sealed class Unit public sealed class Unit : IEquatable<Unit>
{ {
private static readonly Unit _unique = new(); private static readonly Unit _unique = new();
@ -10,6 +10,11 @@ public sealed class Unit
static Unit() { } static Unit() { }
public override int GetHashCode() => 0; public override int GetHashCode() => 0;
public override bool Equals(object? obj) => this == obj; public override bool Equals(object? obj)
public override string ToString() => "()"; {
return ReferenceEquals(this, obj) || obj is Unit other && Equals(other);
}
public override string ToString() => "()";
public bool Equals(Unit? other) => other != null;
} }

View File

@ -3,64 +3,68 @@ using Sanchime.Functional.Extensions;
using Sanchime.Toolkits; using Sanchime.Toolkits;
try
{
void foo(Option<int> option)
{
var res = option.Map(x => x + 2);
res.WriteLine();
}
"预计打印Some(12)".WriteLine(); // try
foo(10); // {
"预计打印None".WriteLine(); // void foo(Option<int> option)
foo(Optional.None); // {
// var res = option.Map(x => x + 2);
"预计打印Some(String)".WriteLine(); // res.WriteLine();
Optional.Some(1) // }
.Map(x => x + 1.2) //
.Map(x => x.ToString()) // "预计打印Some(12)".WriteLine();
.Map(x => x.GetType().Name) // foo(10);
.WriteLine(); // "预计打印None".WriteLine();
// foo(Optional.None);
// 测试Option的Bind //
var parse = (string s) => Int32.TryParse(s, out int i) ? Optional.Some(i) : Optional.None; // "预计打印None".WriteLine();
var foo1 = (string s) => s.Pipe(parse).Bind(Age.Of); // Optional.Some(Optional.None).WriteLine();
"预计打印Some(111)".WriteLine(); //
foo1("111").WriteLine(); // "预计打印Some(String)".WriteLine();
"预计打印None".WriteLine(); // Optional.Some(1)
foo1("aaa").WriteLine(); // .Map(x => x + 1.2)
"预计打印Some(123)".WriteLine(); // .Map(x => x.ToString())
foo1("123").WriteLine(); // .Map(x => x.GetType().Name)
// 管道 // .WriteLine();
"预计打印None".WriteLine(); //
foo1("1ab").Pipe(x => x.WriteLine()); // // 测试Option的Bind
} // var parse = (string s) => Int32.TryParse(s, out int i) ? Optional.Some(i) : Optional.None;
catch (Exception ex) // var foo1 = (string s) => s.Pipe(parse).Bind(Age.Of);
{ // "预计打印Some(111)".WriteLine();
ex.Message.WriteLine(); // foo1("111").WriteLine();
ex.StackTrace.WriteLine(); // "预计打印None".WriteLine();
} // foo1("aaa").WriteLine();
// "预计打印Some(123)".WriteLine();
public struct Age // foo1("123").WriteLine();
{ // // 管道
private int _value; // "预计打印None".WriteLine();
// foo1("1ab").Pipe(x => x.WriteLine());
public static Option<Age> Of(int age) // }
=> IsValid(age) ? Optional.Some(new Age(age)) : Optional.None; // catch (Exception ex)
// {
private Age(int age) // ex.Message.WriteLine();
{ // ex.StackTrace.WriteLine();
if (!IsValid(age)) // }
{ //
throw new ArgumentException("输入的年龄是无效的"); // public struct Age
} // {
_value = age; // private int _value;
} //
// public static Option<Age> Of(int age)
private static bool IsValid(int age) // => IsValid(age) ? Optional.Some(new Age(age)) : Optional.None;
=> age is (>= 0 and <= 150); //
// private Age(int age)
public override string ToString() // {
=> _value.ToString(); // if (!IsValid(age))
} // {
// throw new ArgumentException("输入的年龄是无效的");
// }
// _value = age;
// }
//
// private static bool IsValid(int age)
// => age is (>= 0 and <= 150);
//
// public override string ToString()
// => _value.ToString();
// }

View File

@ -3,12 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105 VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Test", "Sanchime.Test\Sanchime.Test.csproj", "{852F212D-00AC-45B8-84FB-A97E0E711317}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Functional", "Sanchime.Functional\Sanchime.Functional.csproj", "{DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Functional", "Sanchime.Functional\Sanchime.Functional.csproj", "{DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Toolkits", "Sanchime.Toolkits\Sanchime.Toolkits.csproj", "{3A94EB2F-04F1-46F8-ADD8-DE091402635F}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Toolkits", "Sanchime.Toolkits\Sanchime.Toolkits.csproj", "{3A94EB2F-04F1-46F8-ADD8-DE091402635F}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Functional.Test", "Sanchime.Functional.Test\Sanchime.Functional.Test.csproj", "{2D591AED-CDFD-4598-9826-F6BE7C65C39D}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -18,10 +18,6 @@ Global
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{852F212D-00AC-45B8-84FB-A97E0E711317}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{852F212D-00AC-45B8-84FB-A97E0E711317}.Debug|Any CPU.Build.0 = Debug|Any CPU
{852F212D-00AC-45B8-84FB-A97E0E711317}.Release|Any CPU.ActiveCfg = Release|Any CPU
{852F212D-00AC-45B8-84FB-A97E0E711317}.Release|Any CPU.Build.0 = Release|Any CPU
{DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}.Debug|Any CPU.Build.0 = Debug|Any CPU {DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}.Release|Any CPU.ActiveCfg = Release|Any CPU {DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -30,5 +26,9 @@ Global
{3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Debug|Any CPU.Build.0 = Debug|Any CPU {3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Release|Any CPU.ActiveCfg = Release|Any CPU {3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Release|Any CPU.Build.0 = Release|Any CPU {3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Release|Any CPU.Build.0 = Release|Any CPU
{2D591AED-CDFD-4598-9826-F6BE7C65C39D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D591AED-CDFD-4598-9826-F6BE7C65C39D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D591AED-CDFD-4598-9826-F6BE7C65C39D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D591AED-CDFD-4598-9826-F6BE7C65C39D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -1,2 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=1924a6da_002D8be1_002D43a7_002Da9c4_002D1ced209b35c2/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &amp;lt;Sanchime.Funtional.Test&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Project Location="/home/sanchime/Desktop/C#/Sanchime.Functional/Sanchime.Functional.Test" Presentation="&amp;lt;Sanchime.Functional.Test&amp;gt;" /&gt;
&lt;/SessionState&gt;</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sanchime/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> <s:Boolean x:Key="/Default/UserDictionary/Words/=Sanchime/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>