mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-09-27 04:26:41 +08:00
constructor: allow nil options
This can make it significantly easier to configure libp2p with optional options.
This commit is contained in:
@@ -133,3 +133,28 @@ func makeRandomHost(t *testing.T, port int) (host.Host, error) {
|
||||
|
||||
return New(ctx, opts...)
|
||||
}
|
||||
|
||||
func TestChainOptions(t *testing.T) {
|
||||
var cfg Config
|
||||
var optsRun []int
|
||||
optcount := 0
|
||||
newOpt := func() Option {
|
||||
index := optcount
|
||||
optcount++
|
||||
return func(c *Config) error {
|
||||
optsRun = append(optsRun, index)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if err := cfg.Apply(newOpt(), nil, ChainOptions(newOpt(), newOpt(), ChainOptions(), ChainOptions(nil, newOpt()))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if optcount != len(optsRun) {
|
||||
t.Errorf("expected to have handled %d options, handled %d", optcount, len(optsRun))
|
||||
}
|
||||
for i, x := range optsRun {
|
||||
if i != x {
|
||||
t.Errorf("expected opt %d, got opt %d", i, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user