github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/metric_rules_test.go (about) 1 package internal 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/lulzWill/go-agent/internal/crossagent" 8 ) 9 10 func TestMetricRules(t *testing.T) { 11 var tcs []struct { 12 Testname string `json:"testname"` 13 Rules metricRules `json:"rules"` 14 Tests []struct { 15 Input string `json:"input"` 16 Expected string `json:"expected"` 17 } `json:"tests"` 18 } 19 20 err := crossagent.ReadJSON("rules.json", &tcs) 21 if err != nil { 22 t.Fatal(err) 23 } 24 25 for _, tc := range tcs { 26 // This test relies upon Perl-specific regex syntax (negative 27 // lookahead assertions) which are not implemented in Go's 28 // regexp package. We believe these types of rules are 29 // exceedingly rare in practice, so we're skipping 30 // implementation of this exotic syntax for now. 31 if tc.Testname == "saxon's test" { 32 continue 33 } 34 35 for _, x := range tc.Tests { 36 out := tc.Rules.Apply(x.Input) 37 if out != x.Expected { 38 t.Fatal(tc.Testname, x.Input, out, x.Expected) 39 } 40 } 41 } 42 } 43 44 func TestMetricRuleWithNegativeLookaheadAssertion(t *testing.T) { 45 js := `[{ 46 "match_expression":"^(?!account|application).*", 47 "replacement":"*", 48 "ignore":false, 49 "eval_order":0, 50 "each_segment":true 51 }]` 52 var rules metricRules 53 err := json.Unmarshal([]byte(js), &rules) 54 if nil != err { 55 t.Fatal(err) 56 } 57 if 0 != rules.Len() { 58 t.Fatal(rules) 59 } 60 } 61 62 func TestNilApplyRules(t *testing.T) { 63 var rules metricRules 64 65 input := "hello" 66 out := rules.Apply(input) 67 if input != out { 68 t.Fatal(input, out) 69 } 70 } 71 72 func TestAmbiguousReplacement(t *testing.T) { 73 js := `[{ 74 "match_expression":"(.*)/[^/]*.(bmp|css|gif|ico|jpg|jpeg|js|png)", 75 "replacement":"\\\\1/*.\\2", 76 "ignore":false, 77 "eval_order":0 78 }]` 79 var rules metricRules 80 err := json.Unmarshal([]byte(js), &rules) 81 if nil != err { 82 t.Fatal(err) 83 } 84 if 0 != rules.Len() { 85 t.Fatal(rules) 86 } 87 } 88 89 func TestBadMetricRulesJSON(t *testing.T) { 90 js := `{}` 91 var rules metricRules 92 err := json.Unmarshal([]byte(js), &rules) 93 if nil == err { 94 t.Fatal("missing bad json error") 95 } 96 }