github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/addons_snaps_test.go (about)

     1  // Copyright 2022 Harness, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package yaml
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"gopkg.in/yaml.v3"
    22  )
    23  
    24  func TestSnap(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Snaps
    28  	}{
    29  		{
    30  			yaml: `"mysql.snap"`,
    31  			want: Snaps{
    32  				Items: []*Snap{
    33  					{Name: "mysql.snap"},
    34  				},
    35  			},
    36  		},
    37  		{
    38  			yaml: `[ "mysql.snap" ]`,
    39  			want: Snaps{
    40  				Items: []*Snap{
    41  					{Name: "mysql.snap"},
    42  				},
    43  			},
    44  		},
    45  		{
    46  			yaml: `{ name: mysql.snap, classic: true, channel: edge }`,
    47  			want: Snaps{
    48  				Items: []*Snap{
    49  					{Name: "mysql.snap", Channel: "edge", Classic: true},
    50  				},
    51  			},
    52  		},
    53  		{
    54  			yaml: `[{ name: mysql.snap, classic: true, channel: edge }]`,
    55  			want: Snaps{
    56  				Items: []*Snap{
    57  					{Name: "mysql.snap", Channel: "edge", Classic: true},
    58  				},
    59  			},
    60  		},
    61  	}
    62  
    63  	for i, test := range tests {
    64  		got := new(Snaps)
    65  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    66  			t.Log(test.yaml)
    67  			t.Error(err)
    68  			return
    69  		}
    70  		if diff := cmp.Diff(got, &test.want); diff != "" {
    71  			t.Log(test.yaml)
    72  			t.Errorf("Unexpected parsing results for test %v", i)
    73  			t.Log(diff)
    74  		}
    75  	}
    76  }
    77  
    78  func TestSnap_Error(t *testing.T) {
    79  	err := yaml.Unmarshal([]byte("[[]]"), new(Snap))
    80  	if err == nil || err.Error() != "failed to unmarshal snap" {
    81  		t.Errorf("Expect error, got %s", err)
    82  	}
    83  }
    84  
    85  func TestSnaps_Error(t *testing.T) {
    86  	err := yaml.Unmarshal([]byte("[[]]"), new(Snaps))
    87  	if err == nil || err.Error() != "failed to unmarshal snaps" {
    88  		t.Errorf("Expect error, got %s", err)
    89  	}
    90  }
    91  
    92  func TestSnaps_Marshal(t *testing.T) {
    93  	tests := []struct {
    94  		before Snaps
    95  		after  string
    96  	}{
    97  		{
    98  			before: Snaps{Items: []*Snap{{Name: "mysql"}}},
    99  			after:  "- name: mysql\n",
   100  		},
   101  		{
   102  			before: Snaps{Items: []*Snap{{Name: "mysql", Classic: true, Channel: "edge"}}},
   103  			after:  "- name: mysql\n  classic: true\n  channel: edge\n",
   104  		},
   105  	}
   106  
   107  	for _, test := range tests {
   108  		after, err := yaml.Marshal(&test.before)
   109  		if err != nil {
   110  			t.Error(err)
   111  			return
   112  		}
   113  		if got, want := string(after), test.after; got != want {
   114  			t.Errorf("want yaml %q, got %q", want, got)
   115  		}
   116  	}
   117  }