github.com/bcampbell/scrapeomat@v0.0.0-20220820232205-23e64141c89e/discover/pat_test.go (about)

     1  package discover
     2  
     3  import (
     4  	//"fmt"
     5  	//	"regexp"
     6  	"testing"
     7  )
     8  
     9  func TestPatToRegexp(t *testing.T) {
    10  	data := []struct {
    11  		pat     string
    12  		match   []string
    13  		noMatch []string
    14  	}{
    15  		{
    16  			pat: "/ID-SLUG$",
    17  			match: []string{
    18  				"/news/space/12345-moon-made-of-cheese",
    19  				"/news/space/12345-moon-made-of-cheese.html",
    20  				"/1234-blah-blah"},
    21  			noMatch: []string{
    22  				"/1234-wibble-pibble/blah.html"},
    23  		},
    24  		{
    25  			pat: "/YYYY/MM/SLUG.html$",
    26  			match: []string{
    27  				"/2001/04/moon-made-of-cheese.html",
    28  			},
    29  			noMatch: []string{
    30  				"/2001/04/moon-made-of-cheese",
    31  				"/2001/04/moon-made-of-cheese.php",
    32  				"/2001/04/moon-made-of-cheese.html/blah-blah",
    33  			},
    34  		},
    35  		{
    36  			pat: "/-/SLUG/ID/-/",
    37  			match: []string{
    38  				"/-/moon-made-of-cheese/12345/-/index.html",
    39  			},
    40  			noMatch: []string{
    41  				"/-/moon-made-of-cheese/1234",
    42  			},
    43  		},
    44  	}
    45  	/*
    46  		pats := []string{
    47  			"/ID-SLUG",
    48  			"/SLUG.html",
    49  		}
    50  	*/
    51  
    52  	for _, dat := range data {
    53  		re, err := patToRegexp(dat.pat)
    54  		if err != nil {
    55  			t.Errorf("%q failed to compile: %s", dat.pat, err)
    56  			continue
    57  		}
    58  		for _, u := range dat.match {
    59  			if !re.MatchString(u) {
    60  				t.Errorf("%q didn't match %q", dat.pat, u)
    61  			}
    62  		}
    63  		for _, u := range dat.noMatch {
    64  			if re.MatchString(u) {
    65  				t.Errorf("%q incorrectly matched %q", dat.pat, u)
    66  			}
    67  		}
    68  	}
    69  }