| 10 | ) |
| 11 | |
| 12 | func TestMetaData(t *testing.T) { |
| 13 | cases := map[string]struct { |
| 14 | Expr eval.Expression |
| 15 | Name string |
| 16 | Values []string |
| 17 | MetaFunc func(e eval.Expression) expr.MetaExpr |
| 18 | Invocations int |
| 19 | }{ |
| 20 | "userType": {&expr.UserTypeExpr{AttributeExpr: &expr.AttributeExpr{}}, "openapi:summary", []string{"Short summary of what endpoint does"}, userTypeMeta, 1}, |
| 21 | "api": {&expr.APIExpr{}, "metadata", []string{"some metadata"}, apiExprMeta, 2}, |
| 22 | "attribute": {&expr.AttributeExpr{}, "attribute_meta", []string{"attr meta", "more attr meta"}, attributeMeta, 2}, |
| 23 | "method": {&expr.MethodExpr{Name: "testmethod"}, "method", []string{"method meta"}, methodMeta, 2}, |
| 24 | "resultType": {&expr.ResultTypeExpr{UserTypeExpr: &expr.UserTypeExpr{AttributeExpr: &expr.AttributeExpr{}}}, "resultTypeMeta", []string{"result type meta"}, resultTypeMeta, 2}, |
| 25 | } |
| 26 | |
| 27 | for k, tc := range cases { |
| 28 | t.Run(k, func(t *testing.T) { |
| 29 | eval.Context = &eval.DSLContext{} |
| 30 | for i := tc.Invocations; i > 0; i-- { |
| 31 | eval.Execute(func() { |
| 32 | Meta(tc.Name, tc.Values...) |
| 33 | }, tc.Expr) |
| 34 | } |
| 35 | if eval.Context.Errors != nil { |
| 36 | t.Errorf("%s: Meta failed unexpectedly with %s", k, eval.Context.Errors) |
| 37 | } |
| 38 | meta := tc.MetaFunc(tc.Expr) |
| 39 | if _, ok := meta[tc.Name]; !ok { |
| 40 | t.Errorf("%s: expected %s to be present", k, tc.Name) |
| 41 | } |
| 42 | if len(meta[tc.Name]) != (len(tc.Values) * tc.Invocations) { |
| 43 | t.Errorf("%s: expected the number of meta values to match %d got %d ", k, len(tc.Values), len(meta[tc.Name])) |
| 44 | } |
| 45 | for _, caseVal := range tc.Values { |
| 46 | if !hasValue(meta[tc.Name], caseVal) { |
| 47 | t.Errorf("%s: meta data %v did not conatin expected value %v", k, meta[tc.Name], caseVal) |
| 48 | } |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func hasValue(vals []string, val string) bool { |
| 55 | return slices.Contains(vals, val) |