Compare commits
	
		
			2 Commits
		
	
	
		
			579921a975
			...
			d3b95c693b
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| d3b95c693b | |||
| 043738668b | 
| @@ -50,6 +50,33 @@ func BuildURI(rawuri, rawpath string) string { | |||||||
| 	return u.String() | 	return u.String() | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func HasCommand(s, prefix string) bool { | ||||||
|  | 	s = strings.TrimSpace(s) | ||||||
|  |  | ||||||
|  | 	if len(s) == 0 || len(prefix) == 0 { | ||||||
|  | 		return false | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if !strings.HasPrefix(s, prefix) { | ||||||
|  | 		return false | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// remove the command prefix | ||||||
|  | 	s = s[len(prefix):] | ||||||
|  |  | ||||||
|  | 	// multiple assignment trick | ||||||
|  | 	cmd, _ := func() (string, string) { | ||||||
|  | 		x := strings.SplitN(s, " ", 2) | ||||||
|  | 		if len(x) > 1 { | ||||||
|  | 			return x[0], x[1] | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		return x[0], "" | ||||||
|  | 	}() | ||||||
|  |  | ||||||
|  | 	return len(cmd) > 0 | ||||||
|  | } | ||||||
|  |  | ||||||
| func ContainsCommand(s, prefix, cmd string) bool { | func ContainsCommand(s, prefix, cmd string) bool { | ||||||
| 	s = strings.TrimSpace(s) | 	s = strings.TrimSpace(s) | ||||||
|  |  | ||||||
| @@ -75,6 +102,10 @@ func ContainsCommand(s, prefix, cmd string) bool { | |||||||
| func SplitCommandAndArgs(s, prefix string) (cmd string, args []string) { | func SplitCommandAndArgs(s, prefix string) (cmd string, args []string) { | ||||||
| 	s = strings.TrimSpace(s) | 	s = strings.TrimSpace(s) | ||||||
|  |  | ||||||
|  | 	if !strings.HasPrefix(s, prefix) { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	x := strings.Split(s, " ") | 	x := strings.Split(s, " ") | ||||||
|  |  | ||||||
| 	if len(x) > 1 { | 	if len(x) > 1 { | ||||||
|   | |||||||
| @@ -26,3 +26,53 @@ func TestContainsCommand(t *testing.T) { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestHasCommandCommand(t *testing.T) { | ||||||
|  | 	tables := []struct { | ||||||
|  | 		s      string | ||||||
|  | 		prefix string | ||||||
|  | 		want   bool | ||||||
|  | 	}{ | ||||||
|  | 		{"!command", "!", true}, | ||||||
|  | 		{"!command x y", "!", true}, | ||||||
|  | 		{"!c x y", "!", true}, | ||||||
|  | 		{"! x y", "!", false}, | ||||||
|  | 		{"hey guy", "!", false}, | ||||||
|  | 		{"hey", "!", false}, | ||||||
|  | 		{"hey", "", false}, | ||||||
|  | 		{"", "!", false}, | ||||||
|  | 		{"", "", false}, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, table := range tables { | ||||||
|  | 		if got, want := HasCommand(table.s, table.prefix), table.want; got != want { | ||||||
|  | 			t.Errorf( | ||||||
|  | 				"s: %s, prefix: %s, got: %t, want: %t", | ||||||
|  | 				table.s, table.prefix, got, want, | ||||||
|  | 			) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestSplitComandAndArgs(t *testing.T) { | ||||||
|  | 	tables := []struct { | ||||||
|  | 		s        string | ||||||
|  | 		prefix   string | ||||||
|  | 		wantCmd  string | ||||||
|  | 		wantArgs []string | ||||||
|  | 	}{ | ||||||
|  | 		{"!command x y", "!", "command", []string{"x", "y"}}, | ||||||
|  | 		{"!command", "!", "command", []string(nil)}, | ||||||
|  | 		{"hey man", "!", "", []string(nil)}, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, table := range tables { | ||||||
|  | 		gotCmd, gotArgs := SplitCommandAndArgs(table.s, table.prefix) | ||||||
|  | 		if gotCmd != table.wantCmd { | ||||||
|  | 			t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd) | ||||||
|  | 		} | ||||||
|  | 		if !reflect.DeepEqual(gotArgs, table.wantArgs) { | ||||||
|  | 			t.Errorf("got: %+v, want: %+v", gotArgs, table.wantArgs) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user