How to use newContextWithConfig method of td Package

Best Go-testdeep code snippet using td.newContextWithConfig

template.go

Source:template.go Github

copy

Full Screen

1package asciidoc2import (3 "bytes"4 "strings"5 "text/template"6 "github.com/mariotoffia/goasciidoc/goparser"7)8// TemplateType specifies the template type9type TemplateType string10const (11 // IndexTemplate is a template that binds all generated asciidoc files into one single index file12 // by referencing (or appending to this file).13 IndexTemplate TemplateType = "index"14 // PackageTemplate specifies that the template is a package15 PackageTemplate TemplateType = "package"16 // ImportTemplate specifies that the template renders a import17 ImportTemplate TemplateType = "import"18 // FunctionsTemplate is a template to render all functions for a given context (package, file)19 FunctionsTemplate TemplateType = "functions"20 // FunctionTemplate is a template to render a function21 FunctionTemplate TemplateType = "function"22 // InterfacesTemplate is a template to render a all interface defintions for a given context (package, file)23 InterfacesTemplate TemplateType = "interfaces"24 // InterfaceTemplate is a template to render a interface definition25 InterfaceTemplate TemplateType = "interface"26 // StructsTemplate specifies that the template renders all struct definitions for a given context (package, file)27 StructsTemplate TemplateType = "structs"28 // StructTemplate specifies that the template renders a struct definition29 StructTemplate TemplateType = "struct"30 // CustomVarTypeDefsTemplate is a template to render all variable type definitions for a given context (package, file)31 CustomVarTypeDefsTemplate TemplateType = "typedefvars"32 // CustomVarTypeDefTemplate is a template to render a type definition of a variable33 CustomVarTypeDefTemplate TemplateType = "typedefvar"34 // CustomFuncTypeDefsTemplate is a template to render all function type definitions for a given context (package, file)35 CustomFuncTypeDefsTemplate TemplateType = "typedeffuncs"36 // CustomFuncTypeDefTemplate is a template to render a function type definition37 CustomFuncTypeDefTemplate TemplateType = "typedeffunc"38 // VarDeclarationsTemplate is a template to render all variable definitions for a given context (package, file)39 VarDeclarationsTemplate TemplateType = "vars"40 // VarDeclarationTemplate is a template to render a variable definition41 VarDeclarationTemplate TemplateType = "var"42 // ConstDeclarationsTemplate is a template to render all const declaration entries for a given context (package, file)43 ConstDeclarationsTemplate TemplateType = "consts"44 // ConstDeclarationTemplate is a template to render a const declaration entry45 ConstDeclarationTemplate TemplateType = "const"46 // ReceiversTemplate is a template that renders receivers functions47 ReceiversTemplate TemplateType = "receivers"48)49func (tt TemplateType) String() string {50 return string(tt)51}52// TemplateAndText is a wrapper of _template.Template_53// but also includes the original text representation54// of the template and not just the parsed tree.55type TemplateAndText struct {56 // Text is the actual template that got parsed by _template.Template_.57 Text string58 // Template is the instance of the parsed _Text_ including functions.59 Template *template.Template60}61// Template is handling all templates and actions62// to perform.63type Template struct {64 // Templates to use when rendering documentation65 Templates map[string]*TemplateAndText66}67// NewTemplateWithOverrides creates a new template with the ability to easily68// override defaults.69func NewTemplateWithOverrides(overrides map[string]string) *Template {70 return &Template{71 Templates: map[string]*TemplateAndText{72 IndexTemplate.String(): createTemplate(IndexTemplate, "", overrides, template.FuncMap{}),73 PackageTemplate.String(): createTemplate(PackageTemplate, "", overrides, template.FuncMap{}),74 ImportTemplate.String(): createTemplate(ImportTemplate, "", overrides, template.FuncMap{75 "render": func(t *TemplateContext) string { return t.File.DeclImports() },76 }),77 FunctionsTemplate.String(): createTemplate(FunctionsTemplate, "", overrides, template.FuncMap{78 "render": func(t *TemplateContext, f *goparser.GoStructMethod) string {79 var buf bytes.Buffer80 t.RenderFunction(&buf, f)81 return buf.String()82 },83 "notreceiver": func(t *TemplateContext, f *goparser.GoStructMethod) bool {84 return len(f.Receivers) == 085 },86 }),87 FunctionTemplate.String(): createTemplate(FunctionTemplate, "", overrides, template.FuncMap{}),88 InterfacesTemplate.String(): createTemplate(InterfacesTemplate, "", overrides, template.FuncMap{89 "render": func(t *TemplateContext, i *goparser.GoInterface) string {90 var buf bytes.Buffer91 t.RenderInterface(&buf, i)92 return buf.String()93 },94 }),95 InterfaceTemplate.String(): createTemplate(InterfaceTemplate, "", overrides, template.FuncMap{96 "tabifylast": func(decl string) string {97 idx := strings.LastIndex(decl, " ")98 if -1 == idx {99 return decl100 }101 return decl[:idx] + "\t" + decl[idx+1:]102 },103 }),104 StructsTemplate.String(): createTemplate(StructsTemplate, "", overrides, template.FuncMap{105 "render": func(t *TemplateContext, s *goparser.GoStruct) string {106 var buf bytes.Buffer107 t.RenderStruct(&buf, s)108 return buf.String()109 },110 }),111 StructTemplate.String(): createTemplate(StructTemplate, "", overrides, template.FuncMap{112 "tabify": func(decl string) string { return strings.Replace(decl, " ", "\t", 1) },113 "render": func(t *TemplateContext, s *goparser.GoStruct) string {114 var buf bytes.Buffer115 t.RenderStruct(&buf, s)116 return buf.String()117 },118 "renderReceivers": func(t *TemplateContext, receiver string) string {119 var buf bytes.Buffer120 t.RenderReceiverFunctions(&buf, receiver)121 return buf.String()122 },123 "hasReceivers": func(t *TemplateContext, receiver string) bool {124 if nil != t.Package {125 return len(t.Package.FindMethodsByReceiver(receiver)) > 0126 }127 return len(t.File.FindMethodsByReceiver(receiver)) > 0128 },129 }),130 ReceiversTemplate.String(): createTemplate(ReceiversTemplate, "", overrides, template.FuncMap{}),131 CustomVarTypeDefsTemplate.String(): createTemplate(CustomVarTypeDefsTemplate, "", overrides, template.FuncMap{132 "render": func(t *TemplateContext, td *goparser.GoCustomType) string {133 var buf bytes.Buffer134 t.RenderVarTypeDef(&buf, td)135 return buf.String()136 },137 }),138 CustomVarTypeDefTemplate.String(): createTemplate(CustomVarTypeDefTemplate, "", overrides, template.FuncMap{139 "renderReceivers": func(t *TemplateContext, receiver string) string {140 var buf bytes.Buffer141 t.RenderReceiverFunctions(&buf, receiver)142 return buf.String()143 },144 "hasReceivers": func(t *TemplateContext, receiver string) bool {145 if nil != t.Package {146 return len(t.Package.FindMethodsByReceiver(receiver)) > 0147 }148 return len(t.File.FindMethodsByReceiver(receiver)) > 0149 },150 }),151 VarDeclarationsTemplate.String(): createTemplate(VarDeclarationsTemplate, "", overrides, template.FuncMap{152 "render": func(t *TemplateContext, a *goparser.GoAssignment) string {153 var buf bytes.Buffer154 t.RenderVarDeclaration(&buf, a)155 return buf.String()156 },157 }),158 VarDeclarationTemplate.String(): createTemplate(VarDeclarationTemplate, "", overrides, template.FuncMap{}),159 ConstDeclarationsTemplate.String(): createTemplate(ConstDeclarationsTemplate, "", overrides, template.FuncMap{160 "tabify": func(decl string) string { return strings.Replace(decl, " ", "\t", 1) },161 "render": func(t *TemplateContext, a *goparser.GoAssignment) string {162 var buf bytes.Buffer163 t.RenderConstDeclaration(&buf, a)164 return buf.String()165 },166 }),167 ConstDeclarationTemplate.String(): createTemplate(ConstDeclarationTemplate, "", overrides, template.FuncMap{}),168 CustomFuncTypeDefsTemplate.String(): createTemplate(CustomFuncTypeDefsTemplate, "", overrides, template.FuncMap{169 "render": func(t *TemplateContext, td *goparser.GoMethod) string {170 var buf bytes.Buffer171 t.RenderTypeDefFunc(&buf, td)172 return buf.String()173 },174 }),175 CustomFuncTypeDefTemplate.String(): createTemplate(CustomFuncTypeDefTemplate, "", overrides, template.FuncMap{}),176 },177 }178}179// NewContext creates a new context to be used for rendering.180func (t *Template) NewContext(f *goparser.GoFile) *TemplateContext {181 return t.NewContextWithConfig(f, nil, &TemplateContextConfig{})182}183// NewContextWithConfig creates a new context with configuration.184//185// If configuration is nil, it will use default configuration.186func (t *Template) NewContextWithConfig(187 f *goparser.GoFile,188 p *goparser.GoPackage,189 config *TemplateContextConfig) *TemplateContext {190 if nil == config {191 config = &TemplateContextConfig{}192 }193 tc := &TemplateContext{194 creator: t,195 File: f,196 Package: p,197 Module: f.Module,198 Config: config,199 Docs: map[string]string{},200 }201 return tc202}203// createTemplate will create a template named name and parses the str204// as template. If fails it will panic with the parse error.205//206// If name is found in override map it will use that string to parse the template207// instead of the provided str.208func createTemplate(name TemplateType, str string, overrides map[string]string, fm template.FuncMap) *TemplateAndText {209 if s, ok := overrides[name.String()]; ok {210 str = s211 }212 pt, err := template.New(name.String()).Funcs(fm).Parse(str)213 if err != nil {214 panic(err)215 }216 return &TemplateAndText{217 Text: str,218 Template: pt,219 }220}...

Full Screen

Full Screen

config.go

Source:config.go Github

copy

Full Screen

...129// newContext creates a new ctxerr.Context using DefaultContextConfig130// configuration.131func newContext(t TestingT) ctxerr.Context {132 if tt, ok := t.(*T); ok {133 return newContextWithConfig(tt, tt.Config)134 }135 tb, _ := t.(testing.TB)136 return newContextWithConfig(tb, DefaultContextConfig)137}138// newContextWithConfig creates a new ctxerr.Context using a specific139// configuration.140func newContextWithConfig(tb testing.TB, config ContextConfig) (ctx ctxerr.Context) {141 config.sanitize()142 ctx = ctxerr.Context{143 Path: ctxerr.NewPath(config.RootName),144 Visited: visited.NewVisited(),145 MaxErrors: config.MaxErrors,146 Anchors: config.anchors,147 Hooks: config.hooks,148 OriginalTB: tb,149 FailureIsFatal: config.FailureIsFatal,150 UseEqual: config.UseEqual,151 BeLax: config.BeLax,152 IgnoreUnexported: config.IgnoreUnexported,153 }154 ctx.InitErrors()...

Full Screen

Full Screen

config_test.go

Source:config_test.go Github

copy

Full Screen

...33 test.EqualStr(t, nctx.Path.String(), "")34 if nctx.OriginalTB != nil {35 t.Error("OriginalTB should be nil")36 }37 if newContextWithConfig(nil, ContextConfig{MaxErrors: -1}).CollectError(nil) != nil {38 t.Errorf("ctx.CollectError(nil) should return nil")39 }40 ctx := ContextConfig{}41 if ctx.Equal(DefaultContextConfig) {42 t.Errorf("Empty ContextConfig should be ≠ from DefaultContextConfig")43 }44 ctx.sanitize()45 if !ctx.Equal(DefaultContextConfig) {46 t.Errorf("Sanitized empty ContextConfig should be = to DefaultContextConfig")47 }48 ctx.RootName = "PIPO"49 test.EqualStr(t, ctx.OriginalPath(), "PIPO")50 nctx = newContext(t)51 nctx.Path = ctxerr.NewPath("BINGO[0].Zip")...

Full Screen

Full Screen

newContextWithConfig

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := telegram.NewClient(telegram.NewConfig("api_id", "api_hash"))4 _, err := client.SendMessage(client.Chats("@mychannel"), &tg.InputMessageText{5 Text: message.NewText("Hello world!"),6 })7 if err != nil {8 log.Fatal(err)9 }10 updates, err := client.Updates()11 if err != nil {12 log.Fatal(err)13 }

Full Screen

Full Screen

newContextWithConfig

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 td := telegram.NewClient(telegram.Options{4 APIID: os.Getenv("API_ID"),5 APIHash: os.Getenv("API_HASH"),6 Config: transport.Config{ServerAddress: "

Full Screen

Full Screen

newContextWithConfig

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 apikey := os.Getenv("TD_API_KEY")4 if apikey == "" {5 fmt.Println("TD_API_KEY is not set")6 os.Exit(1)7 }8 client := td.NewClient(apikey)9 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)10 defer cancel()11 err := client.CreateDatabase(ctx, dbName, nil)12 if err != nil {13 fmt.Println(err)14 os.Exit(1)15 }16 fmt.Printf("database %s created\n", dbName)17 err = client.CreateTable(ctx, dbName, tblName, nil)18 if err != nil {19 fmt.Println(err)20 os.Exit(1)21 }22 fmt.Printf("table %s created\n", tblName)23 ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)24 defer cancel()25 ctx = td.NewContextWithDatabase(ctx, dbName)26 ctx = td.NewContextWithTable(ctx, tblName)27 err = client.Insert(ctx, []map[string]interface{}{28 {"time": time.Now(), "value": 1},29 {"time": time.Now(), "value": 2},30 {"time": time.Now(), "value": 3},31 })32 if err != nil {33 fmt.Println(err)34 os.Exit(1)35 }36 fmt.Printf("data inserted\n")37 ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)38 defer cancel()39 err = client.DeleteTable(ctx, dbName, tblName)40 if err != nil {41 fmt.Println(err)42 os.Exit(1)43 }44 fmt.Printf("table %s deleted\n", tblName)45 ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)46 defer cancel()

Full Screen

Full Screen

newContextWithConfig

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client, err := td.NewClient(td.ClientConfig{4 Host: os.Getenv("TDF_HOST"),5 Username: os.Getenv("TDF_USERNAME"),6 Password: os.Getenv("TDF_PASSWORD"),7 })8 if err != nil {9 log.Fatal(err)10 }11 ctx, err := client.NewContextWithConfig(td.ContextConfig{12 ProjectKey: os.Getenv("TDF_PROJECT_KEY"),13 RetryConfig: &td.RetryConfig{14 RetryOnStatusCode: []int{500, 503},15 },16 })17 if err != nil {18 log.Fatal(err)19 }20 reader, err := ctx.ReadDataset(context.Background(), "my_dataset")21 if err != nil {22 log.Fatal(err)23 }24 data, err := reader.ReadAll()25 if err != nil {26 log.Fatal(err)27 }28 fmt.Println(data)29}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Go-testdeep automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful