github.com/drone/runner-go@v1.12.0/pipeline/runtime/const_test.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 package runtime 6 7 import ( 8 "bytes" 9 "encoding/json" 10 "testing" 11 ) 12 13 // 14 // runtime policy unit tests. 15 // 16 17 func TestRunPolicy_Marshal(t *testing.T) { 18 tests := []struct { 19 policy RunPolicy 20 data string 21 }{ 22 { 23 policy: RunAlways, 24 data: `"always"`, 25 }, 26 { 27 policy: RunOnFailure, 28 data: `"on-failure"`, 29 }, 30 { 31 policy: RunOnSuccess, 32 data: `"on-success"`, 33 }, 34 { 35 policy: RunNever, 36 data: `"never"`, 37 }, 38 } 39 for _, test := range tests { 40 data, err := json.Marshal(&test.policy) 41 if err != nil { 42 t.Error(err) 43 return 44 } 45 if bytes.Equal([]byte(test.data), data) == false { 46 t.Errorf("Failed to marshal policy %s", test.policy) 47 } 48 } 49 } 50 51 func TestRunPolicy_Unmarshal(t *testing.T) { 52 tests := []struct { 53 policy RunPolicy 54 data string 55 }{ 56 { 57 policy: RunAlways, 58 data: `"always"`, 59 }, 60 { 61 policy: RunOnFailure, 62 data: `"on-failure"`, 63 }, 64 { 65 policy: RunOnSuccess, 66 data: `"on-success"`, 67 }, 68 { 69 policy: RunNever, 70 data: `"never"`, 71 }, 72 { 73 // no policy should default to on-success 74 policy: RunOnSuccess, 75 data: `""`, 76 }, 77 } 78 for _, test := range tests { 79 var policy RunPolicy 80 err := json.Unmarshal([]byte(test.data), &policy) 81 if err != nil { 82 t.Error(err) 83 return 84 } 85 if got, want := policy, test.policy; got != want { 86 t.Errorf("Want policy %q, got %q", want, got) 87 } 88 } 89 } 90 91 func TestRunPolicy_UnmarshalTypeError(t *testing.T) { 92 var policy RunPolicy 93 err := json.Unmarshal([]byte("[]"), &policy) 94 if _, ok := err.(*json.UnmarshalTypeError); !ok { 95 t.Errorf("Expect unmarshal error return when JSON invalid") 96 } 97 } 98 99 func TestRunPolicy_String(t *testing.T) { 100 tests := []struct { 101 policy RunPolicy 102 value string 103 }{ 104 { 105 policy: RunAlways, 106 value: "always", 107 }, 108 { 109 policy: RunOnFailure, 110 value: "on-failure", 111 }, 112 { 113 policy: RunOnSuccess, 114 value: "on-success", 115 }, 116 } 117 for _, test := range tests { 118 if got, want := test.policy.String(), test.value; got != want { 119 t.Errorf("Want policy string %q, got %q", want, got) 120 } 121 } 122 }