|
| 1 | +package scanner |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestBaseType(t *testing.T) { |
| 10 | + typ := newBaseType() |
| 11 | + name := "BaseType" |
| 12 | + |
| 13 | + assertRepeatable(t, typ, name) |
| 14 | + assertNullable(t, typ, name) |
| 15 | + assert.Panics(t, func() { typ.TypeString() }, "does not implement TypeString") |
| 16 | + assert.Panics(t, func() { typ.String() }, "does not implement String") |
| 17 | + assert.Panics(t, func() { typ.UnqualifiedName() }, "does not implement UnqualifiedName") |
| 18 | +} |
| 19 | + |
| 20 | +func TestBasic(t *testing.T) { |
| 21 | + typ := NewBasic("basic") |
| 22 | + name := "Basic" |
| 23 | + |
| 24 | + assertRepeatable(t, typ, name) |
| 25 | + |
| 26 | + assert.True(t, typ.IsNullable(), "Basic is nullable by default") |
| 27 | + typ.SetNullable(false) |
| 28 | + assert.True(t, typ.IsNullable(), "Basic cannot be set not nullable") |
| 29 | + |
| 30 | + assert.Equal(t, "basic", typ.String(), "Basic.String returns type's name") |
| 31 | + assert.Equal(t, "basic", typ.TypeString(), "Basic.TypeString returns type's name") |
| 32 | + assert.Equal(t, "basic", typ.UnqualifiedName(), "Basic.UnqualifiedName returns type's name") |
| 33 | +} |
| 34 | + |
| 35 | +func TestNamed_withPath(t *testing.T) { |
| 36 | + typ := NewNamed("pkg", "name") |
| 37 | + name := "Named (with path)" |
| 38 | + |
| 39 | + assertRepeatable(t, typ, name) |
| 40 | + assertNullable(t, typ, name) |
| 41 | + |
| 42 | + assert.Equal(t, "pkg.name", typ.String(), "Named.String() (with path) returns type's name with path") |
| 43 | + assert.Equal(t, "pkg.name", typ.TypeString(), "Named.TypeString() (with path) returns type's name with path") |
| 44 | + assert.Equal(t, "name", typ.UnqualifiedName(), "Named.UnqualifiedName() (with path) returns type's name without path") |
| 45 | +} |
| 46 | + |
| 47 | +func TestNamed_withoutPath(t *testing.T) { |
| 48 | + typ := NewNamed("", "name") |
| 49 | + name := "Named (without path)" |
| 50 | + |
| 51 | + assertRepeatable(t, typ, name) |
| 52 | + assertNullable(t, typ, name) |
| 53 | + |
| 54 | + assert.Equal(t, "name", typ.String(), "Named.String() (with path) returns type's name without path") |
| 55 | + assert.Equal(t, "name", typ.TypeString(), "Named.TypeString() (with path) returns type's name without path") |
| 56 | + assert.Equal(t, "name", typ.UnqualifiedName(), "Named.UnqualifiedName() (with path) returns type's name without path") |
| 57 | +} |
| 58 | + |
| 59 | +func TestAlias_IsNullable(t *testing.T) { |
| 60 | + typ := NewAlias(newBaseType(), newBaseType()).(*Alias) |
| 61 | + |
| 62 | + assert.False(t, typ.IsNullable(), "Alias is not nullable if neither the type nor the underlying is nullable") |
| 63 | + typ.Type.SetNullable(true) |
| 64 | + typ.Underlying.SetNullable(false) |
| 65 | + assert.True(t, typ.IsNullable(), "Alias is nullable if the type is nullable but the underlying is not") |
| 66 | + typ.Type.SetNullable(false) |
| 67 | + typ.Underlying.SetNullable(true) |
| 68 | + assert.True(t, typ.IsNullable(), "Alias is nullable if the type is not nullable but the underlying is") |
| 69 | + typ.Type.SetNullable(true) |
| 70 | + typ.Underlying.SetNullable(true) |
| 71 | + assert.True(t, typ.IsNullable(), "Alias is nullable if both the type and the underlying are") |
| 72 | +} |
| 73 | + |
| 74 | +func TestAlias_IsRepeated(t *testing.T) { |
| 75 | + typ := NewAlias(newBaseType(), newBaseType()).(*Alias) |
| 76 | + |
| 77 | + assert.False(t, typ.IsRepeated(), "Alias is not repeated if neither the type nor the underlying is repeated") |
| 78 | + |
| 79 | + typ.Type.SetRepeated(true) |
| 80 | + typ.Underlying.SetRepeated(false) |
| 81 | + assert.True(t, typ.IsRepeated(), "Alias is repeated if the type is repeated but the underlying is not") |
| 82 | + typ.Type.SetRepeated(false) |
| 83 | + typ.Underlying.SetRepeated(true) |
| 84 | + assert.True(t, typ.IsRepeated(), "Alias is repeated if the type is not repeated but the underlying is") |
| 85 | + typ.Type.SetRepeated(true) |
| 86 | + typ.Underlying.SetRepeated(true) |
| 87 | + assert.True(t, typ.IsRepeated(), "Alias is repeated if both the type and the underlying are") |
| 88 | +} |
| 89 | + |
| 90 | +func TestAlias_stringMethods(t *testing.T) { |
| 91 | + typ := NewAlias(NewNamed("", "Aliasing"), NewBasic("string")) |
| 92 | + |
| 93 | + assert.Equal(t, "type Aliasing string", typ.String(), "Alias.String returns a type declaration for the alias") |
| 94 | + assert.Equal(t, "Aliasing", typ.TypeString(), "Alias.TypeString returns the type string for the alias") |
| 95 | + assert.Equal(t, "Aliasing", typ.UnqualifiedName(), "Alias.UnqualifiedName returns the unqualified name of the alias") |
| 96 | +} |
| 97 | + |
| 98 | +func TestMap(t *testing.T) { |
| 99 | + typ := NewMap(NewBasic("string"), NewBasic("int")) |
| 100 | + name := "Map" |
| 101 | + |
| 102 | + assertRepeatable(t, typ, name) |
| 103 | + assertNullable(t, typ, name) |
| 104 | + assert.Equal(t, "map[string]int", typ.String(), "Map.String returns a map signature") |
| 105 | + assert.Equal(t, "map[string]int", typ.TypeString(), "Map.TypeString returns a map signature") |
| 106 | + assert.Equal(t, "map[string]int", typ.UnqualifiedName(), "Map.UnqualifiedName returns a map signature") |
| 107 | +} |
| 108 | + |
| 109 | +// assertRepeatable asserts a type respond as expected to IsRepeated and SetRepeated. |
| 110 | +func assertRepeatable(t *testing.T, typ Type, name string) { |
| 111 | + assert.False(t, typ.IsRepeated(), "%s is not repeated by default", name) |
| 112 | + typ.SetRepeated(true) |
| 113 | + assert.True(t, typ.IsRepeated(), "%s can be set as repeated", name) |
| 114 | + typ.SetRepeated(false) |
| 115 | + assert.False(t, typ.IsRepeated(), "%s can be set as not repeated", name) |
| 116 | +} |
| 117 | + |
| 118 | +// assertNullable asserts a type responds as expected to IsNullable and SetNullable. |
| 119 | +func assertNullable(t *testing.T, typ Type, name string) { |
| 120 | + assert.False(t, typ.IsNullable(), "%s is not nullable by default", name) |
| 121 | + typ.SetNullable(true) |
| 122 | + assert.True(t, typ.IsNullable(), "%s can be set as nullable", name) |
| 123 | + typ.SetNullable(false) |
| 124 | + assert.False(t, typ.IsNullable(), "%s can be set as not nullable", name) |
| 125 | +} |
0 commit comments