Skip to content

Commit 6ed2b2d

Browse files
committed
falco, string patterns tests
1 parent c25cf8f commit 6ed2b2d

2 files changed

Lines changed: 141 additions & 5 deletions

File tree

src/Falco/String.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ module internal StringPatterns =
6464
| IsBool x when x = false -> Some false
6565
| _ -> None
6666

67+
let (|IsNullOrWhiteSpace|_|) (x : string) =
68+
match String.IsNullOrWhiteSpace x with
69+
| true -> Some ()
70+
| false -> None
71+
6772
let (|IsInt16|_|) = StringParser.parseInt16
6873
let (|IsInt64|_|) = StringParser.parseInt64
6974
let (|IsInt32|_|) = StringParser.parseInt32
@@ -73,8 +78,3 @@ module internal StringPatterns =
7378
let (|IsDateTimeOffset|_|) = StringParser.parseDateTimeOffset
7479
let (|IsTimeSpan|_|) = StringParser.parseTimeSpan
7580
let (|IsGuid|_|) = StringParser.parseGuid
76-
77-
let (|IsNullOrWhiteSpace|_|) (x : string) =
78-
match String.IsNullOrWhiteSpace x with
79-
| true -> Some ()
80-
| false -> None

test/Falco.Tests/StringTests.fs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,139 @@ module StringParser =
122122
StringParser.parseGuid guidStr |> should equal (Some guid)
123123
StringParser.parseGuid "notaguid" |> should equal None
124124
StringParser.parseGuid "" |> should equal None
125+
126+
module StringPatterns =
127+
[<Fact>]
128+
let ``StringPatterns.IsBool should match valid boolean strings`` () =
129+
match "true" with
130+
| StringPatterns.IsBool b -> b |> should equal true
131+
| _ -> failwith "Did not match"
132+
133+
match "FALSE" with
134+
| StringPatterns.IsBool b -> b |> should equal false
135+
| _ -> failwith "Did not match"
136+
137+
match "notabool" with
138+
| StringPatterns.IsBool _ -> failwith "Should not have matched"
139+
| _ -> () // Expected
140+
141+
[<Fact>]
142+
let ``StringPatterns.IsTrue`` () =
143+
match "true" with
144+
| StringPatterns.IsTrue _ -> () // Expected
145+
| _ -> failwith "Did not match"
146+
147+
match "false" with
148+
| StringPatterns.IsTrue _ -> failwith "Should not have matched"
149+
| _ -> () // Expected
150+
151+
[<Fact>]
152+
let ``StringPatterns.IsFalse`` () =
153+
match "false" with
154+
| StringPatterns.IsFalse _ -> () // Expected
155+
| _ -> failwith "Did not match"
156+
157+
match "true" with
158+
| StringPatterns.IsFalse _ -> failwith "Should not have matched"
159+
| _ -> () // Expected
160+
161+
[<Fact>]
162+
let ``StringPatterns.IsNullOrWhiteSpace should match null or whitespace strings`` () =
163+
let isNullOrWhiteSpace (str: string) =
164+
match str with
165+
| StringPatterns.IsNullOrWhiteSpace -> true
166+
| _ -> false
167+
isNullOrWhiteSpace null |> should be True
168+
isNullOrWhiteSpace "" |> should be True
169+
isNullOrWhiteSpace " " |> should be True
170+
isNullOrWhiteSpace "hello" |> should be False
171+
isNullOrWhiteSpace " hello " |> should be False
172+
173+
[<Fact>]
174+
let ``StringPatterns.IsInt16 should match valid int16 strings`` () =
175+
match "123" with
176+
| StringPatterns.IsInt16 i -> i |> should equal 123s
177+
| _ -> failwith "Did not match"
178+
179+
match "notanint" with
180+
| StringPatterns.IsInt16 _ -> failwith "Should not have matched"
181+
| _ -> () // Expected
182+
183+
[<Fact>]
184+
let ``StringPatterns.IsInt32 should match valid int32 strings`` () =
185+
match "123456" with
186+
| StringPatterns.IsInt32 i -> i |> should equal 123456
187+
| _ -> failwith "Did not match"
188+
189+
match "notanint" with
190+
| StringPatterns.IsInt32 _ -> failwith "Should not have matched"
191+
| _ -> () // Expected
192+
193+
[<Fact>]
194+
let ``StringPatterns.IsInt64 should match valid int64 strings`` () =
195+
match "1234567890123" with
196+
| StringPatterns.IsInt64 i -> i |> should equal 1234567890123L
197+
| _ -> failwith "Did not match"
198+
199+
match "notanint" with
200+
| StringPatterns.IsInt64 _ -> failwith "Should not have matched"
201+
| _ -> () // Expected
202+
203+
[<Fact>]
204+
let ``StringPatterns.IsFloat should match valid float strings`` () =
205+
match "123.45" with
206+
| StringPatterns.IsFloat f -> f |> should equal 123.45
207+
| _ -> failwith "Did not match"
208+
209+
match "notafloat" with
210+
| StringPatterns.IsFloat _ -> failwith "Should not have matched"
211+
| _ -> () // Expected
212+
213+
[<Fact>]
214+
let ``StringPatterns.IsDecimal should match valid decimal strings`` () =
215+
match "123.45" with
216+
| StringPatterns.IsDecimal d -> d |> should equal 123.45M
217+
| _ -> failwith "Did not match"
218+
219+
match "notadecimal" with
220+
| StringPatterns.IsDecimal _ -> failwith "Should not have matched"
221+
| _ -> () // Expected
222+
223+
[<Fact>]
224+
let ``StringPatterns.IsDateTime should match valid DateTime strings`` () =
225+
let dateTimeStr = "2021-01-01T12:00:00Z"
226+
let expectedDateTime = DateTime(2021, 1, 1, 12, 0, 0, DateTimeKind.Utc)
227+
228+
match dateTimeStr with
229+
| StringPatterns.IsDateTime dt -> dt |> should equal expectedDateTime
230+
| _ -> failwith "Did not match"
231+
232+
match "notadatetime" with
233+
| StringPatterns.IsDateTime _ -> failwith "Should not have matched"
234+
| _ -> () // Expected
235+
236+
[<Fact>]
237+
let ``StringPatterns.IsDateTimeOffset should match valid DateTimeOffset strings`` () =
238+
let dtoStr = "2021-01-01T12:00:00Z"
239+
let expectedDto = DateTimeOffset(2021, 1, 1, 12, 0, 0, TimeSpan.Zero)
240+
241+
match dtoStr with
242+
| StringPatterns.IsDateTimeOffset dto -> dto |> should equal expectedDto
243+
| _ -> failwith "Did not match"
244+
245+
match "notadatetimeoffset" with
246+
| StringPatterns.IsDateTimeOffset _ -> failwith "Should not have matched"
247+
| _ -> () // Expected
248+
249+
[<Fact>]
250+
let ``StringPatterns.IsGuid should match valid GUID strings`` () =
251+
let guidStr = "d3b07384-d9a0-4c19-9a0c-0305e1b1c8f2"
252+
let expectedGuid = Guid.Parse(guidStr)
253+
254+
match guidStr with
255+
| StringPatterns.IsGuid g -> g |> should equal expectedGuid
256+
| _ -> failwith "Did not match"
257+
258+
match "notaguid" with
259+
| StringPatterns.IsGuid _ -> failwith "Should not have matched"
260+
| _ -> () // Expected

0 commit comments

Comments
 (0)