File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package electrum
2+
3+ import "strings"
4+
5+ // Protocol is a type used for electrum protocol enumeration.
6+ type Protocol int
7+
8+ // Electrum protocols enumeration.
9+ const (
10+ Unknown Protocol = iota
11+ TCP
12+ SSL
13+ )
14+
15+ var (
16+ protocolMap = map [string ]Protocol {
17+ "tcp" : TCP ,
18+ "ssl" : SSL ,
19+ }
20+ )
21+
22+ func (n Protocol ) String () string {
23+ return []string {"unknown" , "tcp" , "ssl" }[n ]
24+ }
25+
26+ // ParseProtocol parses string
27+ func ParseProtocol (str string ) (Protocol , bool ) {
28+ c , ok := protocolMap [strings .ToLower (str )]
29+ return c , ok
30+ }
Original file line number Diff line number Diff line change 1+ package electrum
2+
3+ import (
4+ "testing"
5+ )
6+
7+ func TestParseProtocol (t * testing.T ) {
8+ tests := map [string ]struct {
9+ value string
10+ expectedResult Protocol
11+ expectedOk bool
12+ }{
13+ "empty" : {
14+ value : "" ,
15+ expectedResult : Unknown ,
16+ expectedOk : false ,
17+ },
18+ "lower case: tcp" : {
19+ value : "tcp" ,
20+ expectedResult : TCP ,
21+ expectedOk : true ,
22+ },
23+ "upper case: TCP" : {
24+ value : "TCP" ,
25+ expectedResult : TCP ,
26+ expectedOk : true ,
27+ },
28+ "mixed case: Tcp" : {
29+ value : "Tcp" ,
30+ expectedResult : TCP ,
31+ expectedOk : true ,
32+ },
33+ "lower case: ssl" : {
34+ value : "ssl" ,
35+ expectedResult : SSL ,
36+ expectedOk : true ,
37+ },
38+ "upper case: SSL" : {
39+ value : "SSL" ,
40+ expectedResult : SSL ,
41+ expectedOk : true ,
42+ },
43+ "mixed case: SsL" : {
44+ value : "SsL" ,
45+ expectedResult : SSL ,
46+ expectedOk : true ,
47+ },
48+ "invalid: tls" : {
49+ value : "tls" ,
50+ expectedResult : Unknown ,
51+ expectedOk : false ,
52+ },
53+ }
54+
55+ for testName , test := range tests {
56+ t .Run (testName , func (t * testing.T ) {
57+ actualResult , actualOk := ParseProtocol (test .value )
58+
59+ if test .expectedResult != actualResult {
60+ t .Errorf (
61+ "unexpected result\n expected: [%+v]\n actual: [%+v]" ,
62+ test .expectedResult ,
63+ actualResult ,
64+ )
65+ }
66+
67+ if test .expectedOk != actualOk {
68+ t .Errorf (
69+ "unexpected ok\n expected: [%+v]\n actual: [%+v]" ,
70+ test .expectedOk ,
71+ actualOk ,
72+ )
73+ }
74+
75+ })
76+ }
77+ }
You can’t perform that action at this time.
0 commit comments