How to use init method of ast Package

Best Syzkaller code snippet using ast.init

unary_test.go

Source:unary_test.go Github

copy

Full Screen

1package typecheck2import (3 "golang/ast"4 "testing"5)6func TestUnaryPlus(t *testing.T) {7 p, err := compilePackage("_test/src/unary", []string{"unary-plus.go"}, nil)8 if err != nil {9 t.Fatal(err)10 }11 B := p.Find("B").(*ast.Const)12 c := B.Init.(*ast.ConstValue)13 if !isUntyped(c.Typ) {14 t.Error("`B` should be untyped")15 }16 if _, ok := c.Value.(ast.UntypedInt); !ok {17 t.Error("`B` should be untyped integer")18 }19 if v, err := ToInt(c); err != nil || v != 1 {20 t.Error("`B` should have value 1")21 }22 D := p.Find("D").(*ast.Const)23 c = D.Init.(*ast.ConstValue)24 if !isUntyped(c.Typ) {25 t.Error("`D` should be untyped")26 }27 if _, ok := c.Value.(ast.UntypedRune); !ok {28 t.Error("`D` should be untyped rune")29 }30 if v, err := ToInt(c); err != nil || v != 'a' {31 t.Error("`D` should have value 'a'")32 }33 F := p.Find("F").(*ast.Const)34 c = F.Init.(*ast.ConstValue)35 if !isUntyped(c.Typ) {36 t.Error("`F` should be untyped")37 }38 if _, ok := c.Value.(ast.UntypedFloat); !ok {39 t.Error("`F` should be untyped float")40 }41 if v, err := ToFloat(c); err != nil || v != 1.1 {42 t.Error("`F` should have value 1.1")43 }44 cB := p.Find("cB").(*ast.Const)45 c = cB.Init.(*ast.ConstValue)46 if c.Typ != ast.BuiltinInt {47 t.Error("`cB` should have type `int`")48 }49 if v, err := ToInt(c); err != nil || v != 1 {50 t.Error("`B` should have value 1")51 }52 cD := p.Find("cD").(*ast.Const)53 c = cD.Init.(*ast.ConstValue)54 if c.Typ != ast.BuiltinInt32 {55 t.Error("`cD` should have type `rune`")56 }57 if v, err := ToInt(c); err != nil || v != 'a' {58 t.Error("`cD` should have value 'a'")59 }60 cF := p.Find("cF").(*ast.Const)61 c = cF.Init.(*ast.ConstValue)62 if c.Typ != ast.BuiltinFloat32 {63 t.Error("`cF` should have type `float32`")64 }65 if v, err := ToFloat(c); err != nil || v != 1.125 {66 t.Error("`cF` should have value 1.125")67 }68 // FIXME: check complex constants69 vB := p.Find("vB").(*ast.Var)70 if vB.Type != ast.BuiltinInt {71 t.Error("`vB` should have type `int`")72 }73 vD := p.Find("vD").(*ast.Var)74 if vD.Type != ast.BuiltinInt32 {75 t.Error("`vD` should have type `rune`")76 }77 vF := p.Find("vF").(*ast.Var)78 if vF.Type != ast.BuiltinFloat64 {79 t.Error("`vF` should have type `float64`")80 }81 z0 := p.Find("z0").(*ast.Const)82 if z0.Type != ast.BuiltinInt {83 t.Error("`z0` should have type `int`")84 }85 c = z0.Init.(*ast.ConstValue)86 if v, ok := c.Value.(ast.Int); !ok || v != 1 {87 t.Error("`z0` should have value 1")88 }89 z1 := p.Find("z1").(*ast.Const)90 if z1.Type != ast.BuiltinInt {91 t.Error("`z1` should have type `int`")92 }93 c = z1.Init.(*ast.ConstValue)94 if v, ok := c.Value.(ast.Int); !ok || v != 1 {95 t.Error("`z1` should have value 1")96 }97}98func TestUnaryPlusErr(t *testing.T) {99 expectError(t, "_test/src/unary", []string{"unary-plus-err-1.go"}, "invalid operand")100 expectError(t, "_test/src/unary", []string{"unary-plus-err-2.go"}, "invalid operand")101}102func TestUnaryMinus(t *testing.T) {103 p, err := compilePackage("_test/src/unary", []string{"unary-minus.go"}, nil)104 if err != nil {105 t.Fatal(err)106 }107 B := p.Find("B").(*ast.Const)108 c := B.Init.(*ast.ConstValue)109 if !isUntyped(c.Typ) {110 t.Error("`B` should be untyped")111 }112 if _, ok := c.Value.(ast.UntypedInt); !ok {113 t.Error("`B` should be untyped integer")114 }115 if v, err := ToInt(c); err != nil || v != -1 {116 t.Error("`B` should have value -1")117 }118 D := p.Find("D").(*ast.Const)119 c = D.Init.(*ast.ConstValue)120 if !isUntyped(c.Typ) {121 t.Error("`D` should be untyped")122 }123 if _, ok := c.Value.(ast.UntypedRune); !ok {124 t.Error("`D` should be untyped rune")125 }126 if v, err := ToInt(c); err != nil || v != -97 {127 t.Error("`D` should have value -97")128 }129 F := p.Find("F").(*ast.Const)130 c = F.Init.(*ast.ConstValue)131 if !isUntyped(c.Typ) {132 t.Error("`F` should be untyped")133 }134 if _, ok := c.Value.(ast.UntypedFloat); !ok {135 t.Error("`F` should be untyped float")136 }137 if v, err := ToFloat(c); err != nil || v != -1.1 {138 t.Error("`F` should have value -1.1")139 }140 H := p.Find("H").(*ast.Const)141 c = H.Init.(*ast.ConstValue)142 if !isUntyped(c.Typ) {143 t.Error("`H` should be untyped")144 }145 if _, ok := c.Value.(ast.UntypedComplex); !ok {146 t.Error("`H` should be untyped complex")147 }148 z, err := ToComplex(c)149 if err != nil {150 t.Fatal(err)151 }152 if real(z) != -1.1 {153 t.Error("`real(H)` should have value -1.1")154 }155 if imag(z) != -2.2 {156 t.Error("`imag(H)` should have value -1.1")157 }158 cB := p.Find("cB").(*ast.Const)159 c = cB.Init.(*ast.ConstValue)160 if c.Typ != ast.BuiltinInt {161 t.Error("`cB` should have type `int`")162 }163 if v, err := ToInt(c); err != nil || v != -1 {164 t.Error("`B` should have value -1")165 }166 cD := p.Find("cD").(*ast.Const)167 c = cD.Init.(*ast.ConstValue)168 if c.Typ != ast.BuiltinInt32 {169 t.Error("`cD` should have type `rune`")170 }171 if v, err := ToInt(c); err != nil || v != -97 {172 t.Error("`cD` should have value -97")173 }174 cF := p.Find("cF").(*ast.Const)175 c = cF.Init.(*ast.ConstValue)176 if c.Typ != ast.BuiltinFloat32 {177 t.Error("`cF` should have type `float32`")178 }179 if v, err := ToFloat(c); err != nil || v != -1.125 {180 t.Error("`cF` should have value -1.125")181 }182 cH := p.Find("cH").(*ast.Const)183 c = cH.Init.(*ast.ConstValue)184 if c.Typ != ast.BuiltinComplex64 {185 t.Error("`cH` should have type `complex64`")186 }187 z, err = ToComplex(c)188 if err != nil {189 t.Fatal(err)190 }191 if real(z) != -1.125 {192 t.Error("`real(cH)` should have value -1.125")193 }194 if imag(z) != -2.25 {195 t.Error("`imag(cH)` should have value -2.25")196 }197 vB := p.Find("vB").(*ast.Var)198 if vB.Type != ast.BuiltinInt {199 t.Error("`vB` should have type `int`")200 }201 vD := p.Find("vD").(*ast.Var)202 if vD.Type != ast.BuiltinInt32 {203 t.Error("`vD` should have type `rune`")204 }205 vF := p.Find("vF").(*ast.Var)206 if vF.Type != ast.BuiltinFloat64 {207 t.Error("`vF` should have type `float64`")208 }209 v := p.Find("v").(*ast.Var)210 if v.Type != ast.BuiltinUint {211 t.Error("`v` should have type `uint`")212 }213 z0 := p.Find("z0").(*ast.Const)214 if z0.Type != ast.BuiltinInt {215 t.Error("`z0` should have type `int`")216 }217 c = z0.Init.(*ast.ConstValue)218 if v, ok := c.Value.(ast.Int); !ok || int64(v) != -1 {219 t.Error("`z0` should have value -1")220 }221 z1 := p.Find("z1").(*ast.Const)222 if z1.Type != ast.BuiltinInt {223 t.Error("`z1` should have type `int`")224 }225 c = z1.Init.(*ast.ConstValue)226 if v, ok := c.Value.(ast.Int); !ok || int64(v) != -1 {227 t.Error("`z1` should have value -1")228 }229}230func TestUnaryMinusErr(t *testing.T) {231 expectError(t, "_test/src/unary", []string{"unary-minus-err-1.go"}, "invalid operand")232 expectError(t, "_test/src/unary", []string{"unary-minus-err-2.go"}, "invalid operand")233 expectError(t, "_test/src/unary", []string{"unary-minus-err-3.go"}, "invalid operand")234}235func TestNot(t *testing.T) {236 p, err := compilePackage("_test/src/unary", []string{"not.go"}, nil)237 if err != nil {238 t.Fatal(err)239 }240 B := p.Find("B").(*ast.Const)241 c := B.Init.(*ast.ConstValue)242 if !isUntyped(c.Typ) {243 t.Error("`B` should be untyped")244 }245 v, ok := c.Value.(ast.Bool)246 if !ok {247 t.Fatal("`B` should be untyped boolean")248 }249 if !v {250 t.Error("`B` should have value false")251 }252 cB := p.Find("cB").(*ast.Const)253 c = cB.Init.(*ast.ConstValue)254 if c.Typ != ast.BuiltinBool {255 t.Error("`cB` shold have type `bool`")256 }257 if v = c.Value.(ast.Bool); v {258 t.Error("`cB` should have value false")259 }260 cD := p.Find("cD").(*ast.Const)261 c = cD.Init.(*ast.ConstValue)262 if c.Typ != ast.BuiltinBool {263 t.Error("`cD` shold have type `bool`")264 }265 if v = c.Value.(ast.Bool); !v {266 t.Error("`cD` should have value true")267 }268 D := p.Find("D").(*ast.Const)269 c = D.Init.(*ast.ConstValue)270 if !isUntyped(c.Typ) {271 t.Error("`D` should be untyped")272 }273 v, ok = c.Value.(ast.Bool)274 if !ok {275 t.Fatal("`D` should be untyped boolean")276 }277 if v {278 t.Error("`D` should have value false")279 }280 vB := p.Find("vB").(*ast.Var)281 if vB.Type != ast.BuiltinBool {282 t.Error("`B` should have type `bool`")283 }284 z0 := p.Find("z0").(*ast.Const)285 if z0.Type != ast.BuiltinBool {286 t.Error("`z0` should have type `bool`")287 }288 c = z0.Init.(*ast.ConstValue)289 if v, ok := c.Value.(ast.Bool); !ok || v == true {290 t.Error("`z0` should have value false")291 }292 z1 := p.Find("z1").(*ast.Const)293 if z1.Type != ast.BuiltinBool {294 t.Error("`z1` should have type `bool`")295 }296 c = z1.Init.(*ast.ConstValue)297 if v, ok := c.Value.(ast.Bool); !ok || v == true {298 t.Error("`z1` should have value false")299 }300 Bool := p.Find("Bool").(*ast.TypeDecl)301 u := p.Find("u").(*ast.Var)302 if u.Type != Bool {303 t.Error("`u` shuld have type `Bool`")304 }305 vv := p.Find("v").(*ast.Var)306 if vv.Type != ast.BuiltinBool {307 t.Error("`v` shuld have type `bool`")308 }309}310func TestNotErr(t *testing.T) {311 expectError(t, "_test/src/unary", []string{"not-err-1.go"}, "invalid operand")312 expectError(t, "_test/src/unary", []string{"not-err-2.go"}, "invalid operand")313 expectError(t, "_test/src/unary", []string{"not-err-3.go"}, "invalid operand")314}315func TestCompl(t *testing.T) {316 p, err := compilePackage("_test/src/unary", []string{"compl.go"}, nil)317 if err != nil {318 t.Fatal(err)319 }320 B := p.Find("B").(*ast.Const)321 c := B.Init.(*ast.ConstValue)322 if !isUntyped(c.Typ) {323 t.Error("`B` should be untyped")324 }325 if _, ok := c.Value.(ast.UntypedInt); !ok {326 t.Fatal("`B` should be untyped integer")327 }328 if v, err := ToInt(c); err != nil || v != -2 {329 t.Error("`B` should have value -2")330 }331 cB := p.Find("cB").(*ast.Const)332 c = cB.Init.(*ast.ConstValue)333 if typ, ok := c.Typ.(*ast.TypeDecl); !ok || typ.Name != "T" {334 t.Error("`cB` should have type `T`")335 }336 if v, err := ToInt(c); err != nil || v != 0xfffe {337 t.Error("`cB` should have value 0xfffe")338 }339 cu8 := p.Find("cu8").(*ast.Const)340 c = cu8.Init.(*ast.ConstValue)341 if c.Typ != ast.BuiltinUint8 {342 t.Error("`cu8` should have type `uint8`")343 }344 if v, err := ToInt(c); err != nil || v != 0xfe {345 t.Error("`cu8` should have value 0xfe")346 }347 cu16 := p.Find("cu16").(*ast.Const)348 c = cu16.Init.(*ast.ConstValue)349 if c.Typ != ast.BuiltinUint16 {350 t.Error("`cu16` should have type `uint16`")351 }352 if v, err := ToInt(c); err != nil || v != 0xfffe {353 t.Error("`cu16` should have value 0xfffe")354 }355 cu32 := p.Find("cu32").(*ast.Const)356 c = cu32.Init.(*ast.ConstValue)357 if c.Typ != ast.BuiltinUint32 {358 t.Error("`cu32` should have type `uint32`")359 }360 if v, err := ToInt(c); err != nil || v != 0xfffffffe {361 t.Error("`cu32` should have value 0xfffffffe")362 }363 cu64 := p.Find("cu64").(*ast.Const)364 c = cu64.Init.(*ast.ConstValue)365 if c.Typ != ast.BuiltinUint64 {366 t.Error("`cu64` should have type `uint64`")367 }368 if v := uint64(c.Value.(ast.Int)); v != 0xfffffffffffffffe {369 t.Error("`cu64` should have value 0xfffffffffffffffe")370 }371 cu := p.Find("cu").(*ast.Const)372 c = cu.Init.(*ast.ConstValue)373 if c.Typ != ast.BuiltinUint {374 t.Error("`cu` should have type `uint`")375 }376 if v := uint64(c.Value.(ast.Int)); v != 0xfffffffffffffffe {377 t.Error("`cu` should have value 0xfffffffffffffffe")378 }379 cp := p.Find("cp").(*ast.Const)380 c = cp.Init.(*ast.ConstValue)381 if c.Typ != ast.BuiltinUintptr {382 t.Error("`cp` should have type `uintptr`")383 }384 if v := uint64(c.Value.(ast.Int)); v != 0xfffffffffffffffe {385 t.Error("`cp` should have value 0xfffffffffffffffe")386 }387 ci8 := p.Find("ci8").(*ast.Const)388 c = ci8.Init.(*ast.ConstValue)389 if c.Typ != ast.BuiltinInt8 {390 t.Error("`ci8` should have type `int8`")391 }392 if v, err := ToInt(c); err != nil || v != -2 {393 t.Error("`ci8` should have value -2")394 }395 ci16 := p.Find("ci16").(*ast.Const)396 c = ci16.Init.(*ast.ConstValue)397 if c.Typ != ast.BuiltinInt16 {398 t.Error("`ci16` should have type `int16`")399 }400 if v, err := ToInt(c); err != nil || v != -2 {401 t.Error("`ci16` should have value -2")402 }403 ci32 := p.Find("ci32").(*ast.Const)404 c = ci32.Init.(*ast.ConstValue)405 if c.Typ != ast.BuiltinInt32 {406 t.Error("`ci32` should have type `int32`")407 }408 if v, err := ToInt(c); err != nil || v != -2 {409 t.Error("`ci32` should have value -2")410 }411 ci64 := p.Find("ci64").(*ast.Const)412 c = ci64.Init.(*ast.ConstValue)413 if c.Typ != ast.BuiltinInt64 {414 t.Error("`ci64` should have type `int64`")415 }416 if v, err := ToInt(c); err != nil || v != -2 {417 t.Error("`ci64` should have value -2")418 }419 ci := p.Find("ci").(*ast.Const)420 c = ci.Init.(*ast.ConstValue)421 if c.Typ != ast.BuiltinInt {422 t.Error("`ci` should have type `int`")423 }424 if v, err := ToInt(c); err != nil || v != -2 {425 t.Error("`ci` should have value -2")426 }427 cr0 := p.Find("cr0").(*ast.Const)428 c = cr0.Init.(*ast.ConstValue)429 if !isUntyped(c.Typ) {430 t.Error("`cr0` should be untyped")431 }432 if _, ok := c.Value.(ast.UntypedRune); !ok {433 t.Error("`cr0` should be untyped rune")434 }435 if v, err := ToInt(c); err != nil || v != -98 {436 t.Error("`cr0` should have value -98")437 }438 cr1 := p.Find("cr1").(*ast.Const)439 c = cr1.Init.(*ast.ConstValue)440 if c.Typ != ast.BuiltinInt32 {441 t.Error("`cr1` should have type `rune`")442 }443 if v, err := ToInt(c); err != nil || v != -98 {444 t.Error("`cr1` should have value -98")445 }446 vB := p.Find("vB").(*ast.Var)447 if typ, ok := vB.Type.(*ast.TypeDecl); !ok || typ.Name != "T" {448 t.Error("`vB` should have type `T`")449 }450 z0 := p.Find("z0").(*ast.Const)451 c = z0.Init.(*ast.ConstValue)452 if c.Typ != ast.BuiltinInt {453 t.Error("`z0` should have type `int`")454 }455 if v, err := ToInt(c); err != nil || v != -2 {456 t.Error("`z0` should have value -2")457 }458 z1 := p.Find("z1").(*ast.Const)459 c = z1.Init.(*ast.ConstValue)460 if c.Typ != ast.BuiltinInt {461 t.Error("`z1` should have type `int`")462 }463 if v, err := ToInt(c); err != nil || v != -2 {464 t.Error("`z1` should have value -2")465 }466}467func TestComplErr(t *testing.T) {468 expectError(t, "_test/src/unary", []string{"compl-err-1.go"}, "invalid operand")469 expectError(t, "_test/src/unary", []string{"compl-err-2.go"}, "invalid operand")470 expectError(t, "_test/src/unary", []string{"compl-err-3.go"}, "invalid operand")471}472func TestIndirection(t *testing.T) {473 p, err := compilePackage("_test/src/unary", []string{"deref.go"}, nil)474 if err != nil {475 t.Fatal(err)476 }477 B := p.Find("B").(*ast.Var)478 if typ, ok := B.Type.(*ast.TypeDecl); !ok || typ.Name != "T" {479 t.Error("`B` should have type `T`")480 }481}482func TestIndirectionlErr(t *testing.T) {483 expectError(t, "_test/src/unary", []string{"deref-err-1.go"}, "invalid operand")484 expectError(t, "_test/src/unary", []string{"deref-err-2.go"}, "invalid operand")485}486func TestAddr(t *testing.T) {487 p, err := compilePackage("_test/src/unary", []string{"addr.go"}, nil)488 if err != nil {489 t.Fatal(err)490 }491 T := p.Find("T").(*ast.TypeDecl)492 S := p.Find("S").(*ast.TypeDecl)493 B := p.Find("B").(*ast.Var)494 if ptr, ok := B.Type.(*ast.PtrType); !ok || ptr.Base != T {495 t.Error("`B` should have type `*T`")496 }497 C := p.Find("C").(*ast.Var)498 if ptr, ok := C.Type.(*ast.PtrType); !ok || ptr.Base != T {499 t.Error("`C` should have type `*T`")500 }501 E := p.Find("E").(*ast.Var)502 if ptr, ok := E.Type.(*ast.PtrType); !ok || ptr.Base != S {503 t.Error("`E` should have type `*S`")504 }505 F := p.Find("F").(*ast.Var)506 if ptr, ok := F.Type.(*ast.PtrType); !ok || ptr.Base != ast.BuiltinInt {507 t.Error("`F` should have type `*int`")508 }509 H := p.Find("H").(*ast.Var)510 if ptr, ok := H.Type.(*ast.PtrType); !ok || ptr.Base != T {511 t.Error("`H` should have type `*T`")512 }513 I := p.Find("I").(*ast.Var)514 if ptr, ok := I.Type.(*ast.PtrType); !ok || ptr.Base != ast.BuiltinInt {515 t.Error("`I` should have type `*int`")516 }517 X := p.Find("X").(*ast.Var)518 if ptr, ok := X.Type.(*ast.PtrType); !ok || ptr.Base != S {519 t.Error("`X` should have type `*S`")520 }521 Y := p.Find("Y").(*ast.Var)522 ptr, ok := Y.Type.(*ast.PtrType)523 if !ok {524 t.Error("`Y` should have type `*[...]T`")525 }526 if a, ok := ptr.Base.(*ast.ArrayType); !ok || a.Elt != T {527 t.Error("`Y` should have type `*[...]T`")528 }529 Z := p.Find("Z").(*ast.Var)530 ptr, ok = Z.Type.(*ast.PtrType)531 if !ok {532 t.Error("`Z` should have type `*[]T`")533 }534 if s, ok := ptr.Base.(*ast.SliceType); !ok || s.Elt != T {535 t.Error("`Y` should have type `*[]T`")536 }537}538func TestAddrErr(t *testing.T) {539 expectError(t, "_test/src/unary", []string{"addr-err-1.go"},540 "value must be addresable")541 expectError(t, "_test/src/unary", []string{"addr-err-2.go"},542 "value must be addresable")543 expectError(t, "_test/src/unary", []string{"addr-err-3.go"},544 "value must be addresable")545 expectError(t, "_test/src/unary", []string{"addr-err-4.go"},546 "value must be addresable")547}548func TestRecv(t *testing.T) {549 p, err := compilePackage("_test/src/unary", []string{"recv.go"}, nil)550 if err != nil {551 t.Fatal(err)552 }553 T := p.Find("T").(*ast.TypeDecl)554 for _, name := range []string{"A", "B", "C"} {555 v := p.Find(name).(*ast.Var)556 if v.Type != T {557 t.Errorf("`%s` should have type `T`\n", name)558 }559 }560 ok := p.Find("ok").(*ast.Var)561 if ok.Type != ast.BuiltinBool {562 t.Error("`ok` should have type `bool`")563 }564}565func TestRecvErr(t *testing.T) {566 expectError(t, "_test/src/unary", []string{"recv-err-1.go"}, "invalid operand")567 expectError(t, "_test/src/unary", []string{"recv-err-2.go"}, "invalid operand")568 expectError(t, "_test/src/unary", []string{"recv-err-3.go"}, "invalid operand")569}...

Full Screen

Full Screen

copyast.go

Source:copyast.go Github

copy

Full Screen

1// Copyright 2018 The Wire Authors2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// https://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14package wire15import (16 "fmt"17 "go/ast"18 "golang.org/x/tools/go/ast/astutil"19)20// copyAST performs a deep copy of an AST. *ast.Ident identity will be21// preserved.22//23// This allows using astutil.Apply to rewrite an AST without modifying24// the original AST.25func copyAST(original ast.Node) ast.Node {26 // This function is necessarily long. No utility function exists to do this27 // clone, as most any attempt would need to have customization options, which28 // would need to be as expressive as Apply. A possibility to shorten the code29 // here would be to use reflection, but that trades clarity for shorter code.30 m := make(map[ast.Node]ast.Node)31 astutil.Apply(original, nil, func(c *astutil.Cursor) bool {32 switch node := c.Node().(type) {33 case nil:34 // No-op.35 case *ast.ArrayType:36 m[node] = &ast.ArrayType{37 Lbrack: node.Lbrack,38 Len: exprFromMap(m, node.Len),39 Elt: exprFromMap(m, node.Elt),40 }41 case *ast.AssignStmt:42 m[node] = &ast.AssignStmt{43 Lhs: copyExprList(m, node.Lhs),44 TokPos: node.TokPos,45 Tok: node.Tok,46 Rhs: copyExprList(m, node.Rhs),47 }48 case *ast.BadDecl:49 m[node] = &ast.BadDecl{50 From: node.From,51 To: node.To,52 }53 case *ast.BadExpr:54 m[node] = &ast.BadExpr{55 From: node.From,56 To: node.To,57 }58 case *ast.BadStmt:59 m[node] = &ast.BadStmt{60 From: node.From,61 To: node.To,62 }63 case *ast.BasicLit:64 m[node] = &ast.BasicLit{65 ValuePos: node.ValuePos,66 Kind: node.Kind,67 Value: node.Value,68 }69 case *ast.BinaryExpr:70 m[node] = &ast.BinaryExpr{71 X: exprFromMap(m, node.X),72 OpPos: node.OpPos,73 Op: node.Op,74 Y: exprFromMap(m, node.Y),75 }76 case *ast.BlockStmt:77 m[node] = &ast.BlockStmt{78 Lbrace: node.Lbrace,79 List: copyStmtList(m, node.List),80 Rbrace: node.Rbrace,81 }82 case *ast.BranchStmt:83 m[node] = &ast.BranchStmt{84 TokPos: node.TokPos,85 Tok: node.Tok,86 Label: identFromMap(m, node.Label),87 }88 case *ast.CallExpr:89 m[node] = &ast.CallExpr{90 Fun: exprFromMap(m, node.Fun),91 Lparen: node.Lparen,92 Args: copyExprList(m, node.Args),93 Ellipsis: node.Ellipsis,94 Rparen: node.Rparen,95 }96 case *ast.CaseClause:97 m[node] = &ast.CaseClause{98 Case: node.Case,99 List: copyExprList(m, node.List),100 Colon: node.Colon,101 Body: copyStmtList(m, node.Body),102 }103 case *ast.ChanType:104 m[node] = &ast.ChanType{105 Begin: node.Begin,106 Arrow: node.Arrow,107 Dir: node.Dir,108 Value: exprFromMap(m, node.Value),109 }110 case *ast.CommClause:111 m[node] = &ast.CommClause{112 Case: node.Case,113 Comm: stmtFromMap(m, node.Comm),114 Colon: node.Colon,115 Body: copyStmtList(m, node.Body),116 }117 case *ast.Comment:118 m[node] = &ast.Comment{119 Slash: node.Slash,120 Text: node.Text,121 }122 case *ast.CommentGroup:123 cg := new(ast.CommentGroup)124 if node.List != nil {125 cg.List = make([]*ast.Comment, len(node.List))126 for i := range node.List {127 cg.List[i] = m[node.List[i]].(*ast.Comment)128 }129 }130 m[node] = cg131 case *ast.CompositeLit:132 m[node] = &ast.CompositeLit{133 Type: exprFromMap(m, node.Type),134 Lbrace: node.Lbrace,135 Elts: copyExprList(m, node.Elts),136 Rbrace: node.Rbrace,137 }138 case *ast.DeclStmt:139 m[node] = &ast.DeclStmt{140 Decl: m[node.Decl].(ast.Decl),141 }142 case *ast.DeferStmt:143 m[node] = &ast.DeferStmt{144 Defer: node.Defer,145 Call: callExprFromMap(m, node.Call),146 }147 case *ast.Ellipsis:148 m[node] = &ast.Ellipsis{149 Ellipsis: node.Ellipsis,150 Elt: exprFromMap(m, node.Elt),151 }152 case *ast.EmptyStmt:153 m[node] = &ast.EmptyStmt{154 Semicolon: node.Semicolon,155 Implicit: node.Implicit,156 }157 case *ast.ExprStmt:158 m[node] = &ast.ExprStmt{159 X: exprFromMap(m, node.X),160 }161 case *ast.Field:162 m[node] = &ast.Field{163 Doc: commentGroupFromMap(m, node.Doc),164 Names: copyIdentList(m, node.Names),165 Type: exprFromMap(m, node.Type),166 Tag: basicLitFromMap(m, node.Tag),167 Comment: commentGroupFromMap(m, node.Comment),168 }169 case *ast.FieldList:170 fl := &ast.FieldList{171 Opening: node.Opening,172 Closing: node.Closing,173 }174 if node.List != nil {175 fl.List = make([]*ast.Field, len(node.List))176 for i := range node.List {177 fl.List[i] = m[node.List[i]].(*ast.Field)178 }179 }180 m[node] = fl181 case *ast.ForStmt:182 m[node] = &ast.ForStmt{183 For: node.For,184 Init: stmtFromMap(m, node.Init),185 Cond: exprFromMap(m, node.Cond),186 Post: stmtFromMap(m, node.Post),187 Body: blockStmtFromMap(m, node.Body),188 }189 case *ast.FuncDecl:190 m[node] = &ast.FuncDecl{191 Doc: commentGroupFromMap(m, node.Doc),192 Recv: fieldListFromMap(m, node.Recv),193 Name: identFromMap(m, node.Name),194 Type: funcTypeFromMap(m, node.Type),195 Body: blockStmtFromMap(m, node.Body),196 }197 case *ast.FuncLit:198 m[node] = &ast.FuncLit{199 Type: funcTypeFromMap(m, node.Type),200 Body: blockStmtFromMap(m, node.Body),201 }202 case *ast.FuncType:203 m[node] = &ast.FuncType{204 Func: node.Func,205 Params: fieldListFromMap(m, node.Params),206 Results: fieldListFromMap(m, node.Results),207 }208 case *ast.GenDecl:209 decl := &ast.GenDecl{210 Doc: commentGroupFromMap(m, node.Doc),211 TokPos: node.TokPos,212 Tok: node.Tok,213 Lparen: node.Lparen,214 Rparen: node.Rparen,215 }216 if node.Specs != nil {217 decl.Specs = make([]ast.Spec, len(node.Specs))218 for i := range node.Specs {219 decl.Specs[i] = m[node.Specs[i]].(ast.Spec)220 }221 }222 m[node] = decl223 case *ast.GoStmt:224 m[node] = &ast.GoStmt{225 Go: node.Go,226 Call: callExprFromMap(m, node.Call),227 }228 case *ast.Ident:229 // Keep identifiers the same identity so they can be conveniently230 // used with the original *types.Info.231 m[node] = node232 case *ast.IfStmt:233 m[node] = &ast.IfStmt{234 If: node.If,235 Init: stmtFromMap(m, node.Init),236 Cond: exprFromMap(m, node.Cond),237 Body: blockStmtFromMap(m, node.Body),238 Else: stmtFromMap(m, node.Else),239 }240 case *ast.ImportSpec:241 m[node] = &ast.ImportSpec{242 Doc: commentGroupFromMap(m, node.Doc),243 Name: identFromMap(m, node.Name),244 Path: basicLitFromMap(m, node.Path),245 Comment: commentGroupFromMap(m, node.Comment),246 EndPos: node.EndPos,247 }248 case *ast.IncDecStmt:249 m[node] = &ast.IncDecStmt{250 X: exprFromMap(m, node.X),251 TokPos: node.TokPos,252 Tok: node.Tok,253 }254 case *ast.IndexExpr:255 m[node] = &ast.IndexExpr{256 X: exprFromMap(m, node.X),257 Lbrack: node.Lbrack,258 Index: exprFromMap(m, node.Index),259 Rbrack: node.Rbrack,260 }261 case *ast.InterfaceType:262 m[node] = &ast.InterfaceType{263 Interface: node.Interface,264 Methods: fieldListFromMap(m, node.Methods),265 Incomplete: node.Incomplete,266 }267 case *ast.KeyValueExpr:268 m[node] = &ast.KeyValueExpr{269 Key: exprFromMap(m, node.Key),270 Colon: node.Colon,271 Value: exprFromMap(m, node.Value),272 }273 case *ast.LabeledStmt:274 m[node] = &ast.LabeledStmt{275 Label: identFromMap(m, node.Label),276 Colon: node.Colon,277 Stmt: stmtFromMap(m, node.Stmt),278 }279 case *ast.MapType:280 m[node] = &ast.MapType{281 Map: node.Map,282 Key: exprFromMap(m, node.Key),283 Value: exprFromMap(m, node.Value),284 }285 case *ast.ParenExpr:286 m[node] = &ast.ParenExpr{287 Lparen: node.Lparen,288 X: exprFromMap(m, node.X),289 Rparen: node.Rparen,290 }291 case *ast.RangeStmt:292 m[node] = &ast.RangeStmt{293 For: node.For,294 Key: exprFromMap(m, node.Key),295 Value: exprFromMap(m, node.Value),296 TokPos: node.TokPos,297 Tok: node.Tok,298 X: exprFromMap(m, node.X),299 Body: blockStmtFromMap(m, node.Body),300 }301 case *ast.ReturnStmt:302 m[node] = &ast.ReturnStmt{303 Return: node.Return,304 Results: copyExprList(m, node.Results),305 }306 case *ast.SelectStmt:307 m[node] = &ast.SelectStmt{308 Select: node.Select,309 Body: blockStmtFromMap(m, node.Body),310 }311 case *ast.SelectorExpr:312 m[node] = &ast.SelectorExpr{313 X: exprFromMap(m, node.X),314 Sel: identFromMap(m, node.Sel),315 }316 case *ast.SendStmt:317 m[node] = &ast.SendStmt{318 Chan: exprFromMap(m, node.Chan),319 Arrow: node.Arrow,320 Value: exprFromMap(m, node.Value),321 }322 case *ast.SliceExpr:323 m[node] = &ast.SliceExpr{324 X: exprFromMap(m, node.X),325 Lbrack: node.Lbrack,326 Low: exprFromMap(m, node.Low),327 High: exprFromMap(m, node.High),328 Max: exprFromMap(m, node.Max),329 Slice3: node.Slice3,330 Rbrack: node.Rbrack,331 }332 case *ast.StarExpr:333 m[node] = &ast.StarExpr{334 Star: node.Star,335 X: exprFromMap(m, node.X),336 }337 case *ast.StructType:338 m[node] = &ast.StructType{339 Struct: node.Struct,340 Fields: fieldListFromMap(m, node.Fields),341 Incomplete: node.Incomplete,342 }343 case *ast.SwitchStmt:344 m[node] = &ast.SwitchStmt{345 Switch: node.Switch,346 Init: stmtFromMap(m, node.Init),347 Tag: exprFromMap(m, node.Tag),348 Body: blockStmtFromMap(m, node.Body),349 }350 case *ast.TypeAssertExpr:351 m[node] = &ast.TypeAssertExpr{352 X: exprFromMap(m, node.X),353 Lparen: node.Lparen,354 Type: exprFromMap(m, node.Type),355 Rparen: node.Rparen,356 }357 case *ast.TypeSpec:358 m[node] = &ast.TypeSpec{359 Doc: commentGroupFromMap(m, node.Doc),360 Name: identFromMap(m, node.Name),361 Assign: node.Assign,362 Type: exprFromMap(m, node.Type),363 Comment: commentGroupFromMap(m, node.Comment),364 }365 case *ast.TypeSwitchStmt:366 m[node] = &ast.TypeSwitchStmt{367 Switch: node.Switch,368 Init: stmtFromMap(m, node.Init),369 Assign: stmtFromMap(m, node.Assign),370 Body: blockStmtFromMap(m, node.Body),371 }372 case *ast.UnaryExpr:373 m[node] = &ast.UnaryExpr{374 OpPos: node.OpPos,375 Op: node.Op,376 X: exprFromMap(m, node.X),377 }378 case *ast.ValueSpec:379 m[node] = &ast.ValueSpec{380 Doc: commentGroupFromMap(m, node.Doc),381 Names: copyIdentList(m, node.Names),382 Type: exprFromMap(m, node.Type),383 Values: copyExprList(m, node.Values),384 Comment: commentGroupFromMap(m, node.Comment),385 }386 default:387 panic(fmt.Sprintf("unhandled AST node: %T", node))388 }389 return true390 })391 return m[original]392}393func commentGroupFromMap(m map[ast.Node]ast.Node, key *ast.CommentGroup) *ast.CommentGroup {394 if key == nil {395 return nil396 }397 return m[key].(*ast.CommentGroup)398}399func exprFromMap(m map[ast.Node]ast.Node, key ast.Expr) ast.Expr {400 if key == nil {401 return nil402 }403 return m[key].(ast.Expr)404}405func stmtFromMap(m map[ast.Node]ast.Node, key ast.Stmt) ast.Stmt {406 if key == nil {407 return nil408 }409 return m[key].(ast.Stmt)410}411func identFromMap(m map[ast.Node]ast.Node, key *ast.Ident) *ast.Ident {412 if key == nil {413 return nil414 }415 return m[key].(*ast.Ident)416}417func blockStmtFromMap(m map[ast.Node]ast.Node, key *ast.BlockStmt) *ast.BlockStmt {418 if key == nil {419 return nil420 }421 return m[key].(*ast.BlockStmt)422}423func fieldListFromMap(m map[ast.Node]ast.Node, key *ast.FieldList) *ast.FieldList {424 if key == nil {425 return nil426 }427 return m[key].(*ast.FieldList)428}429func callExprFromMap(m map[ast.Node]ast.Node, key *ast.CallExpr) *ast.CallExpr {430 if key == nil {431 return nil432 }433 return m[key].(*ast.CallExpr)434}435func basicLitFromMap(m map[ast.Node]ast.Node, key *ast.BasicLit) *ast.BasicLit {436 if key == nil {437 return nil438 }439 return m[key].(*ast.BasicLit)440}441func funcTypeFromMap(m map[ast.Node]ast.Node, key *ast.FuncType) *ast.FuncType {442 if key == nil {443 return nil444 }445 return m[key].(*ast.FuncType)446}447func copyExprList(m map[ast.Node]ast.Node, exprs []ast.Expr) []ast.Expr {448 if exprs == nil {449 return nil450 }451 newExprs := make([]ast.Expr, len(exprs))452 for i := range exprs {453 newExprs[i] = m[exprs[i]].(ast.Expr)454 }455 return newExprs456}457func copyStmtList(m map[ast.Node]ast.Node, stmts []ast.Stmt) []ast.Stmt {458 if stmts == nil {459 return nil460 }461 newStmts := make([]ast.Stmt, len(stmts))462 for i := range stmts {463 newStmts[i] = m[stmts[i]].(ast.Stmt)464 }465 return newStmts466}467func copyIdentList(m map[ast.Node]ast.Node, idents []*ast.Ident) []*ast.Ident {468 if idents == nil {469 return nil470 }471 newIdents := make([]*ast.Ident, len(idents))472 for i := range idents {473 newIdents[i] = m[idents[i]].(*ast.Ident)474 }475 return newIdents476}...

Full Screen

Full Screen

rewrite.go

Source:rewrite.go Github

copy

Full Screen

...32// i.e., token.Pos, Scopes, Objects, and fields of basic types33// (strings, etc.) are ignored.34//35// Children are traversed in the order in which they appear in the36// respective node's struct definition. A package's files are37// traversed in the filenames' alphabetical order.38//39func Apply(root ast.Node, pre, post ApplyFunc) (result ast.Node) {40 parent := &struct{ ast.Node }{root}41 defer func() {42 if r := recover(); r != nil && r != abort {43 panic(r)44 }45 result = parent.Node46 }()47 a := &application{pre: pre, post: post}48 a.apply(parent, "Node", nil, root)49 return50}...

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "test.go", nil, parser.ParseComments)4 if err != nil {5 fmt.Println(err)6 }7 ast.Print(fset, f)8}9import (10func main() {11 f, err := parser.ParseFile(fset, "test.go", nil, parser.ParseComments)12 if err != nil {13 fmt.Println(err)14 }15 ast.Print(fset, f)16}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "2.go", nil, 0)4 if err != nil {5 panic(err)6 }7 ast.Print(fset, f)8}9import (10func main() {11 f, err := parser.ParseFile(fset, "2.go", nil, 0)12 if err != nil {13 panic(err)14 }15 ast.Print(fset, f)16 ast.Inspect(f, func(n ast.Node) bool {17 switch x := n.(type) {18 fmt.Printf("%v: %v19", fset.Position(x.Pos()), x.Name)20 }21 })22}23import (24func main() {25 f, err := parser.ParseFile(fset, "2.go", nil, 0)26 if err != nil {27 panic(err)28 }29 ast.Print(fset, f)30 ast.Inspect(f, func(n ast.Node) bool {31 switch x := n.(type) {32 fmt.Printf("%v: %v33", fset.Position(x.Pos()), x.Name)34 }35 })36 fmt.Println("Hello, playground")37}38import (39func main() {40 f, err := parser.ParseFile(fset, "2.go", nil, 0)

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "2.go", nil, parser.ParseComments)4 if err != nil {5 panic(err)6 }7 ast.Print(fset, f)8 for _, g := range f.Comments {9 fmt.Printf("%v: %s10", fset.Position(g.Pos()), g.Text())11 }12}13import (14func main() {15 f, err := parser.ParseFile(fset, "3.go", nil, parser.ParseComments)16 if err != nil {17 panic(err)18 }19 ast.Print(fset, f)20 for _, g := range f.Comments {21 fmt.Printf("%v: %s22", fset.Position(g.Pos()), g.Text())23 }24}25import (26func main() {27 f, err := parser.ParseFile(fset, "4.go", nil, parser.ParseComments)28 if err != nil {29 panic(err)30 }31 ast.Print(fset, f)32 for _, g := range f.Comments {33 fmt.Printf("%v: %s34", fset.Position(g.Pos()), g.Text())35 }36}37import (38func main() {

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func init() {3 fmt.Println("init() function is called before main()")4}5func main() {6 fmt.Println("Hello World")7}8init() function is called before main()92. init() method of main package10import (11func init() {12 fmt.Println("init() function is called before main()")13}14func main() {15 fmt.Println("Hello World")16}17init() function is called before main()183. init() method of imported packages19import (20func init() {21 fmt.Println("init() function is called before main()")22}23func main() {24 fmt.Println("Hello World")25}26init() function is called before main()274. init() method of imported packages28import (29func init() {30 fmt.Println("init() function is called before main()")31}32func main() {33 fmt.Println("Hello World")34}35init() function is called before main()365. init() method of imported packages37The init() method of the imported packages is

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful