Best Gauge code snippet using formatter.FormatSpecFiles
formatter.go
Source:formatter.go
...26)27const (28 tableLeftSpacing = 329)30func FormatSpecFiles(specFiles ...string) []*parser.ParseResult {31 specs, results := parser.ParseSpecFiles(specFiles, &gauge.ConceptDictionary{}, gauge.NewBuildErrors())32 resultsMap := getParseResult(results)33 filesSkipped := make([]string, 0)34 for _, spec := range specs {35 result := resultsMap[spec.FileName]36 if !result.Ok {37 filesSkipped = append(filesSkipped, spec.FileName)38 continue39 }40 if err := formatAndSave(spec); err != nil {41 result.ParseErrors = []parser.ParseError{parser.ParseError{Message: err.Error()}}42 } else {43 logger.Debugf(true, "Successfully formatted spec: %s", util.RelPathToProjectRoot(spec.FileName))44 }45 }46 if len(filesSkipped) > 0 {47 logger.Errorf(true, "Skipping %d file(s), due to following error(s):", len(filesSkipped))48 }49 return results50}51func getParseResult(results []*parser.ParseResult) map[string]*parser.ParseResult {52 resultsMap := make(map[string]*parser.ParseResult)53 for _, result := range results {54 resultsMap[result.FileName] = result55 }56 return resultsMap57}58func FormatStep(step *gauge.Step) string {59 text := step.Value60 paramCount := strings.Count(text, gauge.ParameterPlaceholder)61 for i := 0; i < paramCount; i++ {62 argument := step.Args[i]63 formattedArg := ""64 if argument.ArgType == gauge.TableArg {65 formattedTable := FormatTable(&argument.Table)66 formattedArg = fmt.Sprintf("\n%s", formattedTable)67 } else if argument.ArgType == gauge.Dynamic {68 formattedArg = fmt.Sprintf("<%s>", parser.GetUnescapedString(argument.Name))69 } else if argument.ArgType == gauge.SpecialString || argument.ArgType == gauge.SpecialTable {70 formattedArg = fmt.Sprintf("<%s>", parser.GetUnescapedString(argument.Name))71 } else {72 formattedArg = fmt.Sprintf("\"%s\"", parser.GetUnescapedString(argument.Value))73 }74 text = strings.Replace(text, gauge.ParameterPlaceholder, formattedArg, 1)75 }76 stepText := ""77 if strings.HasSuffix(text, "\n") {78 stepText = fmt.Sprintf("* %s", text)79 } else {80 stepText = fmt.Sprintf("* %s%s\n", text, step.Suffix)81 }82 return stepText83}84func FormatStepWithResolvedArgs(step *gauge.Step) string {85 text := step.Value86 paramCount := strings.Count(text, gauge.ParameterPlaceholder)87 for i := 0; i < paramCount; i++ {88 argument := step.Args[i]89 for i := range step.GetFragments() {90 stepFragmet := step.GetFragments()[i]91 if argument.ArgType == gauge.Dynamic && stepFragmet.FragmentType == gauge_messages.Fragment_Parameter && stepFragmet.Parameter.ParameterType == gauge_messages.Parameter_Dynamic {92 formattedArg := fmt.Sprintf("\"%s\"", stepFragmet.GetParameter().Value)93 text = strings.Replace(text, gauge.ParameterPlaceholder, formattedArg, 1)94 }95 }96 }97 stepText := ""98 if strings.HasSuffix(text, "\n") {99 stepText = fmt.Sprintf("* %s", text)100 } else {101 stepText = fmt.Sprintf("* %s%s\n", text, step.Suffix)102 }103 return stepText104}105func FormatHeading(heading, headingChar string) string {106 trimmedHeading := strings.TrimSpace(heading)107 return fmt.Sprintf("%s %s\n", headingChar, trimmedHeading)108}109func FormatTable(table *gauge.Table) string {110 columnToWidthMap := make(map[int]int)111 for i, header := range table.Headers {112 //table.get(header) returns a list of cells in that particular column113 cells, _ := table.Get(header)114 columnToWidthMap[i] = findLongestCellWidth(cells, len(header))115 }116 var tableStringBuffer bytes.Buffer117 tableStringBuffer.WriteString("\n")118 tableStringBuffer.WriteString(fmt.Sprintf("%s|", getRepeatedChars(" ", tableLeftSpacing)))119 for i, header := range table.Headers {120 width := columnToWidthMap[i]121 tableStringBuffer.WriteString(fmt.Sprintf("%s|", addPaddingToCell(header, width)))122 }123 tableStringBuffer.WriteString("\n")124 tableStringBuffer.WriteString(fmt.Sprintf("%s|", getRepeatedChars(" ", tableLeftSpacing)))125 for i := range table.Headers {126 width := columnToWidthMap[i]127 cell := getRepeatedChars("-", width)128 tableStringBuffer.WriteString(fmt.Sprintf("%s|", addPaddingToCell(cell, width)))129 }130 tableStringBuffer.WriteString("\n")131 for _, row := range table.Rows() {132 tableStringBuffer.WriteString(fmt.Sprintf("%s|", getRepeatedChars(" ", tableLeftSpacing)))133 for i, cell := range row {134 width := columnToWidthMap[i]135 tableStringBuffer.WriteString(fmt.Sprintf("%s|", addPaddingToCell(cell, width)))136 }137 tableStringBuffer.WriteString("\n")138 }139 return string(tableStringBuffer.Bytes())140}141func addPaddingToCell(cellValue string, width int) string {142 padding := getRepeatedChars(" ", width-len(cellValue))143 return fmt.Sprintf("%s%s", cellValue, padding)144}145func findLongestCellWidth(columnCells []gauge.TableCell, minValue int) int {146 longestLength := minValue147 for _, cellValue := range columnCells {148 cellValueLen := len(cellValue.GetValue())149 if cellValueLen > longestLength {150 longestLength = cellValueLen151 }152 }153 return longestLength154}155func FormatComment(comment *gauge.Comment) string {156 if comment.Value == "\n" {157 return comment.Value158 }159 return fmt.Sprintf("%s\n", comment.Value)160}161func FormatTags(tags *gauge.Tags) string {162 if tags == nil || len(tags.RawValues) == 0 {163 return ""164 }165 var b bytes.Buffer166 b.WriteString("tags: ")167 for i, tag := range tags.RawValues {168 for j, tagString := range tag {169 b.WriteString(tagString)170 if (i != len(tags.RawValues)-1) || (j != len(tag)-1) {171 b.WriteString(", ")172 }173 }174 b.WriteString("\n")175 if i != len(tags.RawValues)-1 {176 b.WriteString(" ")177 }178 }179 return string(b.Bytes())180}181func formatExternalDataTable(dataTable *gauge.DataTable) string {182 if dataTable == nil || len(dataTable.Value) == 0 {183 return ""184 }185 var b bytes.Buffer186 b.WriteString(dataTable.Value)187 b.WriteString("\n")188 return string(b.Bytes())189}190func formatAndSave(spec *gauge.Specification) error {191 formatted := FormatSpecification(spec)192 if err := common.SaveFile(spec.FileName, formatted, true); err != nil {193 return err194 }195 return nil196}197func FormatSpecification(specification *gauge.Specification) string {198 var formattedSpec bytes.Buffer199 queue := &gauge.ItemQueue{Items: specification.AllItems()}200 formatter := &formatter{buffer: formattedSpec, itemQueue: queue}201 specification.Traverse(formatter, queue)202 return string(formatter.buffer.Bytes())203}204func sortConcepts(conceptDictionary *gauge.ConceptDictionary, conceptMap map[string]string) []*gauge.Concept {205 var concepts []*gauge.Concept206 for _, concept := range conceptDictionary.ConceptsMap {207 conceptMap[concept.FileName] = ""208 concepts = append(concepts, concept)209 }210 sort.Sort(gauge.ByLineNo(concepts))211 return concepts212}213func formatConceptSteps(conceptMap map[string]string, concept *gauge.Concept) {214 conceptMap[concept.FileName] += strings.TrimSpace(strings.Replace(FormatStep(concept.ConceptStep), "*", "#", 1)) + "\n"215 for i := 1; i < len(concept.ConceptStep.Items); i++ {216 conceptMap[concept.FileName] += formatItem(concept.ConceptStep.Items[i])217 }218}219func FormatConcepts(conceptDictionary *gauge.ConceptDictionary) map[string]string {220 conceptMap := make(map[string]string)221 for _, concept := range sortConcepts(conceptDictionary, conceptMap) {222 for _, comment := range concept.ConceptStep.PreComments {223 conceptMap[concept.FileName] += FormatComment(comment)224 }225 formatConceptSteps(conceptMap, concept)226 }227 return conceptMap228}229func formatItem(item gauge.Item) string {230 switch item.Kind() {231 case gauge.CommentKind:232 comment := item.(*gauge.Comment)233 if comment.Value == "\n" {234 return comment.Value235 }236 return fmt.Sprintf("%s\n", comment.Value)237 case gauge.StepKind:238 step := item.(*gauge.Step)239 return FormatStep(step)240 case gauge.DataTableKind:241 dataTable := item.(*gauge.DataTable)242 return FormatTable(&dataTable.Table)243 case gauge.TagKind:244 tags := item.(*gauge.Tags)245 return FormatTags(tags)246 }247 return ""248}249func getRepeatedChars(character string, repeatCount int) string {250 formatted := ""251 for i := 0; i < repeatCount; i++ {252 formatted = fmt.Sprintf("%s%s", formatted, character)253 }254 return formatted255}256func FormatSpecFilesIn(filesLocation string) {257 specFiles := util.GetSpecFiles([]string{filesLocation})258 parseResults := FormatSpecFiles(specFiles...)259 if parser.HandleParseResult(parseResults...) {260 os.Exit(1)261 }262}...
FormatSpecFiles
Using AI Code Generation
1import (2func main() {3 formatter := format.NewFormatter(settings.NewProvider(4 settings.NewProviderWithConfigData([]byte(`version: "1"`)),5 file, err := ioutil.ReadFile("test.proto")6 if err != nil {7 log.Fatal(err)8 }9 _, err = formatter.Format(file)10 if err != nil {11 log.Fatal(err)12 }13 fmt.Println(proto.MarshalTextString(file))14}15syntax = "proto3";16message Test {17 string name = 1;18}19syntax = "proto3";20message Test {21 string name = 1;22}
FormatSpecFiles
Using AI Code Generation
1func main() {2 formatter := new(Formatter)3 formatter.FormatSpecFiles("spec1.json", "spec2.json")4}5func main() {6 formatter := new(Formatter)7 formatter.FormatSpecFiles("spec1.json", "spec2.json")8}9import (10type Formatter struct {11}12func (f *Formatter) FormatSpecFiles(specFiles ...string) {13 for _, specFile := range specFiles {14 fmt.Println(specFile)15 }16}17import (18func main() {19 formatter := new(formatter.Formatter)20 formatter.FormatSpecFiles("spec1.json", "spec2.json")21}22import (23func main() {24 formatter := new(formatter.Formatter)25 formatter.FormatSpecFiles("spec1.json", "spec2.json")26}27import (28func FormatSpecFiles(specFiles ...string) {29 for _, specFile := range specFiles {30 fmt.Println(specFile)31 }32}33import (34func main() {
FormatSpecFiles
Using AI Code Generation
1import (2 "golang.org/x/tools/imports"3func main() {4 dmp := diffmatchpatch.New()5 data, err := ioutil.ReadFile("2.go")6 if err != nil {7 fmt.Println(err)8 os.Exit(1)9 }10 res, err := imports.Process("2.go", data, nil)11 if err != nil {12 fmt.Println(err)13 os.Exit(1)14 }15 diffs := dmp.DiffMain(string(data), string(res), false)16 fmt.Println(dmp.DiffPrettyText(diffs))17}18func main() {19 dmp := diffmatchpatch.New()20 data, err := ioutil.ReadFile("2.go")21 if err != nil {22 fmt.Println(err)23 os.Exit(1)24 }25 res, err := imports.Process("2.go", data, nil)26 if err != nil {27 fmt.Println(err)28 os.Exit(1)29 }30 diffs := dmp.DiffMain(string(data), string(res), false)31 fmt.Println(dmp.DiffPrettyText(diffs))32}33func main() {34 dmp := diffmatchpatch.New()35 data, err := ioutil.ReadFile("2.go")36 if err != nil {37 fmt.Println(err)38 os.Exit(1)39 }40 res, err := imports.Process("2.go", data, nil)41 if err != nil {42 fmt.Println(err)43 os.Exit(1)44 }45 diffs := dmp.DiffMain(string(data), string(res), false)46 fmt.Println(dmp.DiffPrettyText(diffs))47}
FormatSpecFiles
Using AI Code Generation
1func main() {2 formatter := new(formatter.Formatter)3 formatter.FormatSpecFiles("specfile1", "specfile2", "specfile3")4}5Release: 1%{?dist}6Release: 1%{?dist}7Release: 1%{?dist}8Go: Format Spec File (2)9Go: Format Spec File (3)10Go: Format Spec File (4)11Go: Format Spec File (5)12Go: Format Spec File (6)13Go: Format Spec File (7)14Go: Format Spec File (8)15Go: Format Spec File (9)16Go: Format Spec File (10)17Go: Format Spec File (11)18Go: Format Spec File (12)19Go: Format Spec File (13)20Go: Format Spec File (14)21Go: Format Spec File (15)22Go: Format Spec File (16)23Go: Format Spec File (17)24Go: Format Spec File (18)25Go: Format Spec File (19)26Go: Format Spec File (20)27Go: Format Spec File (21)28Go: Format Spec File (22)29Go: Format Spec File (23)30Go: Format Spec File (24)31Go: Format Spec File (25)32Go: Format Spec File (26)33Go: Format Spec File (27)34Go: Format Spec File (28)35Go: Format Spec File (29)36Go: Format Spec File (30)37Go: Format Spec File (31)38Go: Format Spec File (32)
FormatSpecFiles
Using AI Code Generation
1import (2func main() {3 formatSpecFiles = append(formatSpecFiles, "formatSpecFile1.go")4 formatSpecFiles = append(formatSpecFiles, "formatSpecFile2.go")5 result = formatter.FormatSpecFiles(formatSpecFiles)6 fmt.Println(result)7}8import (9func main() {10 result := formatter.FormatSpec(formatSpec)11 fmt.Println(result)12}13import (14func main() {15 result := formatter.FormatSpec(formatSpec)16 fmt.Println(result)17}
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!!