main
Raw Download raw file
  1package cmd
  2
  3import (
  4	"regexp"
  5	"strings"
  6	"testing"
  7)
  8
  9func TestGrepMatcher(t *testing.T) {
 10	tests := []struct {
 11		name            string
 12		pattern         string
 13		text            string
 14		caseInsensitive bool
 15		regex           bool
 16		expected        bool
 17	}{
 18		{
 19			name:     "simple string match",
 20			pattern:  "hello",
 21			text:     "hello world",
 22			expected: true,
 23		},
 24		{
 25			name:     "simple string no match",
 26			pattern:  "hello",
 27			text:     "goodbye world",
 28			expected: false,
 29		},
 30		{
 31			name:            "case insensitive match",
 32			pattern:         "HELLO",
 33			text:            "hello world",
 34			caseInsensitive: true,
 35			expected:        true,
 36		},
 37		{
 38			name:            "case sensitive no match",
 39			pattern:         "HELLO",
 40			text:            "hello world",
 41			caseInsensitive: false,
 42			expected:        false,
 43		},
 44		{
 45			name:     "regex match",
 46			pattern:  "hel+o",
 47			text:     "hello world",
 48			regex:    true,
 49			expected: true,
 50		},
 51		{
 52			name:     "regex no match",
 53			pattern:  "hel{2,}o",
 54			text:     "helo world",
 55			regex:    true,
 56			expected: false,
 57		},
 58		{
 59			name:            "regex case insensitive",
 60			pattern:         "HEL+O",
 61			text:            "hello world",
 62			regex:           true,
 63			caseInsensitive: true,
 64			expected:        true,
 65		},
 66	}
 67
 68	for _, tt := range tests {
 69		t.Run(tt.name, func(t *testing.T) {
 70			// Simulate the matcher creation logic from runGrep
 71			var matcher func(string) bool
 72			if tt.regex {
 73				regexPattern := tt.pattern
 74				if tt.caseInsensitive {
 75					regexPattern = "(?i)" + tt.pattern
 76				}
 77				re, err := regexp.Compile(regexPattern)
 78				if err != nil {
 79					t.Fatalf("Failed to compile regex: %v", err)
 80				}
 81				matcher = re.MatchString
 82			} else {
 83				searchPattern := tt.pattern
 84				if tt.caseInsensitive {
 85					searchPattern = strings.ToLower(tt.pattern)
 86				}
 87				matcher = func(text string) bool {
 88					searchText := text
 89					if tt.caseInsensitive {
 90						searchText = strings.ToLower(text)
 91					}
 92					return strings.Contains(searchText, searchPattern)
 93				}
 94			}
 95
 96			result := matcher(tt.text)
 97			if result != tt.expected {
 98				t.Errorf("Expected %v, got %v for pattern %q against text %q", tt.expected, result, tt.pattern, tt.text)
 99			}
100		})
101	}
102}
103
104func TestGrepCommandStringConstruction(t *testing.T) {
105	tests := []struct {
106		name     string
107		command  string
108		params   string
109		expected string
110	}{
111		{
112			name:     "command with params",
113			command:  "ls",
114			params:   "-la /tmp",
115			expected: "ls -la /tmp",
116		},
117		{
118			name:     "command without params",
119			command:  "whoami",
120			params:   "",
121			expected: "whoami",
122		},
123		{
124			name:     "command with complex params",
125			command:  "powershell",
126			params:   "-ExecutionPolicy Bypass -Command \"Get-Process\"",
127			expected: "powershell -ExecutionPolicy Bypass -Command \"Get-Process\"",
128		},
129	}
130
131	for _, tt := range tests {
132		t.Run(tt.name, func(t *testing.T) {
133			// Simulate the command string construction from worker function
134			commandString := tt.command
135			if tt.params != "" {
136				commandString += " " + tt.params
137			}
138
139			if commandString != tt.expected {
140				t.Errorf("Expected %q, got %q", tt.expected, commandString)
141			}
142		})
143	}
144}