Best Go-testdeep code snippet using test.Errorf
util_test.go
Source:util_test.go
...60 f := func(t *testing.T, imp func([]string) []string, in, want []string) {61 t.Helper()62 out := imp(in)63 if !reflect.DeepEqual(out, want) {64 t.Errorf("incorrect output:")65 t.Errorf(" input: %#v", in)66 t.Errorf(" expected: %#v", want)67 t.Errorf(" got: %#v", out)68 }69 }70 for _, testCase := range firstUniqueStringsTestCases {71 t.Run("list", func(t *testing.T) {72 f(t, firstUniqueStringsList, testCase.in, testCase.out)73 })74 t.Run("map", func(t *testing.T) {75 f(t, firstUniqueStringsMap, testCase.in, testCase.out)76 })77 }78}79var lastUniqueStringsTestCases = []struct {80 in []string81 out []string82}{83 {84 in: []string{"a"},85 out: []string{"a"},86 },87 {88 in: []string{"a", "b"},89 out: []string{"a", "b"},90 },91 {92 in: []string{"a", "a"},93 out: []string{"a"},94 },95 {96 in: []string{"a", "b", "a"},97 out: []string{"b", "a"},98 },99 {100 in: []string{"b", "a", "a"},101 out: []string{"b", "a"},102 },103 {104 in: []string{"a", "a", "b"},105 out: []string{"a", "b"},106 },107 {108 in: []string{"a", "b", "a", "b"},109 out: []string{"a", "b"},110 },111 {112 in: []string{"liblog", "libdl", "libc++", "libdl", "libc", "libm"},113 out: []string{"liblog", "libc++", "libdl", "libc", "libm"},114 },115}116func TestLastUniqueStrings(t *testing.T) {117 for _, testCase := range lastUniqueStringsTestCases {118 out := LastUniqueStrings(testCase.in)119 if !reflect.DeepEqual(out, testCase.out) {120 t.Errorf("incorrect output:")121 t.Errorf(" input: %#v", testCase.in)122 t.Errorf(" expected: %#v", testCase.out)123 t.Errorf(" got: %#v", out)124 }125 }126}127func TestJoinWithPrefix(t *testing.T) {128 testcases := []struct {129 name string130 input []string131 expected string132 }{133 {134 name: "zero_inputs",135 input: []string{},136 expected: "",137 },138 {139 name: "one_input",140 input: []string{"a"},141 expected: "prefix:a",142 },143 {144 name: "two_inputs",145 input: []string{"a", "b"},146 expected: "prefix:a prefix:b",147 },148 }149 prefix := "prefix:"150 for _, testCase := range testcases {151 t.Run(testCase.name, func(t *testing.T) {152 out := JoinWithPrefix(testCase.input, prefix)153 if out != testCase.expected {154 t.Errorf("incorrect output:")155 t.Errorf(" input: %#v", testCase.input)156 t.Errorf(" prefix: %#v", prefix)157 t.Errorf(" expected: %#v", testCase.expected)158 t.Errorf(" got: %#v", out)159 }160 })161 }162}163func TestIndexList(t *testing.T) {164 input := []string{"a", "b", "c"}165 testcases := []struct {166 key string167 expected int168 }{169 {170 key: "a",171 expected: 0,172 },173 {174 key: "b",175 expected: 1,176 },177 {178 key: "c",179 expected: 2,180 },181 {182 key: "X",183 expected: -1,184 },185 }186 for _, testCase := range testcases {187 t.Run(testCase.key, func(t *testing.T) {188 out := IndexList(testCase.key, input)189 if out != testCase.expected {190 t.Errorf("incorrect output:")191 t.Errorf(" key: %#v", testCase.key)192 t.Errorf(" input: %#v", input)193 t.Errorf(" expected: %#v", testCase.expected)194 t.Errorf(" got: %#v", out)195 }196 })197 }198}199func TestInList(t *testing.T) {200 input := []string{"a"}201 testcases := []struct {202 key string203 expected bool204 }{205 {206 key: "a",207 expected: true,208 },209 {210 key: "X",211 expected: false,212 },213 }214 for _, testCase := range testcases {215 t.Run(testCase.key, func(t *testing.T) {216 out := InList(testCase.key, input)217 if out != testCase.expected {218 t.Errorf("incorrect output:")219 t.Errorf(" key: %#v", testCase.key)220 t.Errorf(" input: %#v", input)221 t.Errorf(" expected: %#v", testCase.expected)222 t.Errorf(" got: %#v", out)223 }224 })225 }226}227func TestPrefixInList(t *testing.T) {228 prefixes := []string{"a", "b"}229 testcases := []struct {230 str string231 expected bool232 }{233 {234 str: "a-example",235 expected: true,236 },237 {238 str: "b-example",239 expected: true,240 },241 {242 str: "X-example",243 expected: false,244 },245 }246 for _, testCase := range testcases {247 t.Run(testCase.str, func(t *testing.T) {248 out := HasAnyPrefix(testCase.str, prefixes)249 if out != testCase.expected {250 t.Errorf("incorrect output:")251 t.Errorf(" str: %#v", testCase.str)252 t.Errorf(" prefixes: %#v", prefixes)253 t.Errorf(" expected: %#v", testCase.expected)254 t.Errorf(" got: %#v", out)255 }256 })257 }258}259func TestFilterList(t *testing.T) {260 input := []string{"a", "b", "c", "c", "b", "d", "a"}261 filter := []string{"a", "c"}262 remainder, filtered := FilterList(input, filter)263 expected := []string{"b", "b", "d"}264 if !reflect.DeepEqual(remainder, expected) {265 t.Errorf("incorrect remainder output:")266 t.Errorf(" input: %#v", input)267 t.Errorf(" filter: %#v", filter)268 t.Errorf(" expected: %#v", expected)269 t.Errorf(" got: %#v", remainder)270 }271 expected = []string{"a", "c", "c", "a"}272 if !reflect.DeepEqual(filtered, expected) {273 t.Errorf("incorrect filtered output:")274 t.Errorf(" input: %#v", input)275 t.Errorf(" filter: %#v", filter)276 t.Errorf(" expected: %#v", expected)277 t.Errorf(" got: %#v", filtered)278 }279}280func TestFilterListPred(t *testing.T) {281 pred := func(s string) bool { return strings.HasPrefix(s, "a/") }282 AssertArrayString(t, "filter", FilterListPred([]string{"a/c", "b/a", "a/b"}, pred), []string{"a/c", "a/b"})283 AssertArrayString(t, "filter", FilterListPred([]string{"b/c", "a/a", "b/b"}, pred), []string{"a/a"})284 AssertArrayString(t, "filter", FilterListPred([]string{"c/c", "b/a", "c/b"}, pred), []string{})285 AssertArrayString(t, "filter", FilterListPred([]string{"a/c", "a/a", "a/b"}, pred), []string{"a/c", "a/a", "a/b"})286}287func TestRemoveListFromList(t *testing.T) {288 input := []string{"a", "b", "c", "d", "a", "c", "d"}289 filter := []string{"a", "c"}290 expected := []string{"b", "d", "d"}291 out := RemoveListFromList(input, filter)292 if !reflect.DeepEqual(out, expected) {293 t.Errorf("incorrect output:")294 t.Errorf(" input: %#v", input)295 t.Errorf(" filter: %#v", filter)296 t.Errorf(" expected: %#v", expected)297 t.Errorf(" got: %#v", out)298 }299}300func TestRemoveFromList(t *testing.T) {301 testcases := []struct {302 name string303 key string304 input []string305 expectedFound bool306 expectedOut []string307 }{308 {309 name: "remove_one_match",310 key: "a",311 input: []string{"a", "b", "c"},312 expectedFound: true,313 expectedOut: []string{"b", "c"},314 },315 {316 name: "remove_three_matches",317 key: "a",318 input: []string{"a", "b", "a", "c", "a"},319 expectedFound: true,320 expectedOut: []string{"b", "c"},321 },322 {323 name: "remove_zero_matches",324 key: "X",325 input: []string{"a", "b", "a", "c", "a"},326 expectedFound: false,327 expectedOut: []string{"a", "b", "a", "c", "a"},328 },329 {330 name: "remove_all_matches",331 key: "a",332 input: []string{"a", "a", "a", "a"},333 expectedFound: true,334 expectedOut: []string{},335 },336 }337 for _, testCase := range testcases {338 t.Run(testCase.name, func(t *testing.T) {339 found, out := RemoveFromList(testCase.key, testCase.input)340 if found != testCase.expectedFound {341 t.Errorf("incorrect output:")342 t.Errorf(" key: %#v", testCase.key)343 t.Errorf(" input: %#v", testCase.input)344 t.Errorf(" expected: %#v", testCase.expectedFound)345 t.Errorf(" got: %#v", found)346 }347 if !reflect.DeepEqual(out, testCase.expectedOut) {348 t.Errorf("incorrect output:")349 t.Errorf(" key: %#v", testCase.key)350 t.Errorf(" input: %#v", testCase.input)351 t.Errorf(" expected: %#v", testCase.expectedOut)352 t.Errorf(" got: %#v", out)353 }354 })355 }356}357func ExampleCopyOf() {358 a := []string{"1", "2", "3"}359 b := CopyOf(a)360 a[0] = "-1"361 fmt.Printf("a = %q\n", a)362 fmt.Printf("b = %q\n", b)363 // Output:364 // a = ["-1" "2" "3"]365 // b = ["1" "2" "3"]366}367func ExampleCopyOf_append() {368 a := make([]string, 1, 2)369 a[0] = "foo"370 fmt.Println("Without CopyOf:")371 b := append(a, "bar")372 c := append(a, "baz")373 fmt.Printf("a = %q\n", a)374 fmt.Printf("b = %q\n", b)375 fmt.Printf("c = %q\n", c)376 a = make([]string, 1, 2)377 a[0] = "foo"378 fmt.Println("With CopyOf:")379 b = append(CopyOf(a), "bar")380 c = append(CopyOf(a), "baz")381 fmt.Printf("a = %q\n", a)382 fmt.Printf("b = %q\n", b)383 fmt.Printf("c = %q\n", c)384 // Output:385 // Without CopyOf:386 // a = ["foo"]387 // b = ["foo" "baz"]388 // c = ["foo" "baz"]389 // With CopyOf:390 // a = ["foo"]391 // b = ["foo" "bar"]392 // c = ["foo" "baz"]393}394func TestSplitFileExt(t *testing.T) {395 t.Run("soname with version", func(t *testing.T) {396 root, suffix, ext := SplitFileExt("libtest.so.1.0.30")397 expected := "libtest"398 if root != expected {399 t.Errorf("root should be %q but got %q", expected, root)400 }401 expected = ".so.1.0.30"402 if suffix != expected {403 t.Errorf("suffix should be %q but got %q", expected, suffix)404 }405 expected = ".so"406 if ext != expected {407 t.Errorf("ext should be %q but got %q", expected, ext)408 }409 })410 t.Run("soname with svn version", func(t *testing.T) {411 root, suffix, ext := SplitFileExt("libtest.so.1svn")412 expected := "libtest"413 if root != expected {414 t.Errorf("root should be %q but got %q", expected, root)415 }416 expected = ".so.1svn"417 if suffix != expected {418 t.Errorf("suffix should be %q but got %q", expected, suffix)419 }420 expected = ".so"421 if ext != expected {422 t.Errorf("ext should be %q but got %q", expected, ext)423 }424 })425 t.Run("version numbers in the middle should be ignored", func(t *testing.T) {426 root, suffix, ext := SplitFileExt("libtest.1.0.30.so")427 expected := "libtest.1.0.30"428 if root != expected {429 t.Errorf("root should be %q but got %q", expected, root)430 }431 expected = ".so"432 if suffix != expected {433 t.Errorf("suffix should be %q but got %q", expected, suffix)434 }435 expected = ".so"436 if ext != expected {437 t.Errorf("ext should be %q but got %q", expected, ext)438 }439 })440 t.Run("no known file extension", func(t *testing.T) {441 root, suffix, ext := SplitFileExt("test.exe")442 expected := "test"443 if root != expected {444 t.Errorf("root should be %q but got %q", expected, root)445 }446 expected = ".exe"447 if suffix != expected {448 t.Errorf("suffix should be %q but got %q", expected, suffix)449 }450 if ext != expected {451 t.Errorf("ext should be %q but got %q", expected, ext)452 }453 })454}455func Test_Shard(t *testing.T) {456 type args struct {457 strings []string458 shardSize int459 }460 tests := []struct {461 name string462 args args463 want [][]string464 }{465 {466 name: "empty",467 args: args{468 strings: nil,469 shardSize: 1,470 },471 want: [][]string(nil),472 },473 {474 name: "single shard",475 args: args{476 strings: []string{"a", "b"},477 shardSize: 2,478 },479 want: [][]string{{"a", "b"}},480 },481 {482 name: "single short shard",483 args: args{484 strings: []string{"a", "b"},485 shardSize: 3,486 },487 want: [][]string{{"a", "b"}},488 },489 {490 name: "shard per input",491 args: args{492 strings: []string{"a", "b", "c"},493 shardSize: 1,494 },495 want: [][]string{{"a"}, {"b"}, {"c"}},496 },497 {498 name: "balanced shards",499 args: args{500 strings: []string{"a", "b", "c", "d"},501 shardSize: 2,502 },503 want: [][]string{{"a", "b"}, {"c", "d"}},504 },505 {506 name: "unbalanced shards",507 args: args{508 strings: []string{"a", "b", "c"},509 shardSize: 2,510 },511 want: [][]string{{"a", "b"}, {"c"}},512 },513 }514 for _, tt := range tests {515 t.Run(tt.name, func(t *testing.T) {516 t.Run("strings", func(t *testing.T) {517 if got := ShardStrings(tt.args.strings, tt.args.shardSize); !reflect.DeepEqual(got, tt.want) {518 t.Errorf("ShardStrings(%v, %v) = %v, want %v",519 tt.args.strings, tt.args.shardSize, got, tt.want)520 }521 })522 t.Run("paths", func(t *testing.T) {523 stringsToPaths := func(strings []string) Paths {524 if strings == nil {525 return nil526 }527 paths := make(Paths, len(strings))528 for i, s := range strings {529 paths[i] = PathForTesting(s)530 }531 return paths532 }533 paths := stringsToPaths(tt.args.strings)534 var want []Paths535 if sWant := tt.want; sWant != nil {536 want = make([]Paths, len(sWant))537 for i, w := range sWant {538 want[i] = stringsToPaths(w)539 }540 }541 if got := ShardPaths(paths, tt.args.shardSize); !reflect.DeepEqual(got, want) {542 t.Errorf("ShardPaths(%v, %v) = %v, want %v",543 paths, tt.args.shardSize, got, want)544 }545 })546 })547 }548}549func BenchmarkFirstUniqueStrings(b *testing.B) {550 implementations := []struct {551 name string552 f func([]string) []string553 }{554 {555 name: "list",556 f: firstUniqueStringsList,557 },558 {559 name: "map",560 f: firstUniqueStringsMap,561 },562 {563 name: "optimal",564 f: FirstUniqueStrings,565 },566 }567 const maxSize = 1024568 uniqueStrings := make([]string, maxSize)569 for i := range uniqueStrings {570 uniqueStrings[i] = strconv.Itoa(i)571 }572 sameString := make([]string, maxSize)573 for i := range sameString {574 sameString[i] = uniqueStrings[0]575 }576 f := func(b *testing.B, imp func([]string) []string, s []string) {577 for i := 0; i < b.N; i++ {578 b.ReportAllocs()579 s = append([]string(nil), s...)580 imp(s)581 }582 }583 for n := 1; n <= maxSize; n <<= 1 {584 b.Run(strconv.Itoa(n), func(b *testing.B) {585 for _, implementation := range implementations {586 b.Run(implementation.name, func(b *testing.B) {587 b.Run("same", func(b *testing.B) {588 f(b, implementation.f, sameString[:n])589 })590 b.Run("unique", func(b *testing.B) {591 f(b, implementation.f, uniqueStrings[:n])592 })593 })594 }595 })596 }597}598func TestSortedStringKeys(t *testing.T) {599 testCases := []struct {600 name string601 in interface{}602 expected []string603 }{604 {605 name: "nil",606 in: map[string]string(nil),607 expected: nil,608 },609 {610 name: "empty",611 in: map[string]string{},612 expected: nil,613 },614 {615 name: "simple",616 in: map[string]string{"a": "foo", "b": "bar"},617 expected: []string{"a", "b"},618 },619 {620 name: "interface values",621 in: map[string]interface{}{"a": nil, "b": nil},622 expected: []string{"a", "b"},623 },624 }625 for _, tt := range testCases {626 t.Run(tt.name, func(t *testing.T) {627 got := SortedStringKeys(tt.in)628 if g, w := got, tt.expected; !reflect.DeepEqual(g, w) {629 t.Errorf("wanted %q, got %q", w, g)630 }631 })632 }633}634func TestSortedStringValues(t *testing.T) {635 testCases := []struct {636 name string637 in interface{}638 expected []string639 }{640 {641 name: "nil",642 in: map[string]string(nil),643 expected: nil,644 },645 {646 name: "empty",647 in: map[string]string{},648 expected: nil,649 },650 {651 name: "simple",652 in: map[string]string{"foo": "a", "bar": "b"},653 expected: []string{"a", "b"},654 },655 {656 name: "duplicates",657 in: map[string]string{"foo": "a", "bar": "b", "baz": "b"},658 expected: []string{"a", "b", "b"},659 },660 }661 for _, tt := range testCases {662 t.Run(tt.name, func(t *testing.T) {663 got := SortedStringValues(tt.in)664 if g, w := got, tt.expected; !reflect.DeepEqual(g, w) {665 t.Errorf("wanted %q, got %q", w, g)666 }667 })668 }669}670func TestSortedUniqueStringValues(t *testing.T) {671 testCases := []struct {672 name string673 in interface{}674 expected []string675 }{676 {677 name: "nil",678 in: map[string]string(nil),679 expected: nil,680 },681 {682 name: "empty",683 in: map[string]string{},684 expected: nil,685 },686 {687 name: "simple",688 in: map[string]string{"foo": "a", "bar": "b"},689 expected: []string{"a", "b"},690 },691 {692 name: "duplicates",693 in: map[string]string{"foo": "a", "bar": "b", "baz": "b"},694 expected: []string{"a", "b"},695 },696 }697 for _, tt := range testCases {698 t.Run(tt.name, func(t *testing.T) {699 got := SortedUniqueStringValues(tt.in)700 if g, w := got, tt.expected; !reflect.DeepEqual(g, w) {701 t.Errorf("wanted %q, got %q", w, g)702 }703 })704 }705}...
js_test.go
Source:js_test.go
...41func TestBool(t *testing.T) {42 want := true43 o := dummys.Get("someBool")44 if got := o.Bool(); got != want {45 t.Errorf("got %#v, want %#v", got, want)46 }47 dummys.Set("otherBool", want)48 if got := dummys.Get("otherBool").Bool(); got != want {49 t.Errorf("got %#v, want %#v", got, want)50 }51 if !dummys.Get("someBool").Equal(dummys.Get("someBool")) {52 t.Errorf("same value not equal")53 }54}55func TestString(t *testing.T) {56 want := "abc\u1234"57 o := dummys.Get("someString")58 if got := o.String(); got != want {59 t.Errorf("got %#v, want %#v", got, want)60 }61 dummys.Set("otherString", want)62 if got := dummys.Get("otherString").String(); got != want {63 t.Errorf("got %#v, want %#v", got, want)64 }65 if !dummys.Get("someString").Equal(dummys.Get("someString")) {66 t.Errorf("same value not equal")67 }68 if got, want := js.Undefined().String(), "<undefined>"; got != want {69 t.Errorf("got %#v, want %#v", got, want)70 }71 if got, want := js.Null().String(), "<null>"; got != want {72 t.Errorf("got %#v, want %#v", got, want)73 }74 if got, want := js.ValueOf(true).String(), "<boolean: true>"; got != want {75 t.Errorf("got %#v, want %#v", got, want)76 }77 if got, want := js.ValueOf(42.5).String(), "<number: 42.5>"; got != want {78 t.Errorf("got %#v, want %#v", got, want)79 }80 if got, want := js.Global().Call("Symbol").String(), "<symbol>"; got != want {81 t.Errorf("got %#v, want %#v", got, want)82 }83 if got, want := js.Global().String(), "<object>"; got != want {84 t.Errorf("got %#v, want %#v", got, want)85 }86 if got, want := js.Global().Get("setTimeout").String(), "<function>"; got != want {87 t.Errorf("got %#v, want %#v", got, want)88 }89}90func TestInt(t *testing.T) {91 want := 4292 o := dummys.Get("someInt")93 if got := o.Int(); got != want {94 t.Errorf("got %#v, want %#v", got, want)95 }96 dummys.Set("otherInt", want)97 if got := dummys.Get("otherInt").Int(); got != want {98 t.Errorf("got %#v, want %#v", got, want)99 }100 if !dummys.Get("someInt").Equal(dummys.Get("someInt")) {101 t.Errorf("same value not equal")102 }103 if got := dummys.Get("zero").Int(); got != 0 {104 t.Errorf("got %#v, want %#v", got, 0)105 }106}107func TestIntConversion(t *testing.T) {108 testIntConversion(t, 0)109 testIntConversion(t, 1)110 testIntConversion(t, -1)111 testIntConversion(t, 1<<20)112 testIntConversion(t, -1<<20)113 testIntConversion(t, 1<<40)114 testIntConversion(t, -1<<40)115 testIntConversion(t, 1<<60)116 testIntConversion(t, -1<<60)117}118func testIntConversion(t *testing.T, want int) {119 if got := js.ValueOf(want).Int(); got != want {120 t.Errorf("got %#v, want %#v", got, want)121 }122}123func TestFloat(t *testing.T) {124 want := 42.123125 o := dummys.Get("someFloat")126 if got := o.Float(); got != want {127 t.Errorf("got %#v, want %#v", got, want)128 }129 dummys.Set("otherFloat", want)130 if got := dummys.Get("otherFloat").Float(); got != want {131 t.Errorf("got %#v, want %#v", got, want)132 }133 if !dummys.Get("someFloat").Equal(dummys.Get("someFloat")) {134 t.Errorf("same value not equal")135 }136}137func TestObject(t *testing.T) {138 if !dummys.Get("someArray").Equal(dummys.Get("someArray")) {139 t.Errorf("same value not equal")140 }141 // An object and its prototype should not be equal.142 proto := js.Global().Get("Object").Get("prototype")143 o := js.Global().Call("eval", "new Object()")144 if proto.Equal(o) {145 t.Errorf("object equals to its prototype")146 }147}148func TestFrozenObject(t *testing.T) {149 o := js.Global().Call("eval", "(function () { let o = new Object(); o.field = 5; Object.freeze(o); return o; })()")150 want := 5151 if got := o.Get("field").Int(); want != got {152 t.Errorf("got %#v, want %#v", got, want)153 }154}155func TestEqual(t *testing.T) {156 if !dummys.Get("someFloat").Equal(dummys.Get("someFloat")) {157 t.Errorf("same float is not equal")158 }159 if !dummys.Get("emptyObj").Equal(dummys.Get("emptyObj")) {160 t.Errorf("same object is not equal")161 }162 if dummys.Get("someFloat").Equal(dummys.Get("someInt")) {163 t.Errorf("different values are not unequal")164 }165}166func TestNaN(t *testing.T) {167 if !dummys.Get("NaN").IsNaN() {168 t.Errorf("JS NaN is not NaN")169 }170 if !js.ValueOf(math.NaN()).IsNaN() {171 t.Errorf("Go NaN is not NaN")172 }173 if dummys.Get("NaN").Equal(dummys.Get("NaN")) {174 t.Errorf("NaN is equal to NaN")175 }176}177func TestUndefined(t *testing.T) {178 if !js.Undefined().IsUndefined() {179 t.Errorf("undefined is not undefined")180 }181 if !js.Undefined().Equal(js.Undefined()) {182 t.Errorf("undefined is not equal to undefined")183 }184 if dummys.IsUndefined() {185 t.Errorf("object is undefined")186 }187 if js.Undefined().IsNull() {188 t.Errorf("undefined is null")189 }190 if dummys.Set("test", js.Undefined()); !dummys.Get("test").IsUndefined() {191 t.Errorf("could not set undefined")192 }193}194func TestNull(t *testing.T) {195 if !js.Null().IsNull() {196 t.Errorf("null is not null")197 }198 if !js.Null().Equal(js.Null()) {199 t.Errorf("null is not equal to null")200 }201 if dummys.IsNull() {202 t.Errorf("object is null")203 }204 if js.Null().IsUndefined() {205 t.Errorf("null is undefined")206 }207 if dummys.Set("test", js.Null()); !dummys.Get("test").IsNull() {208 t.Errorf("could not set null")209 }210 if dummys.Set("test", nil); !dummys.Get("test").IsNull() {211 t.Errorf("could not set nil")212 }213}214func TestLength(t *testing.T) {215 if got := dummys.Get("someArray").Length(); got != 3 {216 t.Errorf("got %#v, want %#v", got, 3)217 }218}219func TestGet(t *testing.T) {220 // positive cases get tested per type221 expectValueError(t, func() {222 dummys.Get("zero").Get("badField")223 })224}225func TestSet(t *testing.T) {226 // positive cases get tested per type227 expectValueError(t, func() {228 dummys.Get("zero").Set("badField", 42)229 })230}231func TestDelete(t *testing.T) {232 dummys.Set("test", 42)233 dummys.Delete("test")234 if dummys.Call("hasOwnProperty", "test").Bool() {235 t.Errorf("property still exists")236 }237 expectValueError(t, func() {238 dummys.Get("zero").Delete("badField")239 })240}241func TestIndex(t *testing.T) {242 if got := dummys.Get("someArray").Index(1).Int(); got != 42 {243 t.Errorf("got %#v, want %#v", got, 42)244 }245 expectValueError(t, func() {246 dummys.Get("zero").Index(1)247 })248}249func TestSetIndex(t *testing.T) {250 dummys.Get("someArray").SetIndex(2, 99)251 if got := dummys.Get("someArray").Index(2).Int(); got != 99 {252 t.Errorf("got %#v, want %#v", got, 99)253 }254 expectValueError(t, func() {255 dummys.Get("zero").SetIndex(2, 99)256 })257}258func TestCall(t *testing.T) {259 var i int64 = 40260 if got := dummys.Call("add", i, 2).Int(); got != 42 {261 t.Errorf("got %#v, want %#v", got, 42)262 }263 if got := dummys.Call("add", js.Global().Call("eval", "40"), 2).Int(); got != 42 {264 t.Errorf("got %#v, want %#v", got, 42)265 }266 expectPanic(t, func() {267 dummys.Call("zero")268 })269 expectValueError(t, func() {270 dummys.Get("zero").Call("badMethod")271 })272}273func TestInvoke(t *testing.T) {274 var i int64 = 40275 if got := dummys.Get("add").Invoke(i, 2).Int(); got != 42 {276 t.Errorf("got %#v, want %#v", got, 42)277 }278 expectValueError(t, func() {279 dummys.Get("zero").Invoke()280 })281}282func TestNew(t *testing.T) {283 if got := js.Global().Get("Array").New(42).Length(); got != 42 {284 t.Errorf("got %#v, want %#v", got, 42)285 }286 expectValueError(t, func() {287 dummys.Get("zero").New()288 })289}290func TestInstanceOf(t *testing.T) {291 someArray := js.Global().Get("Array").New()292 if got, want := someArray.InstanceOf(js.Global().Get("Array")), true; got != want {293 t.Errorf("got %#v, want %#v", got, want)294 }295 if got, want := someArray.InstanceOf(js.Global().Get("Function")), false; got != want {296 t.Errorf("got %#v, want %#v", got, want)297 }298}299func TestType(t *testing.T) {300 if got, want := js.Undefined().Type(), js.TypeUndefined; got != want {301 t.Errorf("got %s, want %s", got, want)302 }303 if got, want := js.Null().Type(), js.TypeNull; got != want {304 t.Errorf("got %s, want %s", got, want)305 }306 if got, want := js.ValueOf(true).Type(), js.TypeBoolean; got != want {307 t.Errorf("got %s, want %s", got, want)308 }309 if got, want := js.ValueOf(0).Type(), js.TypeNumber; got != want {310 t.Errorf("got %s, want %s", got, want)311 }312 if got, want := js.ValueOf(42).Type(), js.TypeNumber; got != want {313 t.Errorf("got %s, want %s", got, want)314 }315 if got, want := js.ValueOf("test").Type(), js.TypeString; got != want {316 t.Errorf("got %s, want %s", got, want)317 }318 if got, want := js.Global().Get("Symbol").Invoke("test").Type(), js.TypeSymbol; got != want {319 t.Errorf("got %s, want %s", got, want)320 }321 if got, want := js.Global().Get("Array").New().Type(), js.TypeObject; got != want {322 t.Errorf("got %s, want %s", got, want)323 }324 if got, want := js.Global().Get("Array").Type(), js.TypeFunction; got != want {325 t.Errorf("got %s, want %s", got, want)326 }327}328type object = map[string]interface{}329type array = []interface{}330func TestValueOf(t *testing.T) {331 a := js.ValueOf(array{0, array{0, 42, 0}, 0})332 if got := a.Index(1).Index(1).Int(); got != 42 {333 t.Errorf("got %v, want %v", got, 42)334 }335 o := js.ValueOf(object{"x": object{"y": 42}})336 if got := o.Get("x").Get("y").Int(); got != 42 {337 t.Errorf("got %v, want %v", got, 42)338 }339}340func TestZeroValue(t *testing.T) {341 var v js.Value342 if !v.IsUndefined() {343 t.Error("zero js.Value is not js.Undefined()")344 }345}346func TestFuncOf(t *testing.T) {347 c := make(chan struct{})348 cb := js.FuncOf(func(this js.Value, args []js.Value) interface{} {349 if got := args[0].Int(); got != 42 {350 t.Errorf("got %#v, want %#v", got, 42)351 }352 c <- struct{}{}353 return nil354 })355 defer cb.Release()356 js.Global().Call("setTimeout", cb, 0, 42)357 <-c358}359func TestInvokeFunction(t *testing.T) {360 called := false361 cb := js.FuncOf(func(this js.Value, args []js.Value) interface{} {362 cb2 := js.FuncOf(func(this js.Value, args []js.Value) interface{} {363 called = true364 return 42365 })366 defer cb2.Release()367 return cb2.Invoke()368 })369 defer cb.Release()370 if got := cb.Invoke().Int(); got != 42 {371 t.Errorf("got %#v, want %#v", got, 42)372 }373 if !called {374 t.Error("function not called")375 }376}377func TestInterleavedFunctions(t *testing.T) {378 c1 := make(chan struct{})379 c2 := make(chan struct{})380 js.Global().Get("setTimeout").Invoke(js.FuncOf(func(this js.Value, args []js.Value) interface{} {381 c1 <- struct{}{}382 <-c2383 return nil384 }), 0)385 <-c1386 c2 <- struct{}{}387 // this goroutine is running, but the callback of setTimeout did not return yet, invoke another function now388 f := js.FuncOf(func(this js.Value, args []js.Value) interface{} {389 return nil390 })391 f.Invoke()392}393func ExampleFuncOf() {394 var cb js.Func395 cb = js.FuncOf(func(this js.Value, args []js.Value) interface{} {396 fmt.Println("button clicked")397 cb.Release() // release the function if the button will not be clicked again398 return nil399 })400 js.Global().Get("document").Call("getElementById", "myButton").Call("addEventListener", "click", cb)401}402// See403// - https://developer.mozilla.org/en-US/docs/Glossary/Truthy404// - https://stackoverflow.com/questions/19839952/all-falsey-values-in-javascript/19839953#19839953405// - http://www.ecma-international.org/ecma-262/5.1/#sec-9.2406func TestTruthy(t *testing.T) {407 want := true408 for _, key := range []string{409 "someBool", "someString", "someInt", "someFloat", "someArray", "someDate",410 "stringZero", // "0" is truthy411 "add", // functions are truthy412 "emptyObj", "emptyArray", "Infinity", "NegInfinity",413 // All objects are truthy, even if they're Number(0) or Boolean(false).414 "objNumber0", "objBooleanFalse",415 } {416 if got := dummys.Get(key).Truthy(); got != want {417 t.Errorf("%s: got %#v, want %#v", key, got, want)418 }419 }420 want = false421 if got := dummys.Get("zero").Truthy(); got != want {422 t.Errorf("got %#v, want %#v", got, want)423 }424 if got := dummys.Get("NaN").Truthy(); got != want {425 t.Errorf("got %#v, want %#v", got, want)426 }427 if got := js.ValueOf("").Truthy(); got != want {428 t.Errorf("got %#v, want %#v", got, want)429 }430 if got := js.Null().Truthy(); got != want {431 t.Errorf("got %#v, want %#v", got, want)432 }433 if got := js.Undefined().Truthy(); got != want {434 t.Errorf("got %#v, want %#v", got, want)435 }436}437func expectValueError(t *testing.T, fn func()) {438 defer func() {439 err := recover()440 if _, ok := err.(*js.ValueError); !ok {441 t.Errorf("expected *js.ValueError, got %T", err)442 }443 }()444 fn()445}446func expectPanic(t *testing.T, fn func()) {447 defer func() {448 err := recover()449 if err == nil {450 t.Errorf("expected panic")451 }452 }()453 fn()454}455var copyTests = []struct {456 srcLen int457 dstLen int458 copyLen int459}{460 {5, 3, 3},461 {3, 5, 3},462 {0, 0, 0},463}464func TestCopyBytesToGo(t *testing.T) {465 for _, tt := range copyTests {466 t.Run(fmt.Sprintf("%d-to-%d", tt.srcLen, tt.dstLen), func(t *testing.T) {467 src := js.Global().Get("Uint8Array").New(tt.srcLen)468 if tt.srcLen >= 2 {469 src.SetIndex(1, 42)470 }471 dst := make([]byte, tt.dstLen)472 if got, want := js.CopyBytesToGo(dst, src), tt.copyLen; got != want {473 t.Errorf("copied %d, want %d", got, want)474 }475 if tt.dstLen >= 2 {476 if got, want := int(dst[1]), 42; got != want {477 t.Errorf("got %d, want %d", got, want)478 }479 }480 })481 }482}483func TestCopyBytesToJS(t *testing.T) {484 for _, tt := range copyTests {485 t.Run(fmt.Sprintf("%d-to-%d", tt.srcLen, tt.dstLen), func(t *testing.T) {486 src := make([]byte, tt.srcLen)487 if tt.srcLen >= 2 {488 src[1] = 42489 }490 dst := js.Global().Get("Uint8Array").New(tt.dstLen)491 if got, want := js.CopyBytesToJS(dst, src), tt.copyLen; got != want {492 t.Errorf("copied %d, want %d", got, want)493 }494 if tt.dstLen >= 2 {495 if got, want := dst.Index(1).Int(), 42; got != want {496 t.Errorf("got %d, want %d", got, want)497 }498 }499 })500 }501}502func TestGarbageCollection(t *testing.T) {503 before := js.JSGo.Get("_values").Length()504 for i := 0; i < 1000; i++ {505 _ = js.Global().Get("Object").New().Call("toString").String()506 runtime.GC()507 }508 after := js.JSGo.Get("_values").Length()509 if after-before > 500 {510 t.Errorf("garbage collection ineffective")511 }512}513// BenchmarkDOM is a simple benchmark which emulates a webapp making DOM operations.514// It creates a div, and sets its id. Then searches by that id and sets some data.515// Finally it removes that div.516func BenchmarkDOM(b *testing.B) {517 document := js.Global().Get("document")518 if document.IsUndefined() {519 b.Skip("Not a browser environment. Skipping.")520 }521 const data = "someString"522 for i := 0; i < b.N; i++ {523 div := document.Call("createElement", "div")524 div.Call("setAttribute", "id", "myDiv")525 document.Get("body").Call("appendChild", div)526 myDiv := document.Call("getElementById", "myDiv")527 myDiv.Set("innerHTML", data)528 if got, want := myDiv.Get("innerHTML").String(), data; got != want {529 b.Errorf("got %s, want %s", got, want)530 }531 document.Get("body").Call("removeChild", div)532 }533}534func TestGlobal(t *testing.T) {535 ident := js.FuncOf(func(this js.Value, args []js.Value) interface{} {536 return args[0]537 })538 defer ident.Release()539 if got := ident.Invoke(js.Global()); !got.Equal(js.Global()) {540 t.Errorf("got %#v, want %#v", got, js.Global())541 }542}...
rewriteCond_test.go
Source:rewriteCond_test.go
...74// var +/- const75func testAddConst64(t *testing.T) {76 if x64+11 < 0 {77 } else {78 t.Errorf("'%#x + 11 < 0' failed", x64)79 }80 if x64+13 <= 0 {81 } else {82 t.Errorf("'%#x + 13 <= 0' failed", x64)83 }84 if y64-11 > 0 {85 } else {86 t.Errorf("'%#x - 11 > 0' failed", y64)87 }88 if y64-13 >= 0 {89 } else {90 t.Errorf("'%#x - 13 >= 0' failed", y64)91 }92 if x64+19 > 0 {93 t.Errorf("'%#x + 19 > 0' failed", x64)94 }95 if x64+23 >= 0 {96 t.Errorf("'%#x + 23 >= 0' failed", x64)97 }98 if y64-19 < 0 {99 t.Errorf("'%#x - 19 < 0' failed", y64)100 }101 if y64-23 <= 0 {102 t.Errorf("'%#x - 23 <= 0' failed", y64)103 }104}105// 32-bit var +/- const106func testAddConst32(t *testing.T) {107 if x32+11 < 0 {108 } else {109 t.Errorf("'%#x + 11 < 0' failed", x32)110 }111 if x32+13 <= 0 {112 } else {113 t.Errorf("'%#x + 13 <= 0' failed", x32)114 }115 if y32-11 > 0 {116 } else {117 t.Errorf("'%#x - 11 > 0' failed", y32)118 }119 if y32-13 >= 0 {120 } else {121 t.Errorf("'%#x - 13 >= 0' failed", y32)122 }123 if x32+19 > 0 {124 t.Errorf("'%#x + 19 > 0' failed", x32)125 }126 if x32+23 >= 0 {127 t.Errorf("'%#x + 23 >= 0' failed", x32)128 }129 if y32-19 < 0 {130 t.Errorf("'%#x - 19 < 0' failed", y32)131 }132 if y32-23 <= 0 {133 t.Errorf("'%#x - 23 <= 0' failed", y32)134 }135}136// var + var137func testAddVar64(t *testing.T) {138 if x64+v64 < 0 {139 } else {140 t.Errorf("'%#x + %#x < 0' failed", x64, v64)141 }142 if x64+v64 <= 0 {143 } else {144 t.Errorf("'%#x + %#x <= 0' failed", x64, v64)145 }146 if y64+v64_n > 0 {147 } else {148 t.Errorf("'%#x + %#x > 0' failed", y64, v64_n)149 }150 if y64+v64_n >= 0 {151 } else {152 t.Errorf("'%#x + %#x >= 0' failed", y64, v64_n)153 }154 if x64+v64 > 0 {155 t.Errorf("'%#x + %#x > 0' failed", x64, v64)156 }157 if x64+v64 >= 0 {158 t.Errorf("'%#x + %#x >= 0' failed", x64, v64)159 }160 if y64+v64_n < 0 {161 t.Errorf("'%#x + %#x < 0' failed", y64, v64_n)162 }163 if y64+v64_n <= 0 {164 t.Errorf("'%#x + %#x <= 0' failed", y64, v64_n)165 }166}167// 32-bit var+var168func testAddVar32(t *testing.T) {169 if x32+v32 < 0 {170 } else {171 t.Errorf("'%#x + %#x < 0' failed", x32, v32)172 }173 if x32+v32 <= 0 {174 } else {175 t.Errorf("'%#x + %#x <= 0' failed", x32, v32)176 }177 if y32+v32_n > 0 {178 } else {179 t.Errorf("'%#x + %#x > 0' failed", y32, v32_n)180 }181 if y32+v32_n >= 0 {182 } else {183 t.Errorf("'%#x + %#x >= 0' failed", y32, v32_n)184 }185 if x32+v32 > 0 {186 t.Errorf("'%#x + %#x > 0' failed", x32, v32)187 }188 if x32+v32 >= 0 {189 t.Errorf("'%#x + %#x >= 0' failed", x32, v32)190 }191 if y32+v32_n < 0 {192 t.Errorf("'%#x + %#x < 0' failed", y32, v32_n)193 }194 if y32+v32_n <= 0 {195 t.Errorf("'%#x + %#x <= 0' failed", y32, v32_n)196 }197}198// multiply-add199func testMAddVar64(t *testing.T) {200 if x64+v64*one64 < 0 {201 } else {202 t.Errorf("'%#x + %#x*1 < 0' failed", x64, v64)203 }204 if x64+v64*one64 <= 0 {205 } else {206 t.Errorf("'%#x + %#x*1 <= 0' failed", x64, v64)207 }208 if y64+v64_n*one64 > 0 {209 } else {210 t.Errorf("'%#x + %#x*1 > 0' failed", y64, v64_n)211 }212 if y64+v64_n*one64 >= 0 {213 } else {214 t.Errorf("'%#x + %#x*1 >= 0' failed", y64, v64_n)215 }216 if x64+v64*one64 > 0 {217 t.Errorf("'%#x + %#x*1 > 0' failed", x64, v64)218 }219 if x64+v64*one64 >= 0 {220 t.Errorf("'%#x + %#x*1 >= 0' failed", x64, v64)221 }222 if y64+v64_n*one64 < 0 {223 t.Errorf("'%#x + %#x*1 < 0' failed", y64, v64_n)224 }225 if y64+v64_n*one64 <= 0 {226 t.Errorf("'%#x + %#x*1 <= 0' failed", y64, v64_n)227 }228}229// 32-bit multiply-add230func testMAddVar32(t *testing.T) {231 if x32+v32*one32 < 0 {232 } else {233 t.Errorf("'%#x + %#x*1 < 0' failed", x32, v32)234 }235 if x32+v32*one32 <= 0 {236 } else {237 t.Errorf("'%#x + %#x*1 <= 0' failed", x32, v32)238 }239 if y32+v32_n*one32 > 0 {240 } else {241 t.Errorf("'%#x + %#x*1 > 0' failed", y32, v32_n)242 }243 if y32+v32_n*one32 >= 0 {244 } else {245 t.Errorf("'%#x + %#x*1 >= 0' failed", y32, v32_n)246 }247 if x32+v32*one32 > 0 {248 t.Errorf("'%#x + %#x*1 > 0' failed", x32, v32)249 }250 if x32+v32*one32 >= 0 {251 t.Errorf("'%#x + %#x*1 >= 0' failed", x32, v32)252 }253 if y32+v32_n*one32 < 0 {254 t.Errorf("'%#x + %#x*1 < 0' failed", y32, v32_n)255 }256 if y32+v32_n*one32 <= 0 {257 t.Errorf("'%#x + %#x*1 <= 0' failed", y32, v32_n)258 }259}260// multiply-sub261func testMSubVar64(t *testing.T) {262 if x64-v64_n*one64 < 0 {263 } else {264 t.Errorf("'%#x - %#x*1 < 0' failed", x64, v64_n)265 }266 if x64-v64_n*one64 <= 0 {267 } else {268 t.Errorf("'%#x - %#x*1 <= 0' failed", x64, v64_n)269 }270 if y64-v64*one64 > 0 {271 } else {272 t.Errorf("'%#x - %#x*1 > 0' failed", y64, v64)273 }274 if y64-v64*one64 >= 0 {275 } else {276 t.Errorf("'%#x - %#x*1 >= 0' failed", y64, v64)277 }278 if x64-v64_n*one64 > 0 {279 t.Errorf("'%#x - %#x*1 > 0' failed", x64, v64_n)280 }281 if x64-v64_n*one64 >= 0 {282 t.Errorf("'%#x - %#x*1 >= 0' failed", x64, v64_n)283 }284 if y64-v64*one64 < 0 {285 t.Errorf("'%#x - %#x*1 < 0' failed", y64, v64)286 }287 if y64-v64*one64 <= 0 {288 t.Errorf("'%#x - %#x*1 <= 0' failed", y64, v64)289 }290 if x64-x64b*one64 < 0 {291 t.Errorf("'%#x - %#x*1 < 0' failed", x64, x64b)292 }293 if x64-x64b*one64 >= 0 {294 } else {295 t.Errorf("'%#x - %#x*1 >= 0' failed", x64, x64b)296 }297}298// 32-bit multiply-sub299func testMSubVar32(t *testing.T) {300 if x32-v32_n*one32 < 0 {301 } else {302 t.Errorf("'%#x - %#x*1 < 0' failed", x32, v32_n)303 }304 if x32-v32_n*one32 <= 0 {305 } else {306 t.Errorf("'%#x - %#x*1 <= 0' failed", x32, v32_n)307 }308 if y32-v32*one32 > 0 {309 } else {310 t.Errorf("'%#x - %#x*1 > 0' failed", y32, v32)311 }312 if y32-v32*one32 >= 0 {313 } else {314 t.Errorf("'%#x - %#x*1 >= 0' failed", y32, v32)315 }316 if x32-v32_n*one32 > 0 {317 t.Errorf("'%#x - %#x*1 > 0' failed", x32, v32_n)318 }319 if x32-v32_n*one32 >= 0 {320 t.Errorf("'%#x - %#x*1 >= 0' failed", x32, v32_n)321 }322 if y32-v32*one32 < 0 {323 t.Errorf("'%#x - %#x*1 < 0' failed", y32, v32)324 }325 if y32-v32*one32 <= 0 {326 t.Errorf("'%#x - %#x*1 <= 0' failed", y32, v32)327 }328 if x32-x32b*one32 < 0 {329 t.Errorf("'%#x - %#x*1 < 0' failed", x32, x32b)330 }331 if x32-x32b*one32 >= 0 {332 } else {333 t.Errorf("'%#x - %#x*1 >= 0' failed", x32, x32b)334 }335}336// 32-bit ADDshift, pick up 1~2 scenarios randomly for each condition337func testAddShift32(t *testing.T) {338 if x32+v32<<1 < 0 {339 } else {340 t.Errorf("'%#x + %#x<<%#x < 0' failed", x32, v32, 1)341 }342 if x32+v32>>1 <= 0 {343 } else {344 t.Errorf("'%#x + %#x>>%#x <= 0' failed", x32, v32, 1)345 }346 if x32+int32(uv32>>1) > 0 {347 t.Errorf("'%#x + int32(%#x>>%#x) > 0' failed", x32, uv32, 1)348 }349 if x32+v32<<uz >= 0 {350 t.Errorf("'%#x + %#x<<%#x >= 0' failed", x32, v32, uz)351 }352 if x32+v32>>uz > 0 {353 t.Errorf("'%#x + %#x>>%#x > 0' failed", x32, v32, uz)354 }355 if x32+int32(uv32>>uz) < 0 {356 } else {357 t.Errorf("'%#x + int32(%#x>>%#x) < 0' failed", x32, uv32, uz)358 }359}360// 32-bit SUBshift, pick up 1~2 scenarios randomly for each condition361func testSubShift32(t *testing.T) {362 if y32-v32<<1 > 0 {363 } else {364 t.Errorf("'%#x - %#x<<%#x > 0' failed", y32, v32, 1)365 }366 if y32-v32>>1 < 0 {367 t.Errorf("'%#x - %#x>>%#x < 0' failed", y32, v32, 1)368 }369 if y32-int32(uv32>>1) >= 0 {370 } else {371 t.Errorf("'%#x - int32(%#x>>%#x) >= 0' failed", y32, uv32, 1)372 }373 if y32-v32<<uz < 0 {374 t.Errorf("'%#x - %#x<<%#x < 0' failed", y32, v32, uz)375 }376 if y32-v32>>uz >= 0 {377 } else {378 t.Errorf("'%#x - %#x>>%#x >= 0' failed", y32, v32, uz)379 }380 if y32-int32(uv32>>uz) <= 0 {381 t.Errorf("'%#x - int32(%#x>>%#x) <= 0' failed", y32, uv32, uz)382 }383}384var rnd = rand.New(rand.NewSource(0))385var sink int64386func benchSoloJump(b *testing.B) {387 r1 := x64388 r2 := x64b389 r3 := x64c390 r4 := y64391 d := rnd.Int63n(10)392 // 6 out 10 conditions evaluate to true393 for i := 0; i < b.N; i++ {394 if r1+r2 < 0 {395 d *= 2...
Errorf
Using AI Code Generation
1func TestErrorf(t *testing.T) {2 t.Errorf("This is an error")3}4func TestFail(t *testing.T) {5 t.Fail()6}7func TestFailNow(t *testing.T) {8 t.FailNow()9}10func TestFatal(t *testing.T) {11 t.Fatal("This is a fatal error")12}13func TestFatalf(t *testing.T) {14 t.Fatalf("This is a fatal error")15}16func TestLog(t *testing.T) {17 t.Log("This is a log")18}19func TestLogf(t *testing.T) {20 t.Logf("This is a log")21}22func TestSkip(t *testing.T) {23 t.Skip("This test is skipped")24}25func TestSkipf(t *testing.T) {26 t.Skipf("This test is skipped")27}28func TestSkipNow(t *testing.T) {29 t.SkipNow()30}31func TestSkipNowf(t *testing.T) {32 t.SkipNow()33}34func TestSkipf(t *testing.T) {35 t.Skipf("This test is skipped")36}37func TestSkipNowf(t *testing.T) {38 t.SkipNow()39}40func TestSkipNow(t *testing.T) {41 t.SkipNow()42}
Errorf
Using AI Code Generation
1func TestErrorf(t *testing.T) {2 t.Errorf("Errorf method")3}4func TestFailNow(t *testing.T) {5 t.FailNow()6}7func TestFatal(t *testing.T) {8 t.Fatal("Fatal method")9}10func TestLog(t *testing.T) {11 t.Log("Log method")12}13func TestLogf(t *testing.T) {14 t.Logf("Logf method")15}16func TestSkip(t *testing.T) {17 t.Skip("Skip method")18}19func TestSkipNow(t *testing.T) {20 t.SkipNow()21}22func TestSkipf(t *testing.T) {23 t.Skipf("Skipf method")24}25func TestHelper(t *testing.T) {26 t.Helper()27}28func TestParallel(t *testing.T) {29 t.Parallel()30}31func TestRun(t *testing.T) {32 t.Run("subtest", func(t *testing.T) {33 t.Log("Run method")34 })35}36func TestCleanup(t *testing.T) {37 t.Cleanup(func() {38 t.Log("Cleanup method")39 })40}41func TestName(t *testing.T) {42 t.Log(t.Name())43}44func TestTempDir(t *testing.T) {45 t.Log(t.TempDir())46}47func TestError(t *
Errorf
Using AI Code Generation
1func TestErrorf(t *testing.T) {2 t.Errorf("This is an error message")3}4--- FAIL: TestErrorf (0.00s)5FailNow()6func TestFailNow(t *testing.T) {7 t.FailNow()8}9--- FAIL: TestFailNow (0.00s)10Fatal()11func TestFatal(t *testing.T) {12 t.Fatal("This is a fatal message")13}14--- FAIL: TestFatal (0.00s)15Skip()16func TestSkip(t *testing.T) {17 t.Skip("Skipping this test")18}19--- SKIP: TestSkip (0.00s)20SkipNow()21func TestSkipNow(t *testing.T) {22 t.SkipNow()23}24--- SKIP: TestSkipNow (0.00s)25Skipf()26func TestSkipf(t *testing.T) {27 t.Skipf("Skipping this test")28}29--- SKIP: TestSkipf (0
Errorf
Using AI Code Generation
1import (2func TestErrorf(t *testing.T) {3 if x != y {4 t.Errorf("x is not equal to y")5 }6}7--- FAIL: TestErrorf (0.00s)8import (9func TestError(t *testing.T) {10 if x != y {11 t.Error("x is not equal to y")12 }13}14--- FAIL: TestError (0.00s)15import (16func TestFail(t *testing.T) {17 if x != y {18 t.Fail()19 }20}21--- FAIL: TestFail (0.00s)22import (23func TestFailNow(t *testing.T) {24 if x != y {25 t.FailNow()26 }27}28--- FAIL: TestFailNow (0.00s)29import (30func TestFatal(t *testing.T)
Errorf
Using AI Code Generation
1import (2func TestErrorf(t *testing.T) {3 t.Errorf("Errorf method")4}5func TestFailNow(t *testing.T) {6 t.FailNow()7 t.Logf("test will not run")8}9func TestFatal(t *testing.T) {10 t.Fatal("Fatal method")11 t.Logf("test will not run")12}13func TestLog(t *testing.T) {14 t.Log("Log method")15}16func TestLogf(t *testing.T) {17 t.Logf("Logf method")18}19func TestSkip(t *testing.T) {20 t.Skip("Skip method")21 t.Logf("test will not run")22}23func TestSkipNow(t *testing.T) {24 t.SkipNow()25 t.Logf("test will not run")26}27func TestSkipf(t *testing.T) {28 t.Skipf("Skipf method")29 t.Logf("test will not run")30}31--- FAIL: TestErrorf (0.00s)32--- FAIL: TestFailNow (0.00s)33--- FAIL: TestFatal (0.00s)34--- PASS: TestLog (0.00s)35--- PASS: TestLogf (0.00s)36--- SKIP: TestSkip (0.00s)37--- SKIP: TestSkipNow (0.00s)38--- SKIP: TestSkipf (0.00s)
Errorf
Using AI Code Generation
1import (2func TestErrorf(t *testing.T) {3 if i < 20 {4 fmt.Println("i is less than 20")5 } else {6 t.Errorf("i is greater than 20")7 }8}9--- FAIL: TestErrorf (0.00s)10import (11func TestFailNow(t *testing.T) {12 if i < 20 {13 fmt.Println("i is less than 20")14 } else {15 t.Errorf("i is greater than 20")16 t.FailNow()17 }18}19--- FAIL: TestFailNow (0.00s)20The Errorf() method is used when we
Errorf
Using AI Code Generation
1import (2func TestErrorf(t *testing.T) {3 t.Errorf("This is an error message")4}5func main() {6 fmt.Println("Hello World")7}8Recommended Posts: GoLang | Testing - FailNow()9GoLang | Testing - Fail()10GoLang | Testing - Error()11GoLang | Testing - Log()12GoLang | Testing - Fatal()13GoLang | Testing - Skip()14GoLang | Testing - SkipNow()15GoLang | Testing - Skipf()16GoLang | Testing - SkipfNow()17GoLang | Testing - Run()18GoLang | Testing - RunTests()19GoLang | Testing - RunExamples()20GoLang | Testing - Benchmark()21GoLang | Testing - Example()
Errorf
Using AI Code Generation
1import (2func TestErrorf(t *testing.T) {3 t.Errorf("Errorf method is used to create a new error with a formatted message")4 fmt.Println("Errorf method is used to create a new error with a formatted message")5}6import (7func TestFailNow(t *testing.T) {8 t.FailNow()9 fmt.Println("FailNow method is used to mark the test as failed and stops its execution")10}
Errorf
Using AI Code Generation
1import (2func TestErrorf(t *testing.T) {3 t.Errorf("Errorf method")4}5import (6func TestFail(t *testing.T) {7 t.Fail()8}9--- FAIL: TestFail (0.00s)10import (11func TestFailNow(t *testing.T) {12 t.FailNow()13}14--- FAIL: TestFailNow (0.00s)15import (16func TestFatal(t *testing.T) {17 t.Fatal()18}19--- FAIL: TestFatal (0.00s)20import (21func TestLog(t *testing.T) {22 t.Log("Log method")23}24import (25func TestLogf(t *testing.T) {26 t.Logf("Logf method")27}28import (29func TestSkip(t *testing.T) {30 t.Skip("Skip method")31}32--- SKIP: TestSkip (0.00s)33import (34func TestSkipNow(t
Errorf
Using AI Code Generation
1func TestErrorf(t *testing.T) {2 if str != "Hello" {3 t.Errorf("String not equal")4 }5}6func TestError(t *testing.T) {7 if str != "Hello" {8 t.Error("String not equal")9 }10}11--- PASS: TestErrorf (0.00s)12--- PASS: TestError (0.00s)
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!!