begin adding tests for regex
This commit is contained in:
@@ -10,7 +10,7 @@ struct
|
||||
, buffer
|
||||
, bufferModifyTime
|
||||
, searchList
|
||||
, searchString
|
||||
, dfa
|
||||
, mode
|
||||
, windowWidth
|
||||
, windowHeight
|
||||
@@ -23,7 +23,7 @@ struct
|
||||
, buffer = buffer
|
||||
, bufferModifyTime = bufferModifyTime
|
||||
, searchList = searchList
|
||||
, searchString = searchString
|
||||
, dfa = dfa
|
||||
, mode = mode
|
||||
, windowWidth = windowWidth
|
||||
, windowHeight = windowHeight
|
||||
|
||||
@@ -15,6 +15,8 @@ sig
|
||||
val nextState: dfa * dfa_state * char -> dfa_state
|
||||
val isFinal: dfa * dfa_state -> bool
|
||||
val isDead: dfa_state -> bool
|
||||
|
||||
val matchString: dfa * string -> (int * int) list
|
||||
end
|
||||
|
||||
functor MakeDfaGen(Fn: DFA_GEN_PARAMS): DFA_GEN =
|
||||
@@ -817,6 +819,41 @@ struct
|
||||
end
|
||||
|
||||
fun isDead (curState: dfa_state) = curState = ~1
|
||||
|
||||
fun helpMatchString (strPos, str, dfa, curState, startPos, prevFinalPos, acc) =
|
||||
if strPos = String.size str then
|
||||
let
|
||||
val acc =
|
||||
if prevFinalPos = ~1 then acc else (startPos, prevFinalPos) :: acc
|
||||
in
|
||||
List.rev acc
|
||||
end
|
||||
else
|
||||
let
|
||||
val chr = String.sub (str, strPos)
|
||||
val newState = nextState (dfa, curState, chr)
|
||||
val prevFinalPos =
|
||||
if isFinal (dfa, newState) then strPos else prevFinalPos
|
||||
in
|
||||
if isDead newState then
|
||||
if prevFinalPos = ~1 then
|
||||
(* restart from startPos *)
|
||||
helpMatchString (startPos + 1, str, dfa, 0, startPos + 1, ~1, acc)
|
||||
else
|
||||
let
|
||||
val acc = (startPos, prevFinalPos) :: acc
|
||||
in
|
||||
helpMatchString
|
||||
(prevFinalPos + 1, str, dfa, 0, prevFinalPos + 1, ~1, acc)
|
||||
end
|
||||
else
|
||||
helpMatchString
|
||||
(strPos + 1, str, dfa, newState, startPos, prevFinalPos, acc)
|
||||
end
|
||||
|
||||
fun matchString (dfa, string) =
|
||||
if Vector.length dfa = 0 then []
|
||||
else helpMatchString (0, string, dfa, 0, 0, ~1, [])
|
||||
end
|
||||
|
||||
structure CaseInsensitiveDfa =
|
||||
|
||||
@@ -67,6 +67,7 @@ $(SML_LIB)/basis/mlton.mlb
|
||||
shell/exception-logger.sml
|
||||
test/Railroad/src/railroad.mlb
|
||||
|
||||
test/regex-tests.sml
|
||||
test/test-utils.sml
|
||||
test/normal-move.sml
|
||||
test/normal-delete.sml
|
||||
|
||||
43
test/regex-tests.sml
Normal file
43
test/regex-tests.sml
Normal file
@@ -0,0 +1,43 @@
|
||||
structure RegexTests =
|
||||
struct
|
||||
open Railroad
|
||||
open Railroad.Test
|
||||
|
||||
structure CiDfa = CaseInsensitiveDfa
|
||||
structure CsDfa = CaseSensitiveDfa
|
||||
|
||||
val caseInsensitiveTests = describe "case insensitive regex"
|
||||
[ test "recognises word 'hello' in string 'Hello world'" (fn _ =>
|
||||
let
|
||||
(* arrange *)
|
||||
val regexString = "hello"
|
||||
val dfa = CiDfa.fromString regexString
|
||||
val inputString = "Hello world"
|
||||
|
||||
(* act *)
|
||||
val matches = CiDfa.matchString (dfa, inputString)
|
||||
|
||||
(* assert *)
|
||||
val expectedMatches = [(0, 4)]
|
||||
in
|
||||
Expect.isTrue (matches = expectedMatches)
|
||||
end)
|
||||
, test "recognises word 'world' in string 'HELLO WORLD'" (fn _ =>
|
||||
let
|
||||
(* arrange *)
|
||||
val regexString = "world"
|
||||
val dfa = CiDfa.fromString regexString
|
||||
val inputString = "HELLO WORLD"
|
||||
|
||||
(* act *)
|
||||
val matches = CiDfa.matchString (dfa, inputString)
|
||||
|
||||
(* assert *)
|
||||
val expectedMatches = [(6, 10)]
|
||||
in
|
||||
Expect.isTrue (matches = expectedMatches)
|
||||
end)
|
||||
]
|
||||
|
||||
val tests = [caseInsensitiveTests]
|
||||
end
|
||||
@@ -6,7 +6,12 @@ struct
|
||||
fun main () =
|
||||
let
|
||||
val tests =
|
||||
List.concat [NormalMove.tests, NormalDelete.tests, Regression.tests]
|
||||
List.concat
|
||||
[ NormalMove.tests
|
||||
, NormalDelete.tests
|
||||
, Regression.tests
|
||||
, RegexTests.tests
|
||||
]
|
||||
val tests = concat tests
|
||||
in
|
||||
runWithConfig [Configuration.PrintPassed false] tests
|
||||
|
||||
Reference in New Issue
Block a user