github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/schedule_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 TestSchedule(t *testing.T) { 25 tests := []struct { 26 yaml string 27 want Schedule 28 }{ 29 { 30 yaml: `{ cron: '30 5,17 * * *' }`, 31 want: Schedule{ 32 Items: []*ScheduleItem{{Cron: "30 5,17 * * *"}}, 33 }, 34 }, 35 { 36 yaml: `[ { cron: '30 5 * * 1,3' }, { cron: '30 5 * * 2,4' } ]`, 37 want: Schedule{ 38 Items: []*ScheduleItem{ 39 {Cron: "30 5 * * 1,3"}, 40 {Cron: "30 5 * * 2,4"}, 41 }, 42 }, 43 }, 44 } 45 46 for i, test := range tests { 47 got := new(Schedule) 48 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 49 t.Log(test.yaml) 50 t.Error(err) 51 return 52 } 53 if diff := cmp.Diff(got, &test.want); diff != "" { 54 t.Log(test.yaml) 55 t.Errorf("Unexpected parsing results for test %v", i) 56 t.Log(diff) 57 } 58 } 59 } 60 61 func TestSchedule_Marshal(t *testing.T) { 62 tests := []struct { 63 before Schedule 64 after string 65 }{ 66 { 67 before: Schedule{Items: []*ScheduleItem{ 68 {Cron: "30 5,17 * * *"}, 69 }}, 70 after: "- cron: 30 5,17 * * *\n", 71 }, 72 { 73 before: Schedule{Items: []*ScheduleItem{ 74 {Cron: "30 5 * * 1,3"}, 75 {Cron: "30 5 * * 2,4"}, 76 }}, 77 after: "- cron: 30 5 * * 1,3\n- cron: 30 5 * * 2,4\n", 78 }, 79 } 80 81 for _, test := range tests { 82 after, err := yaml.Marshal(&test.before) 83 if err != nil { 84 t.Error(err) 85 return 86 } 87 if got, want := string(after), test.after; got != want { 88 t.Errorf("want yaml %q, got %q", want, got) 89 } 90 } 91 } 92 93 func TestSchedule_Error(t *testing.T) { 94 err := yaml.Unmarshal([]byte("[[]]"), new(Schedule)) 95 if err == nil || err.Error() != "failed to unmarshal on.schedule" { 96 t.Errorf("Expect error, got %s", err) 97 } 98 }