|
| 1 | +package assert |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// Match tests whether the string matches the regular expression or not. |
| 9 | +func (a *Assertion) Match(val string, pattern *regexp.Regexp, message ...string) error { |
| 10 | + a.Helper() |
| 11 | + |
| 12 | + return tryMatchRegexp(a.T, false, val, pattern, message...) |
| 13 | +} |
| 14 | + |
| 15 | +// MatchNow tests whether the string matches the regular expression or not, and it will terminate |
| 16 | +// the execution if it does not match. |
| 17 | +func (a *Assertion) MatchNow(val string, pattern *regexp.Regexp, message ...string) error { |
| 18 | + a.Helper() |
| 19 | + |
| 20 | + return tryMatchRegexp(a.T, true, val, pattern, message...) |
| 21 | +} |
| 22 | + |
| 23 | +// MatchString will compile the pattern and test whether the string matches the regular expression |
| 24 | +// or not. It will panic if the pattern is not a valid regular expression. |
| 25 | +func (a *Assertion) MatchString(val, pattern string, message ...string) error { |
| 26 | + a.Helper() |
| 27 | + |
| 28 | + regPattern := regexp.MustCompile(pattern) |
| 29 | + |
| 30 | + return tryMatchRegexp(a.T, false, val, regPattern, message...) |
| 31 | +} |
| 32 | + |
| 33 | +// MatchStringNow will compile the pattern and test whether the string matches the regular |
| 34 | +// expression or not. It will terminate the execution if it does not match, and it will panic if |
| 35 | +// the pattern is not a valid regular expression. |
| 36 | +func (a *Assertion) MatchStringNow(val, pattern string, message ...string) error { |
| 37 | + a.Helper() |
| 38 | + |
| 39 | + regPattern := regexp.MustCompile(pattern) |
| 40 | + |
| 41 | + return tryMatchRegexp(a.T, true, val, regPattern, message...) |
| 42 | +} |
| 43 | + |
| 44 | +// NotMatch tests whether the string matches the regular expression or not, and it set the result |
| 45 | +// to fail if the string matches the pattern. |
| 46 | +func (a *Assertion) NotMatch(val string, pattern *regexp.Regexp, message ...string) error { |
| 47 | + a.Helper() |
| 48 | + |
| 49 | + return tryNotMatchRegexp(a.T, false, val, pattern, message...) |
| 50 | +} |
| 51 | + |
| 52 | +// NotMatchNow tests whether the string matches the regular expression or not, and it will |
| 53 | +// terminate the execution if the string matches the pattern. |
| 54 | +func (a *Assertion) NotMatchNow(val string, pattern *regexp.Regexp, message ...string) error { |
| 55 | + a.Helper() |
| 56 | + |
| 57 | + return tryNotMatchRegexp(a.T, true, val, pattern, message...) |
| 58 | +} |
| 59 | + |
| 60 | +// MatchString will compile the pattern and test whether the string matches the regular expression |
| 61 | +// or not, and it set the result to fail if the string matches the pattern. It will also panic if |
| 62 | +// the pattern is not a valid regular expression. |
| 63 | +func (a *Assertion) NotMatchString(val, pattern string, message ...string) error { |
| 64 | + a.Helper() |
| 65 | + |
| 66 | + regPattern := regexp.MustCompile(pattern) |
| 67 | + |
| 68 | + return tryNotMatchRegexp(a.T, false, val, regPattern, message...) |
| 69 | +} |
| 70 | + |
| 71 | +// NotMatchStringNow will compile the pattern and test whether the string matches the regular |
| 72 | +// expression or not, and it set the result to fail if the string matches the pattern. It will |
| 73 | +// terminate the execution if the string matches the pattern, and it will panic if the pattern is |
| 74 | +// not a valid regular expression. |
| 75 | +func (a *Assertion) NotMatchStringNow(val, pattern string, message ...string) error { |
| 76 | + a.Helper() |
| 77 | + |
| 78 | + regPattern := regexp.MustCompile(pattern) |
| 79 | + |
| 80 | + return tryNotMatchRegexp(a.T, true, val, regPattern, message...) |
| 81 | +} |
| 82 | + |
| 83 | +// tryMatchRegexp tries to test whether the string matches the regular expression pattern or not, |
| 84 | +// and it'll fail if the string does not match. |
| 85 | +func tryMatchRegexp( |
| 86 | + t *testing.T, |
| 87 | + failedNow bool, |
| 88 | + val string, |
| 89 | + pattern *regexp.Regexp, |
| 90 | + message ...string, |
| 91 | +) error { |
| 92 | + t.Helper() |
| 93 | + |
| 94 | + if pattern.Match([]byte(val)) { |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + err := newAssertionError("The input did not match the regular expression", message...) |
| 99 | + failed(t, err, failedNow) |
| 100 | + return err |
| 101 | +} |
| 102 | + |
| 103 | +// tryNotMatchRegexp tries to test whether the string matches the regular expression pattern or |
| 104 | +// not, and it'll fail if the string matches the pattern. |
| 105 | +func tryNotMatchRegexp( |
| 106 | + t *testing.T, |
| 107 | + failedNow bool, |
| 108 | + val string, |
| 109 | + pattern *regexp.Regexp, |
| 110 | + message ...string, |
| 111 | +) error { |
| 112 | + t.Helper() |
| 113 | + |
| 114 | + if !pattern.Match([]byte(val)) { |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + err := newAssertionError("The input match the regular expression", message...) |
| 119 | + failed(t, err, failedNow) |
| 120 | + return err |
| 121 | +} |
0 commit comments