// +build linux package process import ( "fmt" "strconv" "strings" "testing" "github.com/stretchr/testify/assert" ) func Test_Process_splitProcStat(t *testing.T) { expectedFieldsNum := 53 statLineContent := make([]string, expectedFieldsNum-1) for i := 0; i < expectedFieldsNum-1; i++ { statLineContent[i] = strconv.Itoa(i + 1) } cases := []string{ "ok", "ok)", "(ok", "ok )", "ok )(", "ok )()", "() ok )()", "() ok (()", " ) ok )", "(ok) (ok)", } consideredFields := []int{4, 7, 10, 11, 12, 13, 14, 15, 18, 22, 42} commandNameIndex := 2 for _, expectedName := range cases { statLineContent[commandNameIndex-1] = "(" + expectedName + ")" statLine := strings.Join(statLineContent, " ") t.Run(fmt.Sprintf("name: %s", expectedName), func(t *testing.T) { parsedStatLine := splitProcStat([]byte(statLine)) assert.Equal(t, expectedName, parsedStatLine[commandNameIndex]) for _, idx := range consideredFields { expected := strconv.Itoa(idx) parsed := parsedStatLine[idx] assert.Equal( t, expected, parsed, "field %d (index from 1 as in man proc) must be %q but %q is received", idx, expected, parsed, ) } }) } }