github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/yaml/size_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  	"gopkg.in/yaml.v3"
    21  )
    22  
    23  func TestSize_String(t *testing.T) {
    24  	tests := []struct {
    25  		size Size
    26  		want string
    27  	}{
    28  		{
    29  			size: Size1x,
    30  			want: "1x",
    31  		},
    32  		{
    33  			size: Size2x,
    34  			want: "2x",
    35  		},
    36  		{
    37  			size: Size4x,
    38  			want: "4x",
    39  		},
    40  		{
    41  			size: Size8x,
    42  			want: "8x",
    43  		},
    44  		{
    45  			size: SizeNone,
    46  			want: "",
    47  		},
    48  	}
    49  	for _, test := range tests {
    50  		if got, want := test.size.String(), test.want; got != want {
    51  			t.Errorf("Want Size %s, got %s", want, got)
    52  		}
    53  	}
    54  }
    55  
    56  func TestSize_Marshal(t *testing.T) {
    57  	tests := []struct {
    58  		size Size
    59  		want string
    60  	}{
    61  		{
    62  			size: Size1x,
    63  			want: "1x\n",
    64  		},
    65  		{
    66  			size: Size2x,
    67  			want: "2x\n",
    68  		},
    69  		{
    70  			size: Size4x,
    71  			want: "4x\n",
    72  		},
    73  		{
    74  			size: Size8x,
    75  			want: "8x\n",
    76  		},
    77  		{
    78  			size: SizeNone,
    79  			want: "null\n",
    80  		},
    81  	}
    82  	for _, test := range tests {
    83  		got, err := yaml.Marshal(test.size)
    84  		if err != nil {
    85  			t.Error(err)
    86  			return
    87  		}
    88  		if got, want := string(got), test.want; got != want {
    89  			t.Errorf("Want Size %q, got %q", want, got)
    90  		}
    91  	}
    92  }
    93  
    94  func TestSize_Unmarshal(t *testing.T) {
    95  	tests := []struct {
    96  		want Size
    97  		yaml string
    98  	}{
    99  		{
   100  			want: Size1x,
   101  			yaml: "1x\n",
   102  		},
   103  		{
   104  			want: Size2x,
   105  			yaml: "2x\n",
   106  		},
   107  		{
   108  			want: Size4x,
   109  			yaml: "4x\n",
   110  		},
   111  		{
   112  			want: Size8x,
   113  			yaml: "8x\n",
   114  		},
   115  		{
   116  			want: SizeNone,
   117  			yaml: "99x\n", // ignore unknown values
   118  		},
   119  		{
   120  			want: SizeNone,
   121  			yaml: "\n",
   122  		},
   123  	}
   124  	for _, test := range tests {
   125  		var in Size
   126  		err := yaml.Unmarshal([]byte(test.yaml), &in)
   127  		if err != nil {
   128  			t.Error(err)
   129  			return
   130  		}
   131  		if got, want := in, test.want; got != want {
   132  			t.Errorf("Want Size %v, got %v", want, got)
   133  		}
   134  	}
   135  }