Add tests for IRIf

This commit is contained in:
Marius Orcsik 2022-05-23 20:47:23 +02:00
parent ce99195aca
commit 6921b4d323
No known key found for this signature in database
GPG key ID: DBF5E47F5DBC4D21
2 changed files with 69 additions and 3 deletions

View file

@ -126,9 +126,12 @@ func IRIf(i pub.IRI, t CollectionType) pub.IRI {
// IRI gives us the IRI of the t collection type corresponding to the i Item,
// or generates a new one if not found.
func (t CollectionType) IRI(i pub.Item) pub.IRI {
if !ValidCollection(t) {
return pub.EmptyIRI
}
it := t.Of(i)
if pub.IsNil(it) {
return pub.EmptyIRI
return IRIf("", t)
}
if iri := it.GetLink(); len(iri) > 0 {
return iri
@ -139,7 +142,7 @@ func (t CollectionType) IRI(i pub.Item) pub.IRI {
// Of gives us the property of the i Item that corresponds to the t collection type.
func (t CollectionType) Of(i pub.Item) pub.Item {
if pub.IsNil(i) || !i.IsObject() {
return pub.EmptyIRI
return nil
}
var it pub.Item
pub.OnActor(i, func(a *pub.Actor) error {

View file

@ -4,6 +4,7 @@ import (
"testing"
"github.com/go-ap/activitypub"
pub "github.com/go-ap/activitypub"
)
func TestPathTyper_Type(t *testing.T) {
@ -40,6 +41,14 @@ func TestCollectionTypes_Of(t *testing.T) {
args args
want activitypub.Item
}{
{
name: "nil from nil object",
args: args{
o: nil,
t: "likes",
},
want: nil,
},
{
name: "nil from invalid collection type",
args: args{
@ -82,7 +91,61 @@ func TestCollectionTypes_Of(t *testing.T) {
}
func TestCollectionType_IRI(t *testing.T) {
t.Skipf("TODO")
type args struct {
o activitypub.Item
t CollectionType
}
tests := []struct {
name string
args args
want activitypub.IRI
}{
{
name: "just path from nil object",
args: args{
o: nil,
t: "likes",
},
want: pub.IRI("/likes"),
},
{
name: "emptyIRI from invalid collection type",
args: args{
o: activitypub.Object{
Likes: activitypub.IRI("test"),
},
t: "like",
},
want: pub.EmptyIRI,
},
{
name: "just path from object without ID",
args: args{
o: activitypub.Object{},
t: "likes",
},
want: pub.IRI("/likes"),
},
{
name: "likes iri on object",
args: args{
o: activitypub.Object{
ID: "http://example.com",
Likes: activitypub.IRI("test"),
},
t: "likes",
},
want: activitypub.IRI("test"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if ob := test.args.t.IRI(test.args.o); ob != test.want {
t.Errorf("IRI received %q is different, expected %q", ob, test.want)
}
})
}
}
func TestCollectionType_OfActor(t *testing.T) {