github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/yaml/depth_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 TestDepth(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Depth
    28  	}{
    29  		{
    30  			yaml: `"full"`,
    31  			want: Depth{Full: true},
    32  		},
    33  		{
    34  			yaml: `25`,
    35  			want: Depth{
    36  				Value: 25,
    37  			},
    38  		},
    39  	}
    40  
    41  	for i, test := range tests {
    42  		got := new(Depth)
    43  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    44  			t.Log(test.yaml)
    45  			t.Error(err)
    46  			return
    47  		}
    48  		if diff := cmp.Diff(got, &test.want); diff != "" {
    49  			t.Log(test.yaml)
    50  			t.Errorf("Unexpected parsing results for test %v", i)
    51  			t.Log(diff)
    52  		}
    53  	}
    54  }
    55  
    56  func TestDepth_Error(t *testing.T) {
    57  	err := yaml.Unmarshal([]byte("all"), new(Depth)) // only "full" is a valid string
    58  	if err == nil || err.Error() != "failed to unmarshal depth" {
    59  		t.Errorf("Expect error, got %s", err)
    60  	}
    61  }
    62  
    63  func TestDepth_Marshal(t *testing.T) {
    64  	tests := []struct {
    65  		before Depth
    66  		after  string
    67  	}{
    68  		{
    69  			before: Depth{Full: true},
    70  			after:  "full\n",
    71  		},
    72  		{
    73  			before: Depth{Value: 50},
    74  			after:  "50\n",
    75  		},
    76  		{
    77  			before: Depth{Value: 0},
    78  			after:  "null\n",
    79  		},
    80  	}
    81  
    82  	for _, test := range tests {
    83  		after, err := yaml.Marshal(&test.before)
    84  		if err != nil {
    85  			t.Error(err)
    86  			return
    87  		}
    88  		if got, want := string(after), test.after; got != want {
    89  			t.Errorf("want yaml %q, got %q", want, got)
    90  		}
    91  	}
    92  }