@@ -4,6 +4,7 @@ package json
4
4
5
5
import (
6
6
"bytes"
7
+ jsonenc "encoding/json"
7
8
"flag"
8
9
"fmt"
9
10
"os"
@@ -52,8 +53,8 @@ func Test_Read(t *testing.T) {
52
53
return
53
54
}
54
55
55
- if ! cmp .Equal (want , got , cmpopts .IgnoreUnexported (spdx.Package {})) {
56
- t .Errorf ("got incorrect struct after parsing YAML example: %s" , cmp . Diff ( want , got , cmpopts . IgnoreUnexported (spdx. Package {})) )
56
+ if diff := cmp .Diff (want , got , cmpopts .IgnoreUnexported (spdx.Package {}), cmpopts . SortSlices ( relationshipLess )); len ( diff ) > 0 {
57
+ t .Errorf ("got incorrect struct after parsing JSON example: %s" , diff )
57
58
return
58
59
}
59
60
}
@@ -81,8 +82,8 @@ func Test_Write(t *testing.T) {
81
82
return
82
83
}
83
84
84
- if ! cmp .Equal (want , got , cmpopts .IgnoreUnexported (spdx.Package {})) {
85
- t .Errorf ("got incorrect struct after writing and re- parsing JSON example: %s" , cmp . Diff ( want , got , cmpopts . IgnoreUnexported (spdx. Package {})) )
85
+ if diff := cmp .Diff (want , got , cmpopts .IgnoreUnexported (spdx.Package {}), cmpopts . SortSlices ( relationshipLess )); len ( diff ) > 0 {
86
+ t .Errorf ("got incorrect struct after parsing JSON example: %s" , diff )
86
87
return
87
88
}
88
89
}
@@ -139,7 +140,7 @@ func Test_ShorthandFields(t *testing.T) {
139
140
}
140
141
}
141
142
142
- require . Equal ( t , spdx.Document {
143
+ want := spdx.Document {
143
144
SPDXVersion : spdx .Version ,
144
145
DataLicense : spdx .DataLicense ,
145
146
SPDXIdentifier : "DOCUMENT" ,
@@ -190,7 +191,135 @@ func Test_ShorthandFields(t *testing.T) {
190
191
Relationship : common .TypeRelationshipContains ,
191
192
},
192
193
},
193
- }, doc )
194
+ }
195
+
196
+ if diff := cmp .Diff (want , doc , cmpopts .IgnoreUnexported (spdx.Package {}), cmpopts .SortSlices (relationshipLess )); len (diff ) > 0 {
197
+ t .Errorf ("got incorrect struct after parsing JSON example: %s" , cmp .Diff (want , doc , cmpopts .IgnoreUnexported (spdx.Package {})))
198
+ return
199
+ }
200
+ }
201
+
202
+ func Test_ShorthandFieldsNoDuplicates (t * testing.T ) {
203
+ contents := `{
204
+ "spdxVersion": "SPDX-2.3",
205
+ "dataLicense": "CC0-1.0",
206
+ "SPDXID": "SPDXRef-DOCUMENT",
207
+ "name": "SPDX-Tools-v2.0",
208
+ "documentDescribes": [
209
+ "SPDXRef-Container"
210
+ ],
211
+ "packages": [
212
+ {
213
+ "name": "Container",
214
+ "SPDXID": "SPDXRef-Container"
215
+ },
216
+ {
217
+ "name": "Package-1",
218
+ "SPDXID": "SPDXRef-Package-1",
219
+ "versionInfo": "1.1.1",
220
+ "hasFiles": [
221
+ "SPDXRef-File-1",
222
+ "SPDXRef-File-2"
223
+ ]
224
+ },
225
+ {
226
+ "name": "Package-2",
227
+ "SPDXID": "SPDXRef-Package-2",
228
+ "versionInfo": "2.2.2"
229
+ }
230
+ ],
231
+ "files": [
232
+ {
233
+ "fileName": "./f1",
234
+ "SPDXID": "SPDXRef-File-1"
235
+ },
236
+ {
237
+ "fileName": "./f2",
238
+ "SPDXID": "SPDXRef-File-2"
239
+ }
240
+ ],
241
+ "relationships": [
242
+ {
243
+ "spdxElementId": "SPDXRef-Package-1",
244
+ "relationshipType": "CONTAINS",
245
+ "relatedSpdxElement": "SPDXRef-File-1"
246
+ },
247
+ {
248
+ "spdxElementId": "SPDXRef-Package-1",
249
+ "relationshipType": "CONTAINS",
250
+ "relatedSpdxElement": "SPDXRef-File-2"
251
+ }
252
+ ]
253
+ }`
254
+
255
+ doc := spdx.Document {}
256
+ err := json .ReadInto (strings .NewReader (contents ), & doc )
257
+
258
+ require .NoError (t , err )
259
+
260
+ id := func (s string ) common.DocElementID {
261
+ return common.DocElementID {
262
+ ElementRefID : common .ElementID (s ),
263
+ }
264
+ }
265
+
266
+ want := spdx.Document {
267
+ SPDXVersion : spdx .Version ,
268
+ DataLicense : spdx .DataLicense ,
269
+ SPDXIdentifier : "DOCUMENT" ,
270
+ DocumentName : "SPDX-Tools-v2.0" ,
271
+ Packages : []* spdx.Package {
272
+ {
273
+ PackageName : "Container" ,
274
+ PackageSPDXIdentifier : "Container" ,
275
+ FilesAnalyzed : true ,
276
+ },
277
+ {
278
+ PackageName : "Package-1" ,
279
+ PackageSPDXIdentifier : "Package-1" ,
280
+ PackageVersion : "1.1.1" ,
281
+ FilesAnalyzed : true ,
282
+ },
283
+ {
284
+ PackageName : "Package-2" ,
285
+ PackageSPDXIdentifier : "Package-2" ,
286
+ PackageVersion : "2.2.2" ,
287
+ FilesAnalyzed : true ,
288
+ },
289
+ },
290
+ Files : []* spdx.File {
291
+ {
292
+ FileName : "./f1" ,
293
+ FileSPDXIdentifier : "File-1" ,
294
+ },
295
+ {
296
+ FileName : "./f2" ,
297
+ FileSPDXIdentifier : "File-2" ,
298
+ },
299
+ },
300
+ Relationships : []* spdx.Relationship {
301
+ {
302
+ RefA : id ("DOCUMENT" ),
303
+ RefB : id ("Container" ),
304
+ Relationship : common .TypeRelationshipDescribe ,
305
+ },
306
+ {
307
+ RefA : id ("Package-1" ),
308
+ RefB : id ("File-1" ),
309
+ Relationship : common .TypeRelationshipContains ,
310
+ },
311
+ {
312
+ RefA : id ("Package-1" ),
313
+ RefB : id ("File-2" ),
314
+ Relationship : common .TypeRelationshipContains ,
315
+ },
316
+ },
317
+ }
318
+
319
+ if diff := cmp .Diff (want , doc , cmpopts .IgnoreUnexported (spdx.Package {}), cmpopts .SortSlices (relationshipLess )); len (diff ) > 0 {
320
+ t .Errorf ("got incorrect struct after parsing JSON example: %s" , diff )
321
+ return
322
+ }
194
323
}
195
324
196
325
func Test_JsonEnums (t * testing.T ) {
@@ -334,3 +463,9 @@ func Test_JsonEnums(t *testing.T) {
334
463
},
335
464
}, doc )
336
465
}
466
+
467
+ func relationshipLess (a , b * spdx.Relationship ) bool {
468
+ aStr , _ := jsonenc .Marshal (a )
469
+ bStr , _ := jsonenc .Marshal (b )
470
+ return string (aStr ) < string (bStr )
471
+ }
0 commit comments