2010-12-29 00:48:54 -05:00
|
|
|
# Regular Expression Literals
|
|
|
|
# ---------------------------
|
|
|
|
|
2011-01-03 04:17:00 -05:00
|
|
|
# TODO: add method invocation tests: /regex/.toString()
|
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
# * Regexen
|
|
|
|
# * Heregexen
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
test "basic regular expression literals", ->
|
|
|
|
ok 'a'.match(/a/)
|
|
|
|
ok 'a'.match /a/
|
|
|
|
ok 'a'.match(/a/g)
|
|
|
|
ok 'a'.match /a/g
|
|
|
|
|
|
|
|
test "division is not confused for a regular expression", ->
|
|
|
|
eq 2, 4 / 2 / 1
|
|
|
|
|
|
|
|
a = 4
|
|
|
|
b = 2
|
|
|
|
g = 1
|
|
|
|
eq 2, a / b/g
|
|
|
|
|
2011-04-23 13:33:35 -04:00
|
|
|
a = 10
|
|
|
|
b = a /= 4 / 2
|
|
|
|
eq a, 5
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
obj = method: -> 2
|
|
|
|
two = 2
|
|
|
|
eq 2, (obj.method()/two + obj.method()/two)
|
|
|
|
|
|
|
|
i = 1
|
|
|
|
eq 2, (4)/2/i
|
|
|
|
eq 1, i/i/i
|
|
|
|
|
|
|
|
test "#764: regular expressions should be indexable", ->
|
|
|
|
eq /0/['source'], ///#{0}///['source']
|
|
|
|
|
|
|
|
test "#584: slashes are allowed unescaped in character classes", ->
|
|
|
|
ok /^a\/[/]b$/.test 'a//b'
|
|
|
|
|
2011-09-22 04:09:58 -04:00
|
|
|
test "#1724: regular expressions beginning with `*`", ->
|
|
|
|
throws -> CoffeeScript.compile '/*/'
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Heregexe(n|s)
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
test "a heregex will ignore whitespace and comments", ->
|
|
|
|
eq /^I'm\x20+[a]\s+Heregex?\/\/\//gim + '', ///
|
|
|
|
^ I'm \x20+ [a] \s+
|
|
|
|
Heregex? / // # or not
|
|
|
|
///gim + ''
|
|
|
|
|
|
|
|
test "an empty heregex will compile to an empty, non-capturing group", ->
|
|
|
|
eq /(?:)/ + '', /// /// + ''
|
2011-09-22 04:39:13 -04:00
|
|
|
|
|
|
|
test "#1724: regular expressions beginning with `*`", ->
|
|
|
|
throws -> CoffeeScript.compile '/// * ///'
|
2011-10-03 03:47:24 -04:00
|
|
|
|
|
|
|
test "empty regular expressions with flags", ->
|
|
|
|
fn = (x) -> x
|
|
|
|
a = "" + //i
|
|
|
|
fn ""
|
|
|
|
eq '/(?:)/i', a
|
2013-10-21 15:52:36 -04:00
|
|
|
|
|
|
|
test "#3059: don't remove escaped whitespace", ->
|
|
|
|
eq /// One\ cannot [\ ] escape \ \destiny. ///.source,
|
|
|
|
/One cannot[ ]escape \destiny./.source
|
|
|
|
|
|
|
|
test "#2238: don't escape already escaped slashes", ->
|
|
|
|
eq /// \\\/ \/ ///.source, /\\\/\//.source
|
|
|
|
|
|
|
|
test "escaped slashes don't close heregex", ->
|
|
|
|
eq /// \/// ///.source, /\/\/\//.source
|
|
|
|
eq /// \\\////.source, /\\\//.source
|
2013-10-23 18:36:46 -04:00
|
|
|
|
|
|
|
test "escaped linebreaks", ->
|
|
|
|
eq /// \n\
|
|
|
|
\
|
|
|
|
///.source, /\n\n\n/.source
|