Improve tests for IsPrimitivePointer()
of expr.AttributeExpr.
@@ -372,49 +372,11 @@ func TestAttributeExprIsRequiredNoDefault(t *testing.T) {
}
func TestAttributeExprIsPrimitivePointer(t *testing.T) {
- cases := map[string]struct {
- attName string
- useDefault bool
- expected bool
- }{
- "primitive pointer": {
- attName: "foo",
- useDefault: false,
- expected: true,
- },
- "no attribute": {
- attName: "zoo",
- useDefault: false,
- expected: false,
- },
- "not primitive": {
- attName: "bar",
- useDefault: false,
- expected: false,
- },
- "primitive but bytes": {
- attName: "baz",
- useDefault: false,
- expected: false,
- },
- "primitive but any": {
- attName: "qux",
- useDefault: false,
- expected: false,
- },
- "primitive but required": {
- attName: "quux",
- useDefault: false,
- expected: false,
- },
- "primitive but default value": {
- attName: "corge",
- useDefault: true,
- expected: false,
- },
+ var (
+ attributeBoolean = AttributeExpr{
+ Type: Boolean,
}
-
- attribute := AttributeExpr{
+ attributeObject = AttributeExpr{
Type: &Object{
&NamedAttributeExpr{
Name: "foo",
@@ -462,10 +424,80 @@ func TestAttributeExprIsPrimitivePointer(t *testing.T) {
Required: []string{"quux"},
},
}
+ )
+ cases := map[string]struct {
+ attribute AttributeExpr
+ attName string
+ useDefault bool
+ expected bool
+ }{
+ "primitive pointer": {
+ attribute: attributeObject,
+ attName: "foo",
+ useDefault: false,
+ expected: true,
+ },
+ "no attribute": {
+ attribute: attributeObject,
+ attName: "zoo",
+ useDefault: false,
+ expected: false,
+ },
+ "not primitive": {
+ attribute: attributeObject,
+ attName: "bar",
+ useDefault: false,
+ expected: false,
+ },
+ "primitive but bytes": {
+ attribute: attributeObject,
+ attName: "baz",
+ useDefault: false,
+ expected: false,
+ },
+ "primitive but any": {
+ attribute: attributeObject,
+ attName: "qux",
+ useDefault: false,
+ expected: false,
+ },
+ "primitive but required": {
+ attribute: attributeObject,
+ attName: "quux",
+ useDefault: false,
+ expected: false,
+ },
+ "primitive but default value": {
+ attribute: attributeObject,
+ attName: "corge",
+ useDefault: true,
+ expected: false,
+ },
+ "non object": {
+ attribute: attributeBoolean,
+ attName: "", // should have panicked!
+ useDefault: false, // should have panicked!
+ expected: false, // should have panicked!
+ },
+ }
+
for k, tc := range cases {
- if actual := attribute.IsPrimitivePointer(tc.attName, tc.useDefault); tc.expected != actual {
+ func() {
+ // panic recover
+ defer func() {
+ if k != "non object" {
+ return
+ }
+
+ if recover() == nil {
+ t.Errorf("should have panicked!")
+ }
+ }()
+
+ if actual := tc.attribute.IsPrimitivePointer(tc.attName, tc.useDefault); tc.expected != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, tc.expected)
}
+ }()
}
}
