github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/formatter/formatter_test.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 formatter
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/getgauge/gauge/gauge"
    24  	"github.com/getgauge/gauge/parser"
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  func Test(t *testing.T) { TestingT(t) }
    29  
    30  type MySuite struct{}
    31  
    32  var _ = Suite(&MySuite{})
    33  
    34  func (s *MySuite) TestFormatSpecification(c *C) {
    35  	tokens := []*parser.Token{
    36  		&parser.Token{Kind: gauge.SpecKind, Value: "Spec Heading", LineNo: 1},
    37  		&parser.Token{Kind: gauge.ScenarioKind, Value: "Scenario Heading", LineNo: 2},
    38  		&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 3, LineText: "Example step"},
    39  		&parser.Token{Kind: gauge.StepKind, Value: "Step with inline table", LineNo: 3, LineText: "Step with inline table "},
    40  		&parser.Token{Kind: gauge.TableHeader, Args: []string{"id", "name"}},
    41  		&parser.Token{Kind: gauge.TableRow, Args: []string{"<1>", "foo"}},
    42  		&parser.Token{Kind: gauge.TableRow, Args: []string{"2", "bar"}},
    43  	}
    44  
    45  	spec, _ := new(parser.SpecParser).CreateSpecification(tokens, gauge.NewConceptDictionary())
    46  
    47  	formatted := FormatSpecification(spec)
    48  
    49  	c.Assert(formatted, Equals,
    50  		`Spec Heading
    51  ============
    52  Scenario Heading
    53  ----------------
    54  * Example step
    55  * Step with inline table`+" "+`
    56       |id |name|
    57       |---|----|
    58       |<1>|foo |
    59       |2  |bar |
    60  `)
    61  }
    62  
    63  func (s *MySuite) TestFormatConcepts(c *C) {
    64  	dictionary := gauge.NewConceptDictionary()
    65  	step1 := &gauge.Step{Value: "sdsf", LineText: "sdsf", IsConcept: true, LineNo: 1, PreComments: []*gauge.Comment{&gauge.Comment{Value: "COMMENT", LineNo: 1}}}
    66  	step2 := &gauge.Step{Value: "dsfdsfdsf", LineText: "dsfdsfdsf", IsConcept: true, LineNo: 2, Items: []gauge.Item{&gauge.Step{Value: "sfd", LineText: "sfd", IsConcept: false}, &gauge.Step{Value: "sdfsdf" + "T", LineText: "sdfsdf" + "T", IsConcept: false}}}
    67  
    68  	dictionary.ConceptsMap[step1.Value] = &gauge.Concept{ConceptStep: step1, FileName: "file.cpt"}
    69  	dictionary.ConceptsMap[step2.Value] = &gauge.Concept{ConceptStep: step2, FileName: "file.cpt"}
    70  
    71  	formatted := FormatConcepts(dictionary)
    72  	c.Assert(formatted["file.cpt"], Equals, `COMMENT
    73  # sdsf
    74  # dsfdsfdsf
    75  * sdfsdfT
    76  `)
    77  }
    78  
    79  func (s *MySuite) TestFormatSpecificationWithTags(c *C) {
    80  	tokens := []*parser.Token{
    81  		&parser.Token{Kind: gauge.SpecKind, Value: "My Spec Heading", LineNo: 1},
    82  		&parser.Token{Kind: gauge.TagKind, Args: []string{"tag1", "tag2"}, LineNo: 2},
    83  		&parser.Token{Kind: gauge.ScenarioKind, Value: "Scenario Heading", LineNo: 3},
    84  		&parser.Token{Kind: gauge.TagKind, Args: []string{"tag3", "tag4"}, LineNo: 4},
    85  		&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 5, LineText: "Example step"},
    86  		&parser.Token{Kind: gauge.ScenarioKind, Value: "Scenario Heading1", LineNo: 6},
    87  		&parser.Token{Kind: gauge.TagKind, Args: []string{"tag3", "tag4"}, LineNo: 7},
    88  		&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 8, LineText: "Example step"},
    89  	}
    90  
    91  	spec, _ := new(parser.SpecParser).CreateSpecification(tokens, gauge.NewConceptDictionary())
    92  	formatted := FormatSpecification(spec)
    93  	c.Assert(formatted, Equals,
    94  		`My Spec Heading
    95  ===============
    96  tags: tag1, tag2
    97  Scenario Heading
    98  ----------------
    99  tags: tag3, tag4
   100  * Example step
   101  Scenario Heading1
   102  -----------------
   103  tags: tag3, tag4
   104  * Example step
   105  `)
   106  
   107  }
   108  
   109  func (s *MySuite) TestFormatSpecificationWithTeardownSteps(c *C) {
   110  	tokens := []*parser.Token{
   111  		&parser.Token{Kind: gauge.SpecKind, Value: "My Spec Heading", LineNo: 1},
   112  		&parser.Token{Kind: gauge.TagKind, Args: []string{"tag1", "tag2"}, LineNo: 2},
   113  		&parser.Token{Kind: gauge.ScenarioKind, Value: "Scenario Heading", LineNo: 3},
   114  		&parser.Token{Kind: gauge.TagKind, Args: []string{"tag3", "tag4"}, LineNo: 4},
   115  		&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 5, LineText: "Example step"},
   116  		&parser.Token{Kind: gauge.ScenarioKind, Value: "Scenario Heading1", LineNo: 6},
   117  		&parser.Token{Kind: gauge.TagKind, Args: []string{"tag3", "tag4"}, LineNo: 7},
   118  		&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 8, LineText: "Example step"},
   119  		&parser.Token{Kind: gauge.TearDownKind, Value: "____", LineNo: 9},
   120  		&parser.Token{Kind: gauge.StepKind, Value: "Example step1", LineNo: 10, LineText: "Example step1"},
   121  		&parser.Token{Kind: gauge.StepKind, Value: "Example step2", LineNo: 11, LineText: "Example step2"},
   122  	}
   123  
   124  	spec, _ := new(parser.SpecParser).CreateSpecification(tokens, gauge.NewConceptDictionary())
   125  	formatted := FormatSpecification(spec)
   126  	c.Assert(formatted, Equals,
   127  		`My Spec Heading
   128  ===============
   129  tags: tag1, tag2
   130  Scenario Heading
   131  ----------------
   132  tags: tag3, tag4
   133  * Example step
   134  Scenario Heading1
   135  -----------------
   136  tags: tag3, tag4
   137  * Example step
   138  ____
   139  * Example step1
   140  * Example step2
   141  `)
   142  
   143  }
   144  
   145  func (s *MySuite) TestFormatStep(c *C) {
   146  	step := &gauge.Step{Value: "my step with {}, {}, {} and {}", Args: []*gauge.StepArg{&gauge.StepArg{Value: "static \"foo\"", ArgType: gauge.Static},
   147  		&gauge.StepArg{Value: "dynamic \"foo\"", ArgType: gauge.Dynamic},
   148  		&gauge.StepArg{Name: "file:user\".txt", ArgType: gauge.SpecialString},
   149  		&gauge.StepArg{Name: "table :hell\".csv", ArgType: gauge.SpecialTable}}}
   150  	formatted := FormatStep(step)
   151  	c.Assert(formatted, Equals, `* my step with "static \"foo\"", <dynamic \"foo\">, <file:user\".txt> and <table :hell\".csv>
   152  `)
   153  }
   154  
   155  func (s *MySuite) TestFormattingWithTableAsAComment(c *C) {
   156  	tokens := []*parser.Token{
   157  		&parser.Token{Kind: gauge.SpecKind, Value: "My Spec Heading", LineNo: 1},
   158  		&parser.Token{Kind: gauge.ScenarioKind, Value: "Scenario Heading", LineNo: 3},
   159  		&parser.Token{Kind: gauge.TableHeader, Args: []string{"id", "name"}, LineText: " |id|name|"},
   160  		&parser.Token{Kind: gauge.TableRow, Args: []string{"1", "foo"}, LineText: " |1|foo|"},
   161  		&parser.Token{Kind: gauge.TableRow, Args: []string{"2", "bar"}, LineText: "|2|bar|"},
   162  		&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 5, LineText: "Example step"},
   163  	}
   164  
   165  	spec, _ := new(parser.SpecParser).CreateSpecification(tokens, gauge.NewConceptDictionary())
   166  	formatted := FormatSpecification(spec)
   167  	c.Assert(formatted, Equals,
   168  		`My Spec Heading
   169  ===============
   170  Scenario Heading
   171  ----------------
   172   |id|name|
   173   |1|foo|
   174  |2|bar|
   175  * Example step
   176  `)
   177  }
   178  
   179  func (s *MySuite) TestFormatSpecificationWithTableContainingDynamicParameters(c *C) {
   180  	tokens := []*parser.Token{
   181  		&parser.Token{Kind: gauge.SpecKind, Value: "Spec Heading", LineNo: 1},
   182  		&parser.Token{Kind: gauge.TableHeader, Args: []string{"id", "foo"}},
   183  		&parser.Token{Kind: gauge.TableRow, Args: []string{"1", "f"}},
   184  		&parser.Token{Kind: gauge.ScenarioKind, Value: "Scenario Heading", LineNo: 2},
   185  		&parser.Token{Kind: gauge.StepKind, Value: "Example step", LineNo: 3, LineText: "Example step"},
   186  		&parser.Token{Kind: gauge.StepKind, Value: "Step with inline table", LineNo: 3, LineText: "Step with inline table "},
   187  		&parser.Token{Kind: gauge.TableHeader, Args: []string{"id", "name"}},
   188  		&parser.Token{Kind: gauge.TableRow, Args: []string{"1", "<foo>"}},
   189  		&parser.Token{Kind: gauge.TableRow, Args: []string{"2", "bar"}},
   190  	}
   191  
   192  	spec, _ := new(parser.SpecParser).CreateSpecification(tokens, gauge.NewConceptDictionary())
   193  
   194  	formatted := FormatSpecification(spec)
   195  
   196  	c.Assert(formatted, Equals,
   197  		`Spec Heading
   198  ============
   199       |id|foo|
   200       |--|---|
   201       |1 |f  |
   202  Scenario Heading
   203  ----------------
   204  * Example step
   205  * Step with inline table `+`
   206       |id|name |
   207       |--|-----|
   208       |1 |<foo>|
   209       |2 |bar  |
   210  `)
   211  }