Conditions | 5 |
Total Lines | 71 |
Code Lines | 44 |
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 producer_test |
||
15 | func TestNewReProducer(t *testing.T) { |
||
16 | t.Parallel() |
||
17 | |||
18 | methods := []string{http.MethodGet, http.MethodPost} |
||
19 | dictionary := []string{"/home", "/about"} |
||
20 | |||
21 | dictionaryProducer := producer.NewDictionaryProducer(methods, dictionary, 1) |
||
22 | |||
23 | sut := producer.NewReProducer(dictionaryProducer) |
||
24 | |||
25 | result := scan.Result{ |
||
26 | Target: scan.Target{ |
||
27 | Path: "/home", |
||
28 | Method: http.MethodGet, |
||
29 | Depth: 1, |
||
30 | }, |
||
31 | Response: &http.Response{ |
||
32 | StatusCode: http.StatusOK, |
||
33 | Request: &http.Request{ |
||
34 | Method: http.MethodGet, |
||
35 | URL: test.MustParseUrl(t, "http://mysite/contacts"), |
||
36 | }, |
||
37 | }, |
||
38 | } |
||
39 | |||
40 | reproducerFunc := sut.Reproduce() |
||
41 | reproducerChannel := reproducerFunc(result) |
||
42 | |||
43 | targets := make([]scan.Target, 0, 10) |
||
44 | for tar := range reproducerChannel { |
||
45 | targets = append(targets, tar) |
||
46 | } |
||
47 | |||
48 | sort.Slice(targets, func(i, j int) bool { |
||
49 | return targets[i].Path < targets[j].Path && targets[i].Method < targets[j].Method |
||
50 | }) |
||
51 | |||
52 | assert.Len(t, targets, 4) |
||
53 | |||
54 | expectedTargets := []scan.Target{ |
||
55 | { |
||
56 | Path: "/home/home", |
||
57 | Method: http.MethodGet, |
||
58 | Depth: 0, |
||
59 | }, |
||
60 | { |
||
61 | Path: "/home/about", |
||
62 | Method: http.MethodGet, |
||
63 | Depth: 0, |
||
64 | }, |
||
65 | { |
||
66 | Path: "/home/home", |
||
67 | Method: http.MethodPost, |
||
68 | Depth: 0, |
||
69 | }, |
||
70 | { |
||
71 | Path: "/home/about", |
||
72 | Method: http.MethodPost, |
||
73 | Depth: 0, |
||
74 | }, |
||
75 | } |
||
76 | assert.Equal(t, expectedTargets, targets) |
||
77 | |||
78 | // reproducing again on the same result should not yield more targets |
||
79 | reproducerChannel = reproducerFunc(result) |
||
80 | |||
81 | targets = make([]scan.Target, 0) |
||
82 | for tar := range reproducerChannel { |
||
83 | targets = append(targets, tar) |
||
84 | } |
||
85 | assert.Len(t, targets, 0) |
||
86 | } |
||
159 |