Best Go-testdeep code snippet using td.Flatten
td_set.go
Source:td_set.go
...24// Person{Name: "Bob", Age: 32},25// Person{Name: "Alice", Age: 26},26// ))27//28// To flatten a non-[]any slice/array, use [Flatten] function29// and so avoid boring and inefficient copies:30//31// expected := []int{2, 1}32// td.Cmp(t, []int{1, 1, 2}, td.Set(td.Flatten(expected))) // succeeds33// // = td.Cmp(t, []int{1, 1, 2}, td.Set(2, 1))34//35// exp1 := []int{2, 1}36// exp2 := []int{5, 8}37// td.Cmp(t, []int{1, 5, 1, 2, 8, 3, 3},38// td.Set(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds39// // = td.Cmp(t, []int{1, 5, 1, 2, 8, 3, 3}, td.Set(2, 1, 3, 5, 8))40//41// TypeBehind method can return a non-nil [reflect.Type] if all items42// known non-interface types are equal, or if only interface types43// are found (mostly issued from [Isa]) and they are equal.44//45// See also [NotAny], [SubSetOf], [SuperSetOf] and [Bag].46func Set(expectedItems ...any) TestDeep {47 return newSetBase(allSet, true, expectedItems)48}49// summary(SubSetOf): compares the contents of an array or a slice50// ignoring duplicates and without taking care of the order of items51// but with potentially some exclusions52// input(SubSetOf): array,slice,ptr(ptr on array/slice)53// SubSetOf operator compares the contents of an array or a slice (or a54// pointer on array/slice) ignoring duplicates and without taking care55// of the order of items.56//57// During a match, each array/slice item should be matched by an58// expected item to succeed. But some expected items can be missing59// from the compared array/slice.60//61// td.Cmp(t, []int{1, 1}, td.SubSetOf(1, 2)) // succeeds62// td.Cmp(t, []int{1, 1, 2}, td.SubSetOf(1, 3)) // fails, 2 is an extra item63//64// // works with slices/arrays of any type65// td.Cmp(t, personSlice, td.SubSetOf(66// Person{Name: "Bob", Age: 32},67// Person{Name: "Alice", Age: 26},68// ))69//70// To flatten a non-[]any slice/array, use [Flatten] function71// and so avoid boring and inefficient copies:72//73// expected := []int{2, 1}74// td.Cmp(t, []int{1, 1}, td.SubSetOf(td.Flatten(expected))) // succeeds75// // = td.Cmp(t, []int{1, 1}, td.SubSetOf(2, 1))76//77// exp1 := []int{2, 1}78// exp2 := []int{5, 8}79// td.Cmp(t, []int{1, 5, 1, 3, 3},80// td.SubSetOf(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds81// // = td.Cmp(t, []int{1, 5, 1, 3, 3}, td.SubSetOf(2, 1, 3, 5, 8))82//83// TypeBehind method can return a non-nil [reflect.Type] if all items84// known non-interface types are equal, or if only interface types85// are found (mostly issued from [Isa]) and they are equal.86//87// See also [NotAny], [Set] and [SuperSetOf].88func SubSetOf(expectedItems ...any) TestDeep {89 return newSetBase(subSet, true, expectedItems)90}91// summary(SuperSetOf): compares the contents of an array or a slice92// ignoring duplicates and without taking care of the order of items93// but with potentially some extra items94// input(SuperSetOf): array,slice,ptr(ptr on array/slice)95// SuperSetOf operator compares the contents of an array or a slice (or96// a pointer on array/slice) ignoring duplicates and without taking97// care of the order of items.98//99// During a match, each expected item should match in the compared100// array/slice. But some items in the compared array/slice may not be101// expected.102//103// td.Cmp(t, []int{1, 1, 2}, td.SuperSetOf(1)) // succeeds104// td.Cmp(t, []int{1, 1, 2}, td.SuperSetOf(1, 3)) // fails, 3 is missing105//106// // works with slices/arrays of any type107// td.Cmp(t, personSlice, td.SuperSetOf(108// Person{Name: "Bob", Age: 32},109// Person{Name: "Alice", Age: 26},110// ))111//112// To flatten a non-[]any slice/array, use [Flatten] function113// and so avoid boring and inefficient copies:114//115// expected := []int{2, 1}116// td.Cmp(t, []int{1, 1, 2, 8}, td.SuperSetOf(td.Flatten(expected))) // succeeds117// // = td.Cmp(t, []int{1, 1, 2, 8}, td.SubSetOf(2, 1))118//119// exp1 := []int{2, 1}120// exp2 := []int{5, 8}121// td.Cmp(t, []int{1, 5, 1, 8, 42, 3, 3},122// td.SuperSetOf(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds123// // = td.Cmp(t, []int{1, 5, 1, 8, 42, 3, 3}, td.SuperSetOf(2, 1, 3, 5, 8))124//125// TypeBehind method can return a non-nil [reflect.Type] if all items126// known non-interface types are equal, or if only interface types127// are found (mostly issued from [Isa]) and they are equal.128//129// See also [NotAny], [Set] and [SubSetOf].130func SuperSetOf(expectedItems ...any) TestDeep {131 return newSetBase(superSet, true, expectedItems)132}133// summary(NotAny): compares the contents of an array or a slice, no134// values have to match135// input(NotAny): array,slice,ptr(ptr on array/slice)136// NotAny operator checks that the contents of an array or a slice (or137// a pointer on array/slice) does not contain any of "notExpectedItems".138//139// td.Cmp(t, []int{1}, td.NotAny(1, 2, 3)) // fails140// td.Cmp(t, []int{5}, td.NotAny(1, 2, 3)) // succeeds141//142// // works with slices/arrays of any type143// td.Cmp(t, personSlice, td.NotAny(144// Person{Name: "Bob", Age: 32},145// Person{Name: "Alice", Age: 26},146// ))147//148// To flatten a non-[]any slice/array, use [Flatten] function149// and so avoid boring and inefficient copies:150//151// notExpected := []int{2, 1}152// td.Cmp(t, []int{4, 4, 3, 8}, td.NotAny(td.Flatten(notExpected))) // succeeds153// // = td.Cmp(t, []int{4, 4, 3, 8}, td.NotAny(2, 1))154//155// notExp1 := []int{2, 1}156// notExp2 := []int{5, 8}157// td.Cmp(t, []int{4, 4, 42, 8},158// td.NotAny(td.Flatten(notExp1), 3, td.Flatten(notExp2))) // succeeds159// // = td.Cmp(t, []int{4, 4, 42, 8}, td.NotAny(2, 1, 3, 5, 8))160//161// Beware that NotAny(â¦) is not equivalent to Not(Any(â¦)) but is like162// Not(SuperSet(â¦)).163//164// TypeBehind method can return a non-nil [reflect.Type] if all items165// known non-interface types are equal, or if only interface types166// are found (mostly issued from [Isa]) and they are equal.167//168// See also [Set], [SubSetOf] and [SuperSetOf].169func NotAny(notExpectedItems ...any) TestDeep {170 return newSetBase(noneSet, true, notExpectedItems)171}...
entitled_features_test.go
Source:entitled_features_test.go
...33 } else if !tst.expectNilCtxVal && efIfc == nil {34 t.Errorf("tst#%d: FAIL: Expected non-nil context.Value(%s), but got nil",35 idx, string(EntitledFeaturesKey))36 }37 efArr, flattenErr := FlattenRawEntitledFeatures(efIfc)38 if !tst.expectFlattenErr && flattenErr != nil {39 t.Errorf("tst#%d: FAIL: Got unexpected FlattenRawEntitledFeatures(%#v) err: %s", idx, efIfc, flattenErr)40 } else if tst.expectFlattenErr && flattenErr == nil {41 t.Errorf("tst#%d: FAIL: Expected FlattenRawEntitledFeatures(%#v) err, but got nil err", idx, efIfc)42 }43 if efArr == nil {44 continue45 }46 sort.Strings(efArr)47 sort.Strings(tst.expectFlattenVal)48 if !reflect.DeepEqual(tst.expectFlattenVal, efArr) {49 t.Errorf("tst#%d: FAIL: expectFlattenVal=%s\nefArr=%s",50 idx, tst.expectFlattenVal, efArr)51 }52 }53}54var entitledFeaturesTest = []struct {55 regoRespJSON string56 expectNilCtxVal bool57 expectFlattenErr bool58 expectFlattenVal []string59}{60 {61 regoRespJSON: `{62 "allow": true63 }`,64 expectNilCtxVal: true,65 expectFlattenErr: false,66 expectFlattenVal: nil,67 },68 {69 regoRespJSON: `{70 "allow": true,71 "entitled_features": null72 }`,73 expectNilCtxVal: true,74 expectFlattenErr: false,75 expectFlattenVal: nil,76 },77 {78 regoRespJSON: `{79 "allow": true,80 "entitled_features": {}81 }`,82 expectNilCtxVal: false,83 expectFlattenErr: false,84 expectFlattenVal: []string{},85 },86 {87 regoRespJSON: `{88 "allow": true,89 "entitled_features": {"null": null}90 }`,91 expectNilCtxVal: false,92 expectFlattenErr: false,93 expectFlattenVal: []string{},94 },95 {96 regoRespJSON: `{97 "allow": true,98 "entitled_features": "bad entitled_features value"99 }`,100 expectNilCtxVal: false,101 expectFlattenErr: true,102 expectFlattenVal: nil,103 },104 {105 regoRespJSON: `{106 "allow": true,107 "entitled_features": {"str": "str"}108 }`,109 expectNilCtxVal: false,110 expectFlattenErr: true,111 expectFlattenVal: nil,112 },113 {114 regoRespJSON: `{115 "allow": true,116 "entitled_features": {117 "null": null,118 "lic": [ "dhcp", null, "ipam" ],119 "rpz": [ "bogon", null, "malware" ]120 }121 }`,122 expectNilCtxVal: false,123 expectFlattenErr: false,124 expectFlattenVal: []string{"lic.dhcp", "lic.ipam", "rpz.bogon", "rpz.malware"},125 },126}127func Test_opbench_ToJSONBArrStmt(t *testing.T) {128 tests := []struct {129 name string130 rego string131 want string132 wantErr bool133 }{134 {135 name: "OneSvcOneFeatureOk",136 rego: `{137 "allow": true,138 "entitled_features": {...
flatten.go
Source:flatten.go
...8 "reflect"9 "github.com/maxatome/go-testdeep/internal/color"10 "github.com/maxatome/go-testdeep/internal/flat"11)12// Flatten allows to flatten any slice, array or map in parameters of13// operators expecting ...any.14//15// For example the [Set] operator is defined as:16//17// func Set(expectedItems ...any) TestDeep18//19// so when comparing to a []int slice, we usually do:20//21// got := []int{42, 66, 22}22// td.Cmp(t, got, td.Set(22, 42, 66))23//24// it works but if the expected items are already in a []int, we have25// to copy them in a []any as it can not be flattened directly26// in [Set] parameters:27//28// expected := []int{22, 42, 66}29// expectedIf := make([]any, len(expected))30// for i, item := range expected {31// expectedIf[i] = item32// }33// td.Cmp(t, got, td.Set(expectedIf...))34//35// but it is a bit boring and less efficient, as [Set] does not keep36// the []any behind the scene.37//38// The same with Flatten follows:39//40// expected := []int{22, 42, 66}41// td.Cmp(t, got, td.Set(td.Flatten(expected)))42//43// Several Flatten calls can be passed, and even combined with normal44// parameters:45//46// expectedPart1 := []int{11, 22, 33}47// expectedPart2 := []int{55, 66, 77}48// expectedPart3 := []int{99}49// td.Cmp(t, got,50// td.Set(51// td.Flatten(expectedPart1),52// 44,53// td.Flatten(expectedPart2),54// 88,55// td.Flatten(expectedPart3),56// ))57//58// is exactly the same as:59//60// td.Cmp(t, got, td.Set(11, 22, 33, 44, 55, 66, 77, 88, 99))61//62// Note that Flatten calls can even be nested:63//64// td.Cmp(t, got,65// td.Set(66// td.Flatten([]any{67// 11,68// td.Flatten([]int{22, 33}),69// td.Flatten([]int{44, 55, 66}),70// }),71// 77,72// ))73//74// is exactly the same as:75//76// td.Cmp(t, got, td.Set(11, 22, 33, 44, 55, 66, 77))77//78// Maps can be flattened too, keeping in mind there is no particular order:79//80// td.Flatten(map[int]int{1: 2, 3: 4})81//82// is flattened as 1, 2, 3, 4 or 3, 4, 1, 2.83func Flatten(sliceOrMap any) flat.Slice {84 switch reflect.ValueOf(sliceOrMap).Kind() {85 case reflect.Slice, reflect.Array, reflect.Map:86 return flat.Slice{Slice: sliceOrMap}87 default:88 panic(color.BadUsage("Flatten(SLICE|ARRAY|MAP)", sliceOrMap, 1, true))89 }90}...
Flatten
Using AI Code Generation
1import (2func main() {3 data := [][]string{4 []string{"a", "b", "c"},5 []string{"d", "e", "f"},6 []string{"g", "h", "i"},7 }8 table := tablewriter.NewWriter(os.Stdout)9 table.SetHeader([]string{"A", "B", "C"})10 table.SetBorder(false)11 table.AppendBulk(data)12 table.Render()13 table.SetBorder(true)14 table.SetCenterSeparator("|")15 table.SetColumnSeparator("|")16 table.SetRowSeparator("-")17 table.SetHeaderLine(false)18 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)19 table.SetAlignment(tablewriter.ALIGN_LEFT)20 table.Render()21 table.SetAutoWrapText(false)22 table.SetRowLine(true)23 table.SetAutoFormatHeaders(false)24 table.SetAutoFormatHeaders(false)25 table.SetHeaderLine(true)26 table.SetHeaderAlignment(tablewriter.ALIGN_RIGHT)27 table.SetAlignment(tablewriter.ALIGN_RIGHT)28 table.SetHeaderLine(true)29 table.SetHeaderAlignment(tablewriter.ALIGN_CENTER)30 table.SetAlignment(tablewriter.ALIGN_CENTER)31 table.SetHeaderLine(true)32 table.SetFooterLine(true)33 table.SetBorder(false)34 table.SetCenterSeparator("|")35 table.SetColumnSeparator("|")36 table.SetRowSeparator("-")37 table.SetHeaderLine(false)38 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)39 table.SetAlignment(tablewriter.ALIGN_LEFT)40 table.AppendBulk(data)41 table.Render()42 table.SetAutoWrapText(false)43 table.SetAutoFormatHeaders(false)44 table.SetRowLine(true)45 table.SetHeaderLine(true)46 table.SetAlignment(tablewriter.ALIGN_LEFT)47 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)48 table.SetFooterLine(true)49 table.SetBorder(false)50 table.SetCenterSeparator("|")51 table.SetColumnSeparator("|")52 table.SetRowSeparator("-")53 table.SetHeaderLine(false)54 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)55 table.SetAlignment(tablewriter.ALIGN_LEFT)56 table.AppendBulk(data)57 table.Render()58 table.SetAutoWrapText(false)59 table.SetAutoFormatHeaders(false)60 table.SetRowLine(true)61 table.SetHeaderLine(true)62 table.SetAlignment(tablewriter.ALIGN_LEFT)63 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)64 table.SetFooterLine(true)65 table.SetBorder(false)66 table.SetCenterSeparator("|")67 table.SetColumnSeparator("|")68 table.SetRowSeparator("-")69 table.SetHeaderLine(false)70 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)71 table.SetAlignment(tablewriter.ALIGN_LEFT)
Flatten
Using AI Code Generation
1import (2func main() {3 data := [][]string{4 []string{"A", "B", "C", "D"},5 []string{"E", "F", "G", "H"},6 []string{"I", "J", "K", "L"},7 []string{"M", "N", "O", "P"},8 []string{"Q", "R", "S", "T"},9 []string{"U", "V", "W", "X"},10 }11 td := tablewriter.NewWriter(os.Stdout)12 td.SetRowLine(true)13 td.SetHeader([]string{"A", "B", "C", "D"})14 td.AppendBulk(data)15 td.Render()16 fmt.Println(strings.Repeat("-", 100))17 td.Flatten()18 td.Render()19}
Flatten
Using AI Code Generation
1import (2func main() {3 td := xlsx.NewTealegSheet()4 td.AddRow()5 td.AddCell("A1")6 td.AddCell("B1")7 td.AddCell("C1")8 td.AddRow()9 td.AddCell("A2")10 td.AddCell("B2")11 td.AddCell("C2")12 td.AddRow()13 td.AddCell("A3")14 td.AddCell("B3")15 td.AddCell("C3")16 td.AddRow()17 td.AddCell("A4")18 td.AddCell("B4")19 td.AddCell("C4")20 td.AddRow()21 td.AddCell("A5")22 td.AddCell("B5")23 td.AddCell("C5")24 td.Flatten()25 fmt.Println(td)26}27{[{A1 B1 C1} {A2 B2 C2} {A3 B3 C3} {A4 B4 C4} {A5 B5 C5}]}
Flatten
Using AI Code Generation
1import (2func main() {3 vm := otto.New()4 vm.Run(`5 var td = require('testdouble');6 var obj = {foo: {bar: 'baz'}};7 var flattened = td.object(obj);8 flattened, _ := vm.Get("flattened")9 fmt.Println(flattened)10}11import (12func main() {13 vm := otto.New()14 vm.Run(`15 var td = require('testdouble');16 var obj = {foo: {bar: 'baz'}};17 var flattened = td.object(obj);18 flattened, _ := vm.Get("flattened")19 fmt.Println(flattened)20}21import (22func main() {23 vm := otto.New()24 vm.Run(`25 var td = require('testdouble');26 var obj = {foo: {bar: 'baz'}
Flatten
Using AI Code Generation
1import (2func main() {3 td := elastic.NewTermsAggregation().Field("user").Size(10)4 fmt.Println(td)5 tf := td.Flatten()6 fmt.Println(tf)7}8import (9func main() {10 td := elastic.NewTermsAggregation().Field("user").Size(10)11 fmt.Println(td)12 tf := td.Flatten()13 fmt.Println(tf)14}15import (16func main() {17 td := elastic.NewTermsAggregation().Field("user").Size(10)18 fmt.Println(td)19 tf := td.Flatten()20 fmt.Println(tf)21}22import (23func main() {24 td := elastic.NewTermsAggregation().Field("user").Size(10)25 fmt.Println(td)26 tf := td.Flatten()27 fmt.Println(tf)28}29import (
Flatten
Using AI Code Generation
1import (2func main() {3td = td{1, 2, 3}4fmt.Println(td)5fmt.Println(td.Flatten())6}7import (8func main() {9td = td{1, 2, 3}10fmt.Println(td)11fmt.Println(td.Flatten())12}13import (14func main() {15td = td{1, 2, 3}16fmt.Println(td)17fmt.Println(td.Flatten())18}19import (20func main() {21td = td{1, 2, 3}22fmt.Println(td)23fmt.Println(td.Flatten())24}25import (26func main() {27td = td{1, 2, 3}28fmt.Println(td)29fmt.Println(td.Flatten())30}31import (32func main() {33td = td{1, 2, 3}34fmt.Println(td)35fmt.Println(td.Flatten())36}37import (38func main() {39td = td{1, 2, 3}40fmt.Println(td)41fmt.Println(td.Flatten())42}43import (44func main() {45td = td{1, 2, 3}46fmt.Println(td)47fmt.Println(td.Flatten())48}49import (50func main() {51td = td{1, 2, 3}52fmt.Println(td)53fmt.Println(td.Flatten())54}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!