How to use Struct method of td Package

Best Go-testdeep code snippet using td.Struct

td_struct_test.go

Source:td_struct_test.go Github

copy

Full Screen

...12 "github.com/maxatome/go-testdeep/internal/dark"13 "github.com/maxatome/go-testdeep/internal/test"14 "github.com/maxatome/go-testdeep/td"15)16func TestStruct(t *testing.T) {17 gotStruct := MyStruct{18 MyStructMid: MyStructMid{19 MyStructBase: MyStructBase{20 ValBool: true,21 },22 ValStr: "foobar",23 },24 ValInt: 123,25 }26 //27 // Using pointer28 checkOK(t, &gotStruct,29 td.Struct(&MyStruct{}, td.StructFields{30 "ValBool": true,31 "ValStr": "foobar",32 "ValInt": 123,33 "Ptr": nil,34 }))35 checkOK(t, &gotStruct,36 td.Struct(37 &MyStruct{38 MyStructMid: MyStructMid{39 ValStr: "zip",40 },41 ValInt: 666,42 },43 td.StructFields{44 "ValBool": true,45 "> ValStr": "foobar",46 ">ValInt": 123,47 }))48 checkOK(t, &gotStruct,49 td.Struct((*MyStruct)(nil), td.StructFields{50 "ValBool": true,51 "ValStr": "foobar",52 "ValInt": 123,53 "Ptr": nil,54 }))55 checkError(t, 123,56 td.Struct(&MyStruct{}, td.StructFields{}),57 expectedError{58 Message: mustBe("type mismatch"),59 Path: mustBe("DATA"),60 Got: mustContain("int"),61 Expected: mustContain("*td_test.MyStruct"),62 })63 checkError(t, &MyStructBase{},64 td.Struct(&MyStruct{}, td.StructFields{}),65 expectedError{66 Message: mustBe("type mismatch"),67 Path: mustBe("DATA"),68 Got: mustContain("*td_test.MyStructBase"),69 Expected: mustContain("*td_test.MyStruct"),70 })71 checkError(t, &gotStruct,72 td.Struct(&MyStruct{}, td.StructFields{73 "ValBool": false, // ← does not match74 "ValStr": "foobar",75 "ValInt": 123,76 }),77 expectedError{78 Message: mustBe("values differ"),79 Path: mustBe("DATA.ValBool"),80 Got: mustContain("true"),81 Expected: mustContain("false"),82 })83 checkOK(t, &gotStruct,84 td.Struct(&MyStruct{85 MyStructMid: MyStructMid{86 MyStructBase: MyStructBase{87 ValBool: true,88 },89 ValStr: "foobar",90 },91 ValInt: 123,92 }, nil))93 checkError(t, &gotStruct,94 td.Struct(&MyStruct{95 MyStructMid: MyStructMid{96 MyStructBase: MyStructBase{97 ValBool: true,98 },99 ValStr: "foobax", // ← does not match100 },101 ValInt: 123,102 }, nil),103 expectedError{104 Message: mustBe("values differ"),105 Path: mustBe("DATA.ValStr"),106 Got: mustContain("foobar"),107 Expected: mustContain("foobax"),108 })109 // Zero values110 checkOK(t, &MyStruct{},111 td.Struct(&MyStruct{}, td.StructFields{112 "ValBool": false,113 "ValStr": "",114 "ValInt": 0,115 }))116 // nil cases117 checkError(t, nil, td.Struct(&MyStruct{}, nil),118 expectedError{119 Message: mustBe("values differ"),120 Path: mustBe("DATA"),121 Got: mustContain("nil"),122 Expected: mustContain("*td_test.MyStruct"),123 })124 checkError(t, (*MyStruct)(nil), td.Struct(&MyStruct{}, nil),125 expectedError{126 Message: mustBe("values differ"),127 Path: mustBe("DATA"),128 Got: mustContain("nil"),129 Expected: mustBe("non-nil"),130 })131 //132 // Without pointer133 checkOK(t, gotStruct,134 td.Struct(MyStruct{}, td.StructFields{135 "ValBool": true,136 "ValStr": "foobar",137 "ValInt": 123,138 }))139 checkOK(t, gotStruct,140 td.Struct(141 MyStruct{142 MyStructMid: MyStructMid{143 ValStr: "zip",144 },145 ValInt: 666,146 },147 td.StructFields{148 "ValBool": true,149 "> ValStr": "foobar",150 ">ValInt": 123,151 }))152 checkError(t, 123, td.Struct(MyStruct{}, td.StructFields{}),153 expectedError{154 Message: mustBe("type mismatch"),155 Path: mustBe("DATA"),156 Got: mustContain("int"),157 Expected: mustContain("td_test.MyStruct"),158 })159 checkError(t, gotStruct,160 td.Struct(MyStruct{}, td.StructFields{161 "ValBool": false, // ← does not match162 "ValStr": "foobar",163 "ValInt": 123,164 }),165 expectedError{166 Message: mustBe("values differ"),167 Path: mustBe("DATA.ValBool"),168 Got: mustContain("true"),169 Expected: mustContain("false"),170 })171 checkOK(t, gotStruct,172 td.Struct(MyStruct{173 MyStructMid: MyStructMid{174 MyStructBase: MyStructBase{175 ValBool: true,176 },177 ValStr: "foobar",178 },179 ValInt: 123,180 }, nil))181 checkError(t, gotStruct,182 td.Struct(MyStruct{183 MyStructMid: MyStructMid{184 MyStructBase: MyStructBase{185 ValBool: true,186 },187 ValStr: "foobax", // ← does not match188 },189 ValInt: 123,190 }, nil),191 expectedError{192 Message: mustBe("values differ"),193 Path: mustBe("DATA.ValStr"),194 Got: mustContain("foobar"),195 Expected: mustContain("foobax"),196 })197 // Zero values198 checkOK(t, MyStruct{},199 td.Struct(MyStruct{}, td.StructFields{200 "ValBool": false,201 "ValStr": "",202 "ValInt": 0,203 }))204 // nil cases205 checkError(t, nil, td.Struct(MyStruct{}, nil),206 expectedError{207 Message: mustBe("values differ"),208 Path: mustBe("DATA"),209 Got: mustContain("nil"),210 Expected: mustContain("td_test.MyStruct"),211 })212 checkError(t, (*MyStruct)(nil), td.Struct(MyStruct{}, nil),213 expectedError{214 Message: mustBe("type mismatch"),215 Path: mustBe("DATA"),216 Got: mustBe("*td_test.MyStruct"),217 Expected: mustBe("td_test.MyStruct"),218 })219 //220 // Be lax...221 type Struct1 struct {222 name string223 age int224 }225 type Struct2 struct {226 name string227 age int228 }229 // Without Lax → error230 checkError(t,231 Struct1{name: "Bob", age: 42},232 td.Struct(Struct2{name: "Bob", age: 42}, nil),233 expectedError{234 Message: mustBe("type mismatch"),235 })236 // With Lax → OK237 checkOK(t,238 Struct1{name: "Bob", age: 42},239 td.Lax(td.Struct(Struct2{name: "Bob", age: 42}, nil)))240 //241 // IgnoreUnexported242 t.Run("IgnoreUnexported", func(tt *testing.T) {243 type SType struct {244 Public int245 private string246 }247 got := SType{Public: 42, private: "test"}248 expected := td.Struct(SType{Public: 42, private: "zip"}, nil)249 checkError(tt, got, expected,250 expectedError{251 Message: mustBe("values differ"),252 Path: mustBe("DATA.private"),253 Got: mustBe(`"test"`),254 Expected: mustBe(`"zip"`),255 })256 // Ignore unexported globally257 defer func() { td.DefaultContextConfig.IgnoreUnexported = false }()258 td.DefaultContextConfig.IgnoreUnexported = true259 checkOK(tt, got, expected)260 td.DefaultContextConfig.IgnoreUnexported = false261 ttt := test.NewTestingTB(t.Name())262 t := td.NewT(ttt).IgnoreUnexported(SType{}) // ignore only for SType263 test.IsTrue(tt, t.Cmp(got, expected))264 })265 //266 // Bad usage267 checkError(t, "never tested",268 td.Struct("test", nil),269 expectedError{270 Message: mustBe("bad usage of Struct operator"),271 Path: mustBe("DATA"),272 Summary: mustBe("usage: Struct(STRUCT|&STRUCT, EXPECTED_FIELDS), but received string as 1st parameter"),273 })274 i := 12275 checkError(t, "never tested",276 td.Struct(&i, nil),277 expectedError{278 Message: mustBe("bad usage of Struct operator"),279 Path: mustBe("DATA"),280 Summary: mustBe("usage: Struct(STRUCT|&STRUCT, EXPECTED_FIELDS), but received *int (ptr) as 1st parameter"),281 })282 checkError(t, "never tested",283 td.Struct(&MyStruct{}, td.StructFields{"UnknownField": 123}),284 expectedError{285 Message: mustBe("bad usage of Struct operator"),286 Path: mustBe("DATA"),287 Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField"`),288 })289 checkError(t, "never tested",290 td.Struct(&MyStruct{}, td.StructFields{">\tUnknownField": 123}),291 expectedError{292 Message: mustBe("bad usage of Struct operator"),293 Path: mustBe("DATA"),294 Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField" (from ">\tUnknownField")`),295 })296 checkError(t, "never tested",297 td.Struct(&MyStruct{}, td.StructFields{"ValBool": 123}),298 expectedError{299 Message: mustBe("bad usage of Struct operator"),300 Path: mustBe("DATA"),301 Summary: mustBe("type int of field expected value ValBool differs from struct one (bool)"),302 })303 checkError(t, "never tested",304 td.Struct(&MyStruct{}, td.StructFields{">ValBool": 123}),305 expectedError{306 Message: mustBe("bad usage of Struct operator"),307 Path: mustBe("DATA"),308 Summary: mustBe(`type int of field expected value ValBool (from ">ValBool") differs from struct one (bool)`),309 })310 checkError(t, "never tested",311 td.Struct(&MyStruct{}, td.StructFields{"ValBool": nil}),312 expectedError{313 Message: mustBe("bad usage of Struct operator"),314 Path: mustBe("DATA"),315 Summary: mustBe("expected value of field ValBool cannot be nil as it is a bool"),316 })317 checkError(t, "never tested",318 td.Struct(&MyStruct{319 MyStructMid: MyStructMid{320 MyStructBase: MyStructBase{321 ValBool: true,322 },323 },324 },325 td.StructFields{"ValBool": false}),326 expectedError{327 Message: mustBe("bad usage of Struct operator"),328 Path: mustBe("DATA"),329 Summary: mustBe("non zero field ValBool in model already exists in expectedFields"),330 })331 //332 // String333 test.EqualStr(t,334 td.Struct(MyStruct{335 MyStructMid: MyStructMid{336 ValStr: "foobar",337 },338 ValInt: 123,339 },340 td.StructFields{341 "ValBool": false,342 }).String(),343 `Struct(td_test.MyStruct{344 ValBool: false345 ValInt: 123346 ValStr: "foobar"347})`)348 test.EqualStr(t,349 td.Struct(&MyStruct{350 MyStructMid: MyStructMid{351 ValStr: "foobar",352 },353 ValInt: 123,354 },355 td.StructFields{356 "ValBool": false,357 }).String(),358 `Struct(*td_test.MyStruct{359 ValBool: false360 ValInt: 123361 ValStr: "foobar"362})`)363 test.EqualStr(t,364 td.Struct(&MyStruct{}, td.StructFields{}).String(),365 `Struct(*td_test.MyStruct{})`)366 // Erroneous op367 test.EqualStr(t, td.Struct("test", nil).String(), "Struct(<ERROR>)")368}369func TestStructPrivateFields(t *testing.T) {370 type privateKey struct {371 num int372 name string373 }374 type privateValue struct {375 value string376 weight int377 }378 type MyTime time.Time379 type structPrivateFields struct {380 byKey map[privateKey]*privateValue381 name string382 nameb []byte383 err error384 iface any385 properties []int386 birth time.Time387 birth2 MyTime388 next *structPrivateFields389 }390 d := func(rfc3339Date string) (ret time.Time) {391 var err error392 ret, err = time.Parse(time.RFC3339Nano, rfc3339Date)393 if err != nil {394 panic(err)395 }396 return397 }398 got := structPrivateFields{399 byKey: map[privateKey]*privateValue{400 {num: 1, name: "foo"}: {value: "test", weight: 12},401 {num: 2, name: "bar"}: {value: "tset", weight: 23},402 {num: 3, name: "zip"}: {value: "ttse", weight: 34},403 },404 name: "foobar",405 nameb: []byte("foobar"),406 err: errors.New("the error"),407 iface: 1234,408 properties: []int{20, 22, 23, 21},409 birth: d("2018-04-01T10:11:12.123456789Z"),410 birth2: MyTime(d("2018-03-01T09:08:07.987654321Z")),411 next: &structPrivateFields{412 byKey: map[privateKey]*privateValue{},413 name: "sub",414 iface: bytes.NewBufferString("buffer!"),415 birth: d("2018-04-02T10:11:12.123456789Z"),416 birth2: MyTime(d("2018-03-02T09:08:07.987654321Z")),417 },418 }419 checkOK(t, got,420 td.Struct(structPrivateFields{}, td.StructFields{421 "name": "foobar",422 }))423 checkOK(t, got,424 td.Struct(structPrivateFields{}, td.StructFields{425 "name": td.Re("^foo"),426 }))427 checkOK(t, got,428 td.Struct(structPrivateFields{}, td.StructFields{429 "nameb": td.Re("^foo"),430 }))431 checkOKOrPanicIfUnsafeDisabled(t, got,432 td.Struct(structPrivateFields{}, td.StructFields{433 "err": td.Re("error"),434 }))435 checkError(t, got,436 td.Struct(structPrivateFields{}, td.StructFields{437 "iface": td.Re("buffer"),438 }),439 expectedError{440 Message: mustBe("bad type"),441 Path: mustBe("DATA.iface"),442 Got: mustBe("int"),443 Expected: mustBe("string (convertible) OR fmt.Stringer OR error OR []uint8"),444 })445 checkOKOrPanicIfUnsafeDisabled(t, got,446 td.Struct(structPrivateFields{}, td.StructFields{447 "next": td.Struct(&structPrivateFields{}, td.StructFields{448 "iface": td.Re("buffer"),449 }),450 }))451 checkOK(t, got,452 td.Struct(structPrivateFields{}, td.StructFields{453 "properties": []int{20, 22, 23, 21},454 }))455 checkOK(t, got,456 td.Struct(structPrivateFields{}, td.StructFields{457 "properties": td.ArrayEach(td.Between(20, 23)),458 }))459 checkOK(t, got,460 td.Struct(structPrivateFields{}, td.StructFields{461 "byKey": td.MapEach(td.Struct(&privateValue{}, td.StructFields{462 "weight": td.Between(12, 34),463 "value": td.Any(td.HasPrefix("t"), td.HasSuffix("e")),464 })),465 }))466 checkOK(t, got,467 td.Struct(structPrivateFields{}, td.StructFields{468 "byKey": td.SuperMapOf(469 map[privateKey]*privateValue{470 {num: 3, name: "zip"}: {value: "ttse", weight: 34},471 },472 td.MapEntries{473 privateKey{num: 2, name: "bar"}: &privateValue{value: "tset", weight: 23},474 }),475 }))476 expected := td.Struct(structPrivateFields{}, td.StructFields{477 "birth": td.TruncTime(d("2018-04-01T10:11:12Z"), time.Second),478 "birth2": td.TruncTime(MyTime(d("2018-03-01T09:08:07Z")), time.Second),479 })480 if !dark.UnsafeDisabled {481 checkOK(t, got, expected)482 } else {483 checkError(t, got, expected,484 expectedError{485 Message: mustBe("cannot compare"),486 Path: mustBe("DATA.birth"),487 Summary: mustBe("unexported field that cannot be overridden"),488 })489 }490 checkError(t, got,491 td.Struct(structPrivateFields{}, td.StructFields{492 "next": td.Struct(&structPrivateFields{}, td.StructFields{493 "name": "sub",494 "birth": td.Code(func(t time.Time) bool { return true }),495 }),496 }),497 expectedError{498 Message: mustBe("cannot compare unexported field"),499 Path: mustBe("DATA.next.birth"),500 Summary: mustBe("use Code() on surrounding struct instead"),501 })502 checkError(t, got,503 td.Struct(structPrivateFields{}, td.StructFields{504 "next": td.Struct(&structPrivateFields{}, td.StructFields{505 "name": "sub",506 "birth": td.Smuggle(507 func(t time.Time) string { return t.String() },508 "2018-04-01T10:11:12.123456789Z"),509 }),510 }),511 expectedError{512 Message: mustBe("cannot smuggle unexported field"),513 Path: mustBe("DATA.next.birth"),514 Summary: mustBe("work on surrounding struct instead"),515 })516}517func TestStructPatterns(t *testing.T) {518 type paAnon struct {519 alphaNum int520 betaNum int521 }522 type paTest struct {523 paAnon524 Num int525 }526 got := paTest{527 paAnon: paAnon{528 alphaNum: 1000,529 betaNum: 2000,530 },531 Num: 666,532 }533 t.Run("Shell pattern", func(t *testing.T) {534 checkOK(t, got,535 td.Struct(paTest{Num: 666},536 td.StructFields{537 "=*Num": td.Gte(1000), // matches alphaNum & betaNum538 }))539 checkOK(t, got,540 td.Struct(paTest{Num: 666},541 td.StructFields{542 "=a*Num": td.Lt(0), // no remaining fields to match543 "=*": td.Gte(1000), // first, matches alphaNum & betaNum544 "=b*Num": td.Lt(0), // no remaining fields to match545 }),546 "Default sorting uses patterns")547 checkOK(t, got,548 td.Struct(paTest{Num: 666},549 td.StructFields{550 "1 = a*Num": td.Between(999, 1001), // matches alphaNum551 "2 = *": td.Gte(2000), // matches betaNum552 "3 = b*Num": td.Gt(3000), // no remaining fields to match553 }),554 "Explicitly sorted")555 checkOK(t, got,556 td.Struct(paTest{Num: 666},557 td.StructFields{558 "1 ! beta*": 1000, // matches alphaNum559 "2 = *": 2000, // matches betaNum560 }),561 "negative shell pattern")562 checkError(t, "never tested",563 td.Struct(paTest{Num: 666}, td.StructFields{"= al[pha": 123}),564 expectedError{565 Message: mustBe("bad usage of Struct operator"),566 Path: mustBe("DATA"),567 Summary: mustContain("bad shell pattern field `= al[pha`: "),568 })569 checkError(t, "never tested",570 td.Struct(paTest{Num: 666}, td.StructFields{"= alpha*": nil}), expectedError{571 Message: mustBe("bad usage of Struct operator"),572 Path: mustBe("DATA"),573 Summary: mustBe("expected value of field alphaNum (from pattern `= alpha*`) cannot be nil as it is a int"),574 })575 })576 t.Run("Regexp", func(t *testing.T) {577 checkOK(t, got,578 td.Struct(paTest{Num: 666},579 td.StructFields{580 "=~Num$": td.Gte(1000), // matches alphaNum & betaNum581 }))582 checkOK(t, got,583 td.Struct(paTest{Num: 666},584 td.StructFields{585 "=~^a.*Num$": td.Lt(0), // no remaining fields to match586 "=~.": td.Gte(1000), // first, matches alphaNum & betaNum587 "=~^b.*Num$": td.Lt(0), // no remaining fields to match588 }),589 "Default sorting uses patterns")590 checkOK(t, got,591 td.Struct(paTest{Num: 666},592 td.StructFields{593 "1 =~ ^a.*Num$": td.Between(999, 1001), // matches alphaNum594 "2 =~ .": td.Gte(2000), // matches betaNum595 "3 =~ ^b.*Num$": td.Gt(3000), // no remaining fields to match596 }),597 "Explicitly sorted")598 checkOK(t, got,599 td.Struct(paTest{Num: 666},600 td.StructFields{601 "1 !~ ^beta": 1000, // matches alphaNum602 "2 =~ .": 2000, // matches betaNum603 }),604 "negative regexp")605 checkError(t, "never tested",606 td.Struct(paTest{Num: 666}, td.StructFields{"=~ al(*": 123}),607 expectedError{608 Message: mustBe("bad usage of Struct operator"),609 Path: mustBe("DATA"),610 Summary: mustContain("bad regexp field `=~ al(*`: "),611 })612 checkError(t, "never tested",613 td.Struct(paTest{Num: 666}, td.StructFields{"=~ alpha": nil}),614 expectedError{615 Message: mustBe("bad usage of Struct operator"),616 Path: mustBe("DATA"),617 Summary: mustBe("expected value of field alphaNum (from pattern `=~ alpha`) cannot be nil as it is a int"),618 })619 })620}621func TestStructTypeBehind(t *testing.T) {622 equalTypes(t, td.Struct(MyStruct{}, nil), MyStruct{})623 equalTypes(t, td.Struct(&MyStruct{}, nil), &MyStruct{})624 // Erroneous op625 equalTypes(t, td.Struct("test", nil), nil)626}627func TestSStruct(t *testing.T) {628 gotStruct := MyStruct{629 MyStructMid: MyStructMid{630 MyStructBase: MyStructBase{631 ValBool: true,632 },633 ValStr: "foobar",634 },635 ValInt: 123,636 }637 //638 // Using pointer639 checkOK(t, &gotStruct,640 td.SStruct(&MyStruct{}, td.StructFields{641 "ValBool": true,642 "ValStr": "foobar",643 "ValInt": 123,644 // nil Ptr645 }))646 checkOK(t, &gotStruct,647 td.SStruct(648 &MyStruct{649 MyStructMid: MyStructMid{650 ValStr: "zip",651 },652 ValInt: 666,653 },654 td.StructFields{655 "ValBool": true,656 "> ValStr": "foobar",657 ">ValInt": 123,658 }))659 checkOK(t, &gotStruct,660 td.SStruct((*MyStruct)(nil), td.StructFields{661 "ValBool": true,662 "ValStr": "foobar",663 "ValInt": 123,664 // nil Ptr665 }))666 checkError(t, 123,667 td.SStruct(&MyStruct{}, td.StructFields{}),668 expectedError{669 Message: mustBe("type mismatch"),670 Path: mustBe("DATA"),671 Got: mustContain("int"),672 Expected: mustContain("*td_test.MyStruct"),673 })674 checkError(t, &MyStructBase{},675 td.SStruct(&MyStruct{}, td.StructFields{}),676 expectedError{677 Message: mustBe("type mismatch"),678 Path: mustBe("DATA"),679 Got: mustContain("*td_test.MyStructBase"),680 Expected: mustContain("*td_test.MyStruct"),681 })682 checkError(t, &gotStruct,683 td.SStruct(&MyStruct{}, td.StructFields{684 // ValBool false ← does not match685 "ValStr": "foobar",686 "ValInt": 123,687 }),688 expectedError{689 Message: mustBe("values differ"),690 Path: mustBe("DATA.ValBool"),691 Got: mustContain("true"),692 Expected: mustContain("false"),693 })694 checkOK(t, &gotStruct,695 td.SStruct(&MyStruct{696 MyStructMid: MyStructMid{697 MyStructBase: MyStructBase{698 ValBool: true,699 },700 ValStr: "foobar",701 },702 ValInt: 123,703 }, nil))704 checkError(t, &gotStruct,705 td.SStruct(&MyStruct{706 MyStructMid: MyStructMid{707 MyStructBase: MyStructBase{708 ValBool: true,709 },710 ValStr: "foobax", // ← does not match711 },712 ValInt: 123,713 }, nil),714 expectedError{715 Message: mustBe("values differ"),716 Path: mustBe("DATA.ValStr"),717 Got: mustContain("foobar"),718 Expected: mustContain("foobax"),719 })720 // Zero values721 checkOK(t, &MyStruct{}, td.SStruct(&MyStruct{}, nil))722 checkOK(t, &MyStruct{}, td.SStruct(&MyStruct{}, td.StructFields{}))723 // nil cases724 checkError(t, nil, td.SStruct(&MyStruct{}, nil),725 expectedError{726 Message: mustBe("values differ"),727 Path: mustBe("DATA"),728 Got: mustContain("nil"),729 Expected: mustContain("*td_test.MyStruct"),730 })731 checkError(t, (*MyStruct)(nil), td.SStruct(&MyStruct{}, nil),732 expectedError{733 Message: mustBe("values differ"),734 Path: mustBe("DATA"),735 Got: mustContain("nil"),736 Expected: mustBe("non-nil"),737 })738 //739 // Without pointer740 checkOK(t, gotStruct,741 td.SStruct(MyStruct{}, td.StructFields{742 "ValBool": true,743 "ValStr": "foobar",744 "ValInt": 123,745 }))746 checkOK(t, gotStruct,747 td.SStruct(748 MyStruct{749 MyStructMid: MyStructMid{750 ValStr: "zip",751 },752 ValInt: 666,753 },754 td.StructFields{755 "ValBool": true,756 "> ValStr": "foobar",757 ">ValInt": 123,758 }))759 checkError(t, 123, td.SStruct(MyStruct{}, td.StructFields{}),760 expectedError{761 Message: mustBe("type mismatch"),762 Path: mustBe("DATA"),763 Got: mustContain("int"),764 Expected: mustContain("td_test.MyStruct"),765 })766 checkError(t, gotStruct,767 td.SStruct(MyStruct{}, td.StructFields{768 // "ValBool" false ← does not match769 "ValStr": "foobar",770 "ValInt": 123,771 }),772 expectedError{773 Message: mustBe("values differ"),774 Path: mustBe("DATA.ValBool"),775 Got: mustContain("true"),776 Expected: mustContain("false"),777 })778 checkOK(t, gotStruct,779 td.SStruct(MyStruct{780 MyStructMid: MyStructMid{781 MyStructBase: MyStructBase{782 ValBool: true,783 },784 ValStr: "foobar",785 },786 ValInt: 123,787 }, nil))788 checkError(t, gotStruct,789 td.SStruct(MyStruct{790 MyStructMid: MyStructMid{791 MyStructBase: MyStructBase{792 ValBool: true,793 },794 ValStr: "foobax", // ← does not match795 },796 ValInt: 123,797 }, nil),798 expectedError{799 Message: mustBe("values differ"),800 Path: mustBe("DATA.ValStr"),801 Got: mustContain("foobar"),802 Expected: mustContain("foobax"),803 })804 // Zero values805 checkOK(t, MyStruct{}, td.Struct(MyStruct{}, td.StructFields{}))806 checkOK(t, MyStruct{}, td.Struct(MyStruct{}, nil))807 // nil cases808 checkError(t, nil, td.SStruct(MyStruct{}, nil),809 expectedError{810 Message: mustBe("values differ"),811 Path: mustBe("DATA"),812 Got: mustContain("nil"),813 Expected: mustContain("td_test.MyStruct"),814 })815 checkError(t, (*MyStruct)(nil), td.SStruct(MyStruct{}, nil),816 expectedError{817 Message: mustBe("type mismatch"),818 Path: mustBe("DATA"),819 Got: mustBe("*td_test.MyStruct"),820 Expected: mustBe("td_test.MyStruct"),821 })822 //823 // Be lax...824 type Struct1 struct {825 name string826 age int827 }828 type Struct2 struct {829 name string830 age int831 }832 // Without Lax → error833 checkError(t,834 Struct1{name: "Bob", age: 42},835 td.SStruct(Struct2{name: "Bob", age: 42}, nil),836 expectedError{837 Message: mustBe("type mismatch"),838 })839 // With Lax → OK840 checkOK(t,841 Struct1{name: "Bob", age: 42},842 td.Lax(td.SStruct(Struct2{name: "Bob", age: 42}, nil)))843 //844 // IgnoreUnexported845 t.Run("IgnoreUnexported", func(tt *testing.T) {846 type SType struct {847 Public int848 private string849 }850 got := SType{Public: 42, private: "test"}851 expected := td.SStruct(SType{Public: 42}, nil)852 checkError(tt, got, expected,853 expectedError{854 Message: mustBe("values differ"),855 Path: mustBe("DATA.private"),856 Got: mustBe(`"test"`),857 Expected: mustBe(`""`),858 })859 // Ignore unexported globally860 defer func() { td.DefaultContextConfig.IgnoreUnexported = false }()861 td.DefaultContextConfig.IgnoreUnexported = true862 checkOK(tt, got, expected)863 td.DefaultContextConfig.IgnoreUnexported = false864 ttt := test.NewTestingTB(t.Name())865 t := td.NewT(ttt).IgnoreUnexported(SType{}) // ignore only for SType866 test.IsTrue(tt, t.Cmp(got, expected))867 })868 //869 // Bad usage870 checkError(t, "never tested",871 td.SStruct("test", nil),872 expectedError{873 Message: mustBe("bad usage of SStruct operator"),874 Path: mustBe("DATA"),875 Summary: mustBe("usage: SStruct(STRUCT|&STRUCT, EXPECTED_FIELDS), but received string as 1st parameter"),876 })877 i := 12878 checkError(t, "never tested",879 td.SStruct(&i, nil),880 expectedError{881 Message: mustBe("bad usage of SStruct operator"),882 Path: mustBe("DATA"),883 Summary: mustBe("usage: SStruct(STRUCT|&STRUCT, EXPECTED_FIELDS), but received *int (ptr) as 1st parameter"),884 })885 checkError(t, "never tested",886 td.SStruct(&MyStruct{}, td.StructFields{"UnknownField": 123}),887 expectedError{888 Message: mustBe("bad usage of SStruct operator"),889 Path: mustBe("DATA"),890 Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField"`),891 })892 checkError(t, "never tested",893 td.SStruct(&MyStruct{}, td.StructFields{">\tUnknownField": 123}),894 expectedError{895 Message: mustBe("bad usage of SStruct operator"),896 Path: mustBe("DATA"),897 Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField" (from ">\tUnknownField")`),898 })899 checkError(t, "never tested",900 td.SStruct(&MyStruct{}, td.StructFields{"ValBool": 123}),901 expectedError{902 Message: mustBe("bad usage of SStruct operator"),903 Path: mustBe("DATA"),904 Summary: mustBe("type int of field expected value ValBool differs from struct one (bool)"),905 })906 checkError(t, "never tested",907 td.SStruct(&MyStruct{}, td.StructFields{">ValBool": 123}),908 expectedError{909 Message: mustBe("bad usage of SStruct operator"),910 Path: mustBe("DATA"),911 Summary: mustBe(`type int of field expected value ValBool (from ">ValBool") differs from struct one (bool)`),912 })913 checkError(t, "never tested",914 td.SStruct(&MyStruct{}, td.StructFields{"ValBool": nil}),915 expectedError{916 Message: mustBe("bad usage of SStruct operator"),917 Path: mustBe("DATA"),918 Summary: mustBe("expected value of field ValBool cannot be nil as it is a bool"),919 })920 checkError(t, "never tested",921 td.SStruct(&MyStruct{922 MyStructMid: MyStructMid{923 MyStructBase: MyStructBase{924 ValBool: true,925 },926 },927 },928 td.StructFields{"ValBool": false}),929 expectedError{930 Message: mustBe("bad usage of SStruct operator"),931 Path: mustBe("DATA"),932 Summary: mustBe("non zero field ValBool in model already exists in expectedFields"),933 })934 //935 // String936 test.EqualStr(t,937 td.SStruct(MyStruct{938 MyStructMid: MyStructMid{939 ValStr: "foobar",940 },941 ValInt: 123,942 },943 td.StructFields{944 "ValBool": false,945 }).String(),946 `SStruct(td_test.MyStruct{947 Ptr: (*int)(<nil>)948 ValBool: false949 ValInt: 123950 ValStr: "foobar"951})`)952 test.EqualStr(t,953 td.SStruct(&MyStruct{954 MyStructMid: MyStructMid{955 ValStr: "foobar",956 },957 ValInt: 123,958 },959 td.StructFields{960 "ValBool": false,961 }).String(),962 `SStruct(*td_test.MyStruct{963 Ptr: (*int)(<nil>)964 ValBool: false965 ValInt: 123966 ValStr: "foobar"967})`)968 test.EqualStr(t,969 td.SStruct(&MyStruct{}, td.StructFields{}).String(),970 `SStruct(*td_test.MyStruct{971 Ptr: (*int)(<nil>)972 ValBool: false973 ValInt: 0974 ValStr: ""975})`)976 // Erroneous op977 test.EqualStr(t, td.SStruct("test", nil).String(), "SStruct(<ERROR>)")978}979func TestSStructPattern(t *testing.T) {980 // Patterns are already fully tested in TestStructPatterns981 type paAnon struct {982 alphaNum int983 betaNum int984 }985 type paTest struct {986 paAnon987 Num int988 }989 got := paTest{990 paAnon: paAnon{991 alphaNum: 1000,992 betaNum: 2000,993 },994 Num: 666,995 }996 checkOK(t, got,997 td.SStruct(paTest{},998 td.StructFields{999 "=*Num": td.Gte(666), // matches Num, alphaNum & betaNum1000 }))1001 checkOK(t, got,1002 td.SStruct(paTest{},1003 td.StructFields{1004 "=~Num$": td.Gte(666), // matches Num, alphaNum & betaNum1005 }))1006 checkOK(t, paTest{Num: 666},1007 td.SStruct(paTest{},1008 td.StructFields{1009 "=~^Num": 666, // only matches Num1010 // remaining fields are tested as 01011 }))1012}1013func TestSStructTypeBehind(t *testing.T) {1014 equalTypes(t, td.SStruct(MyStruct{}, nil), MyStruct{})1015 equalTypes(t, td.SStruct(&MyStruct{}, nil), &MyStruct{})1016 // Erroneous op1017 equalTypes(t, td.SStruct("test", nil), nil)1018}...

Full Screen

Full Screen

record.go

Source:record.go Github

copy

Full Screen

1package db2import (3 "bytes"4 "html/template"5 "time"6)7type Column struct {8 Name string9 Source string10 Type string11 Description string12}13var (14 ColBezoeknummer = Column{"SEHID", "seh_sehreg.SEHID", "NUMBER", "Uniek Bezoeknummer voor dit bezoek (niet voor de patiënt)"}15 ColMutatieID = Column{"SEHMUTID", "seh_sehmut.SEHMUTID", "NUMBER", "Uniek ID voor deze mutatie"}16 ColLocatie = Column{"Locatie", "seh_sehreg.LOCATIECOD", "STRING", "Code van de locatie"}17 ColAfdeling = Column{"Afdeling", "", "STRING", "Naam van de afdeling, meestal 'seh'"}18 ColAangemeld = Column{"Aangemaakt", "seh_sehreg.DATUM", "DATE", "Datum waarop dit record is aangemaakt"}19 ColBinnenkomstDatum = Column{"BinnenkomstDatum", "seh_sehreg.AANKSDATUM", "STRING", "Datum waarop de patiënt is binnengekomen"}20 ColBinnenkomstTijd = Column{"BinnenkomstTijd", "seh_sehreg.AANKSTIJD", "STRING", "Tijdstip waarop de patiënt is binnengekomen"}21 ColTriageTijd = Column{"TriageTijd", "seh_sehreg.TRIAGETIJD", "STRING", "Tijdstip waarop de triage is afgerond"}22 ColNaarKamerTijd = Column{"NaarKamerTijd", "seh_sehreg.ARTSBHTIJD", "STRING", "Tijdstip waarop de patiënt naar de behandelkamer is gegaan"}23 ColBijArtsTijd = Column{"EersteContactTijd", "seh_sehreg.PATGEZT", "STRING", "Tijdstip waarop de patiënt voor het eerst contact heeft gehad met de behandelend arts"}24 ColArtsKlaarTijd = Column{"ArtsKlaarTijd", "seh_sehref.ARTSKLAARTIJD", "STRING", "Tijdstip waarop de arts volledig klaar is met de behandeling van de patiënt"}25 ColGereedOpnameTijd = Column{"GereedOpnameTijd", "opname_opname.INSCHRTIJD", "STRING", "Tijdstip waarop de patiënt is aangemerkt voor opname"}26 ColVertrekTijd = Column{"VertrekTijd", "seh_sehreg.ARBEHETIJD", "STRING", "Tijdstip waarop de patiënt is vertrokken"}27 ColEindTijd = Column{"EindTijd", "seh_sehreg.eindtijd", "STRING", "Tijdstip waarop het bezoek administratief is afgerond"}28 ColKamer = Column{"Kamer", "seh_sehmut.BEHKAMERCO", "STRING", "Code van de behandelkamer"}29 ColBed = Column{"Bed", "seh_sehmut.BEDNR", "STRING", "Bed nummer"}30 ColIngangsklacht = Column{"Ingangsklacht", "", "STRING", "Ingangsklacht"}31 ColSpecialisme = Column{"Specialisme", "seh_sehreg.SPECIALISM", "STRING", "Code van het specialisme waar de patiënt aan is toegewezen"}32 ColUrgentie = Column{"Triage", "seh_sehreg.TRIANIVCOD", "STRING", "Triage code"}33 ColVervoerder = Column{"Vervoerder", "seh_sehreg.VVCODE", "STRING", "Code van de vervoerder"}34 ColGeboortedatum = Column{"Geboortedatum", "patient_patient.GEBDAT", "DATE", "Geboortedatum van de patient. Deze wordt automatisch omgezet naar een leeftijdscategorie voordat deze verstuurd wordt"}35 ColOpnameAfdeling = Column{"OpnameAfdeling", "opname_opname.AFDELING", "STRING", "Afdeling waar de patiënt is opgenomen"}36 ColOpnameSpecialisme = Column{"OpnameSpecialisme", "opname_opname.SPECIALISM", "STRING", "Specialisme waar de patiënt is opgenomen"}37 ColHerkomst = Column{"Herkomst", "seh_sehreg.VERVOERTYP", "STRING", "Code van de herkomst van de patiënt"}38 ColOntslagbestemming = Column{"OntslagBestemming", "seh_sehreg.BESTEMMING", "STRING", "Code van de ontslagbestemming"}39 ColVervallen = Column{"Vervallen", "seh_sehreg.VERVALL", "NUMBER", "Is dit record vervallen?"}40 ColMutatieEindTijd = Column{"MutatieEindTijd", "seh_sehmut.eindtijd", "STRING", "Eindtijd van deze mutatie"}41 ColMutatieStatus = Column{"MutatieStatus", "seh_sehmut.status", "STRING", "Statuscode van deze mutatie"}42 ColOrderNummer = Column{"ORDERNR", "ORDERNR", "NUMBER", "Uniek order nummer"}43 ColOrderStart = Column{"StartDatumTijd", "STARTDATUMTIJD_order", "TIMESTAMP", "Starttijd van de order"}44 ColOrderEind = Column{"EindDatumTijd", "EINDDATUMTIJD_order", "TIMESTAMP", "Eindtijd van de order (indien beschikbaar)"}45 ColOrderStatus = Column{"Status", "STATUS", "STRING", "Status van de order"}46 ColOrderModule = Column{"Module", "MODULE", "STRING", "Naam van de module"}47 ColOrderSpecialisme = Column{"Specialisme", "RecipientRole", "STRING", "Specialisme voor consult"}48)49var (50 VisitorColumns = []Column{51 ColBezoeknummer,52 ColMutatieID,53 ColLocatie,54 ColAfdeling,55 ColAangemeld,56 ColBinnenkomstDatum,57 ColBinnenkomstTijd,58 ColTriageTijd,59 ColNaarKamerTijd,60 ColBijArtsTijd,61 ColArtsKlaarTijd,62 ColGereedOpnameTijd,63 ColVertrekTijd,64 ColEindTijd,65 ColMutatieEindTijd,66 ColMutatieStatus,67 ColKamer,68 ColBed,69 ColIngangsklacht,70 ColSpecialisme,71 ColUrgentie,72 ColVervoerder,73 ColGeboortedatum,74 ColOpnameAfdeling,75 ColOpnameSpecialisme,76 ColHerkomst,77 ColOntslagbestemming,78 ColVervallen,79 }80 RadiologieColumns = []Column{81 ColBezoeknummer,82 ColOrderNummer,83 ColOrderStatus,84 ColOrderStart,85 ColOrderEind,86 ColOrderModule,87 }88 LabColumns = []Column{89 ColBezoeknummer,90 ColOrderNummer,91 ColOrderStatus,92 ColOrderStart,93 ColOrderEind,94 }95 ConsultColumns = []Column{96 ColBezoeknummer,97 ColOrderNummer,98 ColOrderStatus,99 ColOrderStart,100 ColOrderEind,101 ColOrderSpecialisme,102 }103)104type VisitorRecord struct {105 Bezoeknummer int106 MutatieID int107 Locatie string108 Afdeling string109 Aangemeld time.Time110 BinnenkomstDatum string111 BinnenkomstTijd string112 TriageTijd string113 NaarKamerTijd string114 BijArtsTijd string115 ArtsKlaarTijd string116 GereedOpnameTijd string117 VertrekTijd string118 EindTijd string119 MutatieEindTijd string120 Mutatiestatus string121 Kamer string122 Bed string123 Ingangsklacht string124 Specialisme string125 Urgentie string126 Vervoerder string127 Geboortedatum time.Time128 OpnameAfdeling string129 OpnameSpecialisme string130 Herkomst string131 Ontslagbestemming string132 Vervallen bool133}134func maxCount(length int) int {135 if length > 10 {136 return 10137 }138 return length139}140type VisitorRecords []VisitorRecord141func (v VisitorRecords) AsTable() template.HTML {142 var buf bytes.Buffer143 if err := visitorTableTmpl.Execute(&buf, struct {144 Columns []Column145 QueryResults []VisitorRecord146 }{VisitorColumns, v[:maxCount(len(v))]}); err != nil {147 panic(err)148 }149 return template.HTML(buf.String())150}151var visitorTableTmpl = template.Must(template.New("table").Parse(`152<table class="table">153 <thead>154 <tr>155 {{ range $i, $row := .Columns }}156 <th>{{ $row.Name }}</th>157 {{ end }}158 </tr>159 </thead>160 <tbody>161 {{ range $index, $row := .QueryResults }}162 <tr>163 <td>{{ $row.Bezoeknummer }}</td>164 <td>{{ $row.MutatieID }}</td>165 <td>{{ $row.Locatie }}</td>166 <td>{{ $row.Afdeling }}</td>167 <td>{{ $row.Aangemeld }}</td>168 <td>{{ $row.BinnenkomstDatum }}</td>169 <td>{{ $row.BinnenkomstTijd }}</td>170 <td>{{ $row.TriageTijd }}</td>171 <td>{{ $row.NaarKamerTijd }}</td>172 <td>{{ $row.BijArtsTijd }}</td>173 <td>{{ $row.ArtsKlaarTijd }}</td>174 <td>{{ $row.GereedOpnameTijd }}</td>175 <td>{{ $row.VertrekTijd }}</td>176 <td>{{ $row.EindTijd }}</td>177 <td>{{ $row.MutatieEindTijd }}</td>178 <td>{{ $row.Mutatiestatus }}</td>179 <td>{{ $row.Kamer }}</td>180 <td>{{ $row.Bed }}</td>181 <td>{{ $row.Ingangsklacht }}</td>182 <td>{{ $row.Specialisme }}</td>183 <td>{{ $row.Urgentie }}</td>184 <td>{{ $row.Vervoerder }}</td>185 <td>{{ $row.Geboortedatum }}</td>186 <td>{{ $row.OpnameAfdeling }}</td>187 <td>{{ $row.OpnameSpecialisme }}</td>188 <td>{{ $row.Herkomst }}</td>189 <td>{{ $row.Ontslagbestemming }}</td>190 <td>{{ $row.Vervallen }}</td>191 </tr>192 {{ end }}193 </tbody>194</table>195`))196type RadiologieOrder struct {197 Bezoeknummer int198 Ordernummer int199 Status string200 Start *time.Time201 Eind *time.Time202 Module string203}204type RadiologieOrders []RadiologieOrder205func (r RadiologieOrders) AsTable() template.HTML {206 var buf bytes.Buffer207 if err := radiologieTableTmpl.Execute(&buf, struct {208 Columns []Column209 QueryResults []RadiologieOrder210 }{RadiologieColumns, r[:maxCount(len(r))]}); err != nil {211 panic(err)212 }213 return template.HTML(buf.String())214}215var radiologieTableTmpl = template.Must(template.New("table").Parse(`216<table class="table">217 <thead>218 <tr>219 {{ range $i, $row := .Columns }}220 <th>{{ $row.Name }}</th>221 {{ end }}222 </tr>223 </thead>224 <tbody>225 {{ range $index, $row := .QueryResults }}226 <tr>227 <td>{{ $row.Bezoeknummer }}</td>228 <td>{{ $row.Ordernummer }}</td>229 <td>{{ $row.Status }}</td>230 <td>{{ $row.Start }}</td>231 <td>{{ $row.Eind }}</td>232 <td>{{ $row.Module }}</td>233 </tr>234 {{ end }}235 </tbody>236</table>237`))238type LabOrder struct {239 Bezoeknummer int240 Ordernummer int241 Status string242 Start *time.Time243 Eind *time.Time244}245type LabOrders []LabOrder246func (r LabOrders) AsTable() template.HTML {247 var buf bytes.Buffer248 if err := labTableTmpl.Execute(&buf, struct {249 Columns []Column250 QueryResults []LabOrder251 }{LabColumns, r[:maxCount(len(r))]}); err != nil {252 panic(err)253 }254 return template.HTML(buf.String())255}256var labTableTmpl = template.Must(template.New("table").Parse(`257<table class="table">258 <thead>259 <tr>260 {{ range $i, $row := .Columns }}261 <th>{{ $row.Name }}</th>262 {{ end }}263 </tr>264 </thead>265 <tbody>266 {{ range $index, $row := .QueryResults }}267 <tr>268 <td>{{ $row.Bezoeknummer }}</td>269 <td>{{ $row.Ordernummer }}</td>270 <td>{{ $row.Status }}</td>271 <td>{{ $row.Start }}</td>272 <td>{{ $row.Eind }}</td>273 </tr>274 {{ end }}275 </tbody>276</table>277`))278type ConsultOrder struct {279 Bezoeknummer int280 Ordernummer int281 Status string282 Start *time.Time283 Eind *time.Time284 Specialisme string285}286type ConsultOrders []ConsultOrder287func (r ConsultOrders) AsTable() template.HTML {288 var buf bytes.Buffer289 if err := consultTableTmpl.Execute(&buf, struct {290 Columns []Column291 QueryResults []ConsultOrder292 }{ConsultColumns, r[:maxCount(len(r))]}); err != nil {293 panic(err)294 }295 return template.HTML(buf.String())296}297var consultTableTmpl = template.Must(template.New("table").Parse(`298<table class="table">299 <thead>300 <tr>301 {{ range $i, $row := .Columns }}302 <th>{{ $row.Name }}</th>303 {{ end }}304 </tr>305 </thead>306 <tbody>307 {{ range $index, $row := .QueryResults }}308 <tr>309 <td>{{ $row.Bezoeknummer }}</td>310 <td>{{ $row.Ordernummer }}</td>311 <td>{{ $row.Status }}</td>312 <td>{{ $row.Start }}</td>313 <td>{{ $row.Eind }}</td>314 <td>{{ $row.Specialisme }}</td>315 </tr>316 {{ end }}317 </tbody>318</table>319`))...

Full Screen

Full Screen

types_test.go

Source:types_test.go Github

copy

Full Screen

...41 _, _ = copyType(newCyclicalType(2), nil)42 })43 t.Run("identity", func(t *testing.T) {44 u16 := &Int{Size: 2}45 out, _ := copyType(&Struct{46 Members: []Member{47 {Name: "a", Type: u16},48 {Name: "b", Type: u16},49 },50 }, nil)51 outStruct := out.(*Struct)52 qt.Assert(t, outStruct.Members[0].Type, qt.Equals, outStruct.Members[1].Type)53 })54}55// The following are valid Types.56//57// There currently is no better way to document which58// types implement an interface.59func ExampleType_validTypes() {60 var _ Type = &Void{}61 var _ Type = &Int{}62 var _ Type = &Pointer{}63 var _ Type = &Array{}64 var _ Type = &Struct{}65 var _ Type = &Union{}66 var _ Type = &Enum{}67 var _ Type = &Fwd{}68 var _ Type = &Typedef{}69 var _ Type = &Volatile{}70 var _ Type = &Const{}71 var _ Type = &Restrict{}72 var _ Type = &Func{}73 var _ Type = &FuncProto{}74 var _ Type = &Var{}75 var _ Type = &Datasec{}76}77func TestType(t *testing.T) {78 types := []func() Type{79 func() Type { return &Void{} },80 func() Type { return &Int{Size: 2, Bits: 3} },81 func() Type { return &Pointer{Target: &Void{}} },82 func() Type { return &Array{Type: &Int{}} },83 func() Type {84 return &Struct{85 Members: []Member{{Type: &Void{}}},86 }87 },88 func() Type {89 return &Union{90 Members: []Member{{Type: &Void{}}},91 }92 },93 func() Type { return &Enum{} },94 func() Type { return &Fwd{Name: "thunk"} },95 func() Type { return &Typedef{Type: &Void{}} },96 func() Type { return &Volatile{Type: &Void{}} },97 func() Type { return &Const{Type: &Void{}} },98 func() Type { return &Restrict{Type: &Void{}} },99 func() Type { return &Func{Name: "foo", Type: &Void{}} },100 func() Type {101 return &FuncProto{102 Params: []FuncParam{{Name: "bar", Type: &Void{}}},103 Return: &Void{},104 }105 },106 func() Type { return &Var{Type: &Void{}} },107 func() Type {108 return &Datasec{109 Vars: []VarSecinfo{{Type: &Void{}}},110 }111 },112 }113 compareTypes := cmp.Comparer(func(a, b *Type) bool {114 return a == b115 })116 for _, fn := range types {117 typ := fn()118 t.Run(fmt.Sprintf("%T", typ), func(t *testing.T) {119 t.Logf("%v", typ)120 if typ == typ.copy() {121 t.Error("Copy doesn't copy")122 }123 var first, second typeDeque124 typ.walk(&first)125 typ.walk(&second)126 if diff := cmp.Diff(first.all(), second.all(), compareTypes); diff != "" {127 t.Errorf("Walk mismatch (-want +got):\n%s", diff)128 }129 })130 }131}132func TestTypeDeque(t *testing.T) {133 a, b := new(Type), new(Type)134 t.Run("pop", func(t *testing.T) {135 var td typeDeque136 td.push(a)137 td.push(b)138 if td.pop() != b {139 t.Error("Didn't pop b first")140 }141 if td.pop() != a {142 t.Error("Didn't pop a second")143 }144 if td.pop() != nil {145 t.Error("Didn't pop nil")146 }147 })148 t.Run("shift", func(t *testing.T) {149 var td typeDeque150 td.push(a)151 td.push(b)152 if td.shift() != a {153 t.Error("Didn't shift a second")154 }155 if td.shift() != b {156 t.Error("Didn't shift b first")157 }158 if td.shift() != nil {159 t.Error("Didn't shift nil")160 }161 })162 t.Run("push", func(t *testing.T) {163 var td typeDeque164 td.push(a)165 td.push(b)166 td.shift()167 ts := make([]Type, 12)168 for i := range ts {169 td.push(&ts[i])170 }171 if td.shift() != b {172 t.Error("Didn't shift b first")173 }174 for i := range ts {175 if td.shift() != &ts[i] {176 t.Fatal("Shifted wrong Type at pos", i)177 }178 }179 })180 t.Run("all", func(t *testing.T) {181 var td typeDeque182 td.push(a)183 td.push(b)184 all := td.all()185 if len(all) != 2 {186 t.Fatal("Expected 2 elements, got", len(all))187 }188 if all[0] != a || all[1] != b {189 t.Fatal("Elements don't match")190 }191 })192}193func newCyclicalType(n int) Type {194 ptr := &Pointer{}195 prev := Type(ptr)196 for i := 0; i < n; i++ {197 switch i % 5 {198 case 0:199 prev = &Struct{200 Members: []Member{201 {Type: prev},202 },203 }204 case 1:205 prev = &Const{Type: prev}206 case 2:207 prev = &Volatile{Type: prev}208 case 3:209 prev = &Typedef{Type: prev}210 case 4:211 prev = &Array{Type: prev}212 }213 }...

Full Screen

Full Screen

Struct

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(t.a, t.b, t.c)4 t.print()5 fmt.Println(t.a, t.b, t.c)6}7import "fmt"8func main() {9 fmt.Println(t.a, t.b, t.c)10 t.print()11 fmt.Println(t.a, t.b, t.c)12}13import "fmt"14func main() {15 fmt.Println(t.a, t.b, t.c)16 t.print()17 fmt.Println(t.a, t.b, t.c)18}19import "fmt"20func main() {21 fmt.Println(t.a, t.b, t.c)22 t.print()23 fmt.Println(t.a, t.b, t.c)24}25import "fmt"26func main() {27 fmt.Println(t.a, t.b, t.c)28 t.print()29 fmt.Println(t.a, t.b, t.c)30}31import "fmt"32func main() {33 fmt.Println(t.a, t.b, t.c)34 t.print()35 fmt.Println(t.a, t.b, t.c)36}

Full Screen

Full Screen

Struct

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Struct

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Struct

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Struct

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t := td.New(1, 2, 3)4 fmt.Println(t)5}6import "fmt"7type Td struct {8}9func New(a, b, c int) *Td {10 return &Td{a, b, c}11}12func (t *Td) String() string {13 return fmt.Sprintf("a: %d, b: %d, c: %d", t.a, t.b, t.c)14}15type Td struct {16}17func (t *Td) String() string {18 return fmt.Sprintf("a: %d, b: %d, c: %d", t.a, t.b, t.c)19}20t := Td{1, 2, 3}21t := &Td{1, 2, 3}22t := Td{1, 2, 3}

Full Screen

Full Screen

Struct

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t := td.New()4 t.Set(1, 2, 3)5 t.Display()6 t.Set(10, 20, 30)7 t.Display()8 fmt.Println("t = ", t)9 t1 := td.New()10 t1.Set(100, 200, 300)11 t1.Display()12 t1.Set(1000, 2000, 3000)13 t1.Display()14 fmt.Println("t1 = ", t1)15}16import (17func main() {18 t.Set(1, 2, 3)19 t.Display()20 t.Set(10, 20, 30)21 t.Display()22 fmt.Println("t = ", t)23 t1.Set(100, 200, 300)24 t1.Display()25 t1.Set(1000, 2000, 3000)26 t1.Display()27 fmt.Println("t1 = ", t1)28}29import (30func main() {31 t.Set(1, 2, 3)32 t.Display()33 t.Set(10, 20, 30)34 t.Display()35 fmt.Println("t = ", t)36 t1.Set(100, 200, 300)37 t1.Display()38 t1.Set(1000, 2000, 3000)39 t1.Display()40 fmt.Println("t1 = ", t

Full Screen

Full Screen

Struct

Using AI Code Generation

copy

Full Screen

1import (2type td struct {3}4func main() {5 p := td{6 }7 p.print()8}9func (p td) print() {10 fmt.Println(p.name, p.age)

Full Screen

Full Screen

Struct

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, World!")4 td.print()5}6import "fmt"7type td struct {8}9func (td *td) print() {10 fmt.Println(td.name)11 fmt.Println(td.age)12}13import "fmt"14func main() {15 fmt.Println("Hello, World!")16 td.print()17}18import "fmt"19func main() {20 fmt.Println("Hello, World!")21 td.print()22}23import "fmt"24func main() {25 fmt.Println("Hello, World!")26 td.print()27}28import "fmt"29func main() {30 fmt.Println("Hello, World!")31 td.print()32}33import "fmt"34func main() {35 fmt.Println("Hello, World!")36 td.print()37}38import "fmt"39func main() {40 fmt.Println("Hello, World!")41 td.print()42}43import "fmt"44func main() {45 fmt.Println("Hello, World!")

Full Screen

Full Screen

Struct

Using AI Code Generation

copy

Full Screen

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

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