-
Notifications
You must be signed in to change notification settings - Fork 0
/
args_fields_test.go
51 lines (47 loc) · 1.4 KB
/
args_fields_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package jqtop
import (
"reflect"
"testing"
)
func TestGetFieldsInOrder(t *testing.T) {
s := "cquuc; host_with_protocol = regex_capture(cquuc, \"(.*?://.*?)/\");"
expected := []string{"cquuc", "host_with_protocol"}
fields := getFieldsInOrder(s)
for i, f := range fields {
if expected[i] != f {
t.Errorf("For fields %s, expected: %s, got: %s", s, expected[i], f)
}
}
}
func TestExtractFields(t *testing.T) {
fieldsStr := "cquuc; host_with_protocol = regex_capture(cquuc, \"(.*?://.*?)/\");"
allFields, err := extractFields(fieldsStr)
t.Logf("Extracted fields\nFROM: %s\nGOT: %#v", fieldsStr, allFields)
if err != nil {
t.Errorf("Error parsinga valid field string")
}
derivedFields := make(map[string]*derivedField)
derivedFields["host_with_protocol"] = &derivedField{
NewField: "host_with_protocol",
Fname: "regex_capture",
Args: []string{"cquuc", "(.*?://.*?)/"},
}
expectedValue := Fields{
SimpleFields: []string{"cquuc"},
DerivedFields: derivedFields,
FieldsInOrder: []string{"cquuc", "host_with_protocol"},
FieldsIndexMap: map[string]int{
"cquuc": 0,
"host_with_protocol": 1,
},
}
if !reflect.DeepEqual(allFields, expectedValue) {
t.Errorf("allFields struct was equal to what was expected")
}
// Invalid line
fieldsStr = "field1; c = "
_, err = extractFields(fieldsStr)
if err == nil {
t.Errorf("Didn't throw error for an invalid field string")
}
}