-
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.
SonarCloud: add tests to increase code coverage (2)
- Loading branch information
1 parent
deb63e0
commit d0094bb
Showing
4 changed files
with
279 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package device_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/plgd-dev/device/schema/device" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDeviceGetManufacturerName(t *testing.T) { | ||
type fields struct { | ||
ManufacturerName []device.LocalizedString | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
}{ | ||
{ | ||
name: "Empty", | ||
fields: fields{ | ||
ManufacturerName: nil, | ||
}, | ||
want: "", | ||
}, | ||
{ | ||
name: "Slovak", | ||
fields: fields{ | ||
ManufacturerName: []device.LocalizedString{ | ||
{ | ||
Language: "sk", | ||
Value: "zariadenie", | ||
}, | ||
}, | ||
}, | ||
want: "", | ||
}, | ||
{ | ||
name: "English", | ||
fields: fields{ | ||
ManufacturerName: []device.LocalizedString{ | ||
{ | ||
Language: "en", | ||
Value: "device", | ||
}, | ||
}, | ||
}, | ||
want: "device", | ||
}, | ||
{ | ||
name: "Multiple", | ||
fields: fields{ | ||
ManufacturerName: []device.LocalizedString{ | ||
{ | ||
Language: "sk", | ||
Value: "zariadenie", | ||
}, | ||
{ | ||
Language: "en", | ||
Value: "device", | ||
}, | ||
{ | ||
Language: "de", | ||
Value: "Gerät", | ||
}, | ||
}, | ||
}, | ||
want: "device", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
d := device.Device{ | ||
ManufacturerName: tt.fields.ManufacturerName, | ||
} | ||
require.Equal(t, d.GetManufacturerName(), tt.want) | ||
}) | ||
} | ||
} |
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,198 @@ | ||
package schema_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/plgd-dev/device/schema" | ||
"github.com/plgd-dev/device/schema/acl" | ||
"github.com/plgd-dev/device/schema/ael" | ||
"github.com/plgd-dev/device/schema/credential" | ||
kitNet "github.com/plgd-dev/kit/v2/net" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBitMaskHas(t *testing.T) { | ||
b := schema.BitMask(0) | ||
require.False(t, b.Has(schema.Discoverable)) | ||
require.False(t, b.Has(schema.Observable)) | ||
|
||
b = schema.Discoverable | ||
require.True(t, b.Has(schema.Discoverable)) | ||
require.False(t, b.Has(schema.Observable)) | ||
|
||
b = schema.Discoverable | schema.Observable | ||
require.True(t, b.Has(schema.Discoverable)) | ||
require.True(t, b.Has(schema.Observable)) | ||
} | ||
|
||
func TestResourceLinkHasType(t *testing.T) { | ||
type fields struct { | ||
resourceTypes []string | ||
} | ||
type args struct { | ||
resourceType string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "Empty", | ||
fields: fields{ | ||
resourceTypes: []string{credential.ResourceType, acl.ResourceType}, | ||
}, | ||
args: args{ | ||
resourceType: "", | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Mismatch", | ||
fields: fields{ | ||
resourceTypes: []string{credential.ResourceType, acl.ResourceType}, | ||
}, | ||
args: args{ | ||
resourceType: ael.ResourceType, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Match", | ||
fields: fields{ | ||
resourceTypes: []string{credential.ResourceType, acl.ResourceType}, | ||
}, | ||
args: args{ | ||
resourceType: acl.ResourceType, | ||
}, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := schema.ResourceLink{ | ||
ResourceTypes: tt.fields.resourceTypes, | ||
} | ||
require.Equal(t, tt.want, r.HasType(tt.args.resourceType)) | ||
}) | ||
} | ||
} | ||
|
||
func TestResourceLinkPatchEndpoint(t *testing.T) { | ||
type args struct { | ||
addr kitNet.Addr | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want schema.Endpoints | ||
}{ | ||
{ | ||
name: "IPv4", | ||
args: args{ | ||
addr: kitNet.MakeAddr("coap", "127.0.0.1", 5683), | ||
}, | ||
want: schema.Endpoints{ | ||
schema.Endpoint{URI: "coap://127.0.0.1:5683"}, | ||
schema.Endpoint{URI: "coap+tcp://127.0.0.1:5683"}, | ||
schema.Endpoint{URI: "coaps+tcp://127.0.0.1:5684"}, | ||
}, | ||
}, | ||
{ | ||
name: "IPv6", | ||
args: args{ | ||
addr: kitNet.MakeAddr("coap", "fe80::1", 5683), | ||
}, | ||
want: schema.Endpoints{ | ||
schema.Endpoint{URI: "coap://[fe80::1]:5683"}, | ||
schema.Endpoint{URI: "coap+tcp://[fe80::1]:5683"}, | ||
schema.Endpoint{URI: "coaps+tcp://[fe80::1]:5684"}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := schema.ResourceLink{ | ||
Policy: &schema.Policy{ | ||
UDPPort: 5683, | ||
TCPPort: 5683, | ||
TCPTLSPort: 5684, | ||
}, | ||
} | ||
got := r.PatchEndpoint(tt.args.addr, nil) | ||
require.Len(t, got.Endpoints, len(tt.want)) | ||
if len(tt.want) > 0 { | ||
got.Endpoints = got.Endpoints.Sort() | ||
tt.want = tt.want.Sort() | ||
for i := range got.Endpoints { | ||
require.Equal(t, got.Endpoints[i], tt.want[i]) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestResourceLinkPatchEndpointLinkLocal(t *testing.T) { | ||
type fields struct { | ||
Endpoints schema.Endpoints | ||
} | ||
type args struct { | ||
addr kitNet.Addr | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want schema.Endpoints | ||
}{ | ||
{ | ||
name: "IPv4", | ||
fields: fields{ | ||
Endpoints: schema.Endpoints{ | ||
schema.Endpoint{ | ||
URI: "coap://169.254.0.1:5683", | ||
}, | ||
}, | ||
}, | ||
args: args{ | ||
addr: kitNet.MakeAddr("coap", "169.254.0.1", 5683), | ||
}, | ||
want: schema.Endpoints{ | ||
schema.Endpoint{URI: "coap://169.254.0.1:5683"}, | ||
}, | ||
}, | ||
{ | ||
name: "IPv6", | ||
fields: fields{ | ||
Endpoints: schema.Endpoints{ | ||
schema.Endpoint{ | ||
URI: "coap://[fe80::1]:5683", | ||
}, | ||
}, | ||
}, | ||
args: args{ | ||
addr: kitNet.MakeAddr("coap", "fe80::1%eth0", 0), | ||
}, | ||
want: schema.Endpoints{ | ||
schema.Endpoint{URI: "coap://[fe80::1%eth0]:5683"}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := schema.ResourceLink{ | ||
Endpoints: tt.fields.Endpoints, | ||
} | ||
got := r.PatchEndpoint(tt.args.addr, nil) | ||
require.Len(t, got.Endpoints, len(tt.want)) | ||
if len(tt.want) > 0 { | ||
got.Endpoints = got.Endpoints.Sort() | ||
tt.want = tt.want.Sort() | ||
for i := range got.Endpoints { | ||
require.Equal(t, got.Endpoints[i], tt.want[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
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