github.com/sacloud/iaas-api-go@v1.12.0/types/result_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 TestResult_UnmarshalJSON(t *testing.T) { 25 expects := []struct { 26 input string 27 expect APIResult 28 hasError bool 29 }{ 30 { 31 input: "true", 32 expect: ResultSuccess, 33 hasError: false, 34 }, 35 { 36 input: "false", 37 expect: ResultFailed, 38 hasError: false, 39 }, 40 { 41 input: `"Accepted"`, 42 expect: ResultAccepted, 43 hasError: false, 44 }, 45 { 46 input: "2", 47 expect: ResultUnknown, 48 hasError: true, 49 }, 50 { 51 input: `"Deleted"`, 52 expect: ResultUnknown, 53 hasError: false, 54 }, 55 } 56 57 for _, tc := range expects { 58 var res APIResult 59 err := json.Unmarshal([]byte(tc.input), &res) 60 61 if tc.hasError { 62 require.Error(t, err) 63 } else { 64 require.NoError(t, err) 65 } 66 require.Equal(t, tc.expect, res) 67 } 68 }