| 1 | package session |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "regexp" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestNewModuleHandler(t *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 ModuleHandler |
| 20 | }{ |
| 21 | { |
| 22 | name: "Test NewModuleHandler empty expr", |
| 23 | args: args{name: "test name", desc: "test description"}, |
| 24 | want: ModuleHandler{Name: "test name", Description: "test description"}, |
| 25 | }, |
| 26 | { |
| 27 | name: "Test NewModuleHandler normal", |
| 28 | args: args{name: "test name", desc: "test description", expr: `[a-z]`}, |
| 29 | want: ModuleHandler{Name: "test name", Description: "test description", Parser: 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.Run(tt.name, func(t *testing.T) { |
| 40 | m := NewModuleHandler(tt.args.name, tt.args.expr, tt.args.desc, tt.args.exec) |
| 41 | if m.Parser != nil { |
| 42 | if tt.args.expr == "" { |
| 43 | t.Errorf("If no regexp were provided, should got nil parser but got %+v", m.Parser) |
| 44 | } |
| 45 | if m.Parser.String() != tt.want.Parser.String() { |
| 46 | t.Errorf("Wrong parser, got %+v want %+v", m.Parser.String(), tt.want.Parser.String()) |
| 47 | } |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestModuleHandler_Help(t *testing.T) { |
| 54 | type fields struct { |
| 55 | Name string |
| 56 | Description string |
| 57 | Parser *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.Run(tt.name, func(t *testing.T) { |
| 90 | h := &ModuleHandler{ |
| 91 | Name: tt.fields.Name, |
| 92 | Description: tt.fields.Description, |
| 93 | Parser: tt.fields.Parser, |
| 94 | exec: tt.fields.Exec, |
| 95 | } |
| 96 | if got := h.Help(tt.args.padding); got != tt.want { |
| 97 | t.Errorf("ModuleHandler.Help() = \n%v, want\n%v", got, tt.want) |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestModuleHandler_Parse(t *testing.T) { |
| 104 | type fields struct { |
| 105 | Name string |
| 106 | Description string |
| 107 | Parser *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: 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: regexp.MustCompile("(abcd)")}, |
| 144 | args: args{line: "no match"}, |
| 145 | want: false, |
| 146 | want1: nil, |
| 147 | }, |
| 148 | } |
| 149 | for _, tt := range tests { |
| 150 | t.Run(tt.name, func(t *testing.T) { |
| 151 | h := &ModuleHandler{ |
| 152 | Name: tt.fields.Name, |
| 153 | Description: tt.fields.Description, |
| 154 | Parser: tt.fields.Parser, |
| 155 | exec: tt.fields.Exec, |
| 156 | } |
| 157 | got, got1 := h.Parse(tt.args.line) |
| 158 | if got != tt.want { |
| 159 | t.Errorf("ModuleHandler.Parse() got = %v, want %v", got, tt.want) |
| 160 | } |
| 161 | if !reflect.DeepEqual(got1, tt.want1) { |
| 162 | t.Errorf("ModuleHandler.Parse() got1 = %v, want %v", got1, tt.want1) |
| 163 | } |
| 164 | }) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestModuleHandler_MarshalJSON(t *testing.T) { |
| 169 | type fields struct { |
| 170 | Name string |
| 171 | Description string |
| 172 | Parser *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: 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.Run(tt.name, func(t *testing.T) { |
| 196 | h := ModuleHandler{ |
| 197 | Name: tt.fields.Name, |
| 198 | Description: tt.fields.Description, |
| 199 | Parser: tt.fields.Parser, |
| 200 | exec: tt.fields.Exec, |
| 201 | } |
| 202 | got, err := h.MarshalJSON() |
| 203 | if (err != nil) != tt.wantErr { |
| 204 | t.Errorf("ModuleHandler.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) |
| 205 | return |
| 206 | } |
| 207 | if string(got) != string(tt.want) { |
| 208 | t.Errorf("ModuleHandler.MarshalJSON() = \n%+v, want \n%+v", string(got), string(tt.want)) |
| 209 | } |
| 210 | }) |
| 211 | } |
| 212 | } |