ooni-probe-cli/internal/runtimex/runtimex_test.go
Simone Basso dfa5e708fe
refactor(tor): rewrite using measurex (#652)
This diff rewrites the tor experiment to use measurex "easy" API.

To this end, we need to introduce an "easy" measurex API, which basically
performs easy measurements returning two pieces of data:

1. the resulting measurement, which is already using the OONI
archival data format and is always non-nil

2. a failure (i.e., the pointer to an error string), which
is nil on success and points to a string on failure

With this change, we should now be able to completely dispose of
the original netx API, which was only used by tor.

Reference issue: https://github.com/ooni/probe/issues/1688.
2022-01-05 18:41:11 +01:00

96 lines
2.2 KiB
Go

package runtimex_test
import (
"errors"
"testing"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
func TestPanicOnError(t *testing.T) {
badfunc := func(in error) (out error) {
defer func() {
out = recover().(error)
}()
runtimex.PanicOnError(in, "we expect this assertion to fail")
return
}
t.Run("error is nil", func(t *testing.T) {
runtimex.PanicOnError(nil, "this assertion should not fail")
})
t.Run("error is not nil", func(t *testing.T) {
expected := errors.New("mocked error")
if !errors.Is(badfunc(expected), expected) {
t.Fatal("not the error we expected")
}
})
}
func TestPanicIfFalse(t *testing.T) {
badfunc := func(in bool, message string) (out error) {
defer func() {
out = errors.New(recover().(string))
}()
runtimex.PanicIfFalse(in, message)
return
}
t.Run("assertion is true", func(t *testing.T) {
runtimex.PanicIfFalse(true, "this assertion should not fail")
})
t.Run("assertion is false", func(t *testing.T) {
message := "mocked error"
err := badfunc(false, message)
if err == nil || err.Error() != message {
t.Fatal("not the error we expected", err)
}
})
}
func TestPanicIfTrue(t *testing.T) {
badfunc := func(in bool, message string) (out error) {
defer func() {
out = errors.New(recover().(string))
}()
runtimex.PanicIfTrue(in, message)
return
}
t.Run("assertion is false", func(t *testing.T) {
runtimex.PanicIfTrue(false, "this assertion should not fail")
})
t.Run("assertion is true", func(t *testing.T) {
message := "mocked error"
err := badfunc(true, message)
if err == nil || err.Error() != message {
t.Fatal("not the error we expected", err)
}
})
}
func TestPanicIfNil(t *testing.T) {
badfunc := func(in interface{}, message string) (out error) {
defer func() {
out = errors.New(recover().(string))
}()
runtimex.PanicIfNil(in, message)
return
}
t.Run("value is not nil", func(t *testing.T) {
runtimex.PanicIfNil(false, "this assertion should not fail")
})
t.Run("value is nil", func(t *testing.T) {
message := "mocked error"
err := badfunc(nil, message)
if err == nil || err.Error() != message {
t.Fatal("not the error we expected", err)
}
})
}