converter_test.go 1.92 KB
Newer Older
1 2 3
package helpers

import (
4 5
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
6
	"gopkg.in/yaml.v2"
7
	"reflect"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	"testing"
)

type TestObj struct {
	Text   string `json:"TextJson" yaml:"TextYaml"`
	Number int
}

func TestSimpleYamlMarshalling(t *testing.T) {

	ymlString := ToYAML(TestObj{
		Text:   "example",
		Number: 25,
	})
	expectedYml := "TextYaml: example\nnumber: 25\n"

	if ymlString != expectedYml {
		t.Error("Expected ", expectedYml, ", got ", ymlString)
	}
}

func TestSimpleTomlMarshalling(t *testing.T) {

	tomlString := ToTOML(TestObj{
		Text:   "example",
		Number: 25,
	})
	expectedToml := "Text = \"example\"\nNumber = 25\n"

	if tomlString != expectedToml {
		t.Error("Expected ", expectedToml, ", got ", tomlString)
	}
}
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

func TestToConfigMap(t *testing.T) {
	data := `
build:
    script:
         - echo "1" >> foo
         - cat foo

cache:
    untracked: true
    paths:
        - vendor/
        - foo

test:
    script:
    - make test
`

	config := make(map[string]interface{})
	err := yaml.Unmarshal([]byte(data), config)
	if err != nil {
		t.Error("Error parsing test YAML data")
	}

	expectedCacheConfig := map[string]interface{}{
		"untracked": true,
		"paths":     []interface{}{"vendor/", "foo"},
	}
	cacheConfig, ok := ToConfigMap(config["cache"])

	if !ok {
		t.Error("Conversion failed")
	}

	if !reflect.DeepEqual(cacheConfig, expectedCacheConfig) {
		t.Error("Result ", cacheConfig, " was not equal to ", expectedCacheConfig)
	}
}
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

func TestGetMapKey(t *testing.T) {
	data := `
test:
    script:
    - make test
    cache:
        untracked: true
        paths:
            - vendor/
            - foo
`

	config1 := make(map[string]interface{})
	require.NoError(t, yaml.Unmarshal([]byte(data), config1))

	value, ok := GetMapKey(config1, "test", "cache", "untracked")
	assert.True(t, ok)
	assert.Equal(t, true, value)

	_, ok = GetMapKey(config1, "test", "undefined", "untracked")
	assert.False(t, ok)
}