forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specification.go
1033 lines (907 loc) · 32.5 KB
/
specification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package parser
import (
"fmt"
"github.com/getgauge/gauge/gauge_messages"
"github.com/golang/protobuf/proto"
"regexp"
"strings"
)
type Scenario struct {
Heading *Heading
Steps []*Step
Comments []*Comment
Tags *Tags
Items []Item
}
type ArgType string
const (
Static ArgType = "static"
Dynamic ArgType = "dynamic"
TableArg ArgType = "table"
SpecialString ArgType = "special_string"
SpecialTable ArgType = "special_table"
ParameterPlaceholder = "{}"
)
type StepArg struct {
Name string
Value string
ArgType ArgType
Table Table
}
func (stepArg *StepArg) String() string {
return fmt.Sprintf("{Name: %s,value %s,argType %s,table %v}", stepArg.Name, stepArg.Value, string(stepArg.ArgType), stepArg.Table)
}
type paramNameValue struct {
name string
stepArg *StepArg
}
func (paramNameValue paramNameValue) String() string {
return fmt.Sprintf("ParamName: %s, stepArg: %s", paramNameValue.name, paramNameValue.stepArg)
}
type ArgLookup struct {
//helps to access the index of an arg at O(1)
paramIndexMap map[string]int
paramValue []paramNameValue
}
type SpecItemFilter interface {
Filter(Item) bool
}
func (argLookup ArgLookup) String() string {
return fmt.Sprintln(argLookup.paramValue)
}
type Step struct {
LineNo int
Value string
LineText string
Args []*StepArg
IsConcept bool
Lookup ArgLookup
ConceptSteps []*Step
Fragments []*gauge_messages.Fragment
Parent *Step
HasInlineTable bool
Items []Item
PreComments []*Comment
}
type StepValue struct {
Args []string
StepValue string
ParameterizedStepValue string
}
func (step *Step) getArg(name string) *StepArg {
arg := step.Lookup.getArg(name)
// Return static values
if arg != nil && arg.ArgType != Dynamic {
return arg
}
if step.Parent == nil {
return step.Lookup.getArg(name)
}
return step.Parent.getArg(step.Lookup.getArg(name).Value)
}
func (step *Step) getLineText() string {
if step.HasInlineTable {
return fmt.Sprintf("%s <%s>", step.LineText, TableArg)
}
return step.LineText
}
func (step *Step) Rename(oldStep Step, newStep Step, isRefactored bool, orderMap map[int]int, isConcept *bool) bool {
if strings.TrimSpace(step.Value) != strings.TrimSpace(oldStep.Value) {
return isRefactored
}
if step.IsConcept {
*isConcept = true
}
step.Value = newStep.Value
step.Args = step.getArgsInOrder(newStep, orderMap)
return true
}
func (step *Step) getArgsInOrder(newStep Step, orderMap map[int]int) []*StepArg {
args := make([]*StepArg, len(newStep.Args))
for key, value := range orderMap {
arg := &StepArg{Value: newStep.Args[key].Value, ArgType: Static}
if step.IsConcept {
arg = &StepArg{Value: newStep.Args[key].Value, ArgType: Dynamic}
}
if value != -1 {
arg = step.Args[value]
}
args[key] = arg
}
return args
}
func (step *Step) deepCopyStepArgs() []*StepArg {
copiedStepArgs := make([]*StepArg, 0)
for _, conceptStepArg := range step.Args {
temp := new(StepArg)
*temp = *conceptStepArg
copiedStepArgs = append(copiedStepArgs, temp)
}
return copiedStepArgs
}
func (step *Step) ReplaceArgsWithDynamic(args []*StepArg) {
for i, arg := range step.Args {
for _, conceptArg := range args {
if arg.String() == conceptArg.String() {
step.Args[i] = &StepArg{Value: conceptArg.Value, ArgType: Dynamic}
}
}
}
}
func CreateStepFromStepRequest(stepReq *gauge_messages.ExecuteStepRequest) *Step {
args := createStepArgsFromProtoArguments(stepReq.GetParameters())
step := &Step{Value: stepReq.GetParsedStepText(),
LineText: stepReq.GetActualStepText()}
step.addArgs(args...)
return step
}
func createStepArgsFromProtoArguments(parameters []*gauge_messages.Parameter) []*StepArg {
stepArgs := make([]*StepArg, 0)
for _, parameter := range parameters {
var arg *StepArg
switch parameter.GetParameterType() {
case gauge_messages.Parameter_Static:
arg = &StepArg{ArgType: Static, Value: parameter.GetValue(), Name: parameter.GetName()}
break
case gauge_messages.Parameter_Dynamic:
arg = &StepArg{ArgType: Dynamic, Value: parameter.GetValue(), Name: parameter.GetName()}
break
case gauge_messages.Parameter_Special_String:
arg = &StepArg{ArgType: SpecialString, Value: parameter.GetValue(), Name: parameter.GetName()}
break
case gauge_messages.Parameter_Table:
arg = &StepArg{ArgType: TableArg, Table: *(TableFrom(parameter.GetTable())), Name: parameter.GetName()}
break
case gauge_messages.Parameter_Special_Table:
arg = &StepArg{ArgType: SpecialTable, Table: *(TableFrom(parameter.GetTable())), Name: parameter.GetName()}
break
}
stepArgs = append(stepArgs, arg)
}
return stepArgs
}
type Specification struct {
Heading *Heading
Scenarios []*Scenario
Comments []*Comment
DataTable DataTable
Contexts []*Step
FileName string
Tags *Tags
Items []Item
}
type Item interface {
Kind() TokenKind
}
type HeadingType int
const (
SpecHeading = 0
ScenarioHeading = 1
)
type Heading struct {
Value string
LineNo int
HeadingType HeadingType
}
type Comment struct {
Value string
LineNo int
}
type Tags struct {
Values []string
}
type Warning struct {
Message string
LineNo int
}
type ParseResult struct {
ParseError *ParseError
Warnings []*Warning
Ok bool
FileName string
}
func converterFn(predicate func(token *Token, state *int) bool, apply func(token *Token, spec *Specification, state *int) ParseResult) func(*Token, *int, *Specification) ParseResult {
return func(token *Token, state *int, spec *Specification) ParseResult {
if !predicate(token, state) {
return ParseResult{Ok: true}
}
return apply(token, spec, state)
}
}
func (specParser *SpecParser) CreateSpecification(tokens []*Token, conceptDictionary *ConceptDictionary) (*Specification, *ParseResult) {
specParser.conceptDictionary = conceptDictionary
converters := specParser.initializeConverters()
specification := &Specification{}
finalResult := &ParseResult{}
state := initial
for _, token := range tokens {
for _, converter := range converters {
result := converter(token, &state, specification)
if !result.Ok {
if result.ParseError != nil {
finalResult.Ok = false
finalResult.ParseError = result.ParseError
return nil, finalResult
}
}
if result.Warnings != nil {
if finalResult.Warnings == nil {
finalResult.Warnings = make([]*Warning, 0)
}
finalResult.Warnings = append(finalResult.Warnings, result.Warnings...)
}
}
}
specification.processConceptStepsFrom(conceptDictionary)
validationError := specParser.validateSpec(specification)
if validationError != nil {
finalResult.Ok = false
finalResult.ParseError = validationError
return nil, finalResult
}
finalResult.Ok = true
return specification, finalResult
}
func (specParser *SpecParser) initializeConverters() []func(*Token, *int, *Specification) ParseResult {
specConverter := converterFn(func(token *Token, state *int) bool {
return token.Kind == SpecKind
}, func(token *Token, spec *Specification, state *int) ParseResult {
if spec.Heading != nil {
return ParseResult{Ok: false, ParseError: &ParseError{token.LineNo, "Parse error: Multiple spec headings found in same file", token.LineText}}
}
spec.addHeading(&Heading{LineNo: token.LineNo, Value: token.Value})
addStates(state, specScope)
return ParseResult{Ok: true}
})
scenarioConverter := converterFn(func(token *Token, state *int) bool {
return token.Kind == ScenarioKind
}, func(token *Token, spec *Specification, state *int) ParseResult {
if spec.Heading == nil {
return ParseResult{Ok: false, ParseError: &ParseError{token.LineNo, "Parse error: Scenario should be defined after the spec heading", token.LineText}}
}
for _, scenario := range spec.Scenarios {
if strings.ToLower(scenario.Heading.Value) == strings.ToLower(token.Value) {
return ParseResult{Ok: false, ParseError: &ParseError{token.LineNo, "Parse error: Duplicate scenario definition '" + scenario.Heading.Value + "' found in the same specification", token.LineText}}
}
}
scenario := &Scenario{}
scenario.addHeading(&Heading{Value: token.Value, LineNo: token.LineNo})
spec.addScenario(scenario)
retainStates(state, specScope)
addStates(state, scenarioScope)
return ParseResult{Ok: true}
})
stepConverter := converterFn(func(token *Token, state *int) bool {
return token.Kind == StepKind && isInState(*state, scenarioScope)
}, func(token *Token, spec *Specification, state *int) ParseResult {
latestScenario := spec.latestScenario()
stepToAdd, parseDetails := spec.createStep(token)
if parseDetails != nil && parseDetails.Error != nil {
return ParseResult{ParseError: parseDetails.Error, Ok: false, Warnings: parseDetails.Warnings}
}
latestScenario.addStep(stepToAdd)
retainStates(state, specScope, scenarioScope)
addStates(state, stepScope)
if parseDetails.Warnings != nil {
return ParseResult{Ok: false, Warnings: parseDetails.Warnings}
}
return ParseResult{Ok: true, Warnings: parseDetails.Warnings}
})
contextConverter := converterFn(func(token *Token, state *int) bool {
return token.Kind == StepKind && !isInState(*state, scenarioScope) && isInState(*state, specScope)
}, func(token *Token, spec *Specification, state *int) ParseResult {
stepToAdd, parseDetails := spec.createStep(token)
if parseDetails != nil && parseDetails.Error != nil {
return ParseResult{ParseError: parseDetails.Error, Ok: false, Warnings: parseDetails.Warnings}
}
spec.addContext(stepToAdd)
retainStates(state, specScope)
addStates(state, contextScope)
if parseDetails.Warnings != nil {
return ParseResult{Ok: false, Warnings: parseDetails.Warnings}
}
return ParseResult{Ok: true, Warnings: parseDetails.Warnings}
})
commentConverter := converterFn(func(token *Token, state *int) bool {
return token.Kind == CommentKind
}, func(token *Token, spec *Specification, state *int) ParseResult {
comment := &Comment{token.Value, token.LineNo}
if isInState(*state, scenarioScope) {
spec.latestScenario().addComment(comment)
} else {
spec.addComment(comment)
}
retainStates(state, specScope, scenarioScope)
addStates(state, commentScope)
return ParseResult{Ok: true}
})
keywordConverter := converterFn(func(token *Token, state *int) bool {
return token.Kind == DataTableKind
}, func(token *Token, spec *Specification, state *int) ParseResult {
resolvedArg, _ := newSpecialTypeResolver().resolve(token.Value)
if isInState(*state, specScope) && !spec.DataTable.IsInitialized() {
externalTable := &DataTable{}
externalTable.Table = resolvedArg.Table
externalTable.LineNo = token.LineNo
externalTable.Value = token.Value
externalTable.IsExternal = true
spec.addExternalDataTable(externalTable)
} else if isInState(*state, specScope) && spec.DataTable.IsInitialized() {
value := "Multiple data table present, ignoring table"
spec.addComment(&Comment{token.LineText, token.LineNo})
return ParseResult{Ok: false, Warnings: []*Warning{&Warning{value, token.LineNo}}}
} else {
value := "Data table not associated with spec"
spec.addComment(&Comment{token.LineText, token.LineNo})
return ParseResult{Ok: false, Warnings: []*Warning{&Warning{value, token.LineNo}}}
}
retainStates(state, specScope)
addStates(state, keywordScope)
return ParseResult{Ok: true}
})
tableHeaderConverter := converterFn(func(token *Token, state *int) bool {
return token.Kind == TableHeader && isInState(*state, specScope)
}, func(token *Token, spec *Specification, state *int) ParseResult {
if isInState(*state, stepScope) {
latestScenario := spec.latestScenario()
latestStep := latestScenario.latestStep()
addInlineTableHeader(latestStep, token)
} else if isInState(*state, contextScope) {
latestContext := spec.latestContext()
addInlineTableHeader(latestContext, token)
} else if !isInState(*state, scenarioScope) {
if !spec.DataTable.Table.IsInitialized() {
dataTable := &Table{}
dataTable.LineNo = token.LineNo
dataTable.addHeaders(token.Args)
spec.addDataTable(dataTable)
} else {
value := "Multiple data table present, ignoring table"
spec.addComment(&Comment{token.LineText, token.LineNo})
return ParseResult{Ok: false, Warnings: []*Warning{&Warning{value, token.LineNo}}}
}
} else {
value := "Table not associated with a step, ignoring table"
spec.latestScenario().addComment(&Comment{token.LineText, token.LineNo})
return ParseResult{Ok: false, Warnings: []*Warning{&Warning{value, token.LineNo}}}
}
retainStates(state, specScope, scenarioScope, stepScope, contextScope)
addStates(state, tableScope)
return ParseResult{Ok: true}
})
tableRowConverter := converterFn(func(token *Token, state *int) bool {
return token.Kind == TableRow
}, func(token *Token, spec *Specification, state *int) ParseResult {
var result ParseResult
//When table is to be treated as a comment
if !isInState(*state, tableScope) {
if isInState(*state, scenarioScope) {
spec.latestScenario().addComment(&Comment{token.LineText, token.LineNo})
} else {
spec.addComment(&Comment{token.LineText, token.LineNo})
}
} else if areUnderlined(token.Args) {
// skip table separator
result = ParseResult{Ok: true}
} else if isInState(*state, stepScope) {
latestScenario := spec.latestScenario()
latestStep := latestScenario.latestStep()
result = addInlineTableRow(latestStep, token, new(ArgLookup).fromDataTable(&spec.DataTable.Table))
} else if isInState(*state, contextScope) {
latestContext := spec.latestContext()
result = addInlineTableRow(latestContext, token, new(ArgLookup).fromDataTable(&spec.DataTable.Table))
} else {
//todo validate datatable rows also
spec.DataTable.Table.addRowValues(token.Args)
result = ParseResult{Ok: true}
}
retainStates(state, specScope, scenarioScope, stepScope, contextScope, tableScope)
return result
})
tagConverter := converterFn(func(token *Token, state *int) bool {
return (token.Kind == TagKind)
}, func(token *Token, spec *Specification, state *int) ParseResult {
tags := &Tags{Values: token.Args}
if isInState(*state, scenarioScope) {
spec.latestScenario().addTags(tags)
} else {
spec.addTags(tags)
}
return ParseResult{Ok: true}
})
converter := []func(*Token, *int, *Specification) ParseResult{
specConverter, scenarioConverter, stepConverter, contextConverter, commentConverter, tableHeaderConverter, tableRowConverter, tagConverter, keywordConverter,
}
return converter
}
func (spec *Specification) createStep(stepToken *Token) (*Step, *ParseDetailResult) {
dataTableLookup := new(ArgLookup).fromDataTable(&spec.DataTable.Table)
stepToAdd, parseDetails := spec.CreateStepUsingLookup(stepToken, dataTableLookup)
if parseDetails != nil && parseDetails.Error != nil {
return nil, parseDetails
}
return stepToAdd, parseDetails
}
func (spec *Specification) CreateStepUsingLookup(stepToken *Token, lookup *ArgLookup) (*Step, *ParseDetailResult) {
stepValue, argsType := extractStepValueAndParameterTypes(stepToken.Value)
if argsType != nil && len(argsType) != len(stepToken.Args) {
return nil, &ParseDetailResult{Error: &ParseError{stepToken.LineNo, "Step text should not have '{static}' or '{dynamic}' or '{special}'", stepToken.LineText}, Warnings: nil}
}
step := &Step{LineNo: stepToken.LineNo, Value: stepValue, LineText: strings.TrimSpace(stepToken.LineText)}
arguments := make([]*StepArg, 0)
var warnings []*Warning
for i, argType := range argsType {
argument, parseDetails := spec.createStepArg(stepToken.Args[i], argType, stepToken, lookup)
if parseDetails != nil && parseDetails.Error != nil {
return nil, parseDetails
}
arguments = append(arguments, argument)
if parseDetails != nil && parseDetails.Warnings != nil {
for _, warn := range parseDetails.Warnings {
warnings = append(warnings, warn)
}
}
}
step.addArgs(arguments...)
return step, &ParseDetailResult{Warnings: warnings}
}
func (specification *Specification) processConceptStepsFrom(conceptDictionary *ConceptDictionary) {
for _, step := range specification.Contexts {
specification.processConceptStep(step, conceptDictionary)
}
for _, scenario := range specification.Scenarios {
for _, step := range scenario.Steps {
specification.processConceptStep(step, conceptDictionary)
}
}
}
func (specification *Specification) processConceptStep(step *Step, conceptDictionary *ConceptDictionary) {
if conceptFromDictionary := conceptDictionary.search(step.Value); conceptFromDictionary != nil {
specification.createConceptStep(conceptFromDictionary.ConceptStep, step)
}
}
func (specification *Specification) createConceptStep(concept *Step, originalStep *Step) {
stepCopy := concept.getCopy()
originalArgs := originalStep.Args
originalStep.copyFrom(stepCopy)
originalStep.Args = originalArgs
// set parent of all concept steps to be the current concept (referred as originalStep here)
// this is used to fetch from parent's lookup when nested
for _, conceptStep := range originalStep.ConceptSteps {
conceptStep.Parent = originalStep
}
specification.populateConceptLookup(&originalStep.Lookup, concept.Args, originalStep.Args)
}
func (specification *Specification) addItem(itemToAdd Item) {
if specification.Items == nil {
specification.Items = make([]Item, 0)
}
specification.Items = append(specification.Items, itemToAdd)
}
func (specification *Specification) addHeading(heading *Heading) {
heading.HeadingType = SpecHeading
specification.Heading = heading
}
func (specification *Specification) addScenario(scenario *Scenario) {
specification.Scenarios = append(specification.Scenarios, scenario)
specification.addItem(scenario)
}
func (specification *Specification) addContext(contextStep *Step) {
specification.Contexts = append(specification.Contexts, contextStep)
specification.addItem(contextStep)
}
func (specification *Specification) addComment(comment *Comment) {
specification.Comments = append(specification.Comments, comment)
specification.addItem(comment)
}
func (specification *Specification) addDataTable(table *Table) {
specification.DataTable.Table = *table
specification.addItem(&specification.DataTable)
}
func (specification *Specification) addExternalDataTable(externalTable *DataTable) {
specification.DataTable = *externalTable
specification.addItem(externalTable)
}
func (specification *Specification) addTags(tags *Tags) {
specification.Tags = tags
specification.addItem(tags)
}
func (specification *Specification) latestScenario() *Scenario {
return specification.Scenarios[len(specification.Scenarios)-1]
}
func (specification *Specification) latestContext() *Step {
return specification.Contexts[len(specification.Contexts)-1]
}
func (specParser *SpecParser) validateSpec(specification *Specification) *ParseError {
if len(specification.Items) == 0 {
return &ParseError{LineNo: 1, Message: "Spec does not have any elements"}
}
if specification.Heading == nil {
return &ParseError{LineNo: 1, Message: "Spec heading not found"}
}
dataTable := specification.DataTable.Table
if dataTable.IsInitialized() && dataTable.GetRowCount() == 0 {
return &ParseError{LineNo: dataTable.LineNo, Message: "Data table should have at least 1 data row"}
}
return nil
}
func extractStepValueAndParameterTypes(stepTokenValue string) (string, []string) {
argsType := make([]string, 0)
r := regexp.MustCompile("{(dynamic|static|special)}")
/*
enter {dynamic} and {static}
returns
[
["{dynamic}","dynamic"]
["{static}","static"]
]
*/
args := r.FindAllStringSubmatch(stepTokenValue, -1)
if args == nil {
return stepTokenValue, nil
}
for _, arg := range args {
//arg[1] extracts the first group
argsType = append(argsType, arg[1])
}
return r.ReplaceAllString(stepTokenValue, ParameterPlaceholder), argsType
}
func (step *Step) addArgs(args ...*StepArg) {
step.Args = append(step.Args, args...)
step.PopulateFragments()
}
func (step *Step) addInlineTableHeaders(headers []string) {
tableArg := &StepArg{ArgType: TableArg}
tableArg.Table.addHeaders(headers)
step.addArgs(tableArg)
}
func (step *Step) addInlineTableRow(row []TableCell) {
lastArg := step.Args[len(step.Args)-1]
lastArg.Table.addRows(row)
step.PopulateFragments()
}
func (step *Step) PopulateFragments() {
r := regexp.MustCompile(ParameterPlaceholder)
/*
enter {} and {} bar
returns
[[6 8] [13 15]]
*/
argSplitIndices := r.FindAllStringSubmatchIndex(step.Value, -1)
step.Fragments = make([]*gauge_messages.Fragment, 0)
if len(step.Args) == 0 {
step.Fragments = append(step.Fragments, &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Text.Enum(), Text: proto.String(step.Value)})
return
}
textStartIndex := 0
for argIndex, argIndices := range argSplitIndices {
if textStartIndex < argIndices[0] {
step.Fragments = append(step.Fragments, &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Text.Enum(), Text: proto.String(step.Value[textStartIndex:argIndices[0]])})
}
parameter := convertToProtoParameter(step.Args[argIndex])
step.Fragments = append(step.Fragments, &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Parameter.Enum(), Parameter: parameter})
textStartIndex = argIndices[1]
}
if textStartIndex < len(step.Value) {
step.Fragments = append(step.Fragments, &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Text.Enum(), Text: proto.String(step.Value[textStartIndex:len(step.Value)])})
}
}
func (spec *Specification) Filter(filter SpecItemFilter) {
for i := 0; i < len(spec.Items); i++ {
if filter.Filter(spec.Items[i]) {
spec.removeItem(i)
i--
}
}
}
func (spec *Specification) removeItem(itemIndex int) {
item := spec.Items[itemIndex]
if len(spec.Items)-1 == itemIndex {
spec.Items = spec.Items[:itemIndex]
} else if 0 == itemIndex {
spec.Items = spec.Items[itemIndex+1:]
} else {
spec.Items = append(spec.Items[:itemIndex], spec.Items[itemIndex+1:]...)
}
if item.Kind() == ScenarioKind {
spec.removeScenario(item.(*Scenario))
}
}
func (spec *Specification) removeScenario(scenario *Scenario) {
index := getIndexFor(scenario, spec.Scenarios)
if len(spec.Scenarios)-1 == index {
spec.Scenarios = spec.Scenarios[:index]
} else if index == 0 {
spec.Scenarios = spec.Scenarios[index+1:]
} else {
spec.Scenarios = append(spec.Scenarios[:index], spec.Scenarios[index+1:]...)
}
}
func (spec *Specification) populateConceptLookup(lookup *ArgLookup, conceptArgs []*StepArg, stepArgs []*StepArg) {
for i, arg := range stepArgs {
lookup.addArgValue(conceptArgs[i].Value, &StepArg{Value: arg.Value, ArgType: arg.ArgType, Table: arg.Table, Name: arg.Name})
}
}
func (spec *Specification) RenameSteps(oldStep Step, newStep Step, orderMap map[int]int) bool {
isRefactored := false
for _, step := range spec.Contexts {
isConcept := false
isRefactored = step.Rename(oldStep, newStep, isRefactored, orderMap, &isConcept)
}
for _, scenario := range spec.Scenarios {
refactor := scenario.renameSteps(oldStep, newStep, orderMap)
if refactor {
isRefactored = refactor
}
}
return isRefactored
}
func (spec *Specification) createStepArg(argValue string, typeOfArg string, token *Token, lookup *ArgLookup) (*StepArg, *ParseDetailResult) {
if typeOfArg == "special" {
resolvedArgValue, err := newSpecialTypeResolver().resolve(argValue)
if err != nil {
switch err.(type) {
case invalidSpecialParamError:
return treatArgAsDynamic(argValue, token, lookup)
default:
return nil, &ParseDetailResult{Error: &ParseError{LineNo: token.LineNo, Message: fmt.Sprintf("Dynamic parameter <%s> could not be resolved", argValue), LineText: token.LineText}}
}
}
return resolvedArgValue, nil
} else if typeOfArg == "static" {
return &StepArg{ArgType: Static, Value: argValue}, nil
} else {
return validateDynamicArg(argValue, token, lookup)
}
}
func treatArgAsDynamic(argValue string, token *Token, lookup *ArgLookup) (*StepArg, *ParseDetailResult) {
parseDetailRes := &ParseDetailResult{Warnings: []*Warning{&Warning{LineNo: token.LineNo, Message: fmt.Sprintf("Could not resolve special param type <%s>. Treating it as dynamic param.", argValue)}}}
stepArg, result := validateDynamicArg(argValue, token, lookup)
if result != nil {
if result.Error != nil {
parseDetailRes.Error = result.Error
}
if result.Warnings != nil {
for _, warn := range result.Warnings {
parseDetailRes.Warnings = append(parseDetailRes.Warnings, warn)
}
}
}
return stepArg, parseDetailRes
}
func validateDynamicArg(argValue string, token *Token, lookup *ArgLookup) (*StepArg, *ParseDetailResult) {
if !isConceptHeader(lookup) && !lookup.containsArg(argValue) {
return nil, &ParseDetailResult{Error: &ParseError{LineNo: token.LineNo, Message: fmt.Sprintf("Dynamic parameter <%s> could not be resolved", argValue), LineText: token.LineText}}
}
stepArgument := &StepArg{ArgType: Dynamic, Value: argValue, Name: argValue}
return stepArgument, nil
}
//Step value is modified when inline table is found to account for the new parameter by appending {}
//todo validate headers for dynamic
func addInlineTableHeader(step *Step, token *Token) {
step.Value = fmt.Sprintf("%s %s", step.Value, ParameterPlaceholder)
step.HasInlineTable = true
step.addInlineTableHeaders(token.Args)
}
func addInlineTableRow(step *Step, token *Token, argLookup *ArgLookup) ParseResult {
dynamicArgMatcher := regexp.MustCompile("^<(.*)>$")
tableValues := make([]TableCell, 0)
warnings := make([]*Warning, 0)
for _, tableValue := range token.Args {
if dynamicArgMatcher.MatchString(tableValue) {
match := dynamicArgMatcher.FindAllStringSubmatch(tableValue, -1)
param := match[0][1]
if !argLookup.containsArg(param) {
tableValues = append(tableValues, TableCell{Value: tableValue, CellType: Static})
warnings = append(warnings, &Warning{LineNo: token.LineNo, Message: fmt.Sprintf("Dynamic param <%s> could not be resolved, Treating it as static param", param)})
} else {
tableValues = append(tableValues, TableCell{Value: param, CellType: Dynamic})
}
} else {
tableValues = append(tableValues, TableCell{Value: tableValue, CellType: Static})
}
}
step.addInlineTableRow(tableValues)
return ParseResult{Ok: true, Warnings: warnings}
}
//concept header will have dynamic param and should not be resolved through lookup, so passing nil lookup
func isConceptHeader(lookup *ArgLookup) bool {
return lookup == nil
}
func (lookup *ArgLookup) addArgName(argName string) {
if lookup.paramIndexMap == nil {
lookup.paramIndexMap = make(map[string]int)
lookup.paramValue = make([]paramNameValue, 0)
}
lookup.paramIndexMap[argName] = len(lookup.paramValue)
lookup.paramValue = append(lookup.paramValue, paramNameValue{name: argName})
}
func (lookup *ArgLookup) addArgValue(param string, stepArg *StepArg) {
paramIndex, ok := lookup.paramIndexMap[param]
if !ok {
panic(fmt.Sprintf("Accessing an invalid parameter (%s)", param))
}
lookup.paramValue[paramIndex].stepArg = stepArg
}
func (lookup *ArgLookup) containsArg(param string) bool {
_, ok := lookup.paramIndexMap[param]
return ok
}
func (lookup *ArgLookup) getArg(param string) *StepArg {
paramIndex, ok := lookup.paramIndexMap[param]
if !ok {
panic(fmt.Sprintf("Accessing an invalid parameter (%s)", param))
}
return lookup.paramValue[paramIndex].stepArg
}
func (lookup *ArgLookup) getCopy() *ArgLookup {
lookupCopy := new(ArgLookup)
for key, _ := range lookup.paramIndexMap {
lookupCopy.addArgName(key)
arg := lookup.getArg(key)
if arg != nil {
lookupCopy.addArgValue(key, &StepArg{Value: arg.Value, ArgType: arg.ArgType, Table: arg.Table, Name: arg.Name})
}
}
return lookupCopy
}
func (lookup *ArgLookup) FromDataTableRow(datatable *Table, index int) *ArgLookup {
dataTableLookup := new(ArgLookup)
if !datatable.IsInitialized() {
return dataTableLookup
}
for _, header := range datatable.Headers {
dataTableLookup.addArgName(header)
dataTableLookup.addArgValue(header, &StepArg{Value: datatable.Get(header)[index].Value, ArgType: Static})
}
return dataTableLookup
}
//create an empty lookup with only args to resolve dynamic params for steps
func (lookup *ArgLookup) fromDataTable(datatable *Table) *ArgLookup {
dataTableLookup := new(ArgLookup)
if !datatable.IsInitialized() {
return dataTableLookup
}
for _, header := range datatable.Headers {
dataTableLookup.addArgName(header)
}
return dataTableLookup
}
func (warning *Warning) String() string {
return fmt.Sprintf("line no: %d, %s", warning.LineNo, warning.Message)
}
func (scenario Scenario) Kind() TokenKind {
return ScenarioKind
}
func (scenario *Scenario) addHeading(heading *Heading) {
heading.HeadingType = ScenarioHeading
scenario.Heading = heading
}
func (scenario *Scenario) addStep(step *Step) {
scenario.Steps = append(scenario.Steps, step)
scenario.addItem(step)
}
func (scenario *Scenario) addTags(tags *Tags) {
scenario.Tags = tags
scenario.addItem(tags)
}
func (scenario *Scenario) addComment(comment *Comment) {
scenario.Comments = append(scenario.Comments, comment)
scenario.addItem(comment)
}
func (scenario *Scenario) renameSteps(oldStep Step, newStep Step, orderMap map[int]int) bool {
isRefactored := false
for _, step := range scenario.Steps {
isConcept := false
isRefactored = step.Rename(oldStep, newStep, isRefactored, orderMap, &isConcept)
}
return isRefactored
}
func (scenario *Scenario) addItem(itemToAdd Item) {
if scenario.Items == nil {
scenario.Items = make([]Item, 0)
}
scenario.Items = append(scenario.Items, itemToAdd)
}
func (scenario *Scenario) latestStep() *Step {
return scenario.Steps[len(scenario.Steps)-1]
}
func (heading *Heading) Kind() TokenKind {
return HeadingKind
}
func (comment *Comment) Kind() TokenKind {
return CommentKind
}
func (tags *Tags) Kind() TokenKind {
return TagKind
}
func (step Step) Kind() TokenKind {
return StepKind
}
func (specification *Specification) GetSpecItems() []Item {
specItems := make([]Item, 0)
for _, item := range specification.Items {
if item.Kind() != ScenarioKind {
specItems = append(specItems, item)
}
}
return specItems
}
// Not copying parent as it enters an infinite loop in case of nested concepts. This is because the steps under the concept
// are copied and their parent copying again comes back to copy the same concept.
func (self *Step) getCopy() *Step {
if !self.IsConcept {
return self
}
nestedStepsCopy := make([]*Step, 0)
for _, nestedStep := range self.ConceptSteps {
nestedStepsCopy = append(nestedStepsCopy, nestedStep.getCopy())
}
copiedConceptStep := new(Step)
*copiedConceptStep = *self
copiedConceptStep.ConceptSteps = nestedStepsCopy
copiedConceptStep.Lookup = *self.Lookup.getCopy()
return copiedConceptStep
}
func (self *Step) copyFrom(another *Step) {
self.IsConcept = another.IsConcept
if another.Args == nil {
self.Args = nil
} else {
self.Args = make([]*StepArg, len(another.Args))
copy(self.Args, another.Args)
}
if another.ConceptSteps == nil {
self.ConceptSteps = nil
} else {
self.ConceptSteps = make([]*Step, len(another.ConceptSteps))
copy(self.ConceptSteps, another.ConceptSteps)
}
if another.Fragments == nil {
self.Fragments = nil
} else {
self.Fragments = make([]*gauge_messages.Fragment, len(another.Fragments))
copy(self.Fragments, another.Fragments)
}