Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
module ArraySegmentPrimitivesTest | ||
open System | ||
|
||
let (!!) str = List.ofSeq str | ||
This comment has been minimized.
Sorry, something went wrong. |
||
let (!!!) str = ArraySegment<_>(str) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
let (!!+) str offset = ArraySegment<_>(str, offset, str.Length - offset) | ||
This comment has been minimized.
Sorry, something went wrong.
panesofglass
Author
Owner
|
||
let (!+) str = !!+str 1 | ||
This comment has been minimized.
Sorry, something went wrong.
panesofglass
Author
Owner
|
||
|
||
open NUnit.Framework | ||
open FsUnit | ||
|
||
open Cashel | ||
open Cashel.ArraySegmentPrimitives | ||
|
||
let inline (===>) (actual: (_ * ArraySegment<_>) option) (expected: (_ * ArraySegment<_>) option) = | ||
This comment has been minimized.
Sorry, something went wrong.
panesofglass
Author
Owner
|
||
if actual.IsNone && expected.IsNone then actual |> should equal expected | ||
else | ||
let avalue, asegment = actual |> Option.get | ||
let evalue, esegment = expected |> Option.get | ||
evalue = avalue |> should be True | ||
asegment.Array |> should equal esegment.Array | ||
asegment.Offset |> should equal esegment.Offset | ||
asegment.Count |> should equal esegment.Count | ||
|
||
let test = "test"B | ||
let test_me = "test me"B | ||
let ttttest = "ttttest"B | ||
|
||
[<Test>] | ||
let test_token () = | ||
let exp = Some('t'B, !+test) | ||
token !!!test ===> exp | ||
|
||
[<Test>] | ||
let test_token_empty_list () = | ||
token !!!""B ===> None | ||
|
||
[<Test>] | ||
let test_token_single_token () = | ||
let t = "t"B | ||
let exp = Some('t'B, !+t) | ||
token !!!t ===> exp | ||
|
||
[<Test>] | ||
let test_ignore_with_token () = | ||
let exp = Some((), !+test) | ||
forget token !!!test ===> exp | ||
|
||
[<Test>] | ||
let test_listify_with_token () = | ||
let exp = Some(['t'B], !+test) | ||
listify token !!!test ===> exp | ||
|
||
[<Test>] | ||
let test_filter_simple_predicate () = | ||
let exp = Some('t'B, !+test) | ||
filter token (fun x -> x = 't'B) !!!test ===> exp | ||
|
||
[<Test>] | ||
let test_filter_failure_predicate () = | ||
filter token (fun x -> x = 'e'B) !!!test ===> None | ||
|
||
[<Test>] | ||
let test_any_success_predicate () = | ||
any ['q'B..'v'B] !!!test ===> (Some('t'B, !+test)) | ||
|
||
[<Test>] | ||
let test_any_failure_predicate () = | ||
any ['a'B..'e'B] !!!test ===> None | ||
|
||
[<Test>] | ||
let test_matchToken () = | ||
matchToken 't'B !!!test ===> (Some('t'B, !+test)) | ||
|
||
[<Test>] | ||
let test_matchToken_failure () = | ||
matchToken 'e'B !!!test ===> None | ||
|
||
[<Test>] | ||
let test_matchTokens () = | ||
matchTokens !!test !!!test_me ===> (Some(!!test, !!+test_me 4)) | ||
|
||
[<Test>] | ||
let test_matchTokens_failue () = | ||
matchTokens !!test !!!"tesp me"B ===> None | ||
|
||
[<Test>] | ||
let test_eof () = | ||
eof !!!""B ===> (Some((),!!!""B)) | ||
|
||
[<Test>] | ||
let test_eof_fails_not_at_end () = | ||
eof !!!test ===> None | ||
|
||
[<Test>] | ||
let test_repeat () = | ||
repeat (matchToken 't'B) !!!ttttest ===> (Some(!!"tttt"B, !!+ttttest 4)) | ||
|
||
[<Test>] | ||
let test_repeat_one_match() = | ||
repeat (matchToken 't'B) !!!test ===> (Some(['t'B], !+test)) | ||
|
||
[<Test>] | ||
let test_repeat_no_matches () = | ||
repeat (matchToken 'e'B) !!!ttttest ===> (Some([], !!!ttttest)) | ||
|
||
[<Test>] | ||
let test_repeat1 () = | ||
repeat1 (matchToken 't'B) !!!ttttest ===> (Some(!!"tttt"B, !!+ttttest 4)) | ||
|
||
[<Test>] | ||
let test_repeat1_one_match() = | ||
repeat1 (matchToken 't'B) !!!test ===> (Some(['t'B], !+test)) | ||
|
||
[<Test>] | ||
let test_repeat1_no_matches () = | ||
repeat1 (matchToken 'e'B) !!!ttttest ===> None | ||
|
||
[<Test>] | ||
let test_failure_predicate_parser_success() = | ||
!~ (matchToken 't'B) !!!test ===> None | ||
|
||
[<Test>] | ||
let test_failure_predicate_parser_fails () = | ||
!~ (matchToken 'e'B) !!!test ===> (Some((), !!!test)) | ||
|
||
[<Test>] | ||
let test_Success_predicate_parser_success() = | ||
!& (matchToken 't'B) !!!test ===> (Some((), !!!test)) | ||
|
||
[<Test>] | ||
let test_success_predicate_parser_fails () = | ||
!& (matchToken 'e'B) !!!test ===> None | ||
|
||
[<Test>] | ||
let test_option_predicate_one () = | ||
!? (matchToken 't'B) !!!test ===> (Some(Some('t'B), !+test)) | ||
|
||
[<Test>] | ||
let test_option_predicate_zero () = | ||
!? (matchToken 'e'B) !!!test ===> (Some(None, !!!test)) | ||
|
||
[<Test>] | ||
let test_ignore_left () = | ||
((matchToken 't'B) .>> (matchToken 'e'B)) !!!test ===> (Some('t'B, !!+test 2)) | ||
|
||
[<Test>] | ||
let test_ignore_left_fails () = | ||
((matchToken 'e'B) .>> (matchToken 'e'B)) !!!test ===> None | ||
|
||
[<Test>] | ||
let test_ignore_right () = | ||
((matchToken 't'B) >>. (matchToken 'e'B)) !!!test ===> (Some('e'B, !!+test 2)) | ||
|
||
[<Test>] | ||
let test_ignore_right_fails () = | ||
((matchToken 't'B) >>. (matchToken 's'B)) !!!test ===> None | ||
|
||
[<Test>] | ||
let test_parse_return_value () = | ||
((matchToken 't'B) >>! !!"hello"B) !!!test ===> (Some(!!"hello"B, !+test)) | ||
|
||
[<Test>] | ||
let test_parse_return_value_fails () = | ||
((matchToken 'q'B) >>! !!"hello"B) !!!test ===> None | ||
|
||
[<Test>] | ||
let test_until () = | ||
(until token (matchToken 's'B)) !!!test ===> (Some(!!"te"B, !!+test 3)) | ||
|
||
[<Test>] | ||
let test_until_fail_1 () = | ||
(until token (matchToken 'q'B)) !!!test ===> None | ||
|
||
[<Test>] | ||
let test_until_fail_2 () = | ||
(until (matchToken 'q'B) (matchToken 's'B)) !!!test ===> None | ||
|
||
[<Test>] | ||
let test_skip () = | ||
let exp = Some((), !+test) | ||
skip 't'B !!!test ===> exp | ||
|
||
[<Test>] | ||
let test_skip_fail () = | ||
skip 'e'B !!!test ===> None | ||
|
||
[<Test>] | ||
let test_skips () = | ||
let exp = Some((), !!+test 2) | ||
skips !!"te"B !!!test ===> exp | ||
|
||
[<Test>] | ||
let test_skips_fail () = | ||
skips !!"ts"B !!!test ===> None |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Cashel | ||
|
||
module ArraySegmentPrimitives = | ||
This comment has been minimized.
Sorry, something went wrong.
panesofglass
Author
Owner
|
||
open System | ||
open Cashel | ||
|
||
//-------------------------ArraySegment helpers---------------------------------------------- | ||
|
||
let slice (input:ArraySegment<_>) = | ||
match input with | ||
| x when x.Count = 0 -> None | ||
| x -> Some(x.Array.[x.Offset], ArraySegment<'a>(x.Array, x.Offset + 1, x.Count - 1)) | ||
|
||
//-------------------------ArraySegment primitives------------------------------------------- | ||
|
||
///token assumes the input is a list and returns a tuple of the head and tail | ||
let token : Parser<ArraySegment<'a>, 'a> = slice | ||
This comment has been minimized.
Sorry, something went wrong.
panesofglass
Author
Owner
|
||
|
||
///eof checks that we're at the end of the list being parsed | ||
let eof : Parser<ArraySegment<'a>, unit> = fun input -> | ||
This comment has been minimized.
Sorry, something went wrong. |
||
match input with | ||
| x when x.Count = 0 -> Some((), x) | ||
| _ -> None | ||
|
||
///any checks the value at the start of the input is in the list of items l | ||
let any l = filter token (fun x -> l |> List.exists (fun y -> x = y)) | ||
|
||
///item_equal checks the value at the start of the input matches the value v | ||
let matchToken v = filter token (fun x -> x = v) | ||
|
||
///items_equal recursively uses item_equal to check to see if a list of values l matches the start of the input | ||
let rec matchTokens l = | ||
This comment has been minimized.
Sorry, something went wrong.
panesofglass
Author
Owner
|
||
match l with | ||
| [] -> result [] | ||
| x::xs -> matchToken x >>= (fun i -> matchTokens xs >>= (fun is -> result (i::is))) | ||
|
||
///skip_item calls item_equal but tosses the parse value | ||
let skip v = matchToken v |> forget | ||
|
||
///skip_items calls items_equal but tosses the parse value | ||
let skips l = matchTokens l |> forget | ||
|
||
///listify turns the result of parsing function p into a single item list | ||
let listify p = p >>= (fun x -> result [x]) | ||
|
||
|
Creates a list from a given string.