From 9320261c0c9e8d3d7fe795bcae6812d988a17c26 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 7 Sep 2022 16:23:53 -0700 Subject: [PATCH 1/2] Fix comment on Decapsulate --- interface.go | 4 ++-- multiaddr_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/interface.go b/interface.go index 0c2198b..23d0eb7 100644 --- a/interface.go +++ b/interface.go @@ -47,9 +47,9 @@ type Multiaddr interface { // Encapsulate(Multiaddr) Multiaddr - // Decapsultate removes a Multiaddr wrapping. For example: + // Decapsulate removes a Multiaddr wrapping. For example: // - // /ip4/1.2.3.4/tcp/80 decapsulate /ip4/1.2.3.4 = /tcp/80 + // /ip4/1.2.3.4/tcp/80 decapsulate /tcp/80 = /ip4/1.2.3.4 // Decapsulate(Multiaddr) Multiaddr diff --git a/multiaddr_test.go b/multiaddr_test.go index 1eb9d73..6cba615 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -427,6 +427,15 @@ func TestEncapsulate(t *testing.T) { } } +func TestDecapsulateComment(t *testing.T) { + // shows the behavior from the interface comment + m := StringCast("/ip4/1.2.3.4/tcp/80") + rest := m.Decapsulate(StringCast("/tcp/80")) + if rest.String() != "/ip4/1.2.3.4" { + t.Fatalf("Documented behavior is not correct. Expected %v saw %v", "/ip4/1.2.3.4/", rest.String()) + } +} + func assertValueForProto(t *testing.T, a Multiaddr, p int, exp string) { t.Logf("checking for %s in %s", ProtocolWithCode(p).Name, a) fv, err := a.ValueForProtocol(p) From 2837d354058deeec2461baf81ce80613da8eca2c Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Thu, 8 Sep 2022 07:42:47 -0700 Subject: [PATCH 2/2] Document other behaviors --- interface.go | 2 ++ multiaddr_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/interface.go b/interface.go index 23d0eb7..699c54d 100644 --- a/interface.go +++ b/interface.go @@ -50,6 +50,8 @@ type Multiaddr interface { // Decapsulate removes a Multiaddr wrapping. For example: // // /ip4/1.2.3.4/tcp/80 decapsulate /tcp/80 = /ip4/1.2.3.4 + // /ip4/1.2.3.4/tcp/80 decapsulate /udp/80 = /ip4/1.2.3.4/tcp/80 + // /ip4/1.2.3.4/tcp/80 decapsulate /ip4/1.2.3.4 = nil // Decapsulate(Multiaddr) Multiaddr diff --git a/multiaddr_test.go b/multiaddr_test.go index 6cba615..62ccc32 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -434,6 +434,16 @@ func TestDecapsulateComment(t *testing.T) { if rest.String() != "/ip4/1.2.3.4" { t.Fatalf("Documented behavior is not correct. Expected %v saw %v", "/ip4/1.2.3.4/", rest.String()) } + + m = StringCast("/ip4/1.2.3.4/tcp/80") + rest = m.Decapsulate(StringCast("/udp/80")) + if !rest.Equal(m) { + t.Fatalf("Documented behavior is not correct. Expected %v saw %v", "/ip4/1.2.3.4/tcp/80", rest.String()) + } + + m = StringCast("/ip4/1.2.3.4/tcp/80") + rest = m.Decapsulate(StringCast("/ip4/1.2.3.4")) + require.Nil(t, rest, "expected a nil multiaddr if we decapsulate everything") } func assertValueForProto(t *testing.T, a Multiaddr, p int, exp string) {