Skip to content

Commit

Permalink
Add tests for typed nil returned by Value.Type()
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Mar 21, 2023
1 parent fc348eb commit 9125ce1
Showing 1 changed file with 156 additions and 2 deletions.
158 changes: 156 additions & 2 deletions values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ func TestGetType(t *testing.T) {
reflect.TypeOf(Event{}): {},
reflect.TypeOf(Resource{}): {},
reflect.TypeOf(Struct{}): {},
reflect.TypeOf(Enum{}): {},
reflect.TypeOf(Attachment{}): {},
}

typelessTypes := map[reflect.Type]struct{}{
Expand Down Expand Up @@ -681,14 +683,54 @@ func TestGetType(t *testing.T) {
}
})

t.Run("array value", func(t *testing.T) {
t.Run("array value without type", func(t *testing.T) {
t.Parallel()

typ := NewConstantSizedArrayType(4, TheInt8Type)
// Type is not set.
value := NewArray([]Value{})
assert.Nil(t, value.Type())

// Type is nil.
value = value.WithType(nil)
assert.Nil(t, value.Type())

// Type is ArrayType(nil).
value = value.WithType(ArrayType(nil))
assert.Nil(t, value.Type())
})

t.Run("constant sized array value", func(t *testing.T) {
t.Parallel()

typ := NewConstantSizedArrayType(1, TheInt8Type)
arrayValue := NewArray([]Value{NewInt8(0)}).WithType(typ)
assert.Equal(t, arrayValue.Type(), typ)
})

t.Run("variable sized array value", func(t *testing.T) {
t.Parallel()

typ := NewVariableSizedArrayType(TheInt8Type)
arrayValue := NewArray([]Value{}).WithType(typ)
assert.Equal(t, arrayValue.Type(), typ)
})

t.Run("contract value without type", func(t *testing.T) {
t.Parallel()

// Type is not set.
value := NewContract([]Value{})
assert.Nil(t, value.Type())

// Type is nil.
value = value.WithType(nil)
assert.Nil(t, value.Type())

// Type is (*ContractType)(nil).
value = value.WithType((*ContractType)(nil))
assert.Nil(t, value.Type())
})

t.Run("contract value", func(t *testing.T) {
t.Parallel()

Expand All @@ -697,6 +739,22 @@ func TestGetType(t *testing.T) {
assert.Equal(t, typ, value.Type())
})

t.Run("dictionary value without type", func(t *testing.T) {
t.Parallel()

// Type is not set.
value := NewDictionary([]KeyValuePair{})
assert.Nil(t, value.Type())

// Type is nil.
value = value.WithType(nil)
assert.Nil(t, value.Type())

// Type is (*DictionaryType)(nil).
value = value.WithType((*DictionaryType)(nil))
assert.Nil(t, value.Type())
})

t.Run("dictionary value", func(t *testing.T) {
t.Parallel()

Expand All @@ -705,6 +763,22 @@ func TestGetType(t *testing.T) {
assert.Equal(t, typ, value.Type())
})

t.Run("event value without type", func(t *testing.T) {
t.Parallel()

// Type is not set.
value := NewEvent([]Value{})
assert.Nil(t, value.Type())

// Type is nil.
value = value.WithType(nil)
assert.Nil(t, value.Type())

// Type is (*EventType)(nil).
value = value.WithType((*EventType)(nil))
assert.Nil(t, value.Type())
})

t.Run("event value", func(t *testing.T) {
t.Parallel()

Expand All @@ -713,6 +787,22 @@ func TestGetType(t *testing.T) {
assert.Equal(t, typ, value.Type())
})

t.Run("resource value without type", func(t *testing.T) {
t.Parallel()

// Type is not set.
value := NewResource([]Value{})
assert.Nil(t, value.Type())

// Type is nil.
value = value.WithType(nil)
assert.Nil(t, value.Type())

// Type is (*ResourceType)(nil).
value = value.WithType((*ResourceType)(nil))
assert.Nil(t, value.Type())
})

t.Run("resource value", func(t *testing.T) {
t.Parallel()

Expand All @@ -721,11 +811,75 @@ func TestGetType(t *testing.T) {
assert.Equal(t, typ, value.Type())
})

t.Run("struct value without type", func(t *testing.T) {
t.Parallel()

// Type is not set.
value := NewStruct([]Value{})
assert.Nil(t, value.Type())

// Type is nil.
value = value.WithType(nil)
assert.Nil(t, value.Type())

// Type is (*StructType)(nil).
value = value.WithType((*StructType)(nil))
assert.Nil(t, value.Type())
})

t.Run("struct value", func(t *testing.T) {
t.Parallel()

typ := NewStructType(nil, "Foo", nil, nil)
value := NewStruct([]Value{}).WithType(typ)
assert.Equal(t, typ, value.Type())
})

t.Run("enum value without type", func(t *testing.T) {
t.Parallel()

// Type is not set.
value := NewEnum([]Value{})
assert.Nil(t, value.Type())

// Type is nil.
value = value.WithType(nil)
assert.Nil(t, value.Type())

// Type is (*EnumType)(nil).
value = value.WithType((*EnumType)(nil))
assert.Nil(t, value.Type())
})

t.Run("enum value", func(t *testing.T) {
t.Parallel()

typ := NewEnumType(nil, "Foo", nil, nil, nil)
value := NewEnum([]Value{}).WithType(typ)
assert.Equal(t, typ, value.Type())
})

t.Run("attachment value without type", func(t *testing.T) {
t.Parallel()

// Type is not set.
value := NewAttachment([]Value{})
assert.Nil(t, value.Type())

// Type is nil.
value = value.WithType(nil)
assert.Nil(t, value.Type())

// Type is (*AttachmentType)(nil).
value = value.WithType((*AttachmentType)(nil))
assert.Nil(t, value.Type())
})

t.Run("attachment value", func(t *testing.T) {
t.Parallel()

typ := NewAttachmentType(nil, nil, "Foo", nil, nil)
value := NewAttachment([]Value{}).WithType(typ)
assert.Equal(t, typ, value.Type())
})
}

0 comments on commit 9125ce1

Please sign in to comment.