mirror of
https://github.com/robertkrimen/otto
synced 2025-09-28 18:45:22 +08:00
Sometimes RegExp captures can fail, detect this
Fix #2 (In .exec so far)
This commit is contained in:
parent
89e26f042e
commit
a12391834e
2
Makefile
2
Makefile
|
@ -2,7 +2,6 @@
|
|||
|
||||
export TERST_BASE=$(PWD)
|
||||
|
||||
TEST := -v --run RegExp
|
||||
TEST := -v --run Otto
|
||||
TEST := -v --run underscore
|
||||
TEST := -v --run underscoreCollection
|
||||
|
@ -20,6 +19,7 @@ TEST := -v --run OttoError
|
|||
TEST := -v --run API
|
||||
TEST := -v --run IsValidRegExp
|
||||
TEST := -v --run ParseFailure
|
||||
TEST := -v --run RegExp
|
||||
TEST := .
|
||||
|
||||
test: test-i
|
||||
|
|
11
otto_test.go
11
otto_test.go
|
@ -1468,6 +1468,17 @@ func TestRegExp(t *testing.T) {
|
|||
test(`/abc/.toString()`, "/abc/")
|
||||
test(`/abc/gim.toString()`, "/abc/gim")
|
||||
test(`""+/abc/gi`, "/abc/gi")
|
||||
|
||||
result := test(`/(a)?/.exec('b')`, ",")
|
||||
Is(result._object().Get("0"), "")
|
||||
Is(result._object().Get("1"), "undefined")
|
||||
Is(result._object().Get("length"), "2")
|
||||
|
||||
result = test(`/(a)?(b)?/.exec('b')`, "b,,b")
|
||||
Is(result._object().Get("0"), "b")
|
||||
Is(result._object().Get("1"), "undefined")
|
||||
Is(result._object().Get("2"), "b")
|
||||
Is(result._object().Get("length"), "3")
|
||||
}
|
||||
|
||||
func TestnewFunction(t *testing.T) {
|
||||
|
|
|
@ -89,7 +89,11 @@ func execResultToArray(runtime *_runtime, target string, result []int) *_object
|
|||
valueArray := make([]Value, captureCount)
|
||||
for index := 0; index < captureCount; index++ {
|
||||
offset := 2 * index
|
||||
valueArray[index] = toValue(target[result[offset]:result[offset+1]])
|
||||
if result[offset] != -1 {
|
||||
valueArray[index] = toValue(target[result[offset]:result[offset+1]])
|
||||
} else {
|
||||
valueArray[index] = UndefinedValue()
|
||||
}
|
||||
}
|
||||
return runtime.newArray(valueArray)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user