Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attr: flag test methods #177

Merged
merged 2 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions attr/attr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,34 @@ func TestAttributeContainsTextFlags(t *testing.T) {
}
}
}

func TestAttributeTestMethods(t *testing.T) {
cases := []struct {
Attribute Attribute
Predicate func(Attribute) bool
Expect bool
}{
// Confirm logic works as expected.
{DUPOK | NOSPLIT, Attribute.DUPOK, true},
{DUPOK | NOSPLIT, Attribute.NOSPLIT, true},
{DUPOK | NOSPLIT, Attribute.NOFRAME, false},

// Basic test for every method.
{NOPROF, Attribute.NOPROF, true},
{DUPOK, Attribute.DUPOK, true},
{NOSPLIT, Attribute.NOSPLIT, true},
{RODATA, Attribute.RODATA, true},
{NOPTR, Attribute.NOPTR, true},
{WRAPPER, Attribute.WRAPPER, true},
{NEEDCTXT, Attribute.NEEDCTXT, true},
{TLSBSS, Attribute.TLSBSS, true},
{NOFRAME, Attribute.NOFRAME, true},
{REFLECTMETHOD, Attribute.REFLECTMETHOD, true},
{TOPFRAME, Attribute.TOPFRAME, true},
}
for _, c := range cases {
if c.Predicate(c.Attribute) != c.Expect {
t.Errorf("%s: expected %#v", c.Attribute.Asm(), c.Expect)
}
}
}
6 changes: 6 additions & 0 deletions attr/make_textflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,10 @@ func PrintFlagAttributes(w io.Writer, fs []Flag) {
fmt.Fprintf(w, "\t%s: %q,\n", f.Name, f.Name)
}
fmt.Fprintf(w, "}\n")

// Flag test methods.
for _, f := range fs {
fmt.Fprintf(w, "\n// %s reports whether the %s flag is set.\n", f.Name, f.Name)
fmt.Fprintf(w, "func (a Attribute) %s() bool { return (a & %s) != 0 }\n", f.Name, f.Name)
}
}
33 changes: 33 additions & 0 deletions attr/ztextflag.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.