github.com/microsoft/moc@v0.17.1/pkg/config/config_test.go (about) 1 // Copyright (c) Microsoft Corporation. All rights reserved. 2 // Licensed under the Apache v2.0 license. 3 package config 4 5 import ( 6 "fmt" 7 "testing" 8 9 "github.com/microsoft/moc/pkg/marshal" 10 ) 11 12 type SampleStruct struct { 13 TestString string `json:"testString,omitempty" yaml:testString,omitempty"` 14 TestString2 string `json:"testStrings,omitempty" yaml:testString,omitempty"` 15 TestInt int `json:"testInt,omitempty" yaml:"testInt,omitempty"` 16 TestArray []string `json:"testArray,omitempty" yaml:"testArray,omitempty"` 17 } 18 19 var tmp = SampleStruct{ 20 TestString: "TestString", 21 TestString2: "TestString2", 22 TestInt: 1, 23 TestArray: []string{"TestArray"}, 24 } 25 26 var tmpArray = []SampleStruct{ 27 { 28 TestString: "test1String", 29 TestString2: "test1String2", 30 TestInt: 1, 31 TestArray: []string{"test1Array"}, 32 }, 33 { 34 TestString: "test2String", 35 TestString2: "test2String2", 36 TestInt: 2, 37 TestArray: []string{"test2Array"}, 38 }, 39 } 40 41 func Test_LoadYAMLConfig(t *testing.T) { 42 tmpString := `teststring: TestString 43 testInt: 1 44 testArray: 45 - TestArray` 46 47 tmpData := SampleStruct{} 48 err := LoadYAMLConfig(tmpString, &tmpData) 49 if err != nil { 50 t.Errorf("Failed to load Yaml Config" + err.Error()) 51 } 52 } 53 func Test_PrintYAML(t *testing.T) { 54 tmp := SampleStruct{ 55 TestString: "TestString", 56 TestInt: 1, 57 TestArray: []string{"TestArray"}, 58 } 59 PrintYAML(tmp) 60 } 61 func Test_PrintJSON(t *testing.T) { 62 PrintJSON(tmp) 63 } 64 func Test_PrintYAMLList(t *testing.T) { 65 PrintYAMLList(tmpArray) 66 } 67 func Test_PrintJSONList(t *testing.T) { 68 PrintJSONList(tmpArray) 69 } 70 func Test_PrintTable(t *testing.T) { 71 PrintTable(tmpArray) 72 } 73 74 func Test_PrintFormat(t *testing.T) { 75 PrintFormat(tmp, "", "tsv") 76 PrintFormat(tmp, "", "csv") 77 } 78 79 func Test_PrintFormatList(t *testing.T) { 80 PrintFormatList(tmpArray, "", "tsv") 81 PrintFormatList(tmpArray, "", "csv") 82 } 83 84 func Test_MarshalOutputWithoutQuery(t *testing.T) { 85 err := verifyMarshalOutput(tmpArray, "", 2) 86 if err != nil { 87 t.Errorf("MarshalOutput with empty query failed: %s", err.Error()) 88 } 89 } 90 91 func Test_MarshalOutputWithIntQuery(t *testing.T) { 92 err := verifyMarshalOutput(tmpArray, "[?testInt==`2`]", 1) 93 if err != nil { 94 t.Errorf("MarshalOutput with int query failed: %s", err.Error()) 95 } 96 } 97 98 func Test_MarshalOutputWithStringQuery(t *testing.T) { 99 err := verifyMarshalOutput(tmpArray, "[?testString=='test1String']", 1) 100 if err != nil { 101 t.Errorf("MarshalOutput with string query failed: %s", err.Error()) 102 } 103 } 104 105 func verifyMarshalOutput(data interface{}, query string, expectedResultCount int) error { 106 result, err := MarshalOutput(data, query, "json") 107 if err != nil { 108 return err 109 } 110 111 var filteredArray []SampleStruct 112 err = marshal.FromJSONBytes(result, &filteredArray) 113 if err != nil { 114 return err 115 } 116 117 if len(filteredArray) != expectedResultCount { 118 return fmt.Errorf("Unexpected result count. Expected: %d / Actual: %d", expectedResultCount, len(filteredArray)) 119 } 120 121 return nil 122 }