github.com/simonferquel/app@v0.6.1-0.20181012141724-68b7cccf26ac/pkg/yatee/yatee_test.go (about)

     1  package yatee
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/app/internal/yaml"
     7  	"gotest.tools/assert"
     8  )
     9  
    10  func testEval(t *testing.T, input string, env map[string]interface{}, output interface{}) {
    11  	v, err := eval(input, env, options{})
    12  	assert.NilError(t, err)
    13  	assert.Equal(t, v, output)
    14  }
    15  
    16  func TestEval(t *testing.T) {
    17  	env := map[string]interface{}{"false": "false", "foo": "bar", "baz": "bam", "bar": "pim", "barbam": "poum"}
    18  	testEval(t, "$foo ${baz}", env, "bar bam")
    19  	testEval(t, "${foo}$baz", env, "barbam")
    20  	testEval(t, "$(1 + 1)", env, int64(2))
    21  	testEval(t, "$(1+2*3)", env, int64(9))
    22  	testEval(t, "$(1+(2*3))", env, int64(7))
    23  	testEval(t, "$(1+((2*3)))", env, int64(7))
    24  	testEval(t, "$$$foo $$${baz}", env, "$bar $bam")
    25  	testEval(t, "${$foo}", env, "pim")
    26  	testEval(t, "${${foo}$baz}", env, "poum")
    27  	testEval(t, "${false?foo:bar}", env, "bar")
    28  	testEval(t, "${foo?foo:bar}", env, "foo")
    29  }
    30  
    31  func testProcess(t *testing.T, input, output, settings, error string) {
    32  	ps := make(map[interface{}]interface{})
    33  	err := yaml.Unmarshal([]byte(settings), ps)
    34  	assert.NilError(t, err)
    35  	s := make(map[string]interface{})
    36  	merge(s, ps)
    37  	res, err := Process(input, s)
    38  	if error == "" {
    39  		assert.NilError(t, err)
    40  		sres, err := yaml.Marshal(res)
    41  		assert.NilError(t, err)
    42  		assert.Equal(t, output, string(sres))
    43  	} else {
    44  		assert.Equal(t, err.Error(), error)
    45  	}
    46  }
    47  
    48  func TestProcess(t *testing.T) {
    49  	settings := `
    50  foo: bar
    51  loop: $loop
    52  app:
    53    mode: debug
    54    release: false
    55    count: 2
    56  ab:
    57    - a
    58    - b
    59  `
    60  	testProcess(t,
    61  		`services:
    62    "@if $app.release":
    63       no1: 1
    64    "@if ! $app.release":
    65       yes1: 1
    66       "@else":
    67         no3: 3
    68    "@if $($app.count - 2)":
    69      no2: 2
    70      "@else":
    71        yes3: 3
    72    "@if !$($app.count - 2)":
    73      yes2: 2
    74  `, `services:
    75    yes1: 1
    76    yes2: 2
    77    yes3: 3
    78  `, settings, "")
    79  
    80  	testProcess(t,
    81  		`switch:
    82    "@switch $app.mode":
    83      debug:
    84        yes1: 1
    85      other:
    86        no1: 1
    87      default:
    88        no2: 2
    89    "@switch ${app.mode}":
    90      default:
    91        yes2: 2
    92      release:
    93        no3: 3
    94  `, `switch:
    95    yes1: 1
    96    yes2: 2
    97  `, settings, "")
    98  
    99  	testProcess(t,
   100  		`services:
   101    "@for i in 0..$app.count":
   102      app$i: $($i+1)
   103    "@for i in $ab":
   104      bapp$i: foo$i
   105  `, `services:
   106    app0: 1
   107    app1: 2
   108    bappa: fooa
   109    bappb: foob
   110  `, settings, "")
   111  
   112  	testProcess(t,
   113  		`list:
   114    - v1
   115    - "@if (true) vtrue"
   116    - "@if (false) vfalse"
   117    - "@if (!false) vtrue2"
   118  `, `list:
   119  - v1
   120  - vtrue
   121  - vtrue2
   122  `, settings, "")
   123  
   124  	testProcess(t,
   125  		`services:
   126    "@for i in 0..2":
   127      "@for j in 0..2":
   128        foo$i$j: $($i*2 + $j)
   129        "@if $($i+$j%2)":
   130          bar$i$j: 1
   131  `, `services:
   132    bar01: 1
   133    bar10: 1
   134    foo00: 0
   135    foo01: 1
   136    foo10: 2
   137    foo11: 3
   138  `, settings, "")
   139  
   140  	testProcess(t,
   141  		"services: $loop",
   142  		"",
   143  		settings,
   144  		"eval loop detected")
   145  }
   146  
   147  func testProcessWithOrder(t *testing.T, input, output, error string) {
   148  	settings := make(map[string]interface{})
   149  
   150  	res, err := ProcessWithOrder(input, settings)
   151  
   152  	assert.NilError(t, err, "Error processing input: "+input)
   153  	sres, err := yaml.Marshal(res)
   154  	assert.NilError(t, err)
   155  	assert.Equal(t, output, string(sres), "Input was:"+string(sres)+"\nOutput was:"+output)
   156  }
   157  
   158  func TestProcessWithOrder(t *testing.T) {
   159  	// Test ordering is preserved inside nested structures
   160  	testProcessWithOrder(t,
   161  		`parent:
   162    bb: true
   163    aa: false
   164  `, `parent:
   165    bb: true
   166    aa: false
   167  `, "")
   168  
   169  	// Test ordering is preserved at the top level
   170  	testProcessWithOrder(t,
   171  		`bbb:
   172    nested: true
   173  aaa:
   174    nested: false
   175  `, `bbb:
   176    nested: true
   177  aaa:
   178    nested: false
   179  `, "")
   180  }