Conditions | 4 |
Total Lines | 51 |
Code Lines | 42 |
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 csvqlctl_test |
||
18 | func TestShouldExecuteWithSuccess(t *testing.T) { |
||
19 | var tests = []struct { |
||
20 | args []string |
||
21 | filePath string |
||
22 | fileName string |
||
23 | data string |
||
24 | delimiter string |
||
25 | out string |
||
26 | queries []string |
||
27 | }{ |
||
28 | { |
||
29 | args: []string{"-f", "./../../.tmp/0001.csv", "-d", ";"}, |
||
30 | filePath: "./../../.tmp", |
||
31 | fileName: "0001.csv", |
||
32 | data: strings.Join([]string{ |
||
33 | "id;name;email", |
||
34 | "0001;teste_1;[email protected]", |
||
35 | "0002;teste_2;[email protected]", |
||
36 | "0003;teste_3;[email protected]", |
||
37 | "0004;teste_4;[email protected]", |
||
38 | "0005;teste_5;[email protected]", |
||
39 | }, "\n"), |
||
40 | delimiter: ";", |
||
41 | out: "", |
||
42 | queries: []string{ |
||
43 | "select * from rows;", |
||
44 | }, |
||
45 | }, |
||
46 | } |
||
47 | |||
48 | cmd, err := csvqlctl.New().Command() |
||
49 | assert.NoError(t, err) |
||
50 | |||
51 | for _, test := range tests { |
||
52 | _ = os.MkdirAll(test.filePath, os.ModePerm) |
||
53 | err := os.WriteFile(filepath.Join(test.filePath, test.fileName), bytes.NewBufferString(test.data).Bytes(), FileModeDefault) |
||
54 | assert.NoError(t, err) |
||
55 | |||
56 | buf := new(bytes.Buffer) |
||
57 | cmd.SetOut(buf) |
||
58 | cmd.SetErr(buf) |
||
59 | cmd.SetArgs(test.args) |
||
60 | |||
61 | err = cmd.Execute() |
||
62 | assert.NoError(t, err) |
||
63 | |||
64 | for _, q := range test.queries { |
||
65 | _, err = cmd.OutOrStdout().Write([]byte(q)) |
||
66 | assert.NoError(t, err) |
||
67 | if out := cmd.OutOrStdout(); out != os.Stdout { |
||
68 | assert.Equal(t, q, fmt.Sprintf("%s", out)) |
||
69 | } |
||
73 |