github.com/getgauge/gauge@v1.6.9/gauge/specCollection.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package gauge 8 9 import ( 10 "sync" 11 ) 12 13 type SpecCollection struct { 14 mutex sync.Mutex 15 index int 16 specs [][]*Specification 17 } 18 19 func NewSpecCollection(s []*Specification, groupDataTableSpecs bool) *SpecCollection { 20 if !groupDataTableSpecs { 21 var specs [][]*Specification 22 for _, spec := range s { 23 specs = append(specs, []*Specification{spec}) 24 } 25 return &SpecCollection{specs: specs} 26 } 27 return &SpecCollection{specs: combineDataTableSpecs(s)} 28 } 29 30 func combineDataTableSpecs(s []*Specification) (specs [][]*Specification) { 31 combinedSpecs := make(map[string][]*Specification) 32 for _, spec := range s { 33 combinedSpecs[spec.FileName] = append(combinedSpecs[spec.FileName], spec) 34 } 35 for _, spec := range s { 36 if _, ok := combinedSpecs[spec.FileName]; ok { 37 specs = append(specs, combinedSpecs[spec.FileName]) 38 delete(combinedSpecs, spec.FileName) 39 } 40 } 41 return 42 } 43 44 func (s *SpecCollection) Add(spec *Specification) { 45 s.specs = append(s.specs, []*Specification{spec}) 46 } 47 48 func (s *SpecCollection) Specs() (specs []*Specification) { 49 for _, subSpecs := range s.specs { 50 specs = append(specs, subSpecs...) 51 } 52 return specs 53 } 54 55 func (s *SpecCollection) HasNext() bool { 56 s.mutex.Lock() 57 defer s.mutex.Unlock() 58 return s.index < len(s.specs) 59 } 60 61 func (s *SpecCollection) Next() []*Specification { 62 s.mutex.Lock() 63 defer s.mutex.Unlock() 64 spec := s.specs[s.index] 65 s.index++ 66 return spec 67 } 68 69 func (s *SpecCollection) Size() int { 70 s.mutex.Lock() 71 defer s.mutex.Unlock() 72 length := len(s.specs) 73 return length 74 } 75 76 func (s *SpecCollection) SpecNames() []string { 77 specNames := make([]string, 0) 78 for _, specs := range s.specs { 79 for _, subSpec := range specs { 80 specNames = append(specNames, subSpec.FileName) 81 } 82 } 83 return specNames 84 }