|
| 1 | +package regex |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "math/rand" |
| 7 | + "strconv" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCompile(t *testing.T) { |
| 13 | + reC := Comp("this is test %1", "a") |
| 14 | + if reC.RE.ReplaceAllString(`this is test a`, `this is test b`) != `this is test b` { |
| 15 | + t.Error(`[this is test %1] [a]`, "\n", errors.New("failed to compile params")) |
| 16 | + } |
| 17 | + |
| 18 | + re := `test .*` |
| 19 | + reEscaped := Escape(re) |
| 20 | + if re == reEscaped || Comp(reEscaped).Match([]byte(`test 1`)) { |
| 21 | + t.Error("[", reEscaped, "]\n", errors.New("escape function failed")) |
| 22 | + } |
| 23 | + |
| 24 | + r := Comp(`test %1`, "%2", "a") |
| 25 | + if r.Match([]byte(`test a`)) { |
| 26 | + t.Error(`[test %1] [%2, a]`, "\n", errors.New("escape function failed to escape '%' char")) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestReplaceStr(t *testing.T) { |
| 31 | + var check = func(s string, re, r string, e string) { |
| 32 | + res := Comp(re).RepLit([]byte(s), []byte(r)) |
| 33 | + if !bytes.Equal(res, []byte(e)) { |
| 34 | + t.Error("[", string(res), "]\n", errors.New("result does not match expected result")) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + check("this is a test", `(?#a\s+)test`, "", "this is a ") |
| 39 | + check("string with `block` quotes", `\'.*?\'`, "'single'", "string with 'single' quotes") |
| 40 | +} |
| 41 | + |
| 42 | +func TestReplaceStrComplex(t *testing.T) { |
| 43 | + var check = func(s string, re, r string, e string) { |
| 44 | + res := Comp(re).Rep([]byte(s), []byte(r)) |
| 45 | + if !bytes.Equal(res, []byte(e)) { |
| 46 | + t.Error("[", string(res), "]\n", errors.New("result does not match expected result")) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + check("this is a Test", `(?i)a (test)`, "some $1", "this is some Test") |
| 51 | + check("I Need Coffee!!!", `Coffee(!*)`, "More Coffee$1", "I Need More Coffee!!!") |
| 52 | +} |
| 53 | + |
| 54 | +func TestReplaceFunc(t *testing.T) { |
| 55 | + var check = func(s string, re, r string, e string) { |
| 56 | + res := Comp(re).RepFunc([]byte(s), func(data func(int) []byte) []byte { |
| 57 | + return JoinBytes(data(1), ' ', r) |
| 58 | + }) |
| 59 | + if !bytes.Equal(res, []byte(e)) { |
| 60 | + t.Error("[", string(res), "]\n", errors.New("result does not match expected result")) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + check("this is a new test", `(new) test`, "pizza", "this is a new pizza") |
| 65 | + check("a random string", `(a) random`, "not so random", "a not so random string") |
| 66 | +} |
| 67 | + |
| 68 | +func TestConcurrent(t *testing.T) { |
| 69 | + for i := 0; i < 10; i++ { |
| 70 | + for j := 0; j < 10; j++ { |
| 71 | + go (func() { |
| 72 | + res := Comp(`(t)`).RepFunc([]byte("test"), func(data func(int) []byte) []byte { |
| 73 | + return data(1) |
| 74 | + }) |
| 75 | + _ = res |
| 76 | + time.Sleep(10 * time.Nanosecond) |
| 77 | + })() |
| 78 | + } |
| 79 | + |
| 80 | + // time.Sleep(1000000 * 1000) // 1 second |
| 81 | + time.Sleep(1000000 * 100) // 0.1 second |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestCache(t *testing.T) { |
| 86 | + var check = func(s string, re, r string, e string) { |
| 87 | + res := Comp(re).RepLit([]byte(s), []byte(r)) |
| 88 | + if !bytes.Equal(res, []byte(e)) { |
| 89 | + t.Error("[", string(res), "]\n", errors.New("result does not match expected result")) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + check("this is a test", `\sis\s`, " was ", "this was a test") |
| 94 | + check("this is a test", `\sis\s`, " was ", "this was a test") |
| 95 | +} |
| 96 | + |
| 97 | +func TestFlags(t *testing.T) { |
| 98 | + var check = func(s string, re, r string, e string) { |
| 99 | + res := Comp(re).RepLit([]byte(s), []byte(r)) |
| 100 | + if !bytes.Equal(res, []byte(e)) { |
| 101 | + t.Error("[", string(res), "]\n", errors.New("result does not match expected result")) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + check("this is a\nmultiline text", `(?s)a\s*multiline`, "", "this is text") |
| 106 | + check("list line 1\nlist line 2\n list line 3", `(?m)^list`, "a list", "a list line 1\na list line 2\n list line 3") |
| 107 | + check("a MultiCase text", `(?i)multicase`, "", "a text") |
| 108 | + check("a MultiCase text no flag", `multicase`, "", "a MultiCase text no flag") |
| 109 | + |
| 110 | + // check("a multi\nline text", `multi\s*line`, "", "a multi\nline text") |
| 111 | +} |
| 112 | + |
| 113 | +func TestPerformance(t *testing.T) { |
| 114 | + for i := 0; i < 10000; i++ { |
| 115 | + Comp(strconv.Itoa(rand.Int())) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestValid(t *testing.T) { |
| 120 | + var check = func(re string, e bool) { |
| 121 | + res := IsValid(re) |
| 122 | + if res != e { |
| 123 | + t.Error("[", string(re), "]\n", errors.New("result does not match expected result")) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + check(`[\w_\-]+`, true) |
| 128 | + check(`[\w_-.]+`, false) |
| 129 | + check(`(?<test>)`, true) |
| 130 | + check(`(?i)test`, true) |
| 131 | +} |
0 commit comments