Conditions | 2 |
Total Lines | 90 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package goagi |
||
33 | func TestParseResponse(t *testing.T) { |
||
34 | tests := []struct { |
||
35 | input string |
||
36 | code int |
||
37 | result int |
||
38 | value string |
||
39 | data string |
||
40 | endpos int64 |
||
41 | digit string |
||
42 | sres int |
||
43 | }{ |
||
44 | {"100 result=1 Trying...\n", 100, 1, "Trying...", "Trying...", 0, "", 0}, |
||
45 | {"100 result=0\n", 100, 0, "", "", 0, "", 0}, |
||
46 | {"100 Trying\n", 100, 0, "Trying", "Trying", 0, "", 0}, |
||
47 | {"200 result=1\n", 200, 1, "", "", 0, "", 0}, |
||
48 | {"200 result=\n", 200, 0, "", "", 0, "", 0}, |
||
49 | |||
50 | {"200 result=1 (hangup)\n", 200, 1, "hangup", "", 0, "", 0}, |
||
51 | {"200 result=-1 endpos=11223344\n", 200, -1, "", "", 11223344, "", 0}, |
||
52 | {"200 result=-1 endpos=\n", 200, -1, "", "", 0, "", 0}, |
||
53 | {"200 result=-1 endpos=asf\n", 200, -1, "", "", 0, "", 0}, |
||
54 | {"200 result=1 (en)\n", 200, 1, "en", "", 0, "", 0}, |
||
55 | { |
||
56 | "200 result=1 (\"Alice Johnson\" <2233>)\n", |
||
57 | 200, 1, "\"Alice Johnson\" <2233>", "", 0, "", 0, |
||
58 | }, { |
||
59 | "200 result=1 (Alice Johnson)\n", |
||
60 | 200, 1, "Alice Johnson", "", 0, "", 0, |
||
61 | }, { |
||
62 | "200 result=1 (SIP/9170-12-00000008)\n", |
||
63 | 200, 1, "SIP/9170-12-00000008", "", 0, "", 0, |
||
64 | }, { |
||
65 | "200 result=1 (digit) digit=* endpos=998877660\n", |
||
66 | 200, 1, "digit", "", 998877660, "*", 0, |
||
67 | }, { |
||
68 | "503 result=-2 Memory allocation failure\n", |
||
69 | 503, -2, |
||
70 | "Memory allocation failure", "Memory allocation failure", |
||
71 | 0, "", 0, |
||
72 | }, { |
||
73 | "200 result=1 (speech) endpos=918273 results=123 \n", |
||
74 | 200, 1, "speech", "", 918273, "", 123, |
||
75 | }, { |
||
76 | "510 Invalid or unknown command\n", |
||
77 | 510, 0, |
||
78 | "Invalid or unknown command", "Invalid or unknown command", |
||
79 | 0, "", 0, |
||
80 | }, { |
||
81 | "510 Error", 510, 0, "Error", "Error", 0, "", 0, |
||
82 | }, { |
||
83 | "511 Command Not Permitted on a dead channel or intercept routine\n", |
||
84 | 511, 0, |
||
85 | "Command Not Permitted on a dead channel or intercept routine", |
||
86 | "Command Not Permitted on a dead channel or intercept routine", |
||
87 | 0, "", 0, |
||
88 | }, { |
||
89 | "520 Invalid command syntax. Proper usage not available.\n", |
||
90 | 520, 0, |
||
91 | "Invalid command syntax. Proper usage not available.", |
||
92 | "Invalid command syntax. Proper usage not available.", |
||
93 | 0, "", 0, |
||
94 | }, { |
||
95 | "520-Invalid command syntax. Proper usage follows:\n" + |
||
96 | "Usage: database put <family> <key> <value>\n" + |
||
97 | "Adds or updates an entry in the Asterisk database for\n" + |
||
98 | "a given family, key, and value.\n" + |
||
99 | "520 End of proper usage.\n", |
||
100 | 520, 0, |
||
101 | "Usage: database put <family> <key> <value>\n" + |
||
102 | "Adds or updates an entry in the Asterisk database for\n" + |
||
103 | "a given family, key, and value.", |
||
104 | "Usage: database put <family> <key> <value>\n" + |
||
105 | "Adds or updates an entry in the Asterisk database for\n" + |
||
106 | "a given family, key, and value.", |
||
107 | 0, "", 0, |
||
108 | }, |
||
109 | } |
||
110 | agi := &AGI{} |
||
111 | |||
112 | for _, tc := range tests { |
||
113 | resp, err := agi.parseResponse(tc.input, tc.code) |
||
114 | assert.Nil(t, err) |
||
115 | assert.Equal(t, tc.code, resp.Code()) |
||
116 | assert.Equal(t, tc.result, resp.Result()) |
||
117 | assert.Equal(t, tc.value, resp.Value()) |
||
118 | assert.Equal(t, tc.data, resp.Data()) |
||
119 | assert.Equal(t, tc.input, resp.RawResponse()) |
||
120 | assert.Equal(t, tc.endpos, resp.EndPos()) |
||
121 | assert.Equal(t, tc.digit, resp.Digit()) |
||
122 | assert.Equal(t, tc.sres, resp.SResults()) |
||
123 | } |
||
167 |