调整测试
This commit is contained in:
parent
79402fa1f8
commit
27978f8d32
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -0,0 +1 @@
|
|||
global using NUnit.Framework;
|
|
@ -3,7 +3,7 @@ namespace Sanchime.Functional.Extensions;
|
|||
public static class EnumExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 将字符串转换为相应的枚举值的<see cref="Optional"/
|
||||
/// 将字符串转换为相应的枚举值的<see cref="Optional"/>
|
||||
/// </summary>
|
||||
public static Option<T> Parse<T>(this string @this) where T : struct
|
||||
=> Enum.TryParse(@this, out T val) ? Optional.Some(val) : Optional.None;
|
||||
|
|
|
@ -1,3 +1 @@
|
|||
global using Sanchime.Functional.Products;
|
||||
|
||||
namespace Sanchime.Functional.Extensions;
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
namespace Sanchime.Functional.Products;
|
||||
|
||||
public readonly struct Coyo<V, T>
|
||||
{
|
||||
public V Value {get;}
|
||||
|
||||
public Func<object, T> Func { get; }
|
||||
|
||||
public Coyo(V value, Func<object, T> func)
|
||||
=> (Value, Func) = (value, func);
|
||||
}
|
||||
public record struct Coyo<V, T>(V Value, Func<object, T> Func);
|
||||
|
||||
public static class Coyo
|
||||
{
|
||||
|
|
|
@ -5,11 +5,9 @@ namespace Sanchime.Functional.Products;
|
|||
/// </summary>
|
||||
public readonly partial struct None : IOption<Unit>
|
||||
{
|
||||
public None()
|
||||
{
|
||||
|
||||
}
|
||||
private static readonly Unit _value = Unit.Value;
|
||||
static None() { }
|
||||
public bool IsSome => false;
|
||||
internal static readonly None Default = new();
|
||||
public Unit Value { get; } = Unit.Value;
|
||||
public Unit Value => _value;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace Sanchime.Functional.Products;
|
||||
|
||||
public sealed class Unit
|
||||
public sealed class Unit : IEquatable<Unit>
|
||||
{
|
||||
private static readonly Unit _unique = new();
|
||||
|
||||
|
@ -10,6 +10,11 @@ public sealed class Unit
|
|||
static Unit() { }
|
||||
|
||||
public override int GetHashCode() => 0;
|
||||
public override bool Equals(object? obj) => this == obj;
|
||||
public override string ToString() => "()";
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return ReferenceEquals(this, obj) || obj is Unit other && Equals(other);
|
||||
}
|
||||
|
||||
public override string ToString() => "()";
|
||||
public bool Equals(Unit? other) => other != null;
|
||||
}
|
||||
|
|
|
@ -3,64 +3,68 @@ using Sanchime.Functional.Extensions;
|
|||
using Sanchime.Toolkits;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
void foo(Option<int> option)
|
||||
{
|
||||
var res = option.Map(x => x + 2);
|
||||
res.WriteLine();
|
||||
}
|
||||
|
||||
"预计打印Some(12)".WriteLine();
|
||||
foo(10);
|
||||
"预计打印None".WriteLine();
|
||||
foo(Optional.None);
|
||||
|
||||
"预计打印Some(String)".WriteLine();
|
||||
Optional.Some(1)
|
||||
.Map(x => x + 1.2)
|
||||
.Map(x => x.ToString())
|
||||
.Map(x => x.GetType().Name)
|
||||
.WriteLine();
|
||||
|
||||
// 测试Option的Bind
|
||||
var parse = (string s) => Int32.TryParse(s, out int i) ? Optional.Some(i) : Optional.None;
|
||||
var foo1 = (string s) => s.Pipe(parse).Bind(Age.Of);
|
||||
"预计打印Some(111)".WriteLine();
|
||||
foo1("111").WriteLine();
|
||||
"预计打印None".WriteLine();
|
||||
foo1("aaa").WriteLine();
|
||||
"预计打印Some(123)".WriteLine();
|
||||
foo1("123").WriteLine();
|
||||
// 管道
|
||||
"预计打印None".WriteLine();
|
||||
foo1("1ab").Pipe(x => x.WriteLine());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.Message.WriteLine();
|
||||
ex.StackTrace.WriteLine();
|
||||
}
|
||||
|
||||
public struct Age
|
||||
{
|
||||
private int _value;
|
||||
|
||||
public static Option<Age> Of(int age)
|
||||
=> IsValid(age) ? Optional.Some(new Age(age)) : Optional.None;
|
||||
|
||||
private Age(int age)
|
||||
{
|
||||
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();
|
||||
}
|
||||
// try
|
||||
// {
|
||||
// void foo(Option<int> option)
|
||||
// {
|
||||
// var res = option.Map(x => x + 2);
|
||||
// res.WriteLine();
|
||||
// }
|
||||
//
|
||||
// "预计打印Some(12)".WriteLine();
|
||||
// foo(10);
|
||||
// "预计打印None".WriteLine();
|
||||
// foo(Optional.None);
|
||||
//
|
||||
// "预计打印None".WriteLine();
|
||||
// Optional.Some(Optional.None).WriteLine();
|
||||
//
|
||||
// "预计打印Some(String)".WriteLine();
|
||||
// Optional.Some(1)
|
||||
// .Map(x => x + 1.2)
|
||||
// .Map(x => x.ToString())
|
||||
// .Map(x => x.GetType().Name)
|
||||
// .WriteLine();
|
||||
//
|
||||
// // 测试Option的Bind
|
||||
// var parse = (string s) => Int32.TryParse(s, out int i) ? Optional.Some(i) : Optional.None;
|
||||
// var foo1 = (string s) => s.Pipe(parse).Bind(Age.Of);
|
||||
// "预计打印Some(111)".WriteLine();
|
||||
// foo1("111").WriteLine();
|
||||
// "预计打印None".WriteLine();
|
||||
// foo1("aaa").WriteLine();
|
||||
// "预计打印Some(123)".WriteLine();
|
||||
// foo1("123").WriteLine();
|
||||
// // 管道
|
||||
// "预计打印None".WriteLine();
|
||||
// foo1("1ab").Pipe(x => x.WriteLine());
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// ex.Message.WriteLine();
|
||||
// ex.StackTrace.WriteLine();
|
||||
// }
|
||||
//
|
||||
// public struct Age
|
||||
// {
|
||||
// private int _value;
|
||||
//
|
||||
// public static Option<Age> Of(int age)
|
||||
// => IsValid(age) ? Optional.Some(new Age(age)) : Optional.None;
|
||||
//
|
||||
// private Age(int age)
|
||||
// {
|
||||
// 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();
|
||||
// }
|
||||
|
|
12
Sanchime.sln
12
Sanchime.sln
|
@ -3,12 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30114.105
|
||||
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}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Toolkits", "Sanchime.Toolkits\Sanchime.Toolkits.csproj", "{3A94EB2F-04F1-46F8-ADD8-DE091402635F}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Functional.Test", "Sanchime.Functional.Test\Sanchime.Functional.Test.csproj", "{2D591AED-CDFD-4598-9826-F6BE7C65C39D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -18,10 +18,6 @@ Global
|
|||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
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.Build.0 = Debug|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}.Release|Any CPU.ActiveCfg = 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
|
||||
EndGlobal
|
||||
|
|
|
@ -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">
|
||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=1924a6da_002D8be1_002D43a7_002Da9c4_002D1ced209b35c2/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &lt;Sanchime.Funtional.Test&gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<Project Location="/home/sanchime/Desktop/C#/Sanchime.Functional/Sanchime.Functional.Test" Presentation="&lt;Sanchime.Functional.Test&gt;" />
|
||||
</SessionState></s:String>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sanchime/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
Loading…
Reference in New Issue