Skip to content

Commit

Permalink
Merge pull request #836 from Juniper/task/833-stringSetOrNull-generic
Browse files Browse the repository at this point in the history
Update `stringSliceOrNull()` test helper function to support generic strings
  • Loading branch information
chrismarget-j authored Aug 29, 2024
2 parents d9d5e8c + afcc582 commit cfd7ef2
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (o resourceDataCenterConnectivityTemplateInterface) render(rType, rName str
o.blueprintId,
o.name,
stringOrNull(o.description),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
ipLinks,
routingZoneConstraints,
virtualNetworkMultiples,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (o resourceDataCenterConnectivityTemplateLoopback) render(rType, rName stri
o.blueprintId,
o.name,
stringOrNull(o.description),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
bgpPeeringIpEndoints,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func (o resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkMultiple) r
fmt.Sprintf(resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkMultipleHCL,
o.name,
stringOrNull(o.untaggedVnId),
stringSetOrNull(o.taggedVnIds),
stringSliceOrNull(o.taggedVnIds),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (o resourceDataCenterConnectivityTemplateSvi) render(rType, rName string) s
o.blueprintId,
o.name,
stringOrNull(o.description),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
bgpPeeringIpEndoints,
dynamicBgpPeerings,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (o resourceDataCenterConnectivityTemplateSystem) render(rType, rName string
o.blueprintId,
o.name,
stringOrNull(o.description),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
customStaticRoutes,
)
}
Expand Down
2 changes: 1 addition & 1 deletion apstra/resource_datacenter_device_allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestResourceDatacenterDeviceAllocation(t *testing.T) {
cidrOrNull(in.loopbackIpv4),
cidrOrNull(in.loopbackIpv6),
stringOrNull(in.deployMode),
stringSetOrNull(in.tags),
stringSliceOrNull(in.tags),
)
}

Expand Down
6 changes: 3 additions & 3 deletions apstra/resource_datacenter_routing_zone_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func (o testRoutingZone) render(bpId apstra.ObjectId, rType, rName string) strin

intPtrOrNull(o.vlan),
intPtrOrNull(o.vni),
stringSetOrNull(o.dhcpServers),
stringSliceOrNull(o.dhcpServers),
stringOrNull(o.routingPolicy),
stringSetOrNull(o.importRTs),
stringSetOrNull(o.exportRTs),
stringSliceOrNull(o.importRTs),
stringSliceOrNull(o.exportRTs),
stringOrNull(o.irbMode),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (o resourceAllocGroup) render(rType, rName string) string {
o.blueprintId,
o.name,
utils.StringersToFriendlyString(o.groupType),
stringSetOrNull(o.poolIds),
stringSliceOrNull(o.poolIds),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (o resourceFreeformConfigTemplate) render(rType, rName string) string {
o.blueprintId,
o.name,
o.text,
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
)
}

Expand Down
2 changes: 1 addition & 1 deletion apstra/resource_freeform_link_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (o resourceFreeformLink) render(rType, rName string) string {
rType, rName,
o.blueprintId,
o.name,
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
o.endpoints[0].SystemId,
stringPtrOrNull(o.endpoints[0].Interface.Data.IfName),
intPtrOrNull(o.endpoints[0].Interface.Data.TransformationId),
Expand Down
2 changes: 1 addition & 1 deletion apstra/resource_freeform_resource_integraion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (o resourceFreeformResource) render(rType, rName string) string {
ipNetOrNull(o.ipv4Value),
ipNetOrNull(o.ipv6Value),
stringOrNull(o.allocatedFrom.String()),
stringSetOrNull(o.assignedTo),
stringSliceOrNull(o.assignedTo),
)
}

Expand Down
2 changes: 1 addition & 1 deletion apstra/resource_freeform_system_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (o resourceFreeformSystem) render(rType, rName string) string {
o.hostname,
o.systemType,
stringOrNull(o.deployMode),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
)
}

Expand Down
64 changes: 63 additions & 1 deletion apstra/test_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func intPtrOrNull[A constraints.Integer](in *A) string {
return fmt.Sprintf("%d", *in)
}

func stringSetOrNull(in []string) string {
func stringSliceOrNull[S ~string](in []S) string {
if len(in) == 0 {
return "null"
}
Expand Down Expand Up @@ -407,6 +407,68 @@ func padFormatStr(n, base int) (string, error) {
return fmt.Sprintf("%%%d%s", c, baseChar), nil
}

func TestStringSliceOrNull(t *testing.T) {
type stringTestCase struct {
s []string
e string
}

stringTestCases := map[string]stringTestCase{
"with_strings": {
s: []string{"a", "b", "c"},
e: `[ "a", "b", "c" ]`,
},
"empty": {
s: []string{},
e: "null",
},
"nil": {
s: nil,
e: "null",
},
}

for tName, tCase := range stringTestCases {
t.Run("string_"+tName, func(t *testing.T) {
t.Parallel()
r := stringSliceOrNull(tCase.s)
if tCase.e != r {
t.Fatalf("expected %q, got %q", tCase.e, r)
}
})
}

type idTestCase struct {
s []apstra.ObjectId
e string
}

idTestCases := map[string]idTestCase{
"with_strings": {
s: []apstra.ObjectId{"a", "b", "c"},
e: `[ "a", "b", "c" ]`,
},
"empty": {
s: []apstra.ObjectId{},
e: "null",
},
"nil": {
s: nil,
e: "null",
},
}

for tName, tCase := range idTestCases {
t.Run("ObjectId_"+tName, func(t *testing.T) {
t.Parallel()
r := stringSliceOrNull(tCase.s)
if tCase.e != r {
t.Fatalf("expected %q, got %q", tCase.e, r)
}
})
}
}

func TestPadFormatStr(t *testing.T) {
type testCase struct {
n int
Expand Down

0 comments on commit cfd7ef2

Please sign in to comment.