| 24 | ) |
| 25 | |
| 26 | func TestRegex(t *testing.T) { |
| 27 | regexTests := []struct { |
| 28 | expr string |
| 29 | }{ |
| 30 | // Tests for replace Function |
| 31 | {expr: "regex.replace('abc', '^', 'start_') == 'start_abc'"}, |
| 32 | {expr: "regex.replace('abc', '$', '_end') == 'abc_end'"}, |
| 33 | {expr: `regex.replace('a-b', r'\b', '|') == '|a|-|b|'`}, |
| 34 | {expr: `regex.replace('foo bar', '(fo)o (ba)r', r'\2 \1') == 'ba fo'`}, |
| 35 | {expr: `regex.replace('foo bar', 'foo', r'\\') == '\\ bar'`}, |
| 36 | {expr: "regex.replace('banana', 'ana', 'x') == 'bxna'"}, |
| 37 | {expr: `regex.replace('abc', 'b(.)', r'x\1') == 'axc'`}, |
| 38 | {expr: "regex.replace('hello world hello', 'hello', 'hi') == 'hi world hi'"}, |
| 39 | {expr: `regex.replace('ac', 'a(b)?c', r'[\1]') == '[]'`}, |
| 40 | {expr: "regex.replace('apple pie', 'p', 'X') == 'aXXle Xie'"}, |
| 41 | {expr: `regex.replace('remove all spaces', r'\s', '') == 'removeallspaces'`}, |
| 42 | {expr: `regex.replace('digit:99919291992', r'\d+', '3') == 'digit:3'`}, |
| 43 | {expr: `regex.replace('foo bar baz', r'\w+', r'(\0)') == '(foo) (bar) (baz)'`}, |
| 44 | {expr: "regex.replace('', 'a', 'b') == ''"}, |
| 45 | {expr: `regex.replace('User: Alice, Age: 30', r'User: (?P<name>\w+), Age: (?P<age>\d+)', '${name} is ${age} years old') == '${name} is ${age} years old'`}, |
| 46 | {expr: `regex.replace('User: Alice, Age: 30', r'User: (?P<name>\w+), Age: (?P<age>\d+)', r'\1 is \2 years old') == 'Alice is 30 years old'`}, |
| 47 | {expr: "regex.replace('hello ☃', '☃', '❄') == 'hello ❄'"}, |
| 48 | {expr: `regex.replace('id=123', r'id=(?P<value>\d+)', r'value: \1') == 'value: 123'`}, |
| 49 | {expr: "regex.replace('banana', 'a', 'x') == 'bxnxnx'"}, |
| 50 | {expr: `regex.replace(regex.replace('%(foo) %(bar) %2', r'%\((\w+)\)', r'${\1}'),r'%(\d+)', r'$\1') == '${foo} ${bar} $2'`}, |
| 51 | |
| 52 | // Tests for replace Function with count variable |
| 53 | {expr: "regex.replace('banana', 'a', 'x', 0) == 'banana'"}, |
| 54 | {expr: "regex.replace('banana', 'a', 'x', 1) == 'bxnana'"}, |
| 55 | {expr: "regex.replace('banana', 'a', 'x', 2) == 'bxnxna'"}, |
| 56 | {expr: "regex.replace('banana', 'a', 'x', 100) == 'bxnxnx'"}, |
| 57 | {expr: "regex.replace('banana', 'a', 'x', -1) == 'bxnxnx'"}, |
| 58 | {expr: "regex.replace('banana', 'a', 'x', -100) == 'bxnxnx'"}, |
| 59 | {expr: `regex.replace('cat-dog dog-cat cat-dog dog-cat', '(cat)-(dog)', r'\2-\1', 1) == 'dog-cat dog-cat cat-dog dog-cat'`}, |
| 60 | {expr: `regex.replace('cat-dog dog-cat cat-dog dog-cat', '(cat)-(dog)', r'\2-\1', 2) == 'dog-cat dog-cat dog-cat dog-cat'`}, |
| 61 | {expr: `regex.replace('a.b.c', r'\.', '-', 1) == 'a-b.c'`}, |
| 62 | {expr: `regex.replace('a.b.c', r'\.', '-', -1) == 'a-b-c'`}, |
| 63 | {expr: `regex.replace('abc def', r'(abc)', r'\\1') == r'\1 def'`}, |
| 64 | {expr: `regex.replace('abc def', r'(abc)', r'\\2') == r'\2 def'`}, |
| 65 | {expr: `regex.replace('abc def', r'(abc)', r'\\{word}') == '\\{word} def'`}, |
| 66 | {expr: `regex.replace('abc def', r'(abc)', r'\\word') == '\\word def'`}, |
| 67 | |
| 68 | // Tests for extract Function |
| 69 | {expr: "regex.extract('hello world', 'hello(.*)') == optional.of(' world')"}, |
| 70 | {expr: `regex.extract('item-A, item-B', r'item-(\w+)') == optional.of('A')`}, |
| 71 | {expr: `regex.extract('The color is red', r'The color is (\w+)') == optional.of('red')`}, |
| 72 | {expr: `regex.extract('The color is red', r'The color is \w+') == optional.of('The color is red')`}, |
| 73 | {expr: "regex.extract('brand', 'brand') == optional.of('brand')"}, |
| 74 | {expr: "regex.extract('hello world', 'goodbye (.*)') == optional.none()"}, |
| 75 | {expr: "regex.extract('HELLO', 'hello') == optional.none()"}, |
| 76 | {expr: `regex.extract('', r'\w+') == optional.none()`}, |
| 77 | {expr: "regex.extract('4122345432', '22').or(optional.of('777')) == optional.of('22')"}, |
| 78 | {expr: "regex.extract('4122345432', '22').orValue('777') == '22'"}, |
| 79 | |
| 80 | // Tests for extractAll Function |
| 81 | {expr: "regex.extractAll('id:123, id:456', 'assa') == []"}, |
| 82 | {expr: `regex.extractAll('id:123, id:456', r'id:\d+') == ['id:123', 'id:456']`}, |
| 83 | {expr: `regex.extractAll('Files: f_1.txt, f_2.csv', r'f_(\d+)') == ['1', '2']`}, |