github.com/sacloud/iaas-api-go@v1.12.0/types/web_ui_test.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     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 types
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestWebUI_MarshalJSON(t *testing.T) {
    25  	cases := []struct {
    26  		in  WebUI
    27  		out []byte
    28  	}{
    29  		{
    30  			in:  WebUI(""),
    31  			out: []byte(`false`),
    32  		},
    33  		{
    34  			in:  WebUI("true"),
    35  			out: []byte(`true`),
    36  		},
    37  		{
    38  			in:  WebUI("false"),
    39  			out: []byte(`false`),
    40  		},
    41  		{
    42  			in:  WebUI("http://localhost:8080"),
    43  			out: []byte(`"http://localhost:8080"`),
    44  		},
    45  	}
    46  
    47  	for _, cc := range cases {
    48  		data, err := json.Marshal(cc.in)
    49  		if err != nil {
    50  			t.Fatal(err, cc)
    51  		}
    52  		require.Equal(t, cc.out, data, "target: %#v", cc)
    53  	}
    54  }
    55  
    56  func TestWebUI_UnmarshalJSON(t *testing.T) {
    57  	cases := []struct {
    58  		in      string
    59  		out     WebUI
    60  		enabled bool
    61  	}{
    62  		{
    63  			in:      `""`,
    64  			out:     WebUI(""),
    65  			enabled: false,
    66  		},
    67  		{
    68  			in:      `true`,
    69  			out:     WebUI("true"),
    70  			enabled: true,
    71  		},
    72  		{
    73  			in:      `false`,
    74  			out:     WebUI("false"),
    75  			enabled: false,
    76  		},
    77  		{
    78  			in:      `"http://localhost:8080"`,
    79  			out:     WebUI("http://localhost:8080"),
    80  			enabled: true,
    81  		},
    82  	}
    83  
    84  	for _, cc := range cases {
    85  		var v WebUI
    86  		if err := json.Unmarshal([]byte(cc.in), &v); err != nil {
    87  			t.Fatal(err, cc)
    88  		}
    89  		require.Equal(t, cc.enabled, v.Bool(), "target: %#v", cc)
    90  	}
    91  }