github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/schema/util/int_string_test.go (about)

     1  /*
     2  Copyright 2019 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"gopkg.in/yaml.v3"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/testutil"
    26  )
    27  
    28  func TestFromInt(t *testing.T) {
    29  	i := FromInt(42)
    30  	if i.Type != Int || i.IntVal != 42 {
    31  		t.Errorf("Expected IntVal=42, got %+v", i)
    32  	}
    33  }
    34  
    35  func TestFromString(t *testing.T) {
    36  	i := FromString("test")
    37  	if i.Type != String || i.StrVal != "test" {
    38  		t.Errorf("Expected StrVal=\"test\", got %+v", i)
    39  	}
    40  }
    41  
    42  func TestString(t *testing.T) {
    43  	cases := []struct {
    44  		input  IntOrString
    45  		result string
    46  	}{
    47  		{FromInt(8080), "8080"},
    48  		{FromString("http"), "http"},
    49  	}
    50  
    51  	for _, c := range cases {
    52  		testutil.CheckDeepEqual(t, c.result, c.input.String())
    53  	}
    54  }
    55  
    56  type IntOrStringHolder struct {
    57  	Val IntOrString `json:"val" yaml:"val"`
    58  }
    59  
    60  func TestUnmarshalJSON(t *testing.T) {
    61  	cases := []struct {
    62  		input  string
    63  		result IntOrString
    64  	}{
    65  		{"{\"val\": 8080}", FromInt(8080)},
    66  		{"{\"val\": \"http\"}", FromString("http")},
    67  	}
    68  
    69  	for _, c := range cases {
    70  		var result IntOrStringHolder
    71  		if err := json.Unmarshal([]byte(c.input), &result); err != nil {
    72  			t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
    73  		}
    74  		if result.Val != c.result {
    75  			t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
    76  		}
    77  	}
    78  }
    79  
    80  func TestMarshalJSON(t *testing.T) {
    81  	cases := []struct {
    82  		input  IntOrString
    83  		result string
    84  	}{
    85  		{FromInt(8080), "{\"val\":8080}"},
    86  		{FromString("http"), "{\"val\":\"http\"}"},
    87  	}
    88  
    89  	for _, c := range cases {
    90  		input := IntOrStringHolder{c.input}
    91  		result, err := json.Marshal(&input)
    92  		if err != nil {
    93  			t.Errorf("Failed to marshal input '%v': %v", input, err)
    94  		}
    95  		if string(result) != c.result {
    96  			t.Errorf("Failed to marshal input '%v': expected: %+v, got %q", input, c.result, string(result))
    97  		}
    98  	}
    99  }
   100  
   101  func TestUnmarshalYaml(t *testing.T) {
   102  	cases := []struct {
   103  		input  string
   104  		result IntOrString
   105  	}{
   106  		{"{\"val\": 8080}", FromInt(8080)},
   107  		{"{\"val\": \"http\"}", FromString("http")},
   108  	}
   109  
   110  	for _, c := range cases {
   111  		var result IntOrStringHolder
   112  		if err := yaml.Unmarshal([]byte(c.input), &result); err != nil {
   113  			t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
   114  		}
   115  		if result.Val != c.result {
   116  			t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
   117  		}
   118  	}
   119  }
   120  
   121  func TestMarshalYaml(t *testing.T) {
   122  	cases := []struct {
   123  		input  IntOrString
   124  		result string
   125  	}{
   126  		{FromInt(8080), "val: 8080\n"},
   127  		{FromString("http"), "val: http\n"},
   128  	}
   129  
   130  	for _, c := range cases {
   131  		input := IntOrStringHolder{c.input}
   132  		result, err := yaml.Marshal(&input)
   133  		if err != nil {
   134  			t.Errorf("Failed to marshal input '%v': %v", input, err)
   135  		}
   136  		if string(result) != c.result {
   137  			t.Errorf("Failed to marshal input '%v': expected: %+v, got %q", input, c.result, string(result))
   138  		}
   139  	}
   140  }