| 1 | package sessionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ |
| 2 | |
| 3 | import ( |
| 4 | "reflectscip-go gomod github.com/golang/go/src go1.16 reflect/" |
| 5 | "regexpscip-go gomod github.com/golang/go/src go1.16 regexp/" |
| 6 | "testingscip-go gomod github.com/golang/go/src go1.16 testing/" |
| 7 | ) |
| 8 | |
| 9 | func TestNewModuleHandlerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/TestNewModuleHandler().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 10 | type args struct { |
| 11 | name string |
| 12 | expr string |
| 13 | desc string |
| 14 | exec func(args []string) error |
| 15 | } |
| 16 | tests := []struct { |
| 17 | name string |
| 18 | args args |
| 19 | want ModuleHandlerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler# |
| 20 | }{ |
| 21 | { |
| 22 | name: "Test NewModuleHandler empty expr", |
| 23 | args: args{name: "test name", desc: "test description"}, |
| 24 | want: ModuleHandlerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#{Namescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Name.: "test name", Descriptionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Description.: "test description"}, |
| 25 | }, |
| 26 | { |
| 27 | name: "Test NewModuleHandler normal", |
| 28 | args: args{name: "test name", desc: "test description", expr: `[a-z]`}, |
| 29 | want: ModuleHandlerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#{Namescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Name.: "test name", Descriptionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Description.: "test description", Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser.: regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`[a-z]`)}, |
| 30 | }, |
| 31 | // this test shoud handle panic on bad regexp ? |
| 32 | // { |
| 33 | // name: "Test NewModuleHandler bad regex expr", |
| 34 | // args: args{name: "test name", desc: "test description", expr: "/abcd.(]"}, |
| 35 | // want: ModuleHandler{Name: "test name", Description: "test description"}, |
| 36 | // }, |
| 37 | } |
| 38 | for _, tt := range tests { |
| 39 | t.Runscip-go gomod github.com/golang/go/src go1.16 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 40 | m := NewModuleHandlerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/NewModuleHandler().(tt.args.name, tt.args.expr, tt.args.desc, tt.args.exec) |
| 41 | if m.Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser. != nil { |
| 42 | if tt.args.expr == "" { |
| 43 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("If no regexp were provided, should got nil parser but got %+v", m.Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser.) |
| 44 | } |
| 45 | if m.Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser..Stringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#String().() != tt.want.Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser..Stringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#String().() { |
| 46 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Wrong parser, got %+v want %+v", m.Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser..Stringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#String().(), tt.want.Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser..Stringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#String().()) |
| 47 | } |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestModuleHandler_Helpscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/TestModuleHandler_Help().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 54 | type fields struct { |
| 55 | Name string |
| 56 | Description string |
| 57 | Parser *regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp# |
| 58 | Exec func(args []string) error |
| 59 | } |
| 60 | type args struct { |
| 61 | padding int |
| 62 | } |
| 63 | tests := []struct { |
| 64 | name string |
| 65 | fields fields |
| 66 | args args |
| 67 | want string |
| 68 | }{ |
| 69 | { |
| 70 | name: "Test help with no padding", |
| 71 | fields: fields{Name: "no-padding", Description: "Test without padding"}, |
| 72 | args: args{padding: 0}, |
| 73 | want: " \033[1mno-padding\033[0m : Test without padding\n", |
| 74 | }, |
| 75 | { |
| 76 | name: "Test help with non-needed padding", |
| 77 | fields: fields{Name: "non-needed padding", Description: "Test with non needed padding (5)"}, |
| 78 | args: args{padding: 5}, |
| 79 | want: " \033[1mnon-needed padding\033[0m : Test with non needed padding (5)\n", |
| 80 | }, |
| 81 | { |
| 82 | name: "Test help with 20 padding", |
| 83 | fields: fields{Name: "padding", Description: "Test with 20 padding"}, |
| 84 | args: args{padding: 20}, |
| 85 | want: " \033[1m padding\033[0m : Test with 20 padding\n", |
| 86 | }, |
| 87 | } |
| 88 | for _, tt := range tests { |
| 89 | t.Runscip-go gomod github.com/golang/go/src go1.16 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 90 | h := &ModuleHandlerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#{ |
| 91 | Namescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Name.: tt.fields.Name, |
| 92 | Descriptionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Description.: tt.fields.Description, |
| 93 | Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser.: tt.fields.Parser, |
| 94 | execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#exec.: tt.fields.Exec, |
| 95 | } |
| 96 | if got := h.Helpscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Help().(tt.args.padding); got != tt.want { |
| 97 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("ModuleHandler.Help() = \n%v, want\n%v", got, tt.want) |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestModuleHandler_Parsescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/TestModuleHandler_Parse().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 104 | type fields struct { |
| 105 | Name string |
| 106 | Description string |
| 107 | Parser *regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp# |
| 108 | Exec func(args []string) error |
| 109 | } |
| 110 | type args struct { |
| 111 | line string |
| 112 | } |
| 113 | tests := []struct { |
| 114 | name string |
| 115 | fields fields |
| 116 | args args |
| 117 | want bool |
| 118 | want1 []string |
| 119 | }{ |
| 120 | { |
| 121 | name: "check parse on nil parser match name", |
| 122 | fields: fields{Name: "parser", Description: "description of the parser", Parser: nil}, |
| 123 | args: args{line: "parser"}, |
| 124 | want: true, |
| 125 | want1: nil, |
| 126 | }, |
| 127 | { |
| 128 | name: "check parse on nil parser no match name", |
| 129 | fields: fields{Name: "parser", Description: "description of the parser", Parser: nil}, |
| 130 | args: args{line: "wrongname"}, |
| 131 | want: false, |
| 132 | want1: nil, |
| 133 | }, |
| 134 | { |
| 135 | name: "check parse on existing parser", |
| 136 | fields: fields{Name: "parser", Description: "description of the parser", Parser: regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().("(abcd)")}, |
| 137 | args: args{line: "lol abcd lol"}, |
| 138 | want: true, |
| 139 | want1: []string{"abcd"}, |
| 140 | }, |
| 141 | { |
| 142 | name: "check parse on existing parser", |
| 143 | fields: fields{Name: "parser", Description: "description of the parser", Parser: regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().("(abcd)")}, |
| 144 | args: args{line: "no match"}, |
| 145 | want: false, |
| 146 | want1: nil, |
| 147 | }, |
| 148 | } |
| 149 | for _, tt := range tests { |
| 150 | t.Runscip-go gomod github.com/golang/go/src go1.16 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 151 | h := &ModuleHandlerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#{ |
| 152 | Namescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Name.: tt.fields.Name, |
| 153 | Descriptionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Description.: tt.fields.Description, |
| 154 | Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser.: tt.fields.Parser, |
| 155 | execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#exec.: tt.fields.Exec, |
| 156 | } |
| 157 | got, got1 := h.Parsescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parse().(tt.args.line) |
| 158 | if got != tt.want { |
| 159 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("ModuleHandler.Parse() got = %v, want %v", got, tt.want) |
| 160 | } |
| 161 | if !reflectscip-go gomod github.com/golang/go/src go1.16 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.16 reflect/DeepEqual().(got1, tt.want1) { |
| 162 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("ModuleHandler.Parse() got1 = %v, want %v", got1, tt.want1) |
| 163 | } |
| 164 | }) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestModuleHandler_MarshalJSONscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/TestModuleHandler_MarshalJSON().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 169 | type fields struct { |
| 170 | Name string |
| 171 | Description string |
| 172 | Parser *regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp# |
| 173 | Exec func(args []string) error |
| 174 | } |
| 175 | tests := []struct { |
| 176 | name string |
| 177 | fields fields |
| 178 | want []byte |
| 179 | wantErr bool |
| 180 | }{ |
| 181 | { |
| 182 | name: "generating JSON with empty parser", |
| 183 | fields: fields{Name: "test name", Description: "test description", Parser: nil}, |
| 184 | want: []byte(`{"name":"test name","description":"test description","parser":""}`), |
| 185 | wantErr: false, |
| 186 | }, |
| 187 | { |
| 188 | name: "generating JSON with parser", |
| 189 | fields: fields{Name: "test name", Description: "test description", Parser: regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().("abcd")}, |
| 190 | want: []byte(`{"name":"test name","description":"test description","parser":"abcd"}`), |
| 191 | wantErr: false, |
| 192 | }, |
| 193 | } |
| 194 | for _, tt := range tests { |
| 195 | t.Runscip-go gomod github.com/golang/go/src go1.16 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 196 | h := ModuleHandlerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#{ |
| 197 | Namescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Name.: tt.fields.Name, |
| 198 | Descriptionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Description.: tt.fields.Description, |
| 199 | Parserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#Parser.: tt.fields.Parser, |
| 200 | execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#exec.: tt.fields.Exec, |
| 201 | } |
| 202 | got, err := h.MarshalJSONscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/ModuleHandler#MarshalJSON().() |
| 203 | if (err != nil) != tt.wantErr { |
| 204 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("ModuleHandler.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) |
| 205 | return |
| 206 | } |
| 207 | if string(got) != string(tt.want) { |
| 208 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("ModuleHandler.MarshalJSON() = \n%+v, want \n%+v", string(got), string(tt.want)) |
| 209 | } |
| 210 | }) |
| 211 | } |
| 212 | } |