How to use Grep method of td Package

Best Go-testdeep code snippet using td.Grep

td_grep_test.go

Source:td_grep_test.go Github

copy

Full Screen

...8 "testing"9 "github.com/maxatome/go-testdeep/internal/test"10 "github.com/maxatome/go-testdeep/td"11)12func TestGrep(t *testing.T) {13 t.Run("basic", func(t *testing.T) {14 got := [...]int{-3, -2, -1, 0, 1, 2, 3}15 sgot := got[:]16 testCases := []struct {17 name string18 got any19 }{20 {"slice", sgot},21 {"array", got},22 {"*slice", &sgot},23 {"*array", &got},24 }25 for _, tc := range testCases {26 t.Run(tc.name, func(t *testing.T) {27 checkOK(t, tc.got, td.Grep(td.Gt(0), []int{1, 2, 3}))28 checkOK(t, tc.got, td.Grep(td.Not(td.Between(-2, 2)), []int{-3, 3}))29 checkOK(t, tc.got, td.Grep(30 func(x int) bool { return (x & 1) != 0 },31 []int{-3, -1, 1, 3}))32 checkOK(t, tc.got, td.Grep(33 func(x int64) bool { return (x & 1) != 0 },34 []int{-3, -1, 1, 3}),35 "int64 filter vs int items")36 checkOK(t, tc.got, td.Grep(37 func(x any) bool { return (x.(int) & 1) != 0 },38 []int{-3, -1, 1, 3}),39 "any filter vs int items")40 })41 }42 })43 t.Run("struct", func(t *testing.T) {44 type person struct {45 ID int6446 Name string47 }48 got := [...]person{49 {ID: 1, Name: "Joe"},50 {ID: 2, Name: "Bob"},51 {ID: 3, Name: "Alice"},52 {ID: 4, Name: "Brian"},53 {ID: 5, Name: "Britt"},54 }55 sgot := got[:]56 testCases := []struct {57 name string58 got any59 }{60 {"slice", sgot},61 {"array", got},62 {"*slice", &sgot},63 {"*array", &got},64 }65 for _, tc := range testCases {66 t.Run(tc.name, func(t *testing.T) {67 checkOK(t, tc.got, td.Grep(68 td.JSONPointer("/Name", td.HasPrefix("Br")),69 []person{{ID: 4, Name: "Brian"}, {ID: 5, Name: "Britt"}}))70 checkOK(t, tc.got, td.Grep(71 func(p person) bool { return p.ID < 3 },72 []person{{ID: 1, Name: "Joe"}, {ID: 2, Name: "Bob"}}))73 })74 }75 })76 t.Run("interfaces", func(t *testing.T) {77 got := [...]any{-3, -2, -1, 0, 1, 2, 3}78 sgot := got[:]79 testCases := []struct {80 name string81 got any82 }{83 {"slice", sgot},84 {"array", got},85 {"*slice", &sgot},86 {"*array", &got},87 }88 for _, tc := range testCases {89 t.Run(tc.name, func(t *testing.T) {90 checkOK(t, tc.got, td.Grep(td.Gt(0), []any{1, 2, 3}))91 checkOK(t, tc.got, td.Grep(td.Not(td.Between(-2, 2)), []any{-3, 3}))92 checkOK(t, tc.got, td.Grep(93 func(x int) bool { return (x & 1) != 0 },94 []any{-3, -1, 1, 3}))95 checkOK(t, tc.got, td.Grep(96 func(x int64) bool { return (x & 1) != 0 },97 []any{-3, -1, 1, 3}),98 "int64 filter vs any/int items")99 checkOK(t, tc.got, td.Grep(100 func(x any) bool { return (x.(int) & 1) != 0 },101 []any{-3, -1, 1, 3}),102 "any filter vs any/int items")103 })104 }105 })106 t.Run("interfaces error", func(t *testing.T) {107 got := [...]any{123, "foo"}108 sgot := got[:]109 testCases := []struct {110 name string111 got any112 }{113 {"slice", sgot},114 {"array", got},115 {"*slice", &sgot},116 {"*array", &got},117 }118 for _, tc := range testCases {119 t.Run(tc.name, func(t *testing.T) {120 checkError(t, tc.got,121 td.Grep(func(x int) bool { return true }, []string{"never reached"}),122 expectedError{123 Message: mustBe("incompatible parameter type"),124 Path: mustBe("DATA[1]"),125 Got: mustBe("string"),126 Expected: mustBe("int"),127 })128 })129 }130 })131 t.Run("nil slice", func(t *testing.T) {132 var got []int133 testCases := []struct {134 name string135 got any136 }{137 {"slice", got},138 {"*slice", &got},139 }140 for _, tc := range testCases {141 t.Run(tc.name, func(t *testing.T) {142 checkOK(t, tc.got, td.Grep(td.Gt(666), ([]int)(nil)))143 })144 }145 })146 t.Run("nil pointer", func(t *testing.T) {147 checkError(t, (*[]int)(nil), td.Grep(td.Ignore(), []int{33}),148 expectedError{149 Message: mustBe("nil pointer"),150 Path: mustBe("DATA"),151 Got: mustBe("nil *slice (*[]int type)"),152 Expected: mustBe("non-nil *slice OR *array"),153 })154 })155 t.Run("JSON", func(t *testing.T) {156 got := map[string]any{157 "values": []int{1, 2, 3, 4},158 }159 checkOK(t, got, td.JSON(`{"values": Grep(Gt(2), [3, 4])}`))160 })161 t.Run("errors", func(t *testing.T) {162 for _, filter := range []any{nil, 33} {163 checkError(t, "never tested",164 td.Grep(filter, 42),165 expectedError{166 Message: mustBe("bad usage of Grep operator"),167 Path: mustBe("DATA"),168 Summary: mustBe("usage: Grep(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must be a function or FILTER_TESTDEEP_OPERATOR a TestDeep operator"),169 },170 "filter:", filter)171 }172 for _, filter := range []any{173 func() bool { return true },174 func(a, b int) bool { return true },175 func(a ...int) bool { return true },176 } {177 checkError(t, "never tested",178 td.Grep(filter, 42),179 expectedError{180 Message: mustBe("bad usage of Grep operator"),181 Path: mustBe("DATA"),182 Summary: mustBe("usage: Grep(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must take only one non-variadic argument"),183 },184 "filter:", filter)185 }186 for _, filter := range []any{187 func(a int) {},188 func(a int) int { return 0 },189 func(a int) (bool, bool) { return true, true },190 } {191 checkError(t, "never tested",192 td.Grep(filter, 42),193 expectedError{194 Message: mustBe("bad usage of Grep operator"),195 Path: mustBe("DATA"),196 Summary: mustBe("usage: Grep(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must return bool"),197 },198 "filter:", filter)199 }200 checkError(t, "never tested", td.Grep(td.Ignore(), 42),201 expectedError{202 Message: mustBe("bad usage of Grep operator"),203 Path: mustBe("DATA"),204 Summary: mustBe("usage: Grep(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), EXPECTED_VALUE must be a slice not a int"),205 })206 checkError(t, &struct{}{}, td.Grep(td.Ignore(), []int{33}),207 expectedError{208 Message: mustBe("bad kind"),209 Path: mustBe("DATA"),210 Got: mustBe("*struct (*struct {} type)"),211 Expected: mustBe("slice OR array OR *slice OR *array"),212 })213 checkError(t, nil, td.Grep(td.Ignore(), []int{33}),214 expectedError{215 Message: mustBe("bad kind"),216 Path: mustBe("DATA"),217 Got: mustBe("nil"),218 Expected: mustBe("slice OR array OR *slice OR *array"),219 })220 })221}222func TestGrepTypeBehind(t *testing.T) {223 equalTypes(t, td.Grep(func(n int) bool { return true }, []int{33}), []int{})224 equalTypes(t, td.Grep(td.Gt("0"), []string{"33"}), []string{})225 // Erroneous op226 equalTypes(t, td.Grep(42, 33), nil)227}228func TestGrepString(t *testing.T) {229 test.EqualStr(t,230 td.Grep(func(n int) bool { return true }, []int{}).String(),231 "Grep(func(int) bool)")232 test.EqualStr(t, td.Grep(td.Gt(0), []int{}).String(), "Grep(> 0)")233 // Erroneous op234 test.EqualStr(t, td.Grep(42, []int{}).String(), "Grep(<ERROR>)")235}236func TestFirst(t *testing.T) {237 t.Run("basic", func(t *testing.T) {238 got := [...]int{-3, -2, -1, 0, 1, 2, 3}239 sgot := got[:]240 testCases := []struct {241 name string242 got any243 }{244 {"slice", sgot},245 {"array", got},246 {"*slice", &sgot},247 {"*array", &got},248 }...

Full Screen

Full Screen

td_grep.go

Source:td_grep.go Github

copy

Full Screen

...9 "github.com/maxatome/go-testdeep/internal/ctxerr"10 "github.com/maxatome/go-testdeep/internal/types"11)12const grepUsage = "(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE)"13type tdGrepBase struct {14 tdSmugglerBase15 filter reflect.Value // func (argType ≠ nil) OR TestDeep operator16 argType reflect.Type17}18func (g *tdGrepBase) initGrepBase(filter, expectedValue any) {19 g.tdSmugglerBase = newSmugglerBase(expectedValue, 1)20 if !g.isTestDeeper {21 g.expectedValue = reflect.ValueOf(expectedValue)22 }23 if op, ok := filter.(TestDeep); ok {24 g.filter = reflect.ValueOf(op)25 return26 }27 vfilter := reflect.ValueOf(filter)28 if vfilter.Kind() != reflect.Func {29 g.err = ctxerr.OpBad(g.GetLocation().Func,30 "usage: %s%s, FILTER_FUNC must be a function or FILTER_TESTDEEP_OPERATOR a TestDeep operator",31 g.GetLocation().Func, grepUsage)32 return33 }34 filterType := vfilter.Type()35 if filterType.IsVariadic() || filterType.NumIn() != 1 {36 g.err = ctxerr.OpBad(g.GetLocation().Func,37 "usage: %s%s, FILTER_FUNC must take only one non-variadic argument",38 g.GetLocation().Func, grepUsage)39 return40 }41 if filterType.NumOut() != 1 || filterType.Out(0) != types.Bool {42 g.err = ctxerr.OpBad(g.GetLocation().Func,43 "usage: %s%s, FILTER_FUNC must return bool",44 g.GetLocation().Func, grepUsage)45 return46 }47 g.argType = filterType.In(0)48 g.filter = vfilter49}50func (g *tdGrepBase) matchItem(ctx ctxerr.Context, idx int, item reflect.Value) (bool, *ctxerr.Error) {51 if g.argType == nil {52 // g.filter is a TestDeep operator53 return deepValueEqualFinalOK(ctx, item, g.filter), nil54 }55 // item is an interface, but the filter function does not expect an56 // interface, resolve it57 if item.Kind() == reflect.Interface && g.argType.Kind() != reflect.Interface {58 item = item.Elem()59 }60 if !item.Type().AssignableTo(g.argType) {61 if !types.IsConvertible(item, g.argType) {62 if ctx.BooleanError {63 return false, ctxerr.BooleanError64 }65 return false, ctx.AddArrayIndex(idx).CollectError(&ctxerr.Error{66 Message: "incompatible parameter type",67 Got: types.RawString(item.Type().String()),68 Expected: types.RawString(g.argType.String()),69 })70 }71 item = item.Convert(g.argType)72 }73 return g.filter.Call([]reflect.Value{item})[0].Bool(), nil74}75func (g *tdGrepBase) HandleInvalid() bool {76 return true // Knows how to handle untyped nil values (aka invalid values)77}78func (g *tdGrepBase) String() string {79 if g.err != nil {80 return g.stringError()81 }82 if g.argType == nil {83 return S("%s(%s)", g.GetLocation().Func, g.filter.Interface().(TestDeep))84 }85 return S("%s(%s)", g.GetLocation().Func, g.filter.Type())86}87func (g *tdGrepBase) TypeBehind() reflect.Type {88 if g.err != nil {89 return nil90 }91 return g.internalTypeBehind()92}93// sliceTypeBehind is used by First & Last TypeBehind method.94func (g *tdGrepBase) sliceTypeBehind() reflect.Type {95 typ := g.TypeBehind()96 if typ == nil {97 return nil98 }99 return reflect.SliceOf(typ)100}101func (g *tdGrepBase) notFound(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {102 if ctx.BooleanError {103 return ctxerr.BooleanError104 }105 return ctx.CollectError(&ctxerr.Error{106 Message: "item not found",107 Got: got,108 Expected: types.RawString(g.String()),109 })110}111func grepResolvePtr(ctx ctxerr.Context, got *reflect.Value) *ctxerr.Error {112 if got.Kind() == reflect.Ptr {113 gotElem := got.Elem()114 if !gotElem.IsValid() {115 if ctx.BooleanError {116 return ctxerr.BooleanError117 }118 return ctx.CollectError(ctxerr.NilPointer(*got, "non-nil *slice OR *array"))119 }120 switch gotElem.Kind() {121 case reflect.Slice, reflect.Array:122 *got = gotElem123 }124 }125 return nil126}127func grepBadKind(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {128 if ctx.BooleanError {129 return ctxerr.BooleanError130 }131 return ctx.CollectError(ctxerr.BadKind(got, "slice OR array OR *slice OR *array"))132}133type tdGrep struct {134 tdGrepBase135}136var _ TestDeep = &tdGrep{}137// summary(Grep): reduces a slice or an array before comparing its content138// input(Grep): array,slice,ptr(ptr on array/slice)139// Grep is a smuggler operator. It takes an array, a slice or a140// pointer on array/slice. For each item it applies filter, a141// [TestDeep] operator or a function returning a bool, and produces a142// slice consisting of those items for which the filter matched and143// compares it to expectedValue. The filter matches when it is a:144// - [TestDeep] operator and it matches for the item;145// - function receiving the item and it returns true.146//147// expectedValue can be a [TestDeep] operator or a slice (but never an148// array nor a pointer on a slice/array nor any other kind).149//150// got := []int{-3, -2, -1, 0, 1, 2, 3}151// td.Cmp(t, got, td.Grep(td.Gt(0), []int{1, 2, 3})) // succeeds152// td.Cmp(t, got, td.Grep(153// func(x int) bool { return x%2 == 0 },154// []int{-2, 0, 2})) // succeeds155// td.Cmp(t, got, td.Grep(156// func(x int) bool { return x%2 == 0 },157// td.Set(0, 2, -2))) // succeeds158//159// If Grep receives a nil slice or a pointer on a nil slice, it always160// returns a nil slice:161//162// var got []int163// td.Cmp(t, got, td.Grep(td.Gt(0), ([]int)(nil))) // succeeds164// td.Cmp(t, got, td.Grep(td.Gt(0), td.Nil())) // succeeds165// td.Cmp(t, got, td.Grep(td.Gt(0), []int{})) // fails166//167// See also [First] and [Last].168func Grep(filter, expectedValue any) TestDeep {169 g := tdGrep{}170 g.initGrepBase(filter, expectedValue)171 if g.err == nil && !g.isTestDeeper && g.expectedValue.Kind() != reflect.Slice {172 g.err = ctxerr.OpBad("Grep",173 "usage: Grep%s, EXPECTED_VALUE must be a slice not a %s",174 grepUsage, types.KindType(g.expectedValue))175 }176 return &g177}178func (g *tdGrep) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {179 if g.err != nil {180 return ctx.CollectError(g.err)181 }182 if rErr := grepResolvePtr(ctx, &got); rErr != nil {183 return rErr184 }185 switch got.Kind() {186 case reflect.Slice, reflect.Array:187 const grepped = "<grepped>"188 if got.Kind() == reflect.Slice && got.IsNil() {189 return deepValueEqual(190 ctx.AddCustomLevel(grepped),191 reflect.New(got.Type()).Elem(),192 g.expectedValue,193 )194 }195 l := got.Len()196 out := reflect.MakeSlice(reflect.SliceOf(got.Type().Elem()), 0, l)197 for idx := 0; idx < l; idx++ {198 item := got.Index(idx)199 ok, rErr := g.matchItem(ctx, idx, item)200 if rErr != nil {201 return rErr202 }203 if ok {204 out = reflect.Append(out, item)205 }206 }207 return deepValueEqual(ctx.AddCustomLevel(grepped), out, g.expectedValue)208 }209 return grepBadKind(ctx, got)210}211type tdFirst struct {212 tdGrepBase213}214var _ TestDeep = &tdFirst{}215// summary(First): find the first matching item of a slice or an array216// then compare its content217// input(First): array,slice,ptr(ptr on array/slice)218// First is a smuggler operator. It takes an array, a slice or a219// pointer on array/slice. For each item it applies filter, a220// [TestDeep] operator or a function returning a bool. It takes the221// first item for which the filter matched and compares it to222// expectedValue. The filter matches when it is a:223// - [TestDeep] operator and it matches for the item;224// - function receiving the item and it returns true.225//226// expectedValue can of course be a [TestDeep] operator.227//228// got := []int{-3, -2, -1, 0, 1, 2, 3}229// td.Cmp(t, got, td.First(td.Gt(0), 1)) // succeeds230// td.Cmp(t, got, td.First(func(x int) bool { return x%2 == 0 }, -2)) // succeeds231// td.Cmp(t, got, td.First(func(x int) bool { return x%2 == 0 }, td.Lt(0))) // succeeds232//233// If the input is empty (and/or nil for a slice), an "item not found"234// error is raised before comparing to expectedValue.235//236// var got []int237// td.Cmp(t, got, td.First(td.Gt(0), td.Gt(0))) // fails238// td.Cmp(t, []int{}, td.First(td.Gt(0), td.Gt(0))) // fails239// td.Cmp(t, [0]int{}, td.First(td.Gt(0), td.Gt(0))) // fails240//241// See also [Last] and [Grep].242func First(filter, expectedValue any) TestDeep {243 g := tdFirst{}244 g.initGrepBase(filter, expectedValue)245 return &g246}247func (g *tdFirst) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {248 if g.err != nil {249 return ctx.CollectError(g.err)250 }251 if rErr := grepResolvePtr(ctx, &got); rErr != nil {252 return rErr253 }254 switch got.Kind() {255 case reflect.Slice, reflect.Array:256 for idx, l := 0, got.Len(); idx < l; idx++ {257 item := got.Index(idx)258 ok, rErr := g.matchItem(ctx, idx, item)259 if rErr != nil {260 return rErr261 }262 if ok {263 return deepValueEqual(264 ctx.AddCustomLevel(S("<first#%d>", idx)),265 item,266 g.expectedValue,267 )268 }269 }270 return g.notFound(ctx, got)271 }272 return grepBadKind(ctx, got)273}274func (g *tdFirst) TypeBehind() reflect.Type {275 return g.sliceTypeBehind()276}277type tdLast struct {278 tdGrepBase279}280var _ TestDeep = &tdLast{}281// summary(Last): find the last matching item of a slice or an array282// then compare its content283// input(Last): array,slice,ptr(ptr on array/slice)284// Last is a smuggler operator. It takes an array, a slice or a285// pointer on array/slice. For each item it applies filter, a286// [TestDeep] operator or a function returning a bool. It takes the287// last item for which the filter matched and compares it to288// expectedValue. The filter matches when it is a:289// - [TestDeep] operator and it matches for the item;290// - function receiving the item and it returns true.291//292// expectedValue can of course be a [TestDeep] operator.293//294// got := []int{-3, -2, -1, 0, 1, 2, 3}295// td.Cmp(t, got, td.Last(td.Lt(0), -1)) // succeeds296// td.Cmp(t, got, td.Last(func(x int) bool { return x%2 == 0 }, 2)) // succeeds297// td.Cmp(t, got, td.Last(func(x int) bool { return x%2 == 0 }, td.Gt(0))) // succeeds298//299// If the input is empty (and/or nil for a slice), an "item not found"300// error is raised before comparing to expectedValue.301//302// var got []int303// td.Cmp(t, got, td.Last(td.Gt(0), td.Gt(0))) // fails304// td.Cmp(t, []int{}, td.Last(td.Gt(0), td.Gt(0))) // fails305// td.Cmp(t, [0]int{}, td.Last(td.Gt(0), td.Gt(0))) // fails306//307// See also [First] and [Grep].308func Last(filter, expectedValue any) TestDeep {309 g := tdLast{}310 g.initGrepBase(filter, expectedValue)311 return &g312}313func (g *tdLast) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {314 if g.err != nil {315 return ctx.CollectError(g.err)316 }317 if rErr := grepResolvePtr(ctx, &got); rErr != nil {318 return rErr319 }320 switch got.Kind() {321 case reflect.Slice, reflect.Array:322 for idx := got.Len() - 1; idx >= 0; idx-- {323 item := got.Index(idx)324 ok, rErr := g.matchItem(ctx, idx, item)...

Full Screen

Full Screen

utils_disk.go

Source:utils_disk.go Github

copy

Full Screen

1/*2Name: plume3File: utils_disk.go4Author: Landers5*/6package main7import (8 "encoding/json"9 "fmt"10 "strings"11)12// 获取磁盘信息13func getDiskInfo(d string, debug bool) DiskInfo {14 var disk DiskInfo15 // Size Used Use%16 sh := "df -hl / | awk 'NR==2{print $1, $2, $3, $5}'"17 res, e := cmdRun(sh, debug)18 disk_info := strings.Fields(string(res))19 if e != nil || len(disk_info) < 4 {20 disk.DiskMount = "/"21 disk.DiskUsage = "0"22 disk.DiskUsed = "0"23 disk.DiskAll = "0"24 } else {25 // 容量取整26 disk.DiskMount = disk_info[0]27 disk.DiskAll = strings.Trim(disk_info[1], "\n")28 disk.DiskUsed = strings.Trim(disk_info[2], "\n")29 disk.DiskUsage = strings.Trim(disk_info[3], "%")30 }31 // 使用默认磁盘32 if d != "" {33 sh = fmt.Sprintf("iostat -d %s -x -o JSON", d)34 } else {35 sh = "iostat -d -x -o JSON"36 }37 type Disk struct {38 Read string `json:"kB_read"`39 Write string `json:"kB_wrtn"`40 }41 type Statistics struct {42 Disk []map[string]interface{} `json:"disk"`43 }44 type Hosts struct {45 Statistics []Statistics `json:"statistics"`46 }47 type SysStat struct {48 Hosts []Hosts `json:"hosts"`49 }50 type stat struct {51 SysStat SysStat `json:"sysstat"`52 }53 // 读速率 读延迟 写速率k/s 写延迟54 var td stat55 res, e = cmdRun(sh, debug)56 e = json.Unmarshal(res, &td)57 readDelay := td.SysStat.Hosts[0].Statistics[0].Disk[0]["r_await"]58 writeDelay := td.SysStat.Hosts[0].Statistics[0].Disk[0]["w_await"]59 readRate := td.SysStat.Hosts[0].Statistics[0].Disk[0]["rkB/s"]60 writeRate := td.SysStat.Hosts[0].Statistics[0].Disk[0]["wkB/s"]61 if e != nil {62 disk.DiskReadRate = "0 KB/s"63 disk.DiskWriteRate = "0 KB/s"64 }else {65 disk.DiskReadRate = calcRate(readRate)66 disk.DiskReadDelay = fmt.Sprintf("%.2f", readDelay.(float64))67 disk.DiskWriteRate = calcRate(writeRate)68 disk.DiskWriteDelay = fmt.Sprintf("%.2f", writeDelay.(float64))69 }70 // 读写量kb 因为版本问题不一致 不能使用列判断71 if d != "" {72 sh = fmt.Sprintf("iostat -d %s -o JSON", d)73 }else {74 sh = "iostat -d -o JSON"75 }76 res, e = cmdRun(sh, debug)77 var t stat78 e = json.Unmarshal(res, &t)79 readByte := t.SysStat.Hosts[0].Statistics[0].Disk[0]["kB_read"]80 writeByte := t.SysStat.Hosts[0].Statistics[0].Disk[0]["kB_wrtn"]81 if e != nil {82 disk.DiskReadByte = "0"83 disk.DiskWriteByte = "0"84 }else {85 disk.DiskReadByte = calcByte(readByte)86 disk.DiskWriteByte = calcByte(writeByte)87 }88 return disk89}90func getDiskInfoDetail(debug bool) DiskInfoDetail {91 var disk DiskInfoDetail92 sh := "fdisk -l|grep Disklabel"93 res, e := cmdRun(sh, debug)94 if e != nil {95 disk.Label = "unknown"96 } else {97 disk.Label = strings.Split(string(res), ":")[1]98 }99 sh = "fdisk -l|grep 'model'"100 res, e = cmdRun(sh, debug)101 if e != nil {102 disk.Model = "unknown"103 } else {104 disk.Model = strings.Split(string(res), ":")[1]105 }106 sh = "df -h|grep -v Filesystem|tr '\n' ','"107 res, e = cmdRun(sh, debug)108 if e != nil {109 disk.List = []map[string]string{}110 } else {111 var d []map[string]string112 data := strings.Split(string(res), ",")113 for _, di := range data {114 dis := strings.Fields(di)115 if len(dis) >= 6 {116 d = append(d, map[string]string{117 "system": dis[0],118 "size": dis[1],119 "used": dis[2],120 "avail": dis[3],121 "use": dis[4],122 "mount": dis[5],123 })124 }125 }126 disk.List = d127 }128 return disk129}...

Full Screen

Full Screen

Grep

Using AI Code Generation

copy

Full Screen

1import (2type td struct {3}4func (t td) Grep(pattern string) []string {5 return strings.Split(t.s, pattern)6}7func main() {8 t := td{"Hello World"}9 fmt.Println(t.Grep(" "))10}11import "fmt"12func main() {13 f := func() {14 }15 f()16 fmt.Println(i)17}18import "fmt"19func main() {20 f := func() {21 }22 f()23 fmt.Println(i)24}25import "fmt"26func main() {27 f := func() int {28 }29 fmt.Println(f())30 fmt.Println(i)31}32import "fmt"33func main() {34 f := func() int {35 }36 fmt.Println(f())37 fmt.Println(i)38}39import "fmt"40func main() {41 f := func() int {42 }43 f()44 fmt.Println(i)45}46import "fmt"47func main() {48 f := func() int {49 }50 f()51 fmt.Println(i)52}53import "fmt"54func main() {55 f := func() int {56 }57 fmt.Println(f())58 fmt.Println(i)59}

Full Screen

Full Screen

Grep

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("1.go")4 if err != nil {5 log.Fatal(err)6 }7 defer f.Close()8 t := new(td)9 t.Grep(f, "main")10}11import (12type td struct {13}14func (t *td) Grep(r io.Reader, s string) {15 scanner := bufio.NewScanner(r)16 scanner.Split(bufio.ScanLines)17 for scanner.Scan() {18 if t.contains(scanner.Text(), s) {19 fmt.Printf("%d: %s20", i, scanner.Text())21 }22 }23}24func (t *td) contains(s, substr string) bool {25 return t.index(s, substr) != -126}27func (t *td) index(s, substr string) int {28 for i := 0; i < len(s); i++ {29 if len(s[i:]) < len(substr) {30 }31 if s[i:i+len(substr)] == substr {32 }33 }34}35func main() {36 f, err := os.Open("1.go")37 if err != nil {38 log.Fatal(err)39 }40 defer f.Close()41 t := new(td)42 t.Grep(f, "main")43}

Full Screen

Full Screen

Grep

Using AI Code Generation

copy

Full Screen

1import (2type td struct {3}4func (t *td) Grep(r string) string {5 re := regexp.MustCompile(r)6 return re.FindString(t.s)7}8func main() {9 t := td{"I am a string"}10 fmt.Println(t.Grep("I am"))11}12import (13type td struct {14}15func (t *td) Grep(r string) string {16 re := regexp.MustCompile(r)17 return re.FindString(t.s)18}19func main() {20 t := td{"I am a string"}21 fmt.Println(t.Grep("I am"))22}

Full Screen

Full Screen

Grep

Using AI Code Generation

copy

Full Screen

1import (2type td struct {3}4func (t *td) Grep(pattern string) {5 re := regexp.MustCompile(pattern)6 if re.MatchString(t.text) {7 fmt.Println(re.FindString(t.text))8 }9}10func main() {11 t := &td{text: "Hello World"}12 t.Grep("World")13}

Full Screen

Full Screen

Grep

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 td := &td{}4 fmt.Println(td.Grep("a"))5}6import (7func main() {8 td := &td{}9 fmt.Println(td.Grep("a"))10}11import (12func main() {13 td := &td{}14 fmt.Println(td.Grep("a"))15}16import (17func main() {18 td := &td{}19 fmt.Println(td.Grep("a"))20}21import (22func main() {23 td := &td{}24 fmt.Println(td.Grep("a"))25}26import (27func main() {28 td := &td{}29 fmt.Println(td.Grep("a"))30}31import (32func main() {33 td := &td{}34 fmt.Println(td.Grep("a"))35}36import (37func main() {38 td := &td{}39 fmt.Println(td.Grep("a"))40}41import (42func main() {43 td := &td{}44 fmt.Println(td.Grep("a"))45}46import (47func main() {48 td := &td{}49 fmt.Println(td.Grep

Full Screen

Full Screen

Grep

Using AI Code Generation

copy

Full Screen

1import "fmt"2type td struct {3}4func (t td) Grep(s string) string {5}6func main() {7fmt.Println(t.Grep("hello"))8}9import "fmt"10type td struct {11}12func (t td) td() string {13}14func main() {15fmt.Println(t.td())16}17import "fmt"18type td struct {19}20func (t *td) Grep(s string) string {21}22func main() {23fmt.Println(t.Grep("hello"))24}

Full Screen

Full Screen

Grep

Using AI Code Generation

copy

Full Screen

1import (2type td struct {3}4func main() {5 t := td{text: "abc def"}6 fmt.Println(t.Grep("def"))7}8func (t td) Grep(pattern string) bool {9 matched, _ := regexp.MatchString(pattern, t.text)10}11import (12type td struct {13}14func main() {15 t := td{text: "abc def"}16 fmt.Println(t.Grep("def"))17}18func (t td) Grep(pattern string) bool {19 matched, _ := regexp.MatchString(pattern, t.text)20}21import (22type td struct {23}24func main() {25 t := td{text: "abc def"}26 fmt.Println(t.Grep("def"))27}28func (t td) Grep(pattern string) bool {29 matched, _ := regexp.MatchString(pattern, t.text)30}31import (32type td struct {33}34func main() {35 t := td{text: "abc def"}36 fmt.Println(t.Grep("def"))37}38func (t td) Grep(pattern string) bool {39 matched, _ := regexp.MatchString(pattern, t.text)40}41import (42type td struct {43}44func main() {45 t := td{text: "abc def"}46 fmt.Println(t.Grep("def"))47}48func (t td) Grep(pattern string) bool {49 matched, _ := regexp.MatchString(pattern, t.text)50}51import (52type td struct {53}54func main() {55 t := td{text: "abc def"}56 fmt.Println(t.Grep("def"))57}58func (t td)

Full Screen

Full Screen

Grep

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 td := grep.NewGrep()4 td.SetPattern("test")5 td.SetPath("/tmp")6 td.SetRecursive(true)7 td.SetVerbose(true)8 td.SetDebug(true)9 td.SetExtension("go")10 td.SetFile("test.go")11 td.SetLine(1)12 td.SetLines(1, 10)13 td.SetLinesAfter(1)14 td.SetLinesBefore(1)15 td.SetInvertMatch(true)16 td.SetIgnoreCase(true)17 td.SetCount(true)18 td.SetOnlyMatching(true)19 td.SetNoFilename(true)20 td.SetExcludeDir("vendor")21 td.SetExcludeFile("test.go")22 td.SetExcludeFiles("test.go, test2.go")23 td.SetExcludeDirFiles("vendor, test.go")24 td.SetExcludeDirFile("vendor, test.go")25 td.SetExcludeDirFiles("vendor, test.go, test2.go")26 td.SetExcludeDirFile("vendor, test.go, test2.go")27 td.SetExcludeDirFiles("vendor, test.go, test2.go, test3.go")28 td.SetExcludeDirFile("vendor, test.go, test2.go, test3.go")29 td.SetExcludeDirFiles("vendor, test.go, test2.go, test3.go, test4.go")30 td.SetExcludeDirFile("vendor, test.go, test2.go, test3.go, test4.go")31 td.SetExcludeDirFiles("vendor, test.go, test2.go, test3.go, test4.go, test5.go")32 td.SetExcludeDirFile("vendor, test.go, test2.go, test3.go, test4.go, test5.go")33 td.SetExcludeDirFiles("vendor, test.go, test2.go, test3.go, test4.go, test5.go, test6.go")34 td.SetExcludeDirFile("vendor, test.go, test2.go, test3.go, test4.go, test5.go, test6.go")35 td.SetExcludeDirFiles("vendor, test.go, test2.go, test3.go, test4.go, test5.go, test6.go, test7.go")36 td.SetExcludeDirFile("vendor, test.go, test2.go, test3.go, test

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Go-testdeep automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful