forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ToExpressionSet helper function. (goadesign#2601)
The function takes an arbitrary slice of expressions and returns an ExpressionSet. This is especially convenient for plugins that do not have to loop through all the slice to cast the expression objects to the Expression interface. Yes it does make use of reflect and hides the performance impact but given the use case (tool vs. long running process) this seems like a good trade-off.
- Loading branch information
Showing
3 changed files
with
63 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package eval | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
type Expr int | ||
|
||
func (e Expr) EvalName() string { return "test expression" } | ||
|
||
func TestToExpressionSet(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Slice []interface{} | ||
ExpectPanic bool | ||
}{ | ||
{"simple", []interface{}{Expr(42)}, false}, | ||
{"nil", nil, false}, | ||
{"invalid", []interface{}{42}, true}, | ||
} | ||
for _, c := range cases { | ||
t.Run(c.Name, func(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil && c.ExpectPanic { | ||
t.Errorf("test did not panic") | ||
} | ||
}() | ||
set := ToExpressionSet(c.Slice) | ||
if len(set) != len(c.Slice) { | ||
t.Errorf("got set of length %d, expected %d.", len(set), len(c.Slice)) | ||
} else { | ||
for i, e := range set { | ||
if e != c.Slice[i] { | ||
t.Errorf("got value %v at index %d, expected %v", e, i, c.Slice[i]) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters