Skip to content

Commit 91391e2

Browse files
authored
Merge pull request #181 from multiformats/marco/fix-comment
Fix comment on Decapsulate
2 parents 450ddd9 + 2837d35 commit 91391e2

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

interface.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ type Multiaddr interface {
4747
//
4848
Encapsulate(Multiaddr) Multiaddr
4949

50-
// Decapsultate removes a Multiaddr wrapping. For example:
50+
// Decapsulate removes a Multiaddr wrapping. For example:
5151
//
52-
// /ip4/1.2.3.4/tcp/80 decapsulate /ip4/1.2.3.4 = /tcp/80
52+
// /ip4/1.2.3.4/tcp/80 decapsulate /tcp/80 = /ip4/1.2.3.4
53+
// /ip4/1.2.3.4/tcp/80 decapsulate /udp/80 = /ip4/1.2.3.4/tcp/80
54+
// /ip4/1.2.3.4/tcp/80 decapsulate /ip4/1.2.3.4 = nil
5355
//
5456
Decapsulate(Multiaddr) Multiaddr
5557

multiaddr_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,25 @@ func TestEncapsulate(t *testing.T) {
427427
}
428428
}
429429

430+
func TestDecapsulateComment(t *testing.T) {
431+
// shows the behavior from the interface comment
432+
m := StringCast("/ip4/1.2.3.4/tcp/80")
433+
rest := m.Decapsulate(StringCast("/tcp/80"))
434+
if rest.String() != "/ip4/1.2.3.4" {
435+
t.Fatalf("Documented behavior is not correct. Expected %v saw %v", "/ip4/1.2.3.4/", rest.String())
436+
}
437+
438+
m = StringCast("/ip4/1.2.3.4/tcp/80")
439+
rest = m.Decapsulate(StringCast("/udp/80"))
440+
if !rest.Equal(m) {
441+
t.Fatalf("Documented behavior is not correct. Expected %v saw %v", "/ip4/1.2.3.4/tcp/80", rest.String())
442+
}
443+
444+
m = StringCast("/ip4/1.2.3.4/tcp/80")
445+
rest = m.Decapsulate(StringCast("/ip4/1.2.3.4"))
446+
require.Nil(t, rest, "expected a nil multiaddr if we decapsulate everything")
447+
}
448+
430449
func assertValueForProto(t *testing.T, a Multiaddr, p int, exp string) {
431450
t.Logf("checking for %s in %s", ProtocolWithCode(p).Name, a)
432451
fv, err := a.ValueForProtocol(p)

0 commit comments

Comments
 (0)