| 1 | package xsdscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.14 bytes/" |
| 5 | "testingscip-go gomod github.com/golang/go/src go1.14 testing/" |
| 6 | "timescip-go gomod github.com/golang/go/src go1.14 time/" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | P2Y6M5DT12H35M30Sscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P2Y6M5DT12H35M30S. = 2*Yearishscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/Yearish. + 6*Monthishscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/Monthish. + 5*Dayscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/Day. + 12*timescip-go gomod github.com/golang/go/src go1.14 time/.Hourscip-go gomod github.com/golang/go/src go1.14 time/Hour. + 35*timescip-go gomod github.com/golang/go/src go1.14 time/.Minutescip-go gomod github.com/golang/go/src go1.14 time/Minute. + 30*timescip-go gomod github.com/golang/go/src go1.14 time/.Secondscip-go gomod github.com/golang/go/src go1.14 time/Second. |
| 11 | P1DT2Hscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P1DT2H. = Dayscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/Day. + 2*timescip-go gomod github.com/golang/go/src go1.14 time/.Hourscip-go gomod github.com/golang/go/src go1.14 time/Hour. |
| 12 | P20Mscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P20M. = 20 * Monthishscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/Monthish. |
| 13 | PT20Mscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/PT20M. = 20 * timescip-go gomod github.com/golang/go/src go1.14 time/.Minutescip-go gomod github.com/golang/go/src go1.14 time/Minute. |
| 14 | P0Yscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P0Y. = timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#(0) |
| 15 | NegP60Dscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/NegP60D. = -1 * (60 * Dayscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/Day.) |
| 16 | PT1M30_5Sscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/PT1M30_5S. = timescip-go gomod github.com/golang/go/src go1.14 time/.Minutescip-go gomod github.com/golang/go/src go1.14 time/Minute. + timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#(30.5*float64(timescip-go gomod github.com/golang/go/src go1.14 time/.Secondscip-go gomod github.com/golang/go/src go1.14 time/Second.)) |
| 17 | ) |
| 18 | |
| 19 | func TestMarshalscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/TestMarshal().(t *testingscip-go gomod github.com/golang/go/src go1.14 testing/.Tscip-go gomod github.com/golang/go/src go1.14 testing/T#) { |
| 20 | type args struct { |
| 21 | d timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration# |
| 22 | } |
| 23 | tests := []struct { |
| 24 | name string |
| 25 | args args |
| 26 | want []byte |
| 27 | wantErr bool |
| 28 | }{ |
| 29 | { |
| 30 | name: "2 years, 6 months, 5 days, 12 hours, 35 minutes, 30 seconds", |
| 31 | args: args{P2Y6M5DT12H35M30Sscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P2Y6M5DT12H35M30S.}, |
| 32 | wantErr: false, |
| 33 | want: []byte("P2Y6M5DT12H35M30S"), |
| 34 | }, |
| 35 | { |
| 36 | name: "1 day, 2 hours", |
| 37 | args: args{P1DT2Hscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P1DT2H.}, |
| 38 | wantErr: false, |
| 39 | want: []byte("P1DT2H"), |
| 40 | }, |
| 41 | { |
| 42 | name: "20 months (the number of months can be more than 12)", |
| 43 | args: args{P20Mscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P20M.}, |
| 44 | want: []byte("P1Y8M4D"), |
| 45 | wantErr: false, |
| 46 | }, |
| 47 | { |
| 48 | name: "20 minutes", |
| 49 | args: args{PT20Mscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/PT20M.}, |
| 50 | want: []byte("PT20M"), |
| 51 | wantErr: false, |
| 52 | }, |
| 53 | { |
| 54 | name: "0 years", |
| 55 | args: args{P0Yscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P0Y.}, |
| 56 | want: []byte("PT0S"), |
| 57 | wantErr: false, |
| 58 | }, |
| 59 | { |
| 60 | name: "minus 60 days", |
| 61 | args: args{NegP60Dscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/NegP60D.}, |
| 62 | want: []byte("-P2M"), |
| 63 | wantErr: false, |
| 64 | }, |
| 65 | { |
| 66 | name: "1 minute, 30.5 seconds", |
| 67 | args: args{PT1M30_5Sscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/PT1M30_5S.}, |
| 68 | want: []byte("PT1M30.5S"), |
| 69 | wantErr: false, |
| 70 | }, |
| 71 | } |
| 72 | for _, tt := range tests { |
| 73 | t.Runscip-go gomod github.com/golang/go/src go1.14 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.14 testing/.Tscip-go gomod github.com/golang/go/src go1.14 testing/T#) { |
| 74 | got, err := Marshalscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/Marshal().(tt.args.d) |
| 75 | if (err != nil) != tt.wantErr { |
| 76 | t.Errorfscip-go gomod github.com/golang/go/src go1.14 testing/common#Errorf().("Marshal() error = %v, wantErr %v", err, tt.wantErr) |
| 77 | return |
| 78 | } |
| 79 | if !bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Equalscip-go gomod github.com/golang/go/src go1.14 bytes/Equal().(got, tt.want) { |
| 80 | t.Errorfscip-go gomod github.com/golang/go/src go1.14 testing/common#Errorf().("Marshal() got = %s, want %s", got, tt.want) |
| 81 | } |
| 82 | }) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestUnmarshalscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/TestUnmarshal().(t *testingscip-go gomod github.com/golang/go/src go1.14 testing/.Tscip-go gomod github.com/golang/go/src go1.14 testing/T#) { |
| 87 | type args struct { |
| 88 | data []byte |
| 89 | d *timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration# |
| 90 | } |
| 91 | tests := []struct { |
| 92 | name string |
| 93 | args args |
| 94 | want timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration# |
| 95 | wantErr bool |
| 96 | }{ |
| 97 | { |
| 98 | name: "an empty value should not be valid", |
| 99 | args: args{[]byte{}, nil}, |
| 100 | wantErr: true, |
| 101 | }, |
| 102 | { |
| 103 | name: "at least one number and designator are required", |
| 104 | args: args{[]byte("P"), nil}, |
| 105 | wantErr: true, |
| 106 | }, |
| 107 | { |
| 108 | name: "missing type", |
| 109 | args: args{[]byte("PT"), nil}, |
| 110 | wantErr: true, |
| 111 | }, |
| 112 | { |
| 113 | name: "minus sign must appear first", |
| 114 | args: args{[]byte("P-20M"), nil}, |
| 115 | wantErr: true, |
| 116 | }, |
| 117 | { |
| 118 | name: "no time items are present, so \"T\" must not be present", |
| 119 | args: args{[]byte("P20MT"), nil}, |
| 120 | wantErr: true, |
| 121 | }, |
| 122 | { |
| 123 | name: "no value is specified for months, so \"M\" must not be present", |
| 124 | args: args{[]byte("P1YM5D"), nil}, |
| 125 | wantErr: true, |
| 126 | }, |
| 127 | { |
| 128 | name: "only the seconds can be expressed as a decimal", |
| 129 | args: args{[]byte("P15.5Y"), nil}, |
| 130 | wantErr: true, |
| 131 | }, |
| 132 | { |
| 133 | name: "\"T\" must be present to separate days and hours", |
| 134 | args: args{[]byte("P1D2H"), nil}, |
| 135 | wantErr: true, |
| 136 | }, |
| 137 | { |
| 138 | name: "\"P\" must always be present", |
| 139 | args: args{[]byte("1Y2M"), nil}, |
| 140 | wantErr: true, |
| 141 | }, |
| 142 | { |
| 143 | name: "years must appear before months", |
| 144 | args: args{[]byte("P2M1Y"), nil}, |
| 145 | wantErr: true, |
| 146 | }, |
| 147 | { |
| 148 | name: "at least one digit must follow the decimal point if it appears", |
| 149 | args: args{[]byte("PT15.S"), nil}, |
| 150 | wantErr: true, |
| 151 | }, |
| 152 | { |
| 153 | name: "invalid data at the end", |
| 154 | args: args{[]byte("P2Y6M5DT12H35M30Stest"), nil}, |
| 155 | wantErr: true, |
| 156 | }, |
| 157 | { |
| 158 | name: "invalid data at the start", |
| 159 | args: args{[]byte("testP2Y6M5DT12H35M30S"), nil}, |
| 160 | wantErr: true, |
| 161 | }, |
| 162 | { |
| 163 | name: "2 years, 6 months, 5 days, 12 hours, 35 minutes, 30 seconds", |
| 164 | args: args{[]byte("P2Y6M5DT12H35M30S"), new(timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#)}, |
| 165 | want: P2Y6M5DT12H35M30Sscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P2Y6M5DT12H35M30S., |
| 166 | wantErr: false, |
| 167 | }, |
| 168 | { |
| 169 | name: "1 day, 2 hours", |
| 170 | args: args{[]byte("P1DT2H"), new(timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#)}, |
| 171 | want: P1DT2Hscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P1DT2H., |
| 172 | wantErr: false, |
| 173 | }, |
| 174 | { |
| 175 | name: "20 months (the number of months can be more than 12)", |
| 176 | args: args{[]byte("P20M"), new(timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#)}, |
| 177 | want: P20Mscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P20M., |
| 178 | wantErr: false, |
| 179 | }, |
| 180 | { |
| 181 | name: "20 minutes", |
| 182 | args: args{[]byte("PT20M"), new(timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#)}, |
| 183 | want: PT20Mscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/PT20M., |
| 184 | wantErr: false, |
| 185 | }, |
| 186 | { |
| 187 | name: "20 months (0 is permitted as a number, but is not required)", |
| 188 | args: args{[]byte("P0Y20M0D"), new(timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#)}, |
| 189 | want: P20Mscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P20M., |
| 190 | wantErr: false, |
| 191 | }, |
| 192 | { |
| 193 | name: "0 years", |
| 194 | args: args{[]byte("P0Y"), new(timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#)}, |
| 195 | want: P0Yscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/P0Y., |
| 196 | wantErr: false, |
| 197 | }, |
| 198 | { |
| 199 | name: "minus 60 days", |
| 200 | args: args{[]byte("-P60D"), new(timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#)}, |
| 201 | want: NegP60Dscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/NegP60D., |
| 202 | wantErr: false, |
| 203 | }, |
| 204 | { |
| 205 | name: "1 minute, 30.5 seconds", |
| 206 | args: args{[]byte("PT1M30.5S"), new(timescip-go gomod github.com/golang/go/src go1.14 time/.Durationscip-go gomod github.com/golang/go/src go1.14 time/Duration#)}, |
| 207 | want: PT1M30_5Sscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/PT1M30_5S., |
| 208 | wantErr: false, |
| 209 | }, |
| 210 | } |
| 211 | for _, tt := range tests { |
| 212 | t.Runscip-go gomod github.com/golang/go/src go1.14 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.14 testing/.Tscip-go gomod github.com/golang/go/src go1.14 testing/T#) { |
| 213 | if err := Unmarshalscip-go gomod git.sr.ht/~mariusor/go-xsd-duration . `git.sr.ht/~mariusor/go-xsd-duration`/Unmarshal().(tt.args.data, tt.args.d); (err != nil) != tt.wantErr { |
| 214 | t.Errorfscip-go gomod github.com/golang/go/src go1.14 testing/common#Errorf().("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) |
| 215 | } |
| 216 | if tt.want == 0 { |
| 217 | return |
| 218 | } |
| 219 | if *tt.args.d != tt.want { |
| 220 | t.Errorfscip-go gomod github.com/golang/go/src go1.14 testing/common#Errorf().("Marshal() got = %s, want %s", tt.args.d, tt.want) |
| 221 | } |
| 222 | }) |
| 223 | } |
| 224 | } |