github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/harness/yaml/unit_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 "gopkg.in/yaml.v3" 21 ) 22 23 func TestBytesSize(t *testing.T) { 24 tests := []struct { 25 yaml string 26 size int64 27 text string 28 }{ 29 { 30 yaml: "1KiB", 31 size: 1024, 32 text: "1KiB", 33 }, 34 { 35 yaml: "100Mi", 36 size: 104857600, 37 text: "100MiB", 38 }, 39 { 40 yaml: "1024", 41 size: 1024, 42 text: "1KiB", 43 }, 44 } 45 for _, test := range tests { 46 in := []byte(test.yaml) 47 out := BytesSize(0) 48 err := yaml.Unmarshal(in, &out) 49 if err != nil { 50 t.Error(err) 51 return 52 } 53 if got, want := int64(out), test.size; got != want { 54 t.Errorf("Want byte size %d, got %d", want, got) 55 } 56 if got, want := out.String(), test.text; got != want { 57 t.Errorf("Want byte text %s, got %s", want, got) 58 } 59 } 60 } 61 62 func TestMilliSize(t *testing.T) { 63 tests := []struct { 64 yaml string 65 size int64 66 text string 67 }{ 68 { 69 yaml: "100m", 70 size: 100, 71 text: "100", 72 }, 73 { 74 yaml: "1", 75 size: 1000, 76 text: "1000", 77 }, 78 { 79 yaml: "0m", 80 size: 0, 81 text: "0", 82 }, 83 } 84 for _, test := range tests { 85 in := []byte(test.yaml) 86 out := MilliSize(0) 87 err := yaml.Unmarshal(in, &out) 88 if err != nil { 89 t.Error(err) 90 return 91 } 92 if got, want := int64(out), test.size; got != want { 93 t.Errorf("Want millis %d, got %d", want, got) 94 } 95 if got, want := out.String(), test.text; got != want { 96 t.Errorf("Want text %s, got %s", want, got) 97 } 98 } 99 }