github.com/zntrio/harp/v2@v2.0.9/pkg/template/values/parser_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package values 19 20 import ( 21 "reflect" 22 "testing" 23 24 "github.com/zntrio/harp/v2/pkg/template/values/hcl1" 25 "github.com/zntrio/harp/v2/pkg/template/values/hcl2" 26 "github.com/zntrio/harp/v2/pkg/template/values/hocon" 27 "github.com/zntrio/harp/v2/pkg/template/values/toml" 28 "github.com/zntrio/harp/v2/pkg/template/values/xml" 29 "github.com/zntrio/harp/v2/pkg/template/values/yaml" 30 ) 31 32 func TestGetParser(t *testing.T) { 33 testTable := []struct { 34 name string 35 fileType string 36 expected Parser 37 expectError bool 38 }{ 39 { 40 name: "Test getting HOCON parser", 41 fileType: "hocon", 42 expected: new(hocon.Parser), 43 expectError: false, 44 }, 45 { 46 name: "Test getting TOML parser", 47 fileType: "toml", 48 expected: new(toml.Parser), 49 expectError: false, 50 }, 51 { 52 name: "Test getting XML parser", 53 fileType: "xml", 54 expected: new(xml.Parser), 55 expectError: false, 56 }, 57 { 58 name: "Test getting Terraform parser from HCL1 input", 59 fileType: "hcl1", 60 expected: new(hcl1.Parser), 61 expectError: false, 62 }, 63 { 64 name: "Test getting Terraform parser from HCL2 input", 65 fileType: "tf", 66 expected: new(hcl2.Parser), 67 expectError: false, 68 }, 69 { 70 name: "Test getting Terraform parser from YAML input", 71 fileType: "yaml", 72 expected: new(yaml.Parser), 73 expectError: false, 74 }, 75 { 76 name: "Test getting Terraform parser from JSON input", 77 fileType: "json", 78 expected: new(yaml.Parser), 79 expectError: false, 80 }, 81 { 82 name: "Test getting invalid filetype", 83 fileType: "epicfailure", 84 expected: nil, 85 expectError: true, 86 }, 87 } 88 89 for _, testUnit := range testTable { 90 t.Run(testUnit.name, func(t *testing.T) { 91 received, err := GetParser(testUnit.fileType) 92 93 if !reflect.DeepEqual(received, testUnit.expected) { 94 t.Errorf("expected: %T \n got this: %T", testUnit.expected, received) 95 } 96 if !testUnit.expectError && err != nil { 97 t.Errorf("error here: %v", err) 98 } 99 if testUnit.expectError && err == nil { 100 t.Error("error expected but not received") 101 } 102 }) 103 } 104 }