Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Created February 28, 2014 05:20
Show Gist options
  • Save mdwhatcott/9265769 to your computer and use it in GitHub Desktop.
Save mdwhatcott/9265769 to your computer and use it in GitHub Desktop.
Example of using table-driven testing with GoConvey (also shows how to use `Reset`)
package regexps
import (
"fmt"
"regexp"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestRegexps(t *testing.T) {
Convey("Regexps can match an admonition label at the start of a paragraph", t, func() {
cases := map[string]string{
"NOTE label": "NOTE: Just a little note.",
"TIP label": "TIP: Don't forget!",
}
for description, test := range cases {
Convey(fmt.Sprintf("The '%s' test case should match", description), func() {
So(AdmonitionParagraphRx.MatchString(test), ShouldBeTrue)
})
}
})
Convey("Regexps can match several variants of the passthrough inline macro, which may span multiple lines", t, func() {
cases := map[string]string{
"plus-signs": "+++text+++",
"multiline-plus-signs": "+++text\nline2\nline3+++",
"money": "$$text$$",
"multiline-money": "$$text\nmulutple\nline$$",
"quotes": `pass:quotes[text]`,
"multiline-quotes": "pass:quotes[text\nline2\nline3]",
}
for description, test := range cases {
Convey(fmt.Sprintf("The '%s' test case should match", description), func() {
So(PassInlineMacroRx.MatchString(test), ShouldBeTrue)
})
}
})
Convey("Regexps can detect strings that resemble URIs", t, func() {
// same iterative approach would work here
So(UriSniffRx.MatchString("http://domain"), ShouldBeTrue)
So(UriSniffRx.MatchString("https://domain"), ShouldBeTrue)
So(UriSniffRx.MatchString("data:info"), ShouldBeTrue)
})
Convey("Regexps can detect escaped brackets", t, func() {
// same iterative approach would work here
So(EscapedBracketRx.MatchString(`\]`), ShouldBeTrue)
So(EscapedBracketRx.MatchString(`a\\]a`), ShouldBeTrue)
})
var reres *Reres
Convey("Regexps can encapsulate results in a struct Reres", t, func() {
testRx, _ := regexp.Compile("\\\\?a(b*)c")
// Maybe this regexp is being stretched a little too far (across too many test cases)?
reres = NewReres("xxxabbbbcyyy111aabbbcc222\\ac33", testRx)
Convey("Regexps can create a Reres struct", func() {
So(reres, ShouldNotBeNil)
So(reres.Text(), ShouldEqual, "xxxabbbbcyyy111aabbbcc222\\ac33")
})
Convey("Regexps can test for matches", func() {
So(reres.HasAnyMatch(), ShouldBeTrue)
})
Convey("Regexps can iterate over matches", func() {
So(reres.HasNext(), ShouldBeTrue)
reres.Next()
So(reres.HasNext(), ShouldBeTrue)
reres.Next()
So(reres.HasNext(), ShouldBeTrue)
reres.Next()
So(reres.HasNext(), ShouldBeFalse)
})
Convey("Regexps can get the prefix, string before each match", func() {
So(reres.Prefix(), ShouldEqual, "xxx")
reres.Next()
So(reres.Prefix(), ShouldEqual, "yyy111a")
})
Convey("Regexps can get the suffix, string after each match", func() {
So(reres.Suffix(), ShouldEqual, "yyy111aabbbcc222\\ac33")
reres.Next()
So(reres.Suffix(), ShouldEqual, "c222\\ac33")
reres.Next()
So(reres.Suffix(), ShouldEqual, "33")
})
Convey("Regexps can get the first character of the current match", func() {
So(reres.FirstChar(), ShouldEqual, 'a')
})
Convey("Regexps can test for escape as first charater in the current match", func() {
So(reres.IsEscaped(), ShouldBeFalse)
reres.Next()
So(reres.IsEscaped(), ShouldBeFalse)
reres.Next()
So(reres.IsEscaped(), ShouldBeTrue)
})
Convey("Regexps can get the full string of the current match", func() {
So(reres.FullMatch(), ShouldEqual, "abbbbc")
reres.Next()
So(reres.FullMatch(), ShouldEqual, "abbbc")
reres.Next()
So(reres.FullMatch(), ShouldEqual, "\\ac")
})
Convey("Regexps can test for a group within the current match", func() {
//fmt.Printf("t %v (%d) %v => %d\n", reres.matches, reres.i, reres.matches[reres.i], reres.previous)
So(reres.HasGroup(1), ShouldBeTrue)
So(reres.HasGroup(2), ShouldBeFalse)
reres.Next()
So(reres.HasGroup(1), ShouldBeTrue)
So(reres.HasGroup(2), ShouldBeFalse)
reres.Next()
So(reres.HasGroup(1), ShouldBeFalse)
So(reres.HasGroup(2), ShouldBeFalse)
})
Convey("Regexps can get a group within the current match", func() {
So(reres.Group(1), ShouldEqual, "bbbb")
So(reres.Group(2), ShouldEqual, "")
reres.Next()
So(reres.Group(1), ShouldEqual, "bbb")
So(reres.Group(2), ShouldEqual, "")
reres.Next()
So(reres.Group(1), ShouldEqual, "")
So(reres.Group(2), ShouldEqual, "")
})
Reset(func() {
reres.ResetNext()
})
})
// more stuff here...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment