205 lines
4.3 KiB
Go
205 lines
4.3 KiB
Go
package rx
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRegex(t *testing.T) {
|
|
a := Regex("a")
|
|
|
|
t.Run("ZeroOrMore() method", func(t *testing.T) {
|
|
tcs := []struct {
|
|
rx Regex
|
|
expected string
|
|
}{
|
|
{"a", "aa*"},
|
|
{"ab", "a(?:ab)*"},
|
|
{"[xyz]", "a[xyz]*"},
|
|
{"[xyz][abc]", "a(?:[xyz][abc])*"},
|
|
{"[^xyz]", "a[^xyz]*"},
|
|
{`\d`, `a\d*`},
|
|
{"[[:alpha:]]", "a[[:alpha:]]*"},
|
|
{"[[:^alpha:]]", "a[[:^alpha:]]*"},
|
|
{`\pN`, `a\pN*`},
|
|
{`\p{Greek}`, `a\p{Greek}*`},
|
|
{`\p{Greek}\p{Digit}`, `a(?:\p{Greek}\p{Digit})*`},
|
|
{`\PN`, `a\PN*`},
|
|
{`\P{Greek}`, `a\P{Greek}*`},
|
|
{`\P{Greek}\P{Digit}`, `a(?:\P{Greek}\P{Digit})*`},
|
|
{`\123`, `a\123*`},
|
|
{`\12`, `a\12*`},
|
|
{`\1`, `a\1*`},
|
|
{`\1a3`, `a(?:\1a3)*`},
|
|
{`\x7F`, `a\x7F*`},
|
|
{`\x{10FFFF}`, `a\x{10FFFF}*`},
|
|
{`\x{0000}`, `a\x{0000}*`},
|
|
{`\x{1}`, `a\x{1}*`},
|
|
{`\x{Z}`, `a(?:\x{Z})*`},
|
|
{`\Q...\E`, `a\Q...\E*`},
|
|
{`\Q\E`, `a\Q\E*`},
|
|
}
|
|
|
|
for _, tc := range tcs {
|
|
require.Equal(t, tc.expected, string(a.ZeroOrMore(tc.rx)))
|
|
}
|
|
})
|
|
|
|
t.Run("WithFlags() method", func(t *testing.T) {
|
|
tcs := []struct {
|
|
rx Flags
|
|
expected string
|
|
}{
|
|
{0, "aa"},
|
|
{CaseInsensitive, "a(?i:a)"},
|
|
{CaseSensitive, "a(?-i:a)"},
|
|
{MultiLine, "a(?m:a)"},
|
|
{SingleLine, "a(?-m:a)"},
|
|
{AnyNL, "a(?s:a)"},
|
|
{AnyNoNL, "a(?-s:a)"},
|
|
{Ungreedy, "a(?U:a)"},
|
|
{Greedy, "a(?-U:a)"},
|
|
{CaseInsensitive | MultiLine, "a(?im:a)"},
|
|
{CaseInsensitive | SingleLine, "a(?i-m:a)"},
|
|
{0b11111111, "a(?imsU-imsU:a)"},
|
|
{CaseInsensitive | MultiLine | AnyNoNL | Greedy, "a(?im-sU:a)"},
|
|
}
|
|
|
|
for _, tc := range tcs {
|
|
require.Equal(t, tc.expected, string(a.WithFlags(tc.rx, "a")))
|
|
}
|
|
})
|
|
}
|
|
|
|
func ExampleRegex_In() {
|
|
fmt.Println(Regex("a").In("0-9", "a-z"))
|
|
// Output: a[0-9a-z]
|
|
}
|
|
|
|
func ExampleRegex_NotIn() {
|
|
fmt.Println(Regex("a").NotIn("0-9", "a-z"))
|
|
// Output: a[^0-9a-z]
|
|
}
|
|
|
|
func ExampleRegex_Then() {
|
|
fmt.Println(Regex("a").Then("b", Any))
|
|
// Output: ab.
|
|
}
|
|
|
|
func ExampleRegex_AnyOf() {
|
|
fmt.Println(Regex("a").AnyOf("b", "c"))
|
|
// Output: a(?:b|c)
|
|
}
|
|
|
|
func ExampleRegex_ZeroOrMore() {
|
|
fmt.Println(Regex("a").ZeroOrMore("b"))
|
|
fmt.Println(Regex("a").ZeroOrMore("abc"))
|
|
// Output:
|
|
// ab*
|
|
// a(?:abc)*
|
|
}
|
|
|
|
func ExampleRegex_OneOrMore() {
|
|
fmt.Println(Regex("a").OneOrMore("b"))
|
|
fmt.Println(Regex("a").OneOrMore("abc"))
|
|
// Output:
|
|
// ab+
|
|
// a(?:abc)+
|
|
}
|
|
|
|
func ExampleRegex_ZeroOrOne() {
|
|
fmt.Println(Regex("a").ZeroOrOne("b"))
|
|
fmt.Println(Regex("a").ZeroOrOne("abc"))
|
|
// Output:
|
|
// ab?
|
|
// a(?:abc)?
|
|
}
|
|
|
|
func ExampleRegex_NTimes() {
|
|
fmt.Println(Regex("a").NTimes(3, "b"))
|
|
fmt.Println(Regex("a").NTimes(3, "abc"))
|
|
// Output:
|
|
// ab{3}
|
|
// a(?:abc){3}
|
|
}
|
|
|
|
func ExampleRegex_NOrMore() {
|
|
fmt.Println(Regex("a").NOrMore(3, "b"))
|
|
fmt.Println(Regex("a").NOrMore(3, "abc"))
|
|
// Output:
|
|
// ab{3,}
|
|
// a(?:abc){3,}
|
|
}
|
|
|
|
func ExampleRegex_NUpToM() {
|
|
fmt.Println(Regex("a").NUpToM(3, 5, "b"))
|
|
fmt.Println(Regex("a").NUpToM(3, 5, "abc"))
|
|
// Output:
|
|
// ab{3,5}
|
|
// a(?:abc){3,5}
|
|
}
|
|
|
|
func ExampleRegex_ZeroOrMoreLazy() {
|
|
fmt.Println(Regex("a").ZeroOrMoreLazy("b"))
|
|
fmt.Println(Regex("a").ZeroOrMoreLazy("abc"))
|
|
// Output:
|
|
// ab*?
|
|
// a(?:abc)*?
|
|
}
|
|
|
|
func ExampleRegex_OneOrMoreLazy() {
|
|
fmt.Println(Regex("a").OneOrMoreLazy("b"))
|
|
fmt.Println(Regex("a").OneOrMoreLazy("abc"))
|
|
// Output:
|
|
// ab+?
|
|
// a(?:abc)+?
|
|
}
|
|
|
|
func ExampleRegex_ZeroOrOneLazy() {
|
|
fmt.Println(Regex("a").ZeroOrOneLazy("b"))
|
|
fmt.Println(Regex("a").ZeroOrOneLazy("abc"))
|
|
// Output:
|
|
// ab??
|
|
// a(?:abc)??
|
|
}
|
|
|
|
func ExampleRegex_NTimesLazy() {
|
|
fmt.Println(Regex("a").NTimesLazy(3, "b"))
|
|
fmt.Println(Regex("a").NTimesLazy(3, "abc"))
|
|
// Output:
|
|
// ab{3}?
|
|
// a(?:abc){3}?
|
|
}
|
|
|
|
func ExampleRegex_NOrMoreLazy() {
|
|
fmt.Println(Regex("a").NOrMoreLazy(3, "b"))
|
|
fmt.Println(Regex("a").NOrMoreLazy(3, "abc"))
|
|
// Output:
|
|
// ab{3,}?
|
|
// a(?:abc){3,}?
|
|
}
|
|
|
|
func ExampleRegex_NUpToMLazy() {
|
|
fmt.Println(Regex("a").NUpToMLazy(3, 5, "b"))
|
|
fmt.Println(Regex("a").NUpToMLazy(3, 5, "abc"))
|
|
// Output:
|
|
// ab{3,5}?
|
|
// a(?:abc){3,5}?
|
|
}
|
|
|
|
func ExampleRegex_Capture() {
|
|
fmt.Println(Regex("a").Capture("a", "b"))
|
|
// Output: a(ab)
|
|
}
|
|
|
|
func ExampleRegex_CaptureName() {
|
|
fmt.Println(Regex("a").CaptureName("foo", "a", "b"))
|
|
// Output: a(?P<foo>ab)
|
|
}
|
|
|
|
func ExampleRegex_WithFlags() {
|
|
fmt.Println(Regex("a").WithFlags(AnyNL|CaseSensitive, "a", "b"))
|
|
// Output: a(?s-i:ab)
|
|
}
|