@@ -90,24 +90,66 @@ var exprTests = []exprTest{
9090 {tags.Tags {"a" }, true },
9191 {tags.Tags {"" }, false },
9292 }},
93+
94+ // --- Optimizeable ---
95+ {"(aws|gcp)|legacy" , "(aws | gcp) | legacy" , []exprTestCase {
96+ {tags.Tags {"aws" , "cloud" }, true },
97+ {tags.Tags {"gcp" }, true },
98+ {tags.Tags {"legacy" }, true },
99+ {tags.Tags {"azure" }, false },
100+ }},
101+ {"(aws&gcp)&legacy" , "(aws & gcp) & legacy" , []exprTestCase {
102+ {tags.Tags {"aws" , "cloud" }, false },
103+ {tags.Tags {"gcp" }, false },
104+ {tags.Tags {"aws" , "gcp" , "legacy" }, true },
105+ }},
106+ {"auth&!(admin|super)" , "auth & !(admin | super)" , []exprTestCase {
107+ {tags.Tags {"auth" , "sys-admin" }, true },
108+ {tags.Tags {"auth" , "super-user" }, true },
109+ {tags.Tags {"auth" , "user" }, true },
110+ {tags.Tags {"sys-admin" }, false },
111+ }},
112+ {"!(test&stage)&prod" , "!(test & stage) & prod" , []exprTestCase {
113+ {tags.Tags {"prod" }, true },
114+ {tags.Tags {"test" , "prod" }, true },
115+ {tags.Tags {"stage" , "test" , "prod" }, false },
116+ {tags.Tags {"stage" }, false },
117+ }},
118+ {"!!prod" , "!!prod" , []exprTestCase {
119+ {tags.Tags {"prod" }, true },
120+ {tags.Tags {"test" }, false },
121+ }},
93122}
94123
95- func TestExpr (t * testing.T ) {
124+ func TestExprString (t * testing.T ) {
96125 for _ , et := range exprTests {
97- t .Run (fmt .Sprintf ("Matcher(%q)" , et .matcher ), func (t * testing.T ) {
126+ t .Run (fmt .Sprintf ("Matcher(%q).String() " , et .matcher ), func (t * testing.T ) {
98127 expr , err := tags .ParseMatcher (et .matcher )
99128 if err != nil {
100129 t .Fatalf ("failed to parse matcher %q: %v" , et .matcher , err )
101130 }
102131
103- t .Run ("String()" , func (t * testing.T ) {
104- got := expr .String ()
105- if got != et .rendered {
106- t .Errorf ("got = %q; expected = %q" , got , et .rendered )
107- }
132+ expr = expr .Optimize ()
133+
134+ got := expr .String ()
135+ if got != et .rendered {
136+ t .Errorf ("got = %q; expected = %q" , got , et .rendered )
137+ }
138+
139+ t .Log (got )
140+ })
141+ }
142+ }
143+
144+ func TestExprEval (t * testing.T ) {
145+ for _ , et := range exprTests {
146+ t .Run (fmt .Sprintf ("Matcher(%q)" , et .matcher ), func (t * testing.T ) {
147+ expr , err := tags .ParseMatcher (et .matcher )
148+ if err != nil {
149+ t .Fatalf ("failed to parse matcher %q: %v" , et .matcher , err )
150+ }
108151
109- t .Log (got )
110- })
152+ expr = expr .Optimize ()
111153
112154 for _ , tc := range et .cases {
113155 t .Run (fmt .Sprintf ("Eval(%v)" , tc .tags ), func (t * testing.T ) {
0 commit comments