rx/rx_test.go

166 lines
3.3 KiB
Go
Raw Normal View History

2022-02-26 15:22:46 +01:00
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)))
}
})
}
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}?
}