Skip to content

Commit

Permalink
Merge pull request #181 from multiformats/marco/fix-comment
Browse files Browse the repository at this point in the history
Fix comment on Decapsulate
  • Loading branch information
MarcoPolo authored Sep 8, 2022
2 parents 450ddd9 + 2837d35 commit 91391e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ 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
// /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

Expand Down
19 changes: 19 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,25 @@ 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())
}

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) {
t.Logf("checking for %s in %s", ProtocolWithCode(p).Name, a)
fv, err := a.ValueForProtocol(p)
Expand Down

0 comments on commit 91391e2

Please sign in to comment.