From 67da954f80ab24dcc96a1a509c339193b619a3dd Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 21 Aug 2017 21:34:59 +0200 Subject: [PATCH] refactoring TestOrbitGenerator_WriteOutputFile --- generator/generator_test.go | 89 ++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 36 deletions(-) diff --git a/generator/generator_test.go b/generator/generator_test.go index b2c369c..ec1725e 100644 --- a/generator/generator_test.go +++ b/generator/generator_test.go @@ -12,27 +12,22 @@ import ( "gopkg.in/yaml.v2" ) -// Tests Parse function. -func TestOrbitGenerator_Parse(t *testing.T) { - template, err := filepath.Abs("../.assets/tests/wrong_template.yml") - if err != nil { - panic(err) - } - - ctx, err := context.NewOrbitContext(template, "", "", "") - if err != nil { - panic(err) - } +var ( + // defaultGenerator is an instance of OrbitGenerator used in this test suite which contains one values file and one + // .env file. + defaultGenerator *OrbitGenerator - g := NewOrbitGenerator(ctx) + // manyGenerator is an instance of OrbitGenerator used in this test suite which contains two values files and two + // .env files. + manyGenerator *OrbitGenerator - if _, err := g.Parse(); err == nil { - t.Error("OrbitGenerator should not have been able to parse the template \"wrong_template.yml\"!") - } -} + // expectedResult contains the data from the file "expected_result.yml" + expectedResult interface{} +) -// Tests WriteOutputFile function. -func TestOrbitGenerator_WriteOutputFile(t *testing.T) { +// init instantiates expectedResult plus the OrbitGenerator defaultGenerator and manyGenerator. +func init() { + // retrieves the data from the file "expected_result.yml". expectedResultPath, err := filepath.Abs("../.assets/tests/expected_result.yml") if err != nil { panic(err) @@ -43,11 +38,11 @@ func TestOrbitGenerator_WriteOutputFile(t *testing.T) { panic(err) } - var expectedResult interface{} if err := yaml.Unmarshal(data, &expectedResult); err != nil { panic(err) } + // loads assets. defaultTmpl, err := filepath.Abs("../.assets/tests/template.yml") if err != nil { panic(err) @@ -70,14 +65,48 @@ func TestOrbitGenerator_WriteOutputFile(t *testing.T) { panic(err) } + defaultGenerator = NewOrbitGenerator(ctx) + + manyTmpl, err := filepath.Abs("../.assets/tests/template_many.yml") + if err != nil { + panic(err) + } + + ctx, err = context.NewOrbitContext(manyTmpl, "ru,"+values+";usa,"+values, "ru,"+envFile+";usa,"+envFile, rawData) + if err != nil { + panic(err) + } + + manyGenerator = NewOrbitGenerator(ctx) +} + +// Tests Parse function. +func TestOrbitGenerator_Parse(t *testing.T) { + template, err := filepath.Abs("../.assets/tests/wrong_template.yml") + if err != nil { + panic(err) + } + + ctx, err := context.NewOrbitContext(template, "", "", "") + if err != nil { + panic(err) + } + g := NewOrbitGenerator(ctx) - dataDefaultTmpl, err := g.Parse() + if _, err := g.Parse(); err == nil { + t.Error("OrbitGenerator should not have been able to parse the template \"wrong_template.yml\"!") + } +} + +// Tests WriteOutputFile function. +func TestOrbitGenerator_WriteOutputFile(t *testing.T) { + dataDefaultTmpl, err := defaultGenerator.Parse() if err != nil { t.Error("Failed to parse the default template!") } - if err := g.WriteOutputFile("result.yml", dataDefaultTmpl); err != nil { + if err := defaultGenerator.WriteOutputFile("result.yml", dataDefaultTmpl); err != nil { t.Error("Failed to write the outpout file from the default template!") } @@ -97,24 +126,12 @@ func TestOrbitGenerator_WriteOutputFile(t *testing.T) { t.Error("Result file from the default template should be equal to the expected result!") } - manyTmpl, err := filepath.Abs("../.assets/tests/template_many.yml") - if err != nil { - panic(err) - } - - ctx, err = context.NewOrbitContext(manyTmpl, "ru,"+values+";usa,"+values, "ru,"+envFile+";usa,"+envFile, rawData) - if err != nil { - panic(err) - } - - g = NewOrbitGenerator(ctx) - - dataManyTmpl, err := g.Parse() + dataManyTmpl, err := manyGenerator.Parse() if err != nil { t.Error("Failed to parse the many template!") } - if err := g.WriteOutputFile("result.yml", dataManyTmpl); err != nil { + if err := manyGenerator.WriteOutputFile("result.yml", dataManyTmpl); err != nil { t.Error("Failed to write the outpout file from the many template!") } @@ -134,7 +151,7 @@ func TestOrbitGenerator_WriteOutputFile(t *testing.T) { t.Error("Result file from the many template should be equal to the expected result!") } - if err := g.WriteOutputFile("/...", dataManyTmpl); err == nil { + if err := manyGenerator.WriteOutputFile("/...", dataManyTmpl); err == nil { t.Error("WriteOutputFile should not been able to to write the outpout file \"/...\"!") } }