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