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