github.com/GuanceCloud/cliutils@v1.1.21/dialtesting/success.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package dialtesting 7 8 import ( 9 "fmt" 10 "regexp" 11 "strings" 12 ) 13 14 type DialResult struct { 15 Success bool 16 Reasons []string 17 } 18 19 type SuccessOption struct { 20 Is string `json:"is,omitempty"` 21 IsNot string `json:"is_not,omitempty"` 22 23 MatchRegex string `json:"match_regex,omitempty"` 24 NotMatchRegex string `json:"not_match_regex,omitempty"` 25 matchRe, notMatchRe *regexp.Regexp 26 27 Contains string `json:"contains,omitempty"` 28 NotContains string `json:"not_contains,omitempty"` 29 } 30 31 func (s *SuccessOption) check(val, prompt string) error { 32 if s.Is != "" { 33 if s.Is != val { 34 return fmt.Errorf("%s: expect to be `%s', got `%s'", prompt, s.Is, val) 35 } 36 return nil 37 } 38 39 if s.IsNot != "" { 40 if s.IsNot == val { 41 return fmt.Errorf("%s: shoud not be %s", prompt, s.IsNot) 42 } 43 return nil 44 } 45 46 if s.matchRe != nil { 47 if !s.matchRe.MatchString(val) { 48 return fmt.Errorf("%s: regex `%s` match `%s' failed", prompt, s.MatchRegex, val) 49 } 50 } 51 52 if s.notMatchRe != nil { 53 if s.notMatchRe.MatchString(val) { 54 return fmt.Errorf("%s: regex `%s' should not match `%s'", prompt, s.NotMatchRegex, val) 55 } 56 } 57 58 if s.Contains != "" { 59 if !strings.Contains(val, s.Contains) { 60 return fmt.Errorf("%s: do not contains `%s', got `%s'", prompt, s.Contains, val) 61 } 62 } 63 64 if s.NotContains != "" { 65 if strings.Contains(val, s.NotContains) { 66 return fmt.Errorf("%s: should not contains `%s', got `%s'", prompt, s.NotContains, val) 67 } 68 } 69 return nil 70 } 71 72 type ValueSuccess struct { 73 Op string `json:"op,omitempty"` 74 Target float64 `json:"target,omitempty"` 75 } 76 77 func (v *ValueSuccess) check(val float64) error { 78 switch v.Op { 79 case "eq": 80 if val != v.Target { 81 return fmt.Errorf("%v is not equal to target %v", val, v.Target) 82 } 83 case "lt": 84 if val >= v.Target { 85 return fmt.Errorf("%v is greater equal than target %v", val, v.Target) 86 } 87 case "leq": 88 if val > v.Target { 89 return fmt.Errorf("%v is greater than target %v", val, v.Target) 90 } 91 case "gt": 92 if val <= v.Target { 93 return fmt.Errorf("%v is less than target %v", val, v.Target) 94 } 95 case "geq": 96 if val < v.Target { 97 return fmt.Errorf("%v is less equal than target %v", val, v.Target) 98 } 99 } 100 101 return nil 102 } 103 104 func genReg(v *SuccessOption) error { 105 if v.MatchRegex != "" { 106 if re, err := regexp.Compile(v.MatchRegex); err != nil { 107 return err 108 } else { 109 v.matchRe = re 110 } 111 } 112 113 if v.NotMatchRegex != "" { 114 if re, err := regexp.Compile(v.NotMatchRegex); err != nil { 115 return err 116 } else { 117 v.notMatchRe = re 118 } 119 } 120 121 return nil 122 }