4 people like it.

Contains Phrase

Returns a value indicating whether the specified phrase occurs within the given text.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
open System

/// Returns a value indicating whether the specified phrase occurs within the given text
let containsPhrase (phrase:string) (text:string) =
    let rec contains index =
        if index <= text.Length - phrase.Length then compare index
        else false
    and compare index =        
        if String.Compare(text, index, phrase, 0, phrase.Length) <> 0
        then nextWord index
        else true
    and nextWord index =
        let index = text.IndexOf(' ', index)
        if index >= 0 then contains (index+1)
        else false     
    contains 0

open NUnit.Framework

[<TestFixture>]
module ``Sample Tests`` =

    [<TestCase("", "")>]
    [<TestCase("", " ")>]
    [<TestCase("", "  ")>]
    let [<Test>] ``Match empty phrase`` (phrase:string, text:string) =
        Assert.IsTrue( containsPhrase phrase text )

    [<TestCase("A", "A")>]
    [<TestCase("B", " B")>]
    [<TestCase("C", "C ")>]
    [<TestCase("C", " C ")>]
    [<TestCase("C", "  C ")>]
    let [<Test>] ``Finds single character phrase`` (phrase:string, text:string) =
        Assert.IsTrue( containsPhrase phrase text )

    [<TestCase("apple", "apple")>]
    [<TestCase("orange", " orange")>]
    [<TestCase("pear", "pear ")>]
    [<TestCase("banana", " banana ")>]
    [<TestCase("lemon", "  lemon ")>]
    let [<Test>] ``Finds multiple character phrase`` (phrase:string, text:string) =
        Assert.IsTrue( containsPhrase phrase text )

    [<TestCase("green apple", "green apple")>]
    [<TestCase("orange orange", " orange orange")>]
    [<TestCase("pair of pears", "pair of pears ")>]
    [<TestCase("yellow banana", " yellow banana ")>]
    [<TestCase("zesty lemon", "  zesty lemon ")>]
    let [<Test>] ``Finds multiple word phrase`` (phrase:string, text:string) =
        Assert.IsTrue( containsPhrase phrase text )

    [<TestCase("tea", "steak")>]
    let [<Test>] ``Only finds exact phrases`` (phrase:string, text:string) =
        Assert.IsFalse( containsPhrase phrase text )
namespace System
val containsPhrase : phrase:string -> text:string -> bool

Full name: Script.containsPhrase


 Returns a value indicating whether the specified phrase occurs within the given text
val phrase : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val text : string
val contains : (int -> bool)
val index : int
property String.Length: int
val compare : (int -> bool)
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
String.Compare(strA: string, strB: string) : int
String.Compare(strA: string, strB: string, comparisonType: StringComparison) : int
String.Compare(strA: string, strB: string, ignoreCase: bool) : int
String.Compare(strA: string, strB: string, ignoreCase: bool, culture: Globalization.CultureInfo) : int
String.Compare(strA: string, strB: string, culture: Globalization.CultureInfo, options: Globalization.CompareOptions) : int
String.Compare(strA: string, indexA: int, strB: string, indexB: int, length: int) : int
String.Compare(strA: string, indexA: int, strB: string, indexB: int, length: int, comparisonType: StringComparison) : int
String.Compare(strA: string, indexA: int, strB: string, indexB: int, length: int, ignoreCase: bool) : int
String.Compare(strA: string, indexA: int, strB: string, indexB: int, length: int, culture: Globalization.CultureInfo, options: Globalization.CompareOptions) : int
String.Compare(strA: string, indexA: int, strB: string, indexB: int, length: int, ignoreCase: bool, culture: Globalization.CultureInfo) : int
val nextWord : (int -> bool)
String.IndexOf(value: string) : int
String.IndexOf(value: char) : int
String.IndexOf(value: string, comparisonType: StringComparison) : int
String.IndexOf(value: string, startIndex: int) : int
String.IndexOf(value: char, startIndex: int) : int
String.IndexOf(value: string, startIndex: int, comparisonType: StringComparison) : int
String.IndexOf(value: string, startIndex: int, count: int) : int
String.IndexOf(value: char, startIndex: int, count: int) : int
String.IndexOf(value: string, startIndex: int, count: int, comparisonType: StringComparison) : int
namespace NUnit
namespace NUnit.Framework
Multiple items
type TestFixtureAttribute =
  inherit Attribute
  new : unit -> TestFixtureAttribute + 1 overload
  member Arguments : obj[]
  member Categories : IList
  member Category : string with get, set
  member Description : string with get, set
  member Ignore : bool with get, set
  member IgnoreReason : string with get, set
  member TypeArgs : Type[] with get, set

Full name: NUnit.Framework.TestFixtureAttribute

--------------------
TestFixtureAttribute() : unit
TestFixtureAttribute([<ParamArray>] arguments: obj []) : unit
Multiple items
type TestCaseAttribute =
  inherit Attribute
  new : [<ParamArray>] arguments:obj[] -> TestCaseAttribute + 3 overloads
  member Arguments : obj[]
  member Categories : IList
  member Category : string with get, set
  member Description : string with get, set
  member ExpectedException : Type with get, set
  member ExpectedExceptionName : string with get, set
  member ExpectedMessage : string with get, set
  member ExpectedResult : obj with get, set
  member Explicit : bool with get, set
  ...

Full name: NUnit.Framework.TestCaseAttribute

--------------------
TestCaseAttribute([<ParamArray>] arguments: obj []) : unit
TestCaseAttribute(arg: obj) : unit
TestCaseAttribute(arg1: obj, arg2: obj) : unit
TestCaseAttribute(arg1: obj, arg2: obj, arg3: obj) : unit
Multiple items
type TestAttribute =
  inherit Attribute
  new : unit -> TestAttribute
  member Description : string with get, set

Full name: NUnit.Framework.TestAttribute

--------------------
TestAttribute() : unit
val ( Match empty phrase ) : phrase:string * text:string -> unit

Full name: Script.Sample Tests.( Match empty phrase )
type Assert =
  static member AreEqual : expected:int * actual:int -> unit + 23 overloads
  static member AreNotEqual : expected:int * actual:int -> unit + 23 overloads
  static member AreNotSame : expected:obj * actual:obj -> unit + 2 overloads
  static member AreSame : expected:obj * actual:obj -> unit + 2 overloads
  static member ByVal : actual:obj * expression:IResolveConstraint -> unit + 2 overloads
  static member Catch : code:TestDelegate -> Exception + 8 overloads
  static member Contains : expected:obj * actual:ICollection -> unit + 2 overloads
  static member Counter : int
  static member DoesNotThrow : code:TestDelegate -> unit + 2 overloads
  static member Equals : a:obj * b:obj -> bool
  ...

Full name: NUnit.Framework.Assert
Assert.IsTrue(condition: bool) : unit
Assert.IsTrue(condition: bool, message: string) : unit
Assert.IsTrue(condition: bool, message: string, [<ParamArray>] args: obj []) : unit
val ( Finds single character phrase ) : phrase:string * text:string -> unit

Full name: Script.Sample Tests.( Finds single character phrase )
val ( Finds multiple character phrase ) : phrase:string * text:string -> unit

Full name: Script.Sample Tests.( Finds multiple character phrase )
val ( Finds multiple word phrase ) : phrase:string * text:string -> unit

Full name: Script.Sample Tests.( Finds multiple word phrase )
val ( Only finds exact phrases ) : phrase:string * text:string -> unit

Full name: Script.Sample Tests.( Only finds exact phrases )
Assert.IsFalse(condition: bool) : unit
Assert.IsFalse(condition: bool, message: string) : unit
Assert.IsFalse(condition: bool, message: string, [<ParamArray>] args: obj []) : unit
Raw view New version

More information

Link:http://fssnip.net/f6
Posted:4 years ago
Author:Phillip Trelford
Tags: text