Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
FolderOrigin-RevId: /usr/local/google/home/bstoll/copybara/temp/folder-destination706008309250303315/.
  • Loading branch information
GGN Engprod Team authored and bstoll committed Dec 4, 2024
1 parent fc41e32 commit b07a956
Show file tree
Hide file tree
Showing 29 changed files with 103,651 additions and 84,383 deletions.
16 changes: 16 additions & 0 deletions binding/portgraph/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package portgraph

import (
"fmt"
"regexp"

log "github.com/golang/glog"
Expand Down Expand Up @@ -46,6 +47,7 @@ type LeafPortConstraint interface {
type LeafConstraint interface {
universalLeafConstraint
match(string, bool) bool
string() string
}

type universalLeafConstraint interface {
Expand All @@ -59,6 +61,13 @@ type equal struct {
not bool
}

func (c *equal) string() string {
if c.not {
return fmt.Sprintf("not equal to %s", c.s)
}
return fmt.Sprintf("equal to %s", c.s)
}

func (c *equal) match(s string, ok bool) bool {
if c.not {
return ok && s != c.s
Expand All @@ -79,6 +88,13 @@ func (c *regex) match(s string, ok bool) bool {
return ok && c.re.MatchString(s)
}

func (c *regex) string() string {
if c.not {
return fmt.Sprintf("does not contain regex:%s", c.re.String())
}
return fmt.Sprintf("matches regex:%s", c.re.String())
}

func attrsNodePair(k string, n1, n2 *AbstractNode, abs2ConNode map[*AbstractNode]*ConcreteNode) (string, string, bool) {
v1, ok := attrNode(k, n1, abs2ConNode)
if !ok {
Expand Down
187 changes: 103 additions & 84 deletions binding/portgraph/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,112 +21,131 @@ import (

func TestMatch(t *testing.T) {
tests := []struct {
desc string
c LeafConstraint
v string
b, want bool
desc string
c LeafConstraint
v, wantStr string
b, want bool
}{{
desc: "Equal; match",
c: Equal("match"),
v: "match",
b: true,
want: true,
desc: "Equal; match",
c: Equal("match"),
v: "match",
wantStr: "equal to match",
b: true,
want: true,
}, {
desc: "Equal; match empty string",
c: Equal(""),
v: "",
b: true,
want: true,
desc: "Equal; match empty string",
c: Equal(""),
v: "",
wantStr: "equal to ",
b: true,
want: true,
}, {
desc: "Equal; no match",
c: Equal("match"),
v: "not matching",
b: true,
want: false,
desc: "Equal; no match",
c: Equal("match"),
v: "not matching",
wantStr: "equal to match",
b: true,
want: false,
}, {
desc: "Equal; no match on false",
c: Equal("match"),
v: "match",
b: false,
want: false,
desc: "Equal; no match on false",
c: Equal("match"),
v: "match",
wantStr: "equal to match",
b: false,
want: false,
}, {
desc: "NotEqual; match",
c: NotEqual("don't match this"),
v: "not matching this",
b: true,
want: true,
desc: "NotEqual; match",
c: NotEqual("don't match this"),
v: "not matching this",
wantStr: "not equal to don't match this",
b: true,
want: true,
}, {
desc: "NotEqual; match empty string",
c: NotEqual("don't match this"),
v: "",
b: true,
want: true,
desc: "NotEqual; match empty string",
c: NotEqual("don't match this"),
v: "",
wantStr: "not equal to don't match this",
b: true,
want: true,
}, {
desc: "NotEqual; no match",
c: NotEqual("match"),
v: "match",
b: true,
want: false,
desc: "NotEqual; no match",
c: NotEqual("match"),
v: "match",
wantStr: "not equal to match",
b: true,
want: false,
}, {
desc: "NotEqual; no match on false",
c: NotEqual("don't match this"),
v: "",
b: false,
want: false,
desc: "NotEqual; no match on false",
c: NotEqual("don't match this"),
v: "",
wantStr: "not equal to don't match this",
b: false,
want: false,
}, {
desc: "Regex; match",
c: Regex(regexp.MustCompile(".*")),
v: "anything",
b: true,
want: true,
desc: "Regex; match",
c: Regex(regexp.MustCompile(".*")),
v: "anything",
wantStr: "matches regex:.*",
b: true,
want: true,
}, {
desc: "Regex; match empty string",
c: Regex(regexp.MustCompile(".*")),
v: "",
b: true,
want: true,
desc: "Regex; match empty string",
c: Regex(regexp.MustCompile(".*")),
v: "",
wantStr: "matches regex:.*",
b: true,
want: true,
}, {
desc: "Regex; no match",
c: Regex(regexp.MustCompile("[0-9]+")),
v: "these are not numbers",
b: true,
want: false,
desc: "Regex; no match",
c: Regex(regexp.MustCompile("[0-9]+")),
v: "these are not numbers",
wantStr: "matches regex:[0-9]+",
b: true,
want: false,
}, {
desc: "Regex; no match on false",
c: Regex(regexp.MustCompile(".*")),
v: "",
b: false,
want: false,
desc: "Regex; no match on false",
c: Regex(regexp.MustCompile(".*")),
v: "",
wantStr: "matches regex:.*",
b: false,
want: false,
}, {
desc: "NotRegex; match",
c: NotRegex(regexp.MustCompile("[a-z]+")),
v: "1234",
b: true,
want: true,
desc: "NotRegex; match",
c: NotRegex(regexp.MustCompile("[a-z]+")),
v: "1234",
wantStr: "does not contain regex:[a-z]+",
b: true,
want: true,
}, {
desc: "NotRegex; match empty string",
c: NotRegex(regexp.MustCompile("[a-z]+")),
v: "",
b: true,
want: true,
desc: "NotRegex; match empty string",
c: NotRegex(regexp.MustCompile("[a-z]+")),
v: "",
wantStr: "does not contain regex:[a-z]+",
b: true,
want: true,
}, {
desc: "NotRegex; no match",
c: NotRegex(regexp.MustCompile("[a-z]+")),
v: "these are not numbers",
b: true,
want: false,
desc: "NotRegex; no match",
c: NotRegex(regexp.MustCompile("[a-z]+")),
v: "these are not numbers",
wantStr: "does not contain regex:[a-z]+",
b: true,
want: false,
}, {
desc: "NotRegex; no match on false",
c: NotRegex(regexp.MustCompile("[a-z]+")),
v: "",
b: false,
want: false,
desc: "NotRegex; no match on false",
c: NotRegex(regexp.MustCompile("[a-z]+")),
v: "",
wantStr: "does not contain regex:[a-z]+",
b: false,
want: false,
}}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
if m := tc.c.match(tc.v, tc.b); m != tc.want {
t.Errorf("match() got %t, want %t", m, tc.want)
}
if s := tc.c.string(); s != tc.wantStr {
t.Errorf("string() got %q, want %q", s, tc.wantStr)
}
})
}
}
Expand Down
Loading

0 comments on commit b07a956

Please sign in to comment.