gitee.com/mirrors/gauge@v1.0.6/parser/processor.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package parser
    19  
    20  import (
    21  	"bytes"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/getgauge/gauge/gauge"
    26  )
    27  
    28  func processSpec(parser *SpecParser, token *Token) ([]error, bool) {
    29  	return []error{}, false
    30  }
    31  
    32  func processTearDown(parser *SpecParser, token *Token) ([]error, bool) {
    33  	if len(token.Value) < 3 {
    34  		return []error{fmt.Errorf("Teardown should have at least three underscore characters")}, true
    35  	}
    36  	return []error{}, false
    37  }
    38  
    39  func processDataTable(parser *SpecParser, token *Token) ([]error, bool) {
    40  	if len(strings.TrimSpace(strings.Replace(token.Value, "table:", "", 1))) == 0 {
    41  		return []error{fmt.Errorf("Table location not specified")}, true
    42  	}
    43  	return []error{}, false
    44  }
    45  
    46  func processScenario(parser *SpecParser, token *Token) ([]error, bool) {
    47  	if len(strings.TrimSpace(token.Value)) < 1 {
    48  		return []error{fmt.Errorf("Scenario heading should have at least one character")}, true
    49  	}
    50  	parser.clearState()
    51  	return []error{}, false
    52  }
    53  
    54  func processComment(parser *SpecParser, token *Token) ([]error, bool) {
    55  	parser.clearState()
    56  	addStates(&parser.currentState, commentScope)
    57  	return []error{}, false
    58  }
    59  
    60  func processTag(parser *SpecParser, token *Token) ([]error, bool) {
    61  	if isInState(parser.currentState, tagsScope) {
    62  		retainStates(&parser.currentState, tagsScope)
    63  	} else {
    64  		parser.clearState()
    65  	}
    66  	tokens := splitAndTrimTags(token.Value)
    67  
    68  	for _, tagValue := range tokens {
    69  		if len(tagValue) > 0 {
    70  			token.Args = append(token.Args, tagValue)
    71  		}
    72  	}
    73  	return []error{}, false
    74  }
    75  
    76  func processTable(parser *SpecParser, token *Token) ([]error, bool) {
    77  	var buffer bytes.Buffer
    78  	shouldEscape := false
    79  	var errs []error
    80  	for i, element := range token.Value {
    81  		if i == 0 {
    82  			continue
    83  		}
    84  		if shouldEscape {
    85  			buffer.WriteRune(element)
    86  			shouldEscape = false
    87  			continue
    88  		}
    89  		if element == '\\' {
    90  			shouldEscape = true
    91  			continue
    92  		} else if element == '|' {
    93  			trimmedValue := strings.TrimSpace(buffer.String())
    94  
    95  			if token.Kind == gauge.TableHeader {
    96  				if len(trimmedValue) == 0 {
    97  					errs = append(errs, fmt.Errorf("Table header should not be blank"))
    98  				} else if arrayContains(token.Args, trimmedValue) {
    99  					errs = append(errs, fmt.Errorf("Table header cannot have repeated column values"))
   100  				}
   101  			}
   102  			token.Args = append(token.Args, trimmedValue)
   103  			buffer.Reset()
   104  		} else {
   105  			buffer.WriteRune(element)
   106  		}
   107  	}
   108  
   109  	if !isInState(parser.currentState, tableScope) {
   110  		addStates(&parser.currentState, tableScope)
   111  	} else {
   112  		addStates(&parser.currentState, tableDataScope)
   113  	}
   114  
   115  	return errs, false
   116  }
   117  
   118  func splitAndTrimTags(tag string) []string {
   119  	listOfTags := strings.Split(tag, ",")
   120  	for i, aTag := range listOfTags {
   121  		listOfTags[i] = strings.TrimSpace(aTag)
   122  	}
   123  	return listOfTags
   124  }