Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef4388f

Browse files
ploehlatkin
authored andcommitted
Implement Option.filter
commit ad10115bde4458e5e5b93323ceee96d9bc3831c6Author: latkin <latkin@microsoft.com>Date: Thu Oct 16 18:50:40 2014 -0700 Updating surface area tests, refactoring unit tests to account for older nunit version used internally at msftcommit 3ad70068a9186c837f136268b92199e6b3bc8fb5Merge: 9f6c1a9 b913a52Author: latkin <latkin@microsoft.com>Date: Thu Oct 16 18:25:14 2014 -0700 Merge branch 'option-filter' ofhttps://git01.codeplex.com/forks/ploeh/optionfilter into PRcommit b913a5281d8d125aadb27da8349e6ea52a877484Author: Mark Seemann <mark@ploeh.dk>Date: Wed Apr 16 08:45:26 2014 +0200 Added documentation about Option.filter.commit e937ddf1f1f13865dc1c58007dd92307f2f84855Author: Mark Seemann <mark@ploeh.dk>Date: Wed Apr 16 08:22:51 2014 +0200 Implemented Option.filter correctly, prompted by more tests.commit 55a942a58ad7f5709dc65d1c427e1c768879718eAuthor: Mark Seemann <mark@ploeh.dk>Date: Wed Apr 16 07:55:44 2014 +0200 Demonstrated that None as input into Option.filter produces None as output.commit efb1f5217689f1a1bfb798ef8918adc60efb4626Author: Mark Seemann <mark@ploeh.dk>Date: Wed Apr 16 07:38:19 2014 +0200 Invoked the predicate in order to return None when the predicate returns false. However, using the Devil's Advocate TDD technique, the implementation goes out of its way to do the wrong thing (passing Unchecked.defaultof<'T> instead of the input value), which demonstrates that more test cases are required.commit b08446a3248a69a6c00e0ce099d905ec82d42f62Author: Mark Seemann <mark@ploeh.dk>Date: Wed Apr 16 07:13:37 2014 +0200 Introduced the Option.filter function, although, using TDD, its implementation is currently degenerate, because too few tests have yet been thrown at it. More tests will follow in the next commits.commit da174094ec64d4f7e538df73125b2e706276ea37Author: Mark Seemann <mark@ploeh.dk>Date: Mon Apr 14 08:59:35 2014 +0200 Added a unit test file targeting the Option module. I looked through the entire code base, but couldn't find any unit tests covering the Option module (it's probably covered by either the Cambridge Suite or the Redmond Suite). I even tried to change the implementation of Option.isSome to always return false, to see if that would trigger any test errors, but it didn't. Currently, the Option unit test file doesn't do anything. This commit just adds the file.Refactoring unit tests to account for older nunit version used internally at msft
1 parent929d85b commitef4388f

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core.Unittests.fsproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<CompileInclude="FSharp.Core\Microsoft.FSharp.Core\IntConversions.fs" />
9393
<CompileInclude="FSharp.Core\Microsoft.FSharp.Core\IntConversionsGenerated.fs" />
9494
<NoneInclude="FSharp.Core\Microsoft.FSharp.Core\IntConversionsTestGenerator.fsx" />
95+
<CompileInclude="FSharp.Core\Microsoft.FSharp.Core\OptionModule.fs" />
9596
<CompileInclude="FSharp.Core\Microsoft.FSharp.Core\PrintfTests.fs" />
9697
<CompileInclude="FSharp.Core\Microsoft.FSharp.Control\Cancellation.fs" />
9798
<CompileInclude="FSharp.Core\Microsoft.FSharp.Control\AsyncType.fs" />
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
2+
3+
namespaceFSharp.Core.Unittests.FSharp_Core.Microsoft_FSharp_Core
4+
5+
openNUnit.Framework
6+
7+
// Various tests for the:
8+
// Microsoft.FSharp.Core.Option module
9+
10+
(*
11+
[Test Strategy]
12+
Make sure each method works on:
13+
* Integer option (value type)
14+
* String option (reference type)
15+
* None (0 elements)
16+
*)
17+
18+
[<TestFixture>]
19+
typeOptionModule()=
20+
21+
[<TestCase()>]
22+
memberthis.FilterSomeIntegerWhenPredicateReturnsTrue()=
23+
lettest x=
24+
letactual= x|> Some|> Option.filter(fun _->true)
25+
26+
letexpected= x|> Some
27+
Assert.AreEqual(expected, actual)
28+
[0;1;-1;42]|> List.iter test
29+
30+
[<TestCase()>]
31+
memberthis.FilterSomeStringWhenPredicateReturnsTrue()=
32+
lettest x=
33+
letactual= x|> Some|> Option.filter(fun _->true)
34+
35+
letexpected= x|> Some
36+
Assert.AreEqual(expected, actual)
37+
["";"";"Foo";"Bar"]|> List.iter test
38+
39+
[<TestCase()>]
40+
memberthis.FilterSomeIntegerWhenPredicateReturnsFalse()=
41+
lettest x=
42+
letactual= x|> Some|> Option.filter(fun _->false)
43+
44+
letexpected= None
45+
Assert.AreEqual(expected, actual)
46+
[0;1;-1;1337]|> List.iter test
47+
48+
[<TestCase()>]
49+
memberthis.FilterSomeStringWhenPredicateReturnsFalse()=
50+
lettest x=
51+
letactual= x|> Some|> Option.filter(fun _->false)
52+
53+
letexpected= None
54+
Assert.AreEqual(expected, actual)
55+
["";"";"Ploeh";"Fnaah"]|> List.iter test
56+
57+
[<TestCase()>]
58+
memberthis.FilterNoneReturnsCorrectResult()=
59+
lettest x=
60+
letactual= None|> Option.filter(fun _-> x)
61+
62+
letexpected= None
63+
Assert.AreEqual(expected, actual)
64+
[false;true]|> List.iter test
65+
66+
[<TestCase()>]
67+
memberthis.FilterSomeIntegerWhenPredicateEqualsInput()=
68+
lettest x=
69+
letactual= x|> Some|> Option.filter((=) x)
70+
71+
letexpected= x|> Some
72+
Assert.AreEqual(expected, actual)
73+
[0;1;-1;-2001]|> List.iter test
74+
75+
[<TestCase()>]
76+
memberthis.FilterSomeStringWhenPredicateEqualsInput()=
77+
lettest x=
78+
letactual= x|> Some|> Option.filter((=) x)
79+
80+
letexpected= x|> Some
81+
Assert.AreEqual(expected, actual)
82+
["";"";"Xyzz";"Sgryt"]|> List.iter test
83+
84+
[<TestCase()>]
85+
memberthis.FilterSomeIntegerWhenPredicateDoesNotEqualsInput()=
86+
lettest x=
87+
letactual= x|> Some|> Option.filter((<>) x)
88+
89+
letexpected= None
90+
Assert.AreEqual(expected, actual)
91+
[0;1;-1;927]|> List.iter test
92+
93+
[<TestCase()>]
94+
memberthis.FilterSomeStringWhenPredicateDoesNotEqualsInput()=
95+
lettest x=
96+
letactual= x|> Some|> Option.filter((<>) x)
97+
98+
letexpected= None
99+
Assert.AreEqual(expected, actual)
100+
["";"";"Baz Quux";"Corge grault"]|> List.iter test

‎src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,6 +2633,7 @@ Microsoft.FSharp.Core.OptionModule: Int32 GetHashCode()
26332633
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Collections.FSharpList`1[T] ToList[T](Microsoft.FSharp.Core.FSharpOption`1[T])
26342634
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Bind[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Core.FSharpOption`1[T])
26352635
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpOption`1[T])
2636+
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] Filter[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
26362637
Microsoft.FSharp.Core.OptionModule: System.String ToString()
26372638
Microsoft.FSharp.Core.OptionModule: System.Type GetType()
26382639
Microsoft.FSharp.Core.OptionModule: T GetValue[T](Microsoft.FSharp.Core.FSharpOption`1[T])

‎src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,7 @@ Microsoft.FSharp.Core.OptionModule: Int32 GetHashCode()
26172617
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Collections.FSharpList`1[T] ToList[T](Microsoft.FSharp.Core.FSharpOption`1[T])
26182618
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Bind[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Core.FSharpOption`1[T])
26192619
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpOption`1[T])
2620+
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] Filter[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
26202621
Microsoft.FSharp.Core.OptionModule: System.String ToString()
26212622
Microsoft.FSharp.Core.OptionModule: System.Type GetType()
26222623
Microsoft.FSharp.Core.OptionModule: T GetValue[T](Microsoft.FSharp.Core.FSharpOption`1[T])

‎src/fsharp/FSharp.Core/option.fs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ namespace Microsoft.FSharp.Core
4444
[<CompiledName("Bind")>]
4545
letbind f inp=match inpwith None-> None| Some x-> f x
4646

47+
[<CompiledName("Filter")>]
48+
letfilter f inp=match inpwith None-> None| Some x->if f xthen Some xelse None
49+
4750
[<CompiledName("ToArray")>]
4851
lettoArray option=match optionwith None->[||]| Some x->[| x|]
4952

‎src/fsharp/FSharp.Core/option.fsi‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ namespace Microsoft.FSharp.Core
9494
[<CompiledName("Bind")>]
9595
valbind:binder:('T-> 'U option)->option:'T option->'U option
9696

97+
/// <summary><c>filter f inp</c> evaluates to <c>match inp with None -> None | Some x -> if f x then Some x else None</c>.</summary>
98+
/// <param name="predicate">A function that evaluates whether the value contained in the option should remain, or be filtered out.</param>
99+
/// <param name="option">The input option.</param>
100+
/// <returns>The input if the predicate evaluates to true; otherwise, None.</returns>
101+
[<CompiledName("Filter")>]
102+
valfilter:predicate:('T-> bool)->option:'T option->'T option
97103

98104
/// <summary>Convert the option to an array of length 0 or 1.</summary>
99105
/// <param name="option">The input option.</param>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp