github.com/getgauge/gauge@v1.6.9/filter/specsFilter_test.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 filter
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/getgauge/gauge/gauge"
    13  	. "gopkg.in/check.v1"
    14  )
    15  
    16  func (s *MySuite) TestDistributionOfSpecs(c *C) {
    17  	specs := createSpecsList(10)
    18  	specCollections := DistributeSpecs(specs, 10)
    19  	c.Assert(len(specCollections), Equals, 10)
    20  	verifySpecCollectionsForSize(c, 1, specCollections...)
    21  
    22  	specCollections = DistributeSpecs(specs, 5)
    23  	c.Assert(len(specCollections), Equals, 5)
    24  	verifySpecCollectionsForSize(c, 2, specCollections...)
    25  
    26  	specCollections = DistributeSpecs(specs, 4)
    27  	c.Assert(len(specCollections), Equals, 4)
    28  	verifySpecCollectionsForSize(c, 3, specCollections[:2]...)
    29  	verifySpecCollectionsForSize(c, 2, specCollections[2:]...)
    30  
    31  	specCollections = DistributeSpecs(specs, 3)
    32  	c.Assert(len(specCollections), Equals, 3)
    33  	verifySpecCollectionsForSize(c, 4, specCollections[0])
    34  	verifySpecCollectionsForSize(c, 3, specCollections[1:]...)
    35  
    36  	specs = createSpecsList(0)
    37  	specCollections = DistributeSpecs(specs, 0)
    38  	c.Assert(len(specCollections), Equals, 0)
    39  }
    40  
    41  func verifySpecCollectionsForSize(c *C, size int, specCollections ...*gauge.SpecCollection) {
    42  	for _, collection := range specCollections {
    43  		c.Assert(len(collection.Specs()), Equals, size)
    44  	}
    45  }
    46  
    47  func createSpecsList(number int) []*gauge.Specification {
    48  	var specs []*gauge.Specification
    49  	for i := 0; i < number; i++ {
    50  		specs = append(specs, &gauge.Specification{FileName: fmt.Sprint("spec", i)})
    51  	}
    52  	return specs
    53  }
    54  
    55  func (s *MySuite) TestToRunSpecificSetOfSpecs(c *C) {
    56  	var specs []*gauge.Specification
    57  	spec1 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING1"}}
    58  	spec2 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING2"}}
    59  	heading3 := &gauge.Heading{Value: "SPECHEADING3"}
    60  	spec3 := &gauge.Specification{Heading: heading3}
    61  	spec4 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING4"}}
    62  	spec5 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING5"}}
    63  	spec6 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING6"}}
    64  	specs = append(specs, spec1)
    65  	specs = append(specs, spec2)
    66  	specs = append(specs, spec3)
    67  	specs = append(specs, spec4)
    68  	specs = append(specs, spec5)
    69  	specs = append(specs, spec6)
    70  
    71  	value := 6
    72  	value1 := 3
    73  
    74  	groupFilter := &specsGroupFilter{value1, value}
    75  	specsToExecute := groupFilter.filter(specs)
    76  
    77  	c.Assert(len(specsToExecute), Equals, 1)
    78  	c.Assert(specsToExecute[0].Heading, Equals, heading3)
    79  
    80  }
    81  
    82  func (s *MySuite) TestToRunSpecificSetOfSpecsGivesSameSpecsEverytime(c *C) {
    83  	spec1 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING1"}}
    84  	spec2 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING2"}}
    85  	spec3 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING3"}}
    86  	spec4 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING4"}}
    87  	heading5 := &gauge.Heading{Value: "SPECHEADING5"}
    88  	spec5 := &gauge.Specification{Heading: heading5}
    89  	heading6 := &gauge.Heading{Value: "SPECHEADING6"}
    90  	spec6 := &gauge.Specification{Heading: heading6}
    91  	var specs []*gauge.Specification
    92  	specs = append(specs, spec1)
    93  	specs = append(specs, spec2)
    94  	specs = append(specs, spec3)
    95  	specs = append(specs, spec4)
    96  	specs = append(specs, spec5)
    97  	specs = append(specs, spec6)
    98  
    99  	value := 3
   100  
   101  	groupFilter := &specsGroupFilter{value, value}
   102  	specsToExecute1 := groupFilter.filter(specs)
   103  	c.Assert(len(specsToExecute1), Equals, 2)
   104  
   105  	specsToExecute2 := groupFilter.filter(specs)
   106  	c.Assert(len(specsToExecute2), Equals, 2)
   107  
   108  	specsToExecute3 := groupFilter.filter(specs)
   109  	c.Assert(len(specsToExecute3), Equals, 2)
   110  
   111  	c.Assert(specsToExecute2[0].Heading, Equals, specsToExecute1[0].Heading)
   112  	c.Assert(specsToExecute2[1].Heading, Equals, specsToExecute1[1].Heading)
   113  	c.Assert(specsToExecute3[0].Heading, Equals, specsToExecute1[0].Heading)
   114  	c.Assert(specsToExecute3[1].Heading, Equals, specsToExecute1[1].Heading)
   115  }
   116  
   117  func (s *MySuite) TestToRunNonExistingSpecificSetOfSpecs(c *C) {
   118  	spec1 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING1"}}
   119  	var specs []*gauge.Specification
   120  	specs = append(specs, spec1)
   121  	value := 3
   122  	groupFilter := &specsGroupFilter{value, value}
   123  	specsToExecute := groupFilter.filter(specs)
   124  	c.Assert(len(specsToExecute), Equals, 0)
   125  }
   126  
   127  func (s *MySuite) TestToRunSpecificSetOfSpecsGivesEmptySpecsIfDistributableNumberIsNotValid(c *C) {
   128  	spec1 := &gauge.Specification{Heading: &gauge.Heading{Value: "SPECHEADING1"}}
   129  	var specs []*gauge.Specification
   130  	specs = append(specs, spec1)
   131  
   132  	value := 1
   133  	value1 := 3
   134  	groupFilter := &specsGroupFilter{value1, value}
   135  	specsToExecute1 := groupFilter.filter(specs)
   136  	c.Assert(len(specsToExecute1), Equals, 0)
   137  
   138  	value = 1
   139  	value1 = -3
   140  	groupFilter = &specsGroupFilter{value1, value}
   141  	specsToExecute1 = groupFilter.filter(specs)
   142  	c.Assert(len(specsToExecute1), Equals, 0)
   143  }