How to use TestData method of compiler Package

Best Syzkaller code snippet using compiler.TestData

gcimporter_test.go

Source:gcimporter_test.go Github

copy

Full Screen

1// Copyright 2011 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4// This file is a copy of $GOROOT/src/go/internal/gcimporter/gcimporter_test.go,5// adjusted to make it build with code from (std lib) internal/testenv copied.6package gcimporter7import (8 "bytes"9 "fmt"10 "go/types"11 "io/ioutil"12 "os"13 "os/exec"14 "path/filepath"15 "runtime"16 "strings"17 "testing"18 "time"19 "golang.org/x/tools/internal/testenv"20)21func TestMain(m *testing.M) {22 testenv.ExitIfSmallMachine()23 os.Exit(m.Run())24}25// ----------------------------------------------------------------------------26// The following three functions (Builder, HasGoBuild, MustHaveGoBuild) were27// copied from $GOROOT/src/internal/testenv since that package is not available28// in x/tools.29// Builder reports the name of the builder running this test30// (for example, "linux-amd64" or "windows-386-gce").31// If the test is not running on the build infrastructure,32// Builder returns the empty string.33func Builder() string {34 return os.Getenv("GO_BUILDER_NAME")35}36// HasGoBuild reports whether the current system can build programs with ``go build''37// and then run them with os.StartProcess or exec.Command.38func HasGoBuild() bool {39 switch runtime.GOOS {40 case "android", "nacl":41 return false42 case "darwin":43 if strings.HasPrefix(runtime.GOARCH, "arm") {44 return false45 }46 }47 return true48}49// MustHaveGoBuild checks that the current system can build programs with ``go build''50// and then run them with os.StartProcess or exec.Command.51// If not, MustHaveGoBuild calls t.Skip with an explanation.52func MustHaveGoBuild(t *testing.T) {53 testenv.NeedsTool(t, "go")54 if !HasGoBuild() {55 t.Skipf("skipping test: 'go build' not available on %s/%s", runtime.GOOS, runtime.GOARCH)56 }57}58// ----------------------------------------------------------------------------59// skipSpecialPlatforms causes the test to be skipped for platforms where60// builders (build.golang.org) don't have access to compiled packages for61// import.62func skipSpecialPlatforms(t *testing.T) {63 switch platform := runtime.GOOS + "-" + runtime.GOARCH; platform {64 case "nacl-amd64p32",65 "nacl-386",66 "nacl-arm",67 "darwin-arm",68 "darwin-arm64":69 t.Skipf("no compiled packages available for import on %s", platform)70 }71}72// compile runs the compiler on filename, with dirname as the working directory,73// and writes the output file to outdirname.74func compile(t *testing.T, dirname, filename, outdirname string) string {75 /* testenv. */ MustHaveGoBuild(t)76 // filename must end with ".go"77 if !strings.HasSuffix(filename, ".go") {78 t.Fatalf("filename doesn't end in .go: %s", filename)79 }80 basename := filepath.Base(filename)81 outname := filepath.Join(outdirname, basename[:len(basename)-2]+"o")82 cmd := exec.Command("go", "tool", "compile", "-o", outname, filename)83 cmd.Dir = dirname84 out, err := cmd.CombinedOutput()85 if err != nil {86 t.Logf("%s", out)87 t.Fatalf("go tool compile %s failed: %s", filename, err)88 }89 return outname90}91func testPath(t *testing.T, path, srcDir string) *types.Package {92 t0 := time.Now()93 pkg, err := Import(make(map[string]*types.Package), path, srcDir, nil)94 if err != nil {95 t.Errorf("testPath(%s): %s", path, err)96 return nil97 }98 t.Logf("testPath(%s): %v", path, time.Since(t0))99 return pkg100}101const maxTime = 30 * time.Second102func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) {103 dirname := filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir)104 list, err := ioutil.ReadDir(dirname)105 if err != nil {106 t.Fatalf("testDir(%s): %s", dirname, err)107 }108 for _, f := range list {109 if time.Now().After(endTime) {110 t.Log("testing time used up")111 return112 }113 switch {114 case !f.IsDir():115 // try extensions116 for _, ext := range pkgExts {117 if strings.HasSuffix(f.Name(), ext) {118 name := f.Name()[0 : len(f.Name())-len(ext)] // remove extension119 if testPath(t, filepath.Join(dir, name), dir) != nil {120 nimports++121 }122 }123 }124 case f.IsDir():125 nimports += testDir(t, filepath.Join(dir, f.Name()), endTime)126 }127 }128 return129}130func mktmpdir(t *testing.T) string {131 tmpdir, err := ioutil.TempDir("", "gcimporter_test")132 if err != nil {133 t.Fatal("mktmpdir:", err)134 }135 if err := os.Mkdir(filepath.Join(tmpdir, "testdata"), 0700); err != nil {136 os.RemoveAll(tmpdir)137 t.Fatal("mktmpdir:", err)138 }139 return tmpdir140}141const testfile = "exports.go"142func TestImportTestdata(t *testing.T) {143 // This package only handles gc export data.144 if runtime.Compiler != "gc" {145 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)146 }147 tmpdir := mktmpdir(t)148 defer os.RemoveAll(tmpdir)149 compile(t, "testdata", testfile, filepath.Join(tmpdir, "testdata"))150 // filename should end with ".go"151 filename := testfile[:len(testfile)-3]152 if pkg := testPath(t, "./testdata/"+filename, tmpdir); pkg != nil {153 // The package's Imports list must include all packages154 // explicitly imported by testfile, plus all packages155 // referenced indirectly via exported objects in testfile.156 // With the textual export format (when run against Go1.6),157 // the list may also include additional packages that are158 // not strictly required for import processing alone (they159 // are exported to err "on the safe side").160 // For now, we just test the presence of a few packages161 // that we know are there for sure.162 got := fmt.Sprint(pkg.Imports())163 for _, want := range []string{"go/ast", "go/token"} {164 if !strings.Contains(got, want) {165 t.Errorf(`Package("exports").Imports() = %s, does not contain %s`, got, want)166 }167 }168 }169}170func TestVersionHandling(t *testing.T) {171 skipSpecialPlatforms(t) // we really only need to exclude nacl platforms, but this is fine172 // This package only handles gc export data.173 if runtime.Compiler != "gc" {174 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)175 }176 const dir = "./testdata/versions"177 list, err := ioutil.ReadDir(dir)178 if err != nil {179 t.Fatal(err)180 }181 tmpdir := mktmpdir(t)182 defer os.RemoveAll(tmpdir)183 corruptdir := filepath.Join(tmpdir, "testdata", "versions")184 if err := os.Mkdir(corruptdir, 0700); err != nil {185 t.Fatal(err)186 }187 for _, f := range list {188 name := f.Name()189 if !strings.HasSuffix(name, ".a") {190 continue // not a package file191 }192 if strings.Contains(name, "corrupted") {193 continue // don't process a leftover corrupted file194 }195 pkgpath := "./" + name[:len(name)-2]196 if testing.Verbose() {197 t.Logf("importing %s", name)198 }199 // test that export data can be imported200 _, err := Import(make(map[string]*types.Package), pkgpath, dir, nil)201 if err != nil {202 // ok to fail if it fails with a newer version error for select files203 if strings.Contains(err.Error(), "newer version") {204 switch name {205 case "test_go1.11_999b.a", "test_go1.11_999i.a":206 continue207 }208 // fall through209 }210 t.Errorf("import %q failed: %v", pkgpath, err)211 continue212 }213 // create file with corrupted export data214 // 1) read file215 data, err := ioutil.ReadFile(filepath.Join(dir, name))216 if err != nil {217 t.Fatal(err)218 }219 // 2) find export data220 i := bytes.Index(data, []byte("\n$$B\n")) + 5221 j := bytes.Index(data[i:], []byte("\n$$\n")) + i222 if i < 0 || j < 0 || i > j {223 t.Fatalf("export data section not found (i = %d, j = %d)", i, j)224 }225 // 3) corrupt the data (increment every 7th byte)226 for k := j - 13; k >= i; k -= 7 {227 data[k]++228 }229 // 4) write the file230 pkgpath += "_corrupted"231 filename := filepath.Join(corruptdir, pkgpath) + ".a"232 ioutil.WriteFile(filename, data, 0666)233 // test that importing the corrupted file results in an error234 _, err = Import(make(map[string]*types.Package), pkgpath, corruptdir, nil)235 if err == nil {236 t.Errorf("import corrupted %q succeeded", pkgpath)237 } else if msg := err.Error(); !strings.Contains(msg, "version skew") {238 t.Errorf("import %q error incorrect (%s)", pkgpath, msg)239 }240 }241}242func TestImportStdLib(t *testing.T) {243 skipSpecialPlatforms(t)244 // This package only handles gc export data.245 if runtime.Compiler != "gc" {246 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)247 }248 dt := maxTime249 if testing.Short() && /* testenv. */ Builder() == "" {250 dt = 10 * time.Millisecond251 }252 nimports := testDir(t, "", time.Now().Add(dt)) // installed packages253 t.Logf("tested %d imports", nimports)254}255func TestIssue5815(t *testing.T) {256 skipSpecialPlatforms(t)257 // This package only handles gc export data.258 if runtime.Compiler != "gc" {259 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)260 }261 pkg := importPkg(t, "strings", ".")262 scope := pkg.Scope()263 for _, name := range scope.Names() {264 obj := scope.Lookup(name)265 if obj.Pkg() == nil {266 t.Errorf("no pkg for %s", obj)267 }268 if tname, _ := obj.(*types.TypeName); tname != nil {269 named := tname.Type().(*types.Named)270 for i := 0; i < named.NumMethods(); i++ {271 m := named.Method(i)272 if m.Pkg() == nil {273 t.Errorf("no pkg for %s", m)274 }275 }276 }277 }278}279// Smoke test to ensure that imported methods get the correct package.280func TestCorrectMethodPackage(t *testing.T) {281 skipSpecialPlatforms(t)282 // This package only handles gc export data.283 if runtime.Compiler != "gc" {284 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)285 }286 imports := make(map[string]*types.Package)287 _, err := Import(imports, "net/http", ".", nil)288 if err != nil {289 t.Fatal(err)290 }291 mutex := imports["sync"].Scope().Lookup("Mutex").(*types.TypeName).Type()292 mset := types.NewMethodSet(types.NewPointer(mutex)) // methods of *sync.Mutex293 sel := mset.Lookup(nil, "Lock")294 lock := sel.Obj().(*types.Func)295 if got, want := lock.Pkg().Path(), "sync"; got != want {296 t.Errorf("got package path %q; want %q", got, want)297 }298}299func TestIssue13566(t *testing.T) {300 skipSpecialPlatforms(t)301 // This package only handles gc export data.302 if runtime.Compiler != "gc" {303 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)304 }305 // On windows, we have to set the -D option for the compiler to avoid having a drive306 // letter and an illegal ':' in the import path - just skip it (see also issue #3483).307 if runtime.GOOS == "windows" {308 t.Skip("avoid dealing with relative paths/drive letters on windows")309 }310 tmpdir := mktmpdir(t)311 defer os.RemoveAll(tmpdir)312 testoutdir := filepath.Join(tmpdir, "testdata")313 // b.go needs to be compiled from the output directory so that the compiler can314 // find the compiled package a. We pass the full path to compile() so that we315 // don't have to copy the file to that directory.316 bpath, err := filepath.Abs(filepath.Join("testdata", "b.go"))317 if err != nil {318 t.Fatal(err)319 }320 compile(t, "testdata", "a.go", testoutdir)321 compile(t, testoutdir, bpath, testoutdir)322 // import must succeed (test for issue at hand)323 pkg := importPkg(t, "./testdata/b", tmpdir)324 // make sure all indirectly imported packages have names325 for _, imp := range pkg.Imports() {326 if imp.Name() == "" {327 t.Errorf("no name for %s package", imp.Path())328 }329 }330}331func TestIssue13898(t *testing.T) {332 skipSpecialPlatforms(t)333 // This package only handles gc export data.334 if runtime.Compiler != "gc" {335 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)336 }337 // import go/internal/gcimporter which imports go/types partially338 imports := make(map[string]*types.Package)339 _, err := Import(imports, "go/internal/gcimporter", ".", nil)340 if err != nil {341 t.Fatal(err)342 }343 // look for go/types package344 var goTypesPkg *types.Package345 for path, pkg := range imports {346 if path == "go/types" {347 goTypesPkg = pkg348 break349 }350 }351 if goTypesPkg == nil {352 t.Fatal("go/types not found")353 }354 // look for go/types.Object type355 obj := lookupObj(t, goTypesPkg.Scope(), "Object")356 typ, ok := obj.Type().(*types.Named)357 if !ok {358 t.Fatalf("go/types.Object type is %v; wanted named type", typ)359 }360 // lookup go/types.Object.Pkg method361 m, index, indirect := types.LookupFieldOrMethod(typ, false, nil, "Pkg")362 if m == nil {363 t.Fatalf("go/types.Object.Pkg not found (index = %v, indirect = %v)", index, indirect)364 }365 // the method must belong to go/types366 if m.Pkg().Path() != "go/types" {367 t.Fatalf("found %v; want go/types", m.Pkg())368 }369}370func TestIssue15517(t *testing.T) {371 skipSpecialPlatforms(t)372 // This package only handles gc export data.373 if runtime.Compiler != "gc" {374 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)375 }376 // On windows, we have to set the -D option for the compiler to avoid having a drive377 // letter and an illegal ':' in the import path - just skip it (see also issue #3483).378 if runtime.GOOS == "windows" {379 t.Skip("avoid dealing with relative paths/drive letters on windows")380 }381 tmpdir := mktmpdir(t)382 defer os.RemoveAll(tmpdir)383 compile(t, "testdata", "p.go", filepath.Join(tmpdir, "testdata"))384 // Multiple imports of p must succeed without redeclaration errors.385 // We use an import path that's not cleaned up so that the eventual386 // file path for the package is different from the package path; this387 // will expose the error if it is present.388 //389 // (Issue: Both the textual and the binary importer used the file path390 // of the package to be imported as key into the shared packages map.391 // However, the binary importer then used the package path to identify392 // the imported package to mark it as complete; effectively marking the393 // wrong package as complete. By using an "unclean" package path, the394 // file and package path are different, exposing the problem if present.395 // The same issue occurs with vendoring.)396 imports := make(map[string]*types.Package)397 for i := 0; i < 3; i++ {398 if _, err := Import(imports, "./././testdata/p", tmpdir, nil); err != nil {399 t.Fatal(err)400 }401 }402}403func TestIssue15920(t *testing.T) {404 skipSpecialPlatforms(t)405 // This package only handles gc export data.406 if runtime.Compiler != "gc" {407 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)408 }409 // On windows, we have to set the -D option for the compiler to avoid having a drive410 // letter and an illegal ':' in the import path - just skip it (see also issue #3483).411 if runtime.GOOS == "windows" {412 t.Skip("avoid dealing with relative paths/drive letters on windows")413 }414 compileAndImportPkg(t, "issue15920")415}416func TestIssue20046(t *testing.T) {417 skipSpecialPlatforms(t)418 // This package only handles gc export data.419 if runtime.Compiler != "gc" {420 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)421 }422 // On windows, we have to set the -D option for the compiler to avoid having a drive423 // letter and an illegal ':' in the import path - just skip it (see also issue #3483).424 if runtime.GOOS == "windows" {425 t.Skip("avoid dealing with relative paths/drive letters on windows")426 }427 // "./issue20046".V.M must exist428 pkg := compileAndImportPkg(t, "issue20046")429 obj := lookupObj(t, pkg.Scope(), "V")430 if m, index, indirect := types.LookupFieldOrMethod(obj.Type(), false, nil, "M"); m == nil {431 t.Fatalf("V.M not found (index = %v, indirect = %v)", index, indirect)432 }433}434func importPkg(t *testing.T, path, srcDir string) *types.Package {435 pkg, err := Import(make(map[string]*types.Package), path, srcDir, nil)436 if err != nil {437 t.Fatal(err)438 }439 return pkg440}441func compileAndImportPkg(t *testing.T, name string) *types.Package {442 tmpdir := mktmpdir(t)443 defer os.RemoveAll(tmpdir)444 compile(t, "testdata", name+".go", filepath.Join(tmpdir, "testdata"))445 return importPkg(t, "./testdata/"+name, tmpdir)446}447func lookupObj(t *testing.T, scope *types.Scope, name string) types.Object {448 if obj := scope.Lookup(name); obj != nil {449 return obj450 }451 t.Fatalf("%s not found", name)452 return nil453}...

Full Screen

Full Screen

compiler_test.go

Source:compiler_test.go Github

copy

Full Screen

1package compiler2import (3 "testing"4 "github.com/stretchr/testify/assert"5 "github.com/stretchr/testify/require"6)7func testCompiler(t *testing.T) *compiler {8 opts := Options{9 ImportPath: []string{"../testdata"},10 }11 c, err := newCompiler(opts)12 if err != nil {13 t.Fatal(err)14 }15 return c16}17// Package18func TestCompiler__should_compile_package(t *testing.T) {19 c := testCompiler(t)20 pkg, err := c.Compile("../testdata/pkg1")21 if err != nil {22 t.Fatal(err)23 }24 assert.Equal(t, "pkg1", pkg.Name)25 assert.Equal(t, "../testdata/pkg1", pkg.ID)26}27// File28func TestCompiler__should_compile_files(t *testing.T) {29 c := testCompiler(t)30 pkg, err := c.Compile("../testdata/pkg1")31 if err != nil {32 t.Fatal(err)33 }34 assert.Len(t, pkg.Files, 2)35 file0 := pkg.Files[0]36 file1 := pkg.Files[1]37 assert.Equal(t, "enum.spec", file0.Name)38 assert.Equal(t, "pkg1.spec", file1.Name)39}40// Imports41func TestCompiler__should_compile_imports(t *testing.T) {42 c := testCompiler(t)43 pkg, err := c.Compile("../testdata/pkg1")44 if err != nil {45 t.Fatal(err)46 }47 file0 := pkg.Files[0]48 file1 := pkg.Files[1]49 assert.Len(t, file0.Imports, 0)50 assert.Len(t, file1.Imports, 1)51}52func TestCompiler__should_resolve_imports(t *testing.T) {53 c := testCompiler(t)54 pkg, err := c.Compile("../testdata/pkg1")55 if err != nil {56 t.Fatal(err)57 }58 file := pkg.FileNames["pkg1.spec"]59 require.NotNil(t, file)60 imp := file.Imports[0]61 assert.True(t, imp.Resolved)62 pkg2 := imp.Package63 require.NotNil(t, pkg2)64 assert.Equal(t, "pkg2", pkg2.ID)65}66func TestCompiler__should_recursively_resolve_imports(t *testing.T) {67 c := testCompiler(t)68 pkg, err := c.Compile("../testdata/pkg1")69 if err != nil {70 t.Fatal(err)71 }72 file := pkg.FileNames["pkg1.spec"]73 require.NotNil(t, file)74 imp := file.Imports[0]75 assert.True(t, imp.Resolved)76 pkg2 := imp.Package77 require.NotNil(t, pkg2)78 imp2 := pkg2.Files[0].Imports[0]79 require.NotNil(t, imp2)80 assert.Equal(t, "sub/pkg3", imp2.ID)81 assert.NotNil(t, imp2.Package)82 assert.True(t, imp2.Resolved)83}84// Options85func TestCompiler__should_compile_options(t *testing.T) {86 c := testCompiler(t)87 pkg, err := c.Compile("../testdata/pkg1")88 if err != nil {89 t.Fatal(err)90 }91 file0 := pkg.Files[0]92 file1 := pkg.Files[1]93 assert.Len(t, file0.Options, 0)94 assert.Len(t, file1.Options, 1)95 gopkg := file1.OptionMap["go_package"]96 require.NotNil(t, gopkg)97 assert.Equal(t, "github.com/complex1tech/spec/lang/testgen/golang/pkg1", gopkg.Value)98}99// Definitions100func TestCompiler__should_compile_file_definitions(t *testing.T) {101 c := testCompiler(t)102 pkg, err := c.Compile("../testdata/pkg1")103 if err != nil {104 t.Fatal(err)105 }106 file0 := pkg.Files[0]107 file1 := pkg.Files[1]108 assert.Len(t, file0.Definitions, 1)109 assert.Len(t, file1.Definitions, 3)110}111func TestCompiler__should_compile_package_definitions(t *testing.T) {112 c := testCompiler(t)113 pkg, err := c.Compile("../testdata/pkg1")114 if err != nil {115 t.Fatal(err)116 }117 assert.Len(t, pkg.Definitions, 4)118 assert.Contains(t, pkg.DefinitionNames, "Enum")119 assert.Contains(t, pkg.DefinitionNames, "Message")120 assert.Contains(t, pkg.DefinitionNames, "Node")121 assert.Contains(t, pkg.DefinitionNames, "Struct")122}123// Enums124func TestCompiler__should_compile_enum(t *testing.T) {125 c := testCompiler(t)126 pkg, err := c.Compile("../testdata/pkg1")127 if err != nil {128 t.Fatal(err)129 }130 def := pkg.Files[0].Definitions[0]131 assert.Equal(t, DefinitionEnum, def.Type)132 assert.NotNil(t, def.Enum)133 assert.Len(t, def.Enum.Values, 5)134}135func TestCompiler__should_compile_enum_values(t *testing.T) {136 c := testCompiler(t)137 pkg, err := c.Compile("../testdata/pkg1")138 if err != nil {139 t.Fatal(err)140 }141 def := pkg.Files[0].Definitions[0]142 require.Equal(t, DefinitionEnum, def.Type)143 enum := def.Enum144 assert.Contains(t, enum.ValueNumbers, 0)145 assert.Contains(t, enum.ValueNumbers, 1)146 assert.Contains(t, enum.ValueNumbers, 2)147 assert.Contains(t, enum.ValueNumbers, 3)148 assert.Contains(t, enum.ValueNumbers, 10)149}150func TestCompiler__should_compile_enum_value_names(t *testing.T) {151 c := testCompiler(t)152 pkg, err := c.Compile("../testdata/pkg1")153 if err != nil {154 t.Fatal(err)155 }156 def := pkg.Files[0].Definitions[0]157 require.Equal(t, DefinitionEnum, def.Type)158 enum := def.Enum159 assert.Contains(t, enum.ValueNames, "UNDEFINED")160 assert.Contains(t, enum.ValueNames, "ONE")161 assert.Contains(t, enum.ValueNames, "TWO")162 assert.Contains(t, enum.ValueNames, "THREE")163 assert.Contains(t, enum.ValueNames, "TEN")164}165// Messages166func TestCompiler__should_compile_message(t *testing.T) {167 c := testCompiler(t)168 pkg, err := c.Compile("../testdata/pkg1")169 if err != nil {170 t.Fatal(err)171 }172 def := pkg.Files[1].Definitions[0]173 assert.Equal(t, DefinitionMessage, def.Type)174 assert.NotNil(t, def.Message)175 assert.Len(t, def.Message.Fields, 22)176}177func TestCompiler__should_compile_message_field_names(t *testing.T) {178 c := testCompiler(t)179 pkg, err := c.Compile("../testdata/pkg1")180 if err != nil {181 t.Fatal(err)182 }183 def := pkg.Files[1].Definitions[0]184 require.Equal(t, DefinitionMessage, def.Type)185 msg := def.Message186 require.Len(t, def.Message.FieldNames, 22)187 assert.Contains(t, msg.FieldNames, "field_bool")188 assert.Contains(t, msg.FieldNames, "field_enum")189 assert.Contains(t, msg.FieldNames, "field_byte")190}191func TestCompiler__should_compile_message_field_tags(t *testing.T) {192 c := testCompiler(t)193 pkg, err := c.Compile("../testdata/pkg1")194 if err != nil {195 t.Fatal(err)196 }197 def := pkg.Files[1].Definitions[0]198 require.Equal(t, DefinitionMessage, def.Type)199 msg := def.Message200 require.Len(t, def.Message.FieldTags, 22)201 assert.Contains(t, msg.FieldTags, 1)202 assert.Contains(t, msg.FieldTags, 2)203 assert.Contains(t, msg.FieldTags, 10)204}205// Structs206func TestCompiler__should_compile_struct(t *testing.T) {207 c := testCompiler(t)208 pkg, err := c.Compile("../testdata/pkg1")209 if err != nil {210 t.Fatal(err)211 }212 def := pkg.Files[1].DefinitionNames["Struct"]213 assert.Equal(t, DefinitionStruct, def.Type)214 assert.NotNil(t, def.Struct)215 assert.Len(t, def.Struct.Fields, 2)216}217func TestCompiler__should_compile_struct_field_names(t *testing.T) {218 c := testCompiler(t)219 pkg, err := c.Compile("../testdata/pkg1")220 if err != nil {221 t.Fatal(err)222 }223 def := pkg.Files[1].DefinitionNames["Struct"]224 assert.Equal(t, DefinitionStruct, def.Type)225 str := def.Struct226 require.NotNil(t, str)227 require.Len(t, str.Fields, 2)228 assert.Contains(t, str.FieldNames, "key")229 assert.Contains(t, str.FieldNames, "value")230}231// Types232func TestCompiler__should_compile_builtin_type(t *testing.T) {233 c := testCompiler(t)234 pkg, err := c.Compile("../testdata/pkg1")235 if err != nil {236 t.Fatal(err)237 }238 def := pkg.Files[1].DefinitionNames["Message"]239 require.NotNil(t, def.Message)240 field := def.Message.FieldNames["field_bool"]241 require.NotNil(t, field)242 type_ := field.Type243 assert.Equal(t, "bool", type_.Name)244 assert.Equal(t, KindBool, type_.Kind)245}246func TestCompiler__should_compile_reference_type(t *testing.T) {247 c := testCompiler(t)248 pkg, err := c.Compile("../testdata/pkg1")249 if err != nil {250 t.Fatal(err)251 }252 def := pkg.Files[1].DefinitionNames["Message"]253 require.NotNil(t, def.Message)254 field := def.Message.FieldNames["node"]255 require.NotNil(t, field)256 // resolved257 type_ := field.Type258 assert.Equal(t, "Node", type_.Name)259 assert.Equal(t, KindMessage, type_.Kind)260 assert.NotNil(t, type_.Ref)261 assert.Equal(t, "Node", type_.Ref.Name)262}263func TestCompiler__should_compile_imported_type(t *testing.T) {264 c := testCompiler(t)265 pkg, err := c.Compile("../testdata/pkg1")266 if err != nil {267 t.Fatal(err)268 }269 def := pkg.Files[1].DefinitionNames["Message"]270 require.NotNil(t, def.Message)271 field := def.Message.FieldNames["imported"]272 require.NotNil(t, field)273 // resolved274 type_ := field.Type275 assert.Equal(t, "Submessage", type_.Name)276 assert.Equal(t, "pkg2", type_.ImportName)277 assert.Equal(t, KindMessage, type_.Kind)278 assert.NotNil(t, type_.Ref)279 assert.NotNil(t, type_.Import)280}281func TestCompiler__should_compile_list_type(t *testing.T) {282 c := testCompiler(t)283 pkg, err := c.Compile("../testdata/pkg1")284 if err != nil {285 t.Fatal(err)286 }287 def := pkg.Files[1].DefinitionNames["Message"]288 require.NotNil(t, def.Message)289 field := def.Message.FieldNames["list_values"]290 require.NotNil(t, field)291 // list292 type_ := field.Type293 assert.Equal(t, KindList, type_.Kind)294 // element295 elem := type_.Element296 require.NotNil(t, elem)297 assert.Equal(t, KindStruct, elem.Kind)298}299func TestCompiler__should_compile_list_reference_type(t *testing.T) {300 c := testCompiler(t)301 pkg, err := c.Compile("../testdata/pkg1")302 if err != nil {303 t.Fatal(err)304 }305 def := pkg.Files[1].DefinitionNames["Message"]306 require.NotNil(t, def.Message)307 field := def.Message.FieldNames["list_messages"]308 require.NotNil(t, field)309 // list310 type_ := field.Type311 assert.Equal(t, KindList, type_.Kind)312 // element313 elem := type_.Element314 require.NotNil(t, elem)315 assert.Equal(t, KindMessage, elem.Kind)316 assert.Equal(t, "Node", elem.Name)317 assert.NotNil(t, elem.Ref)318}319func TestCompiler__should_compile_list_imported_type(t *testing.T) {320 c := testCompiler(t)321 pkg, err := c.Compile("../testdata/pkg1")322 if err != nil {323 t.Fatal(err)324 }325 def := pkg.Files[1].DefinitionNames["Message"]326 require.NotNil(t, def.Message)327 field := def.Message.FieldNames["list_imported"]328 require.NotNil(t, field)329 // list330 type_ := field.Type331 assert.Equal(t, KindList, type_.Kind)332 require.NotNil(t, type_.Element)333 // element334 elem := type_.Element335 assert.Equal(t, KindMessage, elem.Kind)336 assert.Equal(t, "Submessage", elem.Name)337 assert.Equal(t, "pkg2", elem.ImportName)338 assert.NotNil(t, elem.Ref)339 assert.NotNil(t, elem.Import)340}...

Full Screen

Full Screen

TestData

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

TestData

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 ret = max(a, b)4 fmt.Printf( "Max value is : %d5}6func max(num1, num2 int) int {7 if (num1 > num2) {8 } else {9 }10}

Full Screen

Full Screen

TestData

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

TestData

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "compiler"3func main() {4 var x = compiler.TestData()5 fmt.Println(x)6}7import "fmt"8import "compiler"9func main() {10 var x = compiler.TestData()11 fmt.Println(x)12}13import "fmt"14import "compiler"15func main() {16 var x = compiler.TestData()17 fmt.Println(x)18}19import "fmt"20import "compiler"21func main() {22 var x = compiler.TestData()23 fmt.Println(x)24}25import "fmt"26import "compiler"27func main() {28 var x = compiler.TestData()29 fmt.Println(x)30}31import "fmt"32import "compiler"33func main() {34 var x = compiler.TestData()35 fmt.Println(x)36}37import "fmt"38import "compiler"39func main() {40 var x = compiler.TestData()41 fmt.Println(x)42}43import "fmt"44import "compiler"45func main() {46 var x = compiler.TestData()47 fmt.Println(x)48}49import "fmt"50import "compiler"51func main() {52 var x = compiler.TestData()53 fmt.Println(x)54}55import "fmt"56import "compiler"57func main() {58 var x = compiler.TestData()59 fmt.Println(x)60}61import "fmt"62import "compiler"63func main() {64 var x = compiler.TestData()65 fmt.Println(x)66}

Full Screen

Full Screen

TestData

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := new(Compiler)4 c.TestData()5 c.Compile()6 c.Run()7}8import (9func main() {10 c := new(Compiler)11 c.TestData()12 c.Compile()13 c.Run()14}15import (16func main() {17 c := new(Compiler)18 c.TestData()19 c.Compile()20 c.Run()21}22import (23func main() {24 c := new(Compiler)25 c.TestData()26 c.Compile()27 c.Run()28}29import (30func main() {31 c := new(Compiler)32 c.TestData()33 c.Compile()34 c.Run()35}36import (37func main() {38 c := new(Compiler)

Full Screen

Full Screen

TestData

Using AI Code Generation

copy

Full Screen

1import (2type Test struct {3}4func main() {5 t := Test{A: 10, B: "Hello"}6 fmt.Println(reflect.ValueOf(t).FieldByName("A").CanSet())7 fmt.Println(reflect.ValueOf(t).FieldByName("B").CanSet())8}9import (10type Test struct {11}12func main() {13 t := Test{A: 10, B: "Hello"}14 fmt.Println(reflect.ValueOf(t).CanAddr())15}16import (

Full Screen

Full Screen

TestData

Using AI Code Generation

copy

Full Screen

1import (2type MyStruct struct {3}4func main() {5 ms := MyStruct{"Hello"}6 fmt.Println(ms)7 fmt.Println(reflect.TypeOf(ms))8 fmt.Println(reflect.ValueOf(ms))9 fmt.Println(reflect.ValueOf(ms).FieldByName("Name"))10}11import (12type MyStruct struct {13}14func main() {15 ms := MyStruct{"Hello"}16 fmt.Println(ms)17 fmt.Println(reflect.TypeOf(ms))18 fmt.Println(reflect.ValueOf(ms))19 fmt.Println(reflect.ValueOf(ms).FieldByName("Name"))20 reflect.ValueOf(&ms).Elem().FieldByName("Name").SetString("World")21 fmt.Println(ms)22}23import (24type MyStruct struct {25}26func main() {27 ms := MyStruct{"Hello"}28 fmt.Println(ms)29 fmt.Println(reflect.TypeOf(ms))30 fmt.Println(reflect.ValueOf(ms))31 fmt.Println(reflect.ValueOf(ms).FieldByName("Name"))32 reflect.ValueOf(&ms).Elem().FieldByName("Name").SetString("World")33 fmt.Println(ms)34 fmt.Println(reflect.ValueOf(&ms).Elem().FieldByName("Name").CanSet())35}36import (37type MyStruct struct {38}39func main() {40 ms := MyStruct{"Hello"}41 fmt.Println(ms)42 fmt.Println(reflect.TypeOf(ms))43 fmt.Println(reflect.ValueOf(ms))44 fmt.Println(reflect.ValueOf(ms).FieldByName("Name"))45 reflect.ValueOf(&ms).Elem().FieldByName("Name").SetString("World")46 fmt.Println(ms)47 fmt.Println(reflect.ValueOf(&ms).Elem().FieldByName("Name").CanSet())48 reflect.ValueOf(&ms).Elem().FieldByName("Name").SetString("Universe")49 fmt.Println(ms)50}51import (52type MyStruct struct {53}54func main() {55 ms := MyStruct{"

Full Screen

Full Screen

TestData

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c.Init()4 fmt.Println(c.TestData())5}6import (7func main() {8 c.Init()9 fmt.Println(c.GetSymbols())10}11import (12func main() {13 c.Init()14 fmt.Println(c.GetSymbolsAtOffset(10))15}16import (17func main() {18 c.Init()19 fmt.Println(c.GetSymbolsAtLine(10))20}21import (22func main() {23 c.Init()24 fmt.Println(c.GetSymbolsInFile(10))25}26import (27func main() {28 c.Init()29 fmt.Println(c.GetSymbolsInPackage(10))30}31import (32func main() {33 c.Init()34 fmt.Println(c.GetSymbolsInScope(10))35}36import (37func main() {38 c.Init()39 fmt.Println(c.GetSymbolsInScopeAtOffset

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 Syzkaller 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