This repository has been archived on 2022-11-27. You can view files and clone it, but cannot push or open issues or pull requests.
activitypub/helpers_test.go
Marius Orcsik 15ac5c20c7
Updated OnX functions to apply the function on an item collection if that's what's being passed to it
This still has the downside that the collection might contain different types of objects

but I can't think of a better way to do it at the moment
2021-01-31 13:30:40 +01:00

271 lines
6.5 KiB
Go

package activitypub
import (
"fmt"
"testing"
)
func assertObjectWithTesting(fn canErrorFunc, expected Item) withObjectFn {
return func (p *Object) error {
if !assertDeepEquals(fn, p , expected) {
return fmt.Errorf("not equal")
}
return nil
}
}
func TestOnObject(t *testing.T) {
testObject := Object{
ID: "https://example.com",
}
type args struct {
it Item
fn func(canErrorFunc, Item) withObjectFn
}
tests := []struct {
name string
args args
expected Item
wantErr bool
}{
{
name: "single",
args: args{testObject, assertObjectWithTesting},
expected: &testObject,
wantErr: false,
},
{
name: "single fails",
args: args{Object{ID: "https://not-equals"}, assertObjectWithTesting},
expected: &testObject,
wantErr: true,
},
{
name: "collectionOfObjects",
args: args{ItemCollection{testObject, testObject}, assertObjectWithTesting},
expected: &testObject,
wantErr: false,
},
{
name: "collectionOfObjects fails",
args: args{ItemCollection{testObject, Object{ID: "https://not-equals"}}, assertObjectWithTesting},
expected: &testObject,
wantErr: true,
},
}
for _, tt := range tests {
var logFn canErrorFunc
if tt.wantErr {
logFn = t.Logf
} else {
logFn = t.Errorf
}
t.Run(tt.name, func(t *testing.T) {
if err := OnObject(tt.args.it, tt.args.fn(logFn, tt.expected)); (err != nil) != tt.wantErr {
t.Errorf("OnObject() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func assertActivityWithTesting(fn canErrorFunc, expected Item) withActivityFn {
return func (p *Activity) error {
if !assertDeepEquals(fn, p , expected) {
return fmt.Errorf("not equal")
}
return nil
}
}
func TestOnActivity(t *testing.T) {
testActivity := Activity{
ID: "https://example.com",
}
type args struct {
it Item
fn func(canErrorFunc, Item) withActivityFn
}
tests := []struct {
name string
args args
expected Item
wantErr bool
}{
{
name: "single",
args: args{testActivity, assertActivityWithTesting},
expected: &testActivity,
wantErr: false,
},
{
name: "single fails",
args: args{Activity{ID: "https://not-equals"}, assertActivityWithTesting},
expected: &testActivity,
wantErr: true,
},
{
name: "collectionOfActivitys",
args: args{ItemCollection{testActivity, testActivity}, assertActivityWithTesting},
expected: &testActivity,
wantErr: false,
},
{
name: "collectionOfActivitys fails",
args: args{ItemCollection{testActivity, Activity{ID: "https://not-equals"}}, assertActivityWithTesting},
expected: &testActivity,
wantErr: true,
},
}
for _, tt := range tests {
var logFn canErrorFunc
if tt.wantErr {
logFn = t.Logf
} else {
logFn = t.Errorf
}
t.Run(tt.name, func(t *testing.T) {
if err := OnActivity(tt.args.it, tt.args.fn(logFn, tt.expected)); (err != nil) != tt.wantErr {
t.Errorf("OnActivity() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func assertIntransitiveActivityWithTesting(fn canErrorFunc, expected Item) withIntransitiveActivityFn {
return func (p *IntransitiveActivity) error {
if !assertDeepEquals(fn, p , expected) {
return fmt.Errorf("not equal")
}
return nil
}
}
func TestOnIntransitiveActivity(t *testing.T) {
testIntransitiveActivity := IntransitiveActivity{
ID: "https://example.com",
}
type args struct {
it Item
fn func(canErrorFunc, Item) withIntransitiveActivityFn
}
tests := []struct {
name string
args args
expected Item
wantErr bool
}{
{
name: "single",
args: args{testIntransitiveActivity, assertIntransitiveActivityWithTesting},
expected: &testIntransitiveActivity,
wantErr: false,
},
{
name: "single fails",
args: args{IntransitiveActivity{ID: "https://not-equals"}, assertIntransitiveActivityWithTesting},
expected: &testIntransitiveActivity,
wantErr: true,
},
{
name: "collectionOfIntransitiveActivitys",
args: args{ItemCollection{testIntransitiveActivity, testIntransitiveActivity}, assertIntransitiveActivityWithTesting},
expected: &testIntransitiveActivity,
wantErr: false,
},
{
name: "collectionOfIntransitiveActivitys fails",
args: args{ItemCollection{testIntransitiveActivity, IntransitiveActivity{ID: "https://not-equals"}}, assertIntransitiveActivityWithTesting},
expected: &testIntransitiveActivity,
wantErr: true,
},
}
for _, tt := range tests {
var logFn canErrorFunc
if tt.wantErr {
logFn = t.Logf
} else {
logFn = t.Errorf
}
t.Run(tt.name, func(t *testing.T) {
if err := OnIntransitiveActivity(tt.args.it, tt.args.fn(logFn, tt.expected)); (err != nil) != tt.wantErr {
t.Errorf("OnIntransitiveActivity() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func assertQuestionWithTesting(fn canErrorFunc, expected Item) withQuestionFn {
return func (p *Question) error {
if !assertDeepEquals(fn, p , expected) {
return fmt.Errorf("not equal")
}
return nil
}
}
func TestOnQuestion(t *testing.T) {
testQuestion := Question{
ID: "https://example.com",
}
type args struct {
it Item
fn func(canErrorFunc, Item) withQuestionFn
}
tests := []struct {
name string
args args
expected Item
wantErr bool
}{
{
name: "single",
args: args{testQuestion, assertQuestionWithTesting},
expected: &testQuestion,
wantErr: false,
},
{
name: "single fails",
args: args{Question{ID: "https://not-equals"}, assertQuestionWithTesting},
expected: &testQuestion,
wantErr: true,
},
{
name: "collectionOfQuestions",
args: args{ItemCollection{testQuestion, testQuestion}, assertQuestionWithTesting},
expected: &testQuestion,
wantErr: false,
},
{
name: "collectionOfQuestions fails",
args: args{ItemCollection{testQuestion, Question{ID: "https://not-equals"}}, assertQuestionWithTesting},
expected: &testQuestion,
wantErr: true,
},
}
for _, tt := range tests {
var logFn canErrorFunc
if tt.wantErr {
logFn = t.Logf
} else {
logFn = t.Errorf
}
t.Run(tt.name, func(t *testing.T) {
if err := OnQuestion(tt.args.it, tt.args.fn(logFn, tt.expected)); (err != nil) != tt.wantErr {
t.Errorf("OnQuestion() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestOnCollection(t *testing.T) {
t.Skipf("TODO")
}
func TestOnCollectionPage(t *testing.T) {
t.Skipf("TODO")
}
func TestOnOrderedCollectionPage(t *testing.T) {
t.Skipf("TODO")
}