ooni-probe-cli/internal/testingx/fakefill_test.go
Simone Basso 5ebdeb56ca
feat: tlsping and tcpping using step-by-step (#815)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/2158
- [x] if you changed anything related how experiments work and you need to reflect these changes in the ooni/spec repository, please link to the related ooni/spec pull request: https://github.com/ooni/spec/pull/250

## Description

This diff refactors the codebase to reimplement tlsping and tcpping
to use the step-by-step measurements style.

See docs/design/dd-003-step-by-step.md for more information on the
step-by-step measurement style.
2022-07-01 12:22:22 +02:00

92 lines
1.8 KiB
Go

package testingx
import (
"testing"
"time"
)
// exampleStructure is an example structure we fill.
type exampleStructure struct {
CategoryCodes string
CountryCode string
Enabled bool
MaxResults int64
Now time.Time
}
func TestFakeFillWorksWithCustomTime(t *testing.T) {
var req *exampleStructure
ff := &FakeFiller{
Now: func() time.Time {
return time.Date(1992, time.January, 24, 17, 53, 0, 0, time.UTC)
},
}
ff.Fill(&req)
if req == nil {
t.Fatal("we expected non nil here")
}
}
func TestFakeFillAllocatesIntoAPointerToPointer(t *testing.T) {
var req *exampleStructure
ff := &FakeFiller{}
ff.Fill(&req)
if req == nil {
t.Fatal("we expected non nil here")
}
}
func TestFakeFillAllocatesIntoAMapLikeWithStringKeys(t *testing.T) {
var resp map[string]*exampleStructure
ff := &FakeFiller{}
ff.Fill(&resp)
if resp == nil {
t.Fatal("we expected non nil here")
}
if len(resp) < 1 {
t.Fatal("we expected some data here")
}
for _, value := range resp {
if value == nil {
t.Fatal("expected non-nil here")
}
}
}
func TestFakeFillAllocatesIntoAMapLikeWithNonStringKeys(t *testing.T) {
var panicmsg string
func() {
defer func() {
if v := recover(); v != nil {
panicmsg = v.(string)
}
}()
var resp map[int64]*exampleStructure
ff := &FakeFiller{}
ff.Fill(&resp)
if resp != nil {
t.Fatal("we expected nil here")
}
}()
if panicmsg != "fakefill: we only support string key types" {
t.Fatal("unexpected panic message", panicmsg)
}
}
func TestFakeFillAllocatesIntoASlice(t *testing.T) {
var resp *[]*exampleStructure
ff := &FakeFiller{}
ff.Fill(&resp)
if resp == nil {
t.Fatal("we expected non nil here")
}
if len(*resp) < 1 {
t.Fatal("we expected some data here")
}
for _, entry := range *resp {
if entry == nil {
t.Fatal("expected non-nil here")
}
}
}