github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/environment_test.go (about) 1 package yaml 2 3 import ( 4 "github.com/google/go-cmp/cmp" 5 "gopkg.in/yaml.v3" 6 "testing" 7 ) 8 9 func TestEnvironment(t *testing.T) { 10 tests := []struct { 11 yaml string 12 want Environment 13 }{ 14 // string value 15 { 16 yaml: `staging_environment`, 17 want: Environment{Name: "staging_environment"}, 18 }, 19 // struct value 20 { 21 yaml: ` 22 name: production_environment 23 url: https://github.com 24 `, 25 want: Environment{ 26 Name: "production_environment", 27 URL: "https://github.com", 28 }, 29 }, 30 } 31 32 for i, test := range tests { 33 got := new(Environment) 34 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 35 t.Log(test.yaml) 36 t.Error(err) 37 return 38 } 39 if diff := cmp.Diff(got, &test.want); diff != "" { 40 t.Log(test.yaml) 41 t.Errorf("Unexpected parsing results for test %v", i) 42 t.Log(diff) 43 } 44 } 45 } 46 47 func TestEnvironment_Error(t *testing.T) { 48 err := yaml.Unmarshal([]byte("[[]]"), new(Environment)) 49 if err == nil || err.Error() != "failed to unmarshal environment" { 50 t.Errorf("Expect error, got %s", err) 51 } 52 }