Debian Codegraph @

spdxexp/parse_test.go

raw symbols SCIP
1 package spdxexp
2
3 import (
4 "errors"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9 )
10
11 func TestParse(t *testing.T) {
12 tests := []struct {
13 name string
14 expression string
15 node *node
16 nodestr string
17 err error
18 }{
19 {"single license",
20 "MIT",
21 &node{
22 role: licenseNode,
23 exp: nil,
24 lic: &licenseNodePartial{
25 license: "MIT", hasPlus: false,
26 hasException: false, exception: ""},
27 ref: nil,
28 },
29 "MIT", nil},
30
31 {"single license - diff case",
32 "mit",
33 &node{
34 role: licenseNode,
35 exp: nil,
36 lic: &licenseNodePartial{
37 license: "MIT", hasPlus: false,
38 hasException: false, exception: ""},
39 ref: nil,
40 },
41 "MIT", nil},
42
43 {"empty expression", "", nil, "", errors.New("parse error - cannot parse empty string")},
44
45 {"invalid license", "NON-EXISTENT-LICENSE", nil, "",
46 errors.New("unknown license 'NON-EXISTENT-LICENSE' at offset 0")},
47
48 {"OR Expression", "MIT OR Apache-2.0",
49 &node{
50 role: expressionNode,
51 exp: &expressionNodePartial{
52 left: &node{
53 role: licenseNode,
54 exp: nil,
55 lic: &licenseNodePartial{
56 license: "MIT", hasPlus: false,
57 hasException: false, exception: ""},
58 ref: nil,
59 },
60 conjunction: "or",
61 right: &node{
62 role: licenseNode,
63 exp: nil,
64 lic: &licenseNodePartial{
65 license: "Apache-2.0", hasPlus: false,
66 hasException: false, exception: ""},
67 ref: nil,
68 },
69 },
70 lic: nil,
71 ref: nil,
72 },
73 "{ LEFT: MIT or RIGHT: Apache-2.0 }", nil},
74 {"AND Expression", "MIT AND Apache-2.0",
75 &node{
76 role: expressionNode,
77 exp: &expressionNodePartial{
78 left: &node{
79 role: licenseNode,
80 exp: nil,
81 lic: &licenseNodePartial{
82 license: "MIT", hasPlus: false,
83 hasException: false, exception: ""},
84 ref: nil,
85 },
86 conjunction: "and",
87 right: &node{
88 role: licenseNode,
89 exp: nil,
90 lic: &licenseNodePartial{
91 license: "Apache-2.0", hasPlus: false,
92 hasException: false, exception: ""},
93 ref: nil,
94 },
95 },
96 lic: nil,
97 ref: nil,
98 },
99 "{ LEFT: MIT and RIGHT: Apache-2.0 }", nil},
100 {"OR-AND Expression", "MIT OR Apache-2.0 AND GPL-2.0",
101 &node{
102 role: expressionNode,
103 exp: &expressionNodePartial{
104 left: &node{
105 role: licenseNode,
106 exp: nil,
107 lic: &licenseNodePartial{
108 license: "MIT", hasPlus: false,
109 hasException: false, exception: ""},
110 ref: nil,
111 },
112 conjunction: "or",
113 right: &node{
114 exp: &expressionNodePartial{
115 left: &node{
116 role: licenseNode,
117 exp: nil,
118 lic: &licenseNodePartial{
119 license: "Apache-2.0",
120 hasPlus: false,
121 hasException: false,
122 exception: "",
123 },
124 ref: nil,
125 },
126 conjunction: "and",
127 right: &node{
128 role: licenseNode,
129 exp: nil,
130 lic: &licenseNodePartial{
131 license: "GPL-2.0",
132 hasPlus: false,
133 hasException: false,
134 exception: "",
135 },
136 ref: nil,
137 },
138 },
139 lic: nil,
140 ref: nil,
141 },
142 },
143 lic: nil,
144 ref: nil,
145 },
146 "{ LEFT: MIT or RIGHT: { LEFT: Apache-2.0 and RIGHT: GPL-2.0 } }", nil},
147 {"OR(AND) Expression", "MIT OR (Apache-2.0 AND GPL-2.0)",
148 &node{
149 role: expressionNode,
150 exp: &expressionNodePartial{
151 left: &node{
152 role: licenseNode,
153 exp: nil,
154 lic: &licenseNodePartial{
155 license: "MIT", hasPlus: false,
156 hasException: false, exception: ""},
157 ref: nil,
158 },
159 conjunction: "or",
160 right: &node{
161 exp: &expressionNodePartial{
162 left: &node{
163 role: licenseNode,
164 exp: nil,
165 lic: &licenseNodePartial{
166 license: "Apache-2.0",
167 hasPlus: false,
168 hasException: false,
169 exception: "",
170 },
171 ref: nil,
172 },
173 conjunction: "and",
174 right: &node{
175 role: licenseNode,
176 exp: nil,
177 lic: &licenseNodePartial{
178 license: "GPL-2.0",
179 hasPlus: false,
180 hasException: false,
181 exception: "",
182 },
183 ref: nil,
184 },
185 },
186 lic: nil,
187 ref: nil,
188 },
189 },
190 lic: nil,
191 ref: nil,
192 },
193 "{ LEFT: MIT or RIGHT: { LEFT: Apache-2.0 and RIGHT: GPL-2.0 } }", nil},
194 {"AND-OR Expression", "MIT AND Apache-2.0 OR GPL-2.0",
195 &node{
196 role: expressionNode,
197 exp: &expressionNodePartial{
198 left: &node{
199 exp: &expressionNodePartial{
200 left: &node{
201 role: licenseNode,
202 exp: nil,
203 lic: &licenseNodePartial{
204 license: "MIT",
205 hasPlus: false,
206 hasException: false,
207 exception: "",
208 },
209 ref: nil,
210 },
211 conjunction: "and",
212 right: &node{
213 role: licenseNode,
214 exp: nil,
215 lic: &licenseNodePartial{
216 license: "Apache-2.0",
217 hasPlus: false,
218 hasException: false,
219 exception: "",
220 },
221 ref: nil,
222 },
223 },
224 lic: nil,
225 ref: nil,
226 },
227 conjunction: "or",
228 right: &node{
229 role: licenseNode,
230 exp: nil,
231 lic: &licenseNodePartial{
232 license: "GPL-2.0", hasPlus: false,
233 hasException: false, exception: ""},
234 ref: nil,
235 },
236 },
237 lic: nil,
238 ref: nil,
239 },
240 "{ LEFT: { LEFT: MIT and RIGHT: Apache-2.0 } or RIGHT: GPL-2.0 }", nil},
241 {"AND(OR) Expression", "MIT AND (Apache-2.0 OR GPL-2.0)",
242 &node{
243 role: expressionNode,
244 exp: &expressionNodePartial{
245 left: &node{
246 role: licenseNode,
247 exp: nil,
248 lic: &licenseNodePartial{
249 license: "MIT",
250 hasPlus: false,
251 hasException: false,
252 exception: "",
253 },
254 ref: nil,
255 },
256 conjunction: "and",
257 right: &node{
258 exp: &expressionNodePartial{
259 left: &node{
260 role: licenseNode,
261 exp: nil,
262 lic: &licenseNodePartial{
263 license: "Apache-2.0",
264 hasPlus: false,
265 hasException: false,
266 exception: "",
267 },
268 ref: nil,
269 },
270 conjunction: "or",
271 right: &node{
272 role: licenseNode,
273 exp: nil,
274 lic: &licenseNodePartial{
275 license: "GPL-2.0",
276 hasPlus: false,
277 hasException: false,
278 exception: "",
279 },
280 ref: nil,
281 },
282 },
283 lic: nil,
284 ref: nil,
285 },
286 },
287 lic: nil,
288 ref: nil,
289 },
290 "{ LEFT: MIT and RIGHT: { LEFT: Apache-2.0 or RIGHT: GPL-2.0 } }", nil},
291 {"OR-AND-OR Expression", "MIT OR ISC AND Apache-2.0 OR GPL-2.0",
292 &node{
293 role: expressionNode,
294 exp: &expressionNodePartial{
295 left: &node{
296 role: licenseNode,
297 exp: nil,
298 lic: &licenseNodePartial{
299 license: "MIT",
300 hasPlus: false,
301 hasException: false,
302 exception: "",
303 },
304 ref: nil,
305 },
306 conjunction: "or",
307 right: &node{
308 exp: &expressionNodePartial{
309 left: &node{
310 role: expressionNode,
311 exp: &expressionNodePartial{
312 left: &node{
313 role: licenseNode,
314 exp: nil,
315 lic: &licenseNodePartial{
316 license: "ISC",
317 hasPlus: false,
318 hasException: false,
319 exception: "",
320 },
321 ref: nil,
322 },
323 conjunction: "and",
324 right: &node{
325 role: licenseNode,
326 exp: nil,
327 lic: &licenseNodePartial{
328 license: "Apache-2.0",
329 hasPlus: false,
330 hasException: false,
331 exception: "",
332 },
333 ref: nil,
334 },
335 },
336 lic: nil,
337 ref: nil,
338 },
339 conjunction: "or",
340 right: &node{
341 role: licenseNode,
342 exp: nil,
343 lic: &licenseNodePartial{
344 license: "GPL-2.0",
345 hasPlus: false,
346 hasException: false,
347 exception: "",
348 },
349 ref: nil,
350 },
351 },
352 lic: nil,
353 ref: nil,
354 },
355 },
356 lic: nil,
357 ref: nil,
358 },
359 "{ LEFT: MIT or RIGHT: { LEFT: { LEFT: ISC and RIGHT: Apache-2.0 } or RIGHT: GPL-2.0 } }", nil},
360 {"(OR)AND(OR) Expression", "(MIT OR ISC) AND (Apache-2.0 OR GPL-2.0)",
361 &node{
362 role: expressionNode,
363 exp: &expressionNodePartial{
364 left: &node{
365 exp: &expressionNodePartial{
366 left: &node{
367 role: licenseNode,
368 exp: nil,
369 lic: &licenseNodePartial{
370 license: "MIT",
371 hasPlus: false,
372 hasException: false,
373 exception: "",
374 },
375 ref: nil,
376 },
377 conjunction: "or",
378 right: &node{
379 role: licenseNode,
380 exp: nil,
381 lic: &licenseNodePartial{
382 license: "ISC",
383 hasPlus: false,
384 hasException: false,
385 exception: "",
386 },
387 ref: nil,
388 },
389 },
390 lic: nil,
391 ref: nil,
392 },
393 conjunction: "and",
394 right: &node{
395 exp: &expressionNodePartial{
396 left: &node{
397 role: licenseNode,
398 exp: nil,
399 lic: &licenseNodePartial{
400 license: "Apache-2.0",
401 hasPlus: false,
402 hasException: false,
403 exception: "",
404 },
405 ref: nil,
406 },
407 conjunction: "or",
408 right: &node{
409 role: licenseNode,
410 exp: nil,
411 lic: &licenseNodePartial{
412 license: "GPL-2.0",
413 hasPlus: false,
414 hasException: false,
415 exception: "",
416 },
417 ref: nil,
418 },
419 },
420 lic: nil,
421 ref: nil,
422 },
423 },
424 lic: nil,
425 ref: nil,
426 },
427 "{ LEFT: { LEFT: MIT or RIGHT: ISC } and RIGHT: { LEFT: Apache-2.0 or RIGHT: GPL-2.0 } }", nil},
428 {"OR(AND)OR Expression", "MIT OR (ISC AND Apache-2.0) OR GPL-2.0",
429 &node{
430 role: expressionNode,
431 exp: &expressionNodePartial{
432 left: &node{
433 role: licenseNode,
434 exp: nil,
435 lic: &licenseNodePartial{
436 license: "MIT",
437 hasPlus: false,
438 hasException: false,
439 exception: "",
440 },
441 ref: nil,
442 },
443 conjunction: "or",
444 right: &node{
445 exp: &expressionNodePartial{
446 left: &node{
447 role: expressionNode,
448 exp: &expressionNodePartial{
449 left: &node{
450 role: licenseNode,
451 exp: nil,
452 lic: &licenseNodePartial{
453 license: "ISC",
454 hasPlus: false,
455 hasException: false,
456 exception: "",
457 },
458 ref: nil,
459 },
460 conjunction: "and",
461 right: &node{
462 role: licenseNode,
463 exp: nil,
464 lic: &licenseNodePartial{
465 license: "Apache-2.0",
466 hasPlus: false,
467 hasException: false,
468 exception: "",
469 },
470 ref: nil,
471 },
472 },
473 lic: nil,
474 ref: nil,
475 },
476 conjunction: "or",
477 right: &node{
478 role: licenseNode,
479 exp: nil,
480 lic: &licenseNodePartial{
481 license: "GPL-2.0",
482 hasPlus: false,
483 hasException: false,
484 exception: "",
485 },
486 ref: nil,
487 },
488 },
489 lic: nil,
490 ref: nil,
491 },
492 },
493 lic: nil,
494 ref: nil,
495 },
496 "{ LEFT: MIT or RIGHT: { LEFT: { LEFT: ISC and RIGHT: Apache-2.0 } or RIGHT: GPL-2.0 } }", nil},
497 {"OR-OR-OR Expression", "MIT OR ISC OR Apache-2.0 OR GPL-2.0",
498 &node{
499 role: expressionNode,
500 exp: &expressionNodePartial{
501 left: &node{
502 role: licenseNode,
503 exp: nil,
504 lic: &licenseNodePartial{
505 license: "MIT",
506 hasPlus: false,
507 hasException: false,
508 exception: "",
509 },
510 ref: nil,
511 },
512 conjunction: "or",
513 right: &node{
514 exp: &expressionNodePartial{
515 left: &node{
516 role: licenseNode,
517 exp: nil,
518 lic: &licenseNodePartial{
519 license: "ISC",
520 hasPlus: false,
521 hasException: false,
522 exception: "",
523 },
524 ref: nil,
525 },
526 conjunction: "or",
527 right: &node{
528 exp: &expressionNodePartial{
529 left: &node{
530 role: licenseNode,
531 exp: nil,
532 lic: &licenseNodePartial{
533 license: "Apache-2.0",
534 hasPlus: false,
535 hasException: false,
536 exception: "",
537 },
538 ref: nil,
539 },
540 conjunction: "or",
541 right: &node{
542 role: licenseNode,
543 exp: nil,
544 lic: &licenseNodePartial{
545 license: "GPL-2.0",
546 hasPlus: false,
547 hasException: false,
548 exception: "",
549 },
550 ref: nil,
551 },
552 },
553 lic: nil,
554 ref: nil,
555 },
556 },
557 lic: nil,
558 ref: nil,
559 },
560 },
561 lic: nil,
562 ref: nil,
563 },
564 "{ LEFT: MIT or RIGHT: { LEFT: ISC or RIGHT: { LEFT: Apache-2.0 or RIGHT: GPL-2.0 } } }", nil},
565 {"AND-OR-AND Expression", "MIT AND ISC OR Apache-2.0 AND GPL-2.0",
566 &node{
567 role: expressionNode,
568 exp: &expressionNodePartial{
569 left: &node{
570 exp: &expressionNodePartial{
571 left: &node{
572 role: licenseNode,
573 exp: nil,
574 lic: &licenseNodePartial{
575 license: "MIT",
576 hasPlus: false,
577 hasException: false,
578 exception: "",
579 },
580 ref: nil,
581 },
582 conjunction: "and",
583 right: &node{
584 role: licenseNode,
585 exp: nil,
586 lic: &licenseNodePartial{
587 license: "ISC",
588 hasPlus: false,
589 hasException: false,
590 exception: "",
591 },
592 ref: nil,
593 },
594 },
595 lic: nil,
596 ref: nil,
597 },
598 conjunction: "or",
599 right: &node{
600 exp: &expressionNodePartial{
601 left: &node{
602 role: licenseNode,
603 exp: nil,
604 lic: &licenseNodePartial{
605 license: "Apache-2.0",
606 hasPlus: false,
607 hasException: false,
608 exception: "",
609 },
610 ref: nil,
611 },
612 conjunction: "and",
613 right: &node{
614 role: licenseNode,
615 exp: nil,
616 lic: &licenseNodePartial{
617 license: "GPL-2.0",
618 hasPlus: false,
619 hasException: false,
620 exception: "",
621 },
622 ref: nil,
623 },
624 },
625 lic: nil,
626 ref: nil,
627 },
628 },
629 lic: nil,
630 ref: nil,
631 },
632 "{ LEFT: { LEFT: MIT and RIGHT: ISC } or RIGHT: { LEFT: Apache-2.0 and RIGHT: GPL-2.0 } }", nil},
633 {"(AND)OR(AND) Expression", "(MIT AND ISC) OR (Apache-2.0 AND GPL-2.0)",
634 &node{
635 role: expressionNode,
636 exp: &expressionNodePartial{
637 left: &node{
638 exp: &expressionNodePartial{
639 left: &node{
640 role: licenseNode,
641 exp: nil,
642 lic: &licenseNodePartial{
643 license: "MIT",
644 hasPlus: false,
645 hasException: false,
646 exception: "",
647 },
648 ref: nil,
649 },
650 conjunction: "and",
651 right: &node{
652 role: licenseNode,
653 exp: nil,
654 lic: &licenseNodePartial{
655 license: "ISC",
656 hasPlus: false,
657 hasException: false,
658 exception: "",
659 },
660 ref: nil,
661 },
662 },
663 lic: nil,
664 ref: nil,
665 },
666 conjunction: "or",
667 right: &node{
668 exp: &expressionNodePartial{
669 left: &node{
670 role: licenseNode,
671 exp: nil,
672 lic: &licenseNodePartial{
673 license: "Apache-2.0",
674 hasPlus: false,
675 hasException: false,
676 exception: "",
677 },
678 ref: nil,
679 },
680 conjunction: "and",
681 right: &node{
682 role: licenseNode,
683 exp: nil,
684 lic: &licenseNodePartial{
685 license: "GPL-2.0",
686 hasPlus: false,
687 hasException: false,
688 exception: "",
689 },
690 ref: nil,
691 },
692 },
693 lic: nil,
694 ref: nil,
695 },
696 },
697 lic: nil,
698 ref: nil,
699 },
700 "{ LEFT: { LEFT: MIT and RIGHT: ISC } or RIGHT: { LEFT: Apache-2.0 and RIGHT: GPL-2.0 } }", nil},
701 {"AND(OR)AND Expression", "MIT AND (ISC OR Apache-2.0) AND GPL-2.0",
702 &node{
703 role: expressionNode,
704 exp: &expressionNodePartial{
705 left: &node{
706 role: licenseNode,
707 exp: nil,
708 lic: &licenseNodePartial{
709 license: "MIT",
710 hasPlus: false,
711 hasException: false,
712 exception: "",
713 },
714 ref: nil,
715 },
716 conjunction: "and",
717 right: &node{
718 role: expressionNode,
719 exp: &expressionNodePartial{
720 left: &node{
721 role: expressionNode,
722 exp: &expressionNodePartial{
723 left: &node{
724 role: licenseNode,
725 exp: nil,
726 lic: &licenseNodePartial{
727 license: "ISC",
728 hasPlus: false,
729 hasException: false,
730 exception: "",
731 },
732 ref: nil,
733 },
734 conjunction: "or",
735 right: &node{
736 role: licenseNode,
737 exp: nil,
738 lic: &licenseNodePartial{
739 license: "Apache-2.0",
740 hasPlus: false,
741 hasException: false,
742 exception: "",
743 },
744 ref: nil,
745 },
746 },
747 lic: nil,
748 ref: nil,
749 },
750 conjunction: "and",
751 right: &node{
752 role: licenseNode,
753 exp: nil,
754 lic: &licenseNodePartial{
755 license: "GPL-2.0",
756 hasPlus: false,
757 hasException: false,
758 exception: "",
759 },
760 ref: nil,
761 },
762 },
763 lic: nil,
764 ref: nil,
765 },
766 },
767 lic: nil,
768 ref: nil,
769 },
770 "{ LEFT: MIT and RIGHT: { LEFT: { LEFT: ISC or RIGHT: Apache-2.0 } and RIGHT: GPL-2.0 } }", nil},
771 {"AND-AND-AND Expression", "MIT AND ISC AND Apache-2.0 AND GPL-2.0",
772 &node{
773 role: expressionNode,
774 exp: &expressionNodePartial{
775 left: &node{
776 role: licenseNode,
777 exp: nil,
778 lic: &licenseNodePartial{
779 license: "MIT",
780 hasPlus: false,
781 hasException: false,
782 exception: "",
783 },
784 ref: nil,
785 },
786 conjunction: "and",
787 right: &node{
788 role: expressionNode,
789 exp: &expressionNodePartial{
790 left: &node{
791 role: licenseNode,
792 exp: nil,
793 lic: &licenseNodePartial{
794 license: "ISC",
795 hasPlus: false,
796 hasException: false,
797 exception: "",
798 },
799 ref: nil,
800 },
801 conjunction: "and",
802 right: &node{
803 role: expressionNode,
804 exp: &expressionNodePartial{
805 left: &node{
806 role: licenseNode,
807 exp: nil,
808 lic: &licenseNodePartial{
809 license: "Apache-2.0",
810 hasPlus: false,
811 hasException: false,
812 exception: "",
813 },
814 ref: nil,
815 },
816 conjunction: "and",
817 right: &node{
818 role: licenseNode,
819 exp: nil,
820 lic: &licenseNodePartial{
821 license: "GPL-2.0",
822 hasPlus: false,
823 hasException: false,
824 exception: "",
825 },
826 ref: nil,
827 },
828 },
829 lic: nil,
830 ref: nil,
831 },
832 },
833 lic: nil,
834 ref: nil,
835 },
836 },
837 lic: nil,
838 ref: nil,
839 },
840 "{ LEFT: MIT and RIGHT: { LEFT: ISC and RIGHT: { LEFT: Apache-2.0 and RIGHT: GPL-2.0 } } }", nil},
841 {"kitchen sink",
842 " (MIT AND Apache-1.0+) OR DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2 OR (GPL-2.0 WITH Bison-exception-2.2)",
843 &node{
844 role: expressionNode,
845 exp: &expressionNodePartial{
846 left: &node{
847 role: expressionNode,
848 exp: &expressionNodePartial{
849 left: &node{
850 role: licenseNode,
851 exp: nil,
852 lic: &licenseNodePartial{
853 license: "MIT",
854 hasPlus: false,
855 hasException: false,
856 exception: "",
857 },
858 ref: nil,
859 },
860 conjunction: "and",
861 right: &node{
862 role: licenseNode,
863 exp: nil,
864 lic: &licenseNodePartial{
865 license: "Apache-1.0",
866 hasPlus: true,
867 hasException: false,
868 exception: "",
869 },
870 ref: nil,
871 },
872 },
873 lic: nil,
874 ref: nil,
875 },
876 conjunction: "or",
877 right: &node{
878 role: expressionNode,
879 exp: &expressionNodePartial{
880 left: &node{
881 role: licenseRefNode,
882 exp: nil,
883 lic: nil,
884 ref: &referenceNodePartial{
885 hasDocumentRef: true,
886 documentRef: "spdx-tool-1.2",
887 licenseRef: "MIT-Style-2",
888 },
889 },
890 conjunction: "or",
891 right: &node{
892 role: licenseNode,
893 exp: nil,
894 lic: &licenseNodePartial{
895 license: "GPL-2.0",
896 hasPlus: false,
897 hasException: true,
898 exception: "Bison-exception-2.2",
899 },
900 ref: nil,
901 },
902 },
903 lic: nil,
904 ref: nil,
905 },
906 },
907 lic: nil,
908 ref: nil,
909 },
910 "{ LEFT: { LEFT: MIT and RIGHT: Apache-1.0+ } or RIGHT: { LEFT: DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2 or RIGHT: GPL-2.0 with Bison-exception-2.2 } }", nil,
911 },
912 }
913
914 for _, test := range tests {
915 test := test
916 t.Run(test.name, func(t *testing.T) {
917 startNode, err := parse(test.expression)
918
919 require.Equal(t, test.err, err)
920 if test.err != nil {
921 // when error, check that returned node is nil
922 var nilNode *node
923 assert.Equal(t, nilNode, startNode, "Expected nil node when error occurs.")
924 return
925 }
926
927 // ref found, check token values are as expected
928 assert.Equal(t, test.node, startNode)
929 assert.Equal(t, test.nodestr, startNode.string())
930 })
931 }
932 }
933
934 func TestParseTokens(t *testing.T) {
935 tests := []struct {
936 name string
937 tokens *tokenStream
938 node *node
939 nodestr string
940 err error
941 }{
942 {"single license",
943 getLicenseTokens(0),
944 &node{
945 role: licenseNode,
946 exp: nil,
947 lic: &licenseNodePartial{
948 license: "MIT", hasPlus: false,
949 hasException: false, exception: ""},
950 ref: nil,
951 },
952 "MIT", nil},
953 {"two licenses using AND",
954 getAndClauseTokens(0),
955 &node{
956 role: expressionNode,
957 exp: &expressionNodePartial{
958 left: &node{
959 role: licenseNode,
960 exp: nil,
961 lic: &licenseNodePartial{
962 license: "MIT", hasPlus: false,
963 hasException: false, exception: ""},
964 ref: nil,
965 },
966 conjunction: "and",
967 right: &node{
968 role: licenseNode,
969 exp: nil,
970 lic: &licenseNodePartial{
971 license: "Apache-2.0", hasPlus: false,
972 hasException: false, exception: ""},
973 ref: nil,
974 },
975 },
976 lic: nil,
977 ref: nil,
978 },
979 "{ LEFT: MIT and RIGHT: Apache-2.0 }", nil},
980 {"two licenses using OR",
981 getOrClauseTokens(0),
982 &node{
983 role: expressionNode,
984 exp: &expressionNodePartial{
985 left: &node{
986 role: licenseNode,
987 exp: nil,
988 lic: &licenseNodePartial{
989 license: "MIT", hasPlus: false,
990 hasException: false, exception: ""},
991 ref: nil,
992 },
993 conjunction: "or",
994 right: &node{
995 role: licenseNode,
996 exp: nil,
997 lic: &licenseNodePartial{
998 license: "Apache-2.0", hasPlus: false,
999 hasException: false, exception: ""},
1000 ref: nil,
1001 },
1002 },
1003 lic: nil,
1004 ref: nil,
1005 },
1006 "{ LEFT: MIT or RIGHT: Apache-2.0 }", nil},
1007 {"kitchen sink",
1008 getKitchSinkTokens(0),
1009 &node{
1010 role: expressionNode,
1011 exp: &expressionNodePartial{
1012 left: &node{
1013 role: expressionNode,
1014 exp: &expressionNodePartial{
1015 left: &node{
1016 role: licenseNode,
1017 exp: nil,
1018 lic: &licenseNodePartial{
1019 license: "MIT",
1020 hasPlus: false,
1021 hasException: false,
1022 exception: "",
1023 },
1024 ref: nil,
1025 },
1026 conjunction: "and",
1027 right: &node{
1028 role: licenseNode,
1029 exp: nil,
1030 lic: &licenseNodePartial{
1031 license: "Apache-1.0",
1032 hasPlus: true,
1033 hasException: false,
1034 exception: "",
1035 },
1036 ref: nil,
1037 },
1038 },
1039 lic: nil,
1040 ref: nil,
1041 },
1042 conjunction: "or",
1043 right: &node{
1044 role: expressionNode,
1045 exp: &expressionNodePartial{
1046 left: &node{
1047 role: licenseRefNode,
1048 exp: nil,
1049 lic: nil,
1050 ref: &referenceNodePartial{
1051 hasDocumentRef: true,
1052 documentRef: "spdx-tool-1.2",
1053 licenseRef: "MIT-Style-2",
1054 },
1055 },
1056 conjunction: "or",
1057 right: &node{
1058 role: licenseNode,
1059 exp: nil,
1060 lic: &licenseNodePartial{
1061 license: "GPL-2.0",
1062 hasPlus: false,
1063 hasException: true,
1064 exception: "Bison-exception-2.2",
1065 },
1066 ref: nil,
1067 },
1068 },
1069 lic: nil,
1070 ref: nil,
1071 },
1072 },
1073 lic: nil,
1074 ref: nil,
1075 },
1076 "{ LEFT: { LEFT: MIT and RIGHT: Apache-1.0+ } or RIGHT: { LEFT: DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2 or RIGHT: GPL-2.0 with Bison-exception-2.2 } }", nil,
1077 },
1078 {"operator error - missing close parenthesis", getMissingEndParenTokens(0),
1079 &node{}, "", errors.New("open parenthesis does not have a matching close parenthesis"),
1080 },
1081 {"operator error - missing open parenthesis", getMissingStartParenTokens(0),
1082 &node{}, "", errors.New("close parenthesis does not have a matching open parenthesis"),
1083 },
1084 {"operator error - missing operator", getMissingOperatorTokens(0),
1085 &node{}, "", errors.New("licenses or expressions are not separated by an operator"),
1086 },
1087 {"operator error - starts with close parenthesis", getStartsWithCloseParenTokens(0),
1088 &node{}, "", errors.New("expression starts with close parenthesis"),
1089 },
1090 {"operator error - starts with OR", getStartsWithORTokens(0),
1091 &node{}, "", errors.New("expression starts with OR"),
1092 },
1093 {"operator error - starts with AND", getStartsWithANDTokens(0),
1094 &node{}, "", errors.New("expression starts with AND"),
1095 },
1096 {"operator error - ends with OR", getEndsWithORTokens(0),
1097 &node{}, "", errors.New("expected expression following OR, but found none"),
1098 },
1099 {"operator error - ends with AND", getEndsWithANDTokens(0),
1100 &node{}, "", errors.New("expected expression following AND, but found none"),
1101 },
1102 {"operator error - OR immediately after operator", getDoubleORTokens(0),
1103 &node{}, "", errors.New("expected license or expression, but found OR"),
1104 },
1105 {"operator error - AND immediately after operator", getDoubleANDTokens(0),
1106 &node{}, "", errors.New("expected license or expression, but found AND"),
1107 },
1108 }
1109
1110 for _, test := range tests {
1111 test := test
1112 t.Run(test.name, func(t *testing.T) {
1113 startNode := test.tokens.parseTokens()
1114
1115 require.Equal(t, test.err, test.tokens.err)
1116 if test.err != nil {
1117 // when error, check that returned node is nil
1118 var nilNode *node
1119 assert.Equal(t, nilNode, startNode, "Expected nil node when error occurs.")
1120 return
1121 }
1122
1123 // ref found, check token values are as expected
1124 assert.Equal(t, test.node, startNode)
1125 assert.Equal(t, test.nodestr, startNode.string())
1126 })
1127 }
1128 }
1129
1130 func TestHasMoreTokens(t *testing.T) {
1131 tests := []struct {
1132 name string
1133 tokens *tokenStream
1134 result bool
1135 }{
1136 {"at start", getAndClauseTokens(0), true},
1137 {"at middle", getAndClauseTokens(1), true},
1138 {"at end", getAndClauseTokens(2), true},
1139 {"past end", getAndClauseTokens(3), false},
1140 }
1141
1142 for _, test := range tests {
1143 test := test
1144 t.Run(test.name, func(t *testing.T) {
1145 assert.Equal(t, test.result, test.tokens.hasMore())
1146 })
1147 }
1148 }
1149
1150 func TestPeek(t *testing.T) {
1151 tests := []struct {
1152 name string
1153 tokens *tokenStream
1154 token *token
1155 }{
1156 {"at start", getAndClauseTokens(0), &(token{role: licenseToken, value: "MIT"})},
1157 {"at middle", getAndClauseTokens(1), &(token{role: operatorToken, value: "AND"})},
1158 {"at end", getAndClauseTokens(2), &(token{role: licenseToken, value: "Apache-2.0"})},
1159 {"past end", getAndClauseTokens(3), nil},
1160 }
1161
1162 for _, test := range tests {
1163 test := test
1164 t.Run(test.name, func(t *testing.T) {
1165 assert.Equal(t, test.token, test.tokens.peek())
1166 })
1167 }
1168 }
1169
1170 func TestNext(t *testing.T) {
1171 tests := []struct {
1172 name string
1173 tokens *tokenStream
1174 newIndex int
1175 err error
1176 }{
1177 {"at start", getAndClauseTokens(0), 1, nil},
1178 {"at middle", getAndClauseTokens(1), 2, nil},
1179 {"at end", getAndClauseTokens(2), 3, nil},
1180 {"past end", getAndClauseTokens(3), 3, errors.New("read past end of tokens")},
1181 }
1182
1183 for _, test := range tests {
1184 test := test
1185 t.Run(test.name, func(t *testing.T) {
1186 test.tokens.next()
1187 assert.Equal(t, test.newIndex, test.tokens.index)
1188 require.Equal(t, test.err, test.tokens.err)
1189 })
1190 }
1191 }
1192
1193 // // TODO: func parseParenthesizedExpression(tokens *[]token, index int) (*node, int, error) {
1194 // // TODO: func parseAtom(tokens *[]token, index int) (*node, int, error) {
1195 // // TODO: func parseExpression(tokens *[]token, index int) (*node, int, error) {
1196 // // TODO: func parseAnd(tokens *[]token, index int) (*node, int, error) {
1197 // // TODO: func parseLicenseRef(tokens *[]token, index int) (*node, int, error) {
1198 // // TODO: func parseLicense(tokens *[]token, index int) (*node, int, error) {
1199
1200 func TestParseOperator(t *testing.T) {
1201 tests := []struct {
1202 name string
1203 tokens *tokenStream
1204 operator string
1205 expectNil bool
1206 newIndex int
1207 }{
1208 {"looking for WITH operator", getWithClauseTokens(1), "WITH", false, 2},
1209 {"looking for AND operator", getAndClauseTokens(1), "AND", false, 2},
1210 {"looking for OR operator", getOrClauseTokens(1), "OR", false, 2},
1211 {"looking for ( operator", getOrAndClauseTokens(2), "(", false, 3},
1212 {"looking for ) operator", getOrAndClauseTokens(6), ")", false, 7},
1213 {"looking for : operator", getColonClauseTokens(1), ":", false, 2},
1214 {"looking for + operator", getPlusClauseTokens(1), "+", false, 2},
1215 {"looking for OR operator, but got AND", getAndClauseTokens(1), "OR", true, 1},
1216 {"looking for OR operator, but got LICENSE", getOrClauseTokens(0), "OR", true, 0},
1217 }
1218
1219 for _, test := range tests {
1220 test := test
1221 t.Run(test.name, func(t *testing.T) {
1222 token := test.tokens.parseOperator(test.operator)
1223 require.Equal(t, test.newIndex, test.tokens.index)
1224 if test.expectNil {
1225 // returned token is nil if it isn't an operator or it is a different operator
1226 var nilToken *string
1227 assert.Equal(t, nilToken, token)
1228 } else {
1229 // index advances when token is the expected operator
1230 assert.Equal(t, test.operator, *token)
1231 }
1232 })
1233 }
1234 }
1235
1236 // func parseWith(tokens *[]token, index int) (*string, int, error) {
1237 func TestParseWith(t *testing.T) {
1238 tests := []struct {
1239 name string
1240 tokens *tokenStream
1241 exception string
1242 expectNil bool
1243 newIndex int
1244 err error
1245 }{
1246 {"WITH followed by EXCEPTION", getWithClauseTokens(1), "Bison-exception-2.2", false, 2, nil},
1247 {"WITH not followed by EXCEPTION", getInvalidWithClauseTokens(1), "", true, 2, errors.New("expected exception after 'WITH'")},
1248 {"not with", getOrClauseTokens(1), "", true, 1, nil},
1249 {"WITH not followed by any tokens", getMalformedWithClauseTokens(1), "", true, 2, errors.New("expected exception after 'WITH'")},
1250 }
1251
1252 for _, test := range tests {
1253 test := test
1254 t.Run(test.name, func(t *testing.T) {
1255 exceptionLicense := test.tokens.parseWith()
1256 assert.Equal(t, test.newIndex, test.tokens.index)
1257
1258 require.Equal(t, test.err, test.tokens.err)
1259 if test.expectNil {
1260 // exception license is nil when error occurs or WITH operator is not found
1261 var nilString *string
1262 assert.Equal(t, nilString, exceptionLicense)
1263 return
1264 }
1265
1266 // WITH found, check exceptionLicense value
1267 assert.Equal(t, test.exception, *exceptionLicense)
1268 })
1269 }
1270 }
1271
1272 // TODO: func (n *node) String() string {
1273
1274 func getLicenseTokens(index int) *tokenStream {
1275 var tokens []token
1276 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1277 return getTokenStream(tokens, index)
1278 }
1279
1280 func getWithClauseTokens(index int) *tokenStream {
1281 var tokens []token
1282 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1283 tokens = append(tokens, token{role: operatorToken, value: "WITH"})
1284 tokens = append(tokens, token{role: exceptionToken, value: "Bison-exception-2.2"})
1285 return getTokenStream(tokens, index)
1286 }
1287
1288 func getInvalidWithClauseTokens(index int) *tokenStream {
1289 var tokens []token
1290 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1291 tokens = append(tokens, token{role: operatorToken, value: "WITH"})
1292 tokens = append(tokens, token{role: licenseToken, value: "Apache-2.0"})
1293 return getTokenStream(tokens, index)
1294 }
1295
1296 func getMalformedWithClauseTokens(index int) *tokenStream {
1297 var tokens []token
1298 tokens = append(tokens, token{role: licenseToken, value: "Apache-2.0"})
1299 tokens = append(tokens, token{role: operatorToken, value: "WITH"})
1300 return getTokenStream(tokens, index)
1301 }
1302
1303 func getAndClauseTokens(index int) *tokenStream {
1304 var tokens []token
1305 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1306 tokens = append(tokens, token{role: operatorToken, value: "AND"})
1307 tokens = append(tokens, token{role: licenseToken, value: "Apache-2.0"})
1308 return getTokenStream(tokens, index)
1309 }
1310
1311 func getOrClauseTokens(index int) *tokenStream {
1312 var tokens []token
1313 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1314 tokens = append(tokens, token{role: operatorToken, value: "OR"})
1315 tokens = append(tokens, token{role: licenseToken, value: "Apache-2.0"})
1316 return getTokenStream(tokens, index)
1317 }
1318
1319 func getOrAndClauseTokens(index int) *tokenStream {
1320 var tokens []token
1321 tokens = append(tokens, token{role: licenseToken, value: "Apache-2.0"})
1322 tokens = append(tokens, token{role: operatorToken, value: "OR"})
1323 tokens = append(tokens, token{role: operatorToken, value: "("})
1324 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1325 tokens = append(tokens, token{role: operatorToken, value: "AND"})
1326 tokens = append(tokens, token{role: licenseToken, value: "Apache 2.0"})
1327 tokens = append(tokens, token{role: operatorToken, value: ")"})
1328 return getTokenStream(tokens, index)
1329 }
1330
1331 func getColonClauseTokens(index int) *tokenStream {
1332 var tokens []token
1333 tokens = append(tokens, token{role: documentRefToken, value: "spdx-tool-1.2"})
1334 tokens = append(tokens, token{role: operatorToken, value: ":"})
1335 tokens = append(tokens, token{role: licenseRefToken, value: "MIT-Style-2"})
1336 return getTokenStream(tokens, index)
1337 }
1338
1339 func getPlusClauseTokens(index int) *tokenStream {
1340 var tokens []token
1341 tokens = append(tokens, token{role: licenseToken, value: "Apache-1.0"})
1342 tokens = append(tokens, token{role: operatorToken, value: "+"})
1343 tokens = append(tokens, token{role: operatorToken, value: "OR"})
1344 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1345 return getTokenStream(tokens, index)
1346 }
1347
1348 func getKitchSinkTokens(index int) *tokenStream {
1349 var tokens []token
1350 tokens = append(tokens, token{role: operatorToken, value: "("})
1351 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1352 tokens = append(tokens, token{role: operatorToken, value: "AND"})
1353 tokens = append(tokens, token{role: licenseToken, value: "Apache-1.0"})
1354 tokens = append(tokens, token{role: operatorToken, value: "+"})
1355 tokens = append(tokens, token{role: operatorToken, value: ")"})
1356 tokens = append(tokens, token{role: operatorToken, value: "OR"})
1357 tokens = append(tokens, token{role: documentRefToken, value: "spdx-tool-1.2"})
1358 tokens = append(tokens, token{role: operatorToken, value: ":"})
1359 tokens = append(tokens, token{role: licenseRefToken, value: "MIT-Style-2"})
1360 tokens = append(tokens, token{role: operatorToken, value: "OR"})
1361 tokens = append(tokens, token{role: operatorToken, value: "("})
1362 tokens = append(tokens, token{role: licenseToken, value: "GPL-2.0"})
1363 tokens = append(tokens, token{role: operatorToken, value: "WITH"})
1364 tokens = append(tokens, token{role: exceptionToken, value: "Bison-exception-2.2"})
1365 tokens = append(tokens, token{role: operatorToken, value: ")"})
1366 return getTokenStream(tokens, index)
1367 }
1368
1369 func getMissingOperatorTokens(index int) *tokenStream {
1370 var tokens []token
1371 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1372 tokens = append(tokens, token{role: licenseToken, value: "Apache-2.0"})
1373 return getTokenStream(tokens, index)
1374 }
1375
1376 func getStartsWithCloseParenTokens(index int) *tokenStream {
1377 var tokens []token
1378 tokens = append(tokens, token{role: operatorToken, value: ")"})
1379 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1380 return getTokenStream(tokens, index)
1381 }
1382
1383 func getStartsWithORTokens(index int) *tokenStream {
1384 var tokens []token
1385 tokens = append(tokens, token{role: operatorToken, value: "OR"})
1386 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1387 return getTokenStream(tokens, index)
1388 }
1389
1390 func getStartsWithANDTokens(index int) *tokenStream {
1391 var tokens []token
1392 tokens = append(tokens, token{role: operatorToken, value: "AND"})
1393 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1394 return getTokenStream(tokens, index)
1395 }
1396
1397 func getEndsWithORTokens(index int) *tokenStream {
1398 var tokens []token
1399 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1400 tokens = append(tokens, token{role: operatorToken, value: "OR"})
1401 return getTokenStream(tokens, index)
1402 }
1403
1404 func getEndsWithANDTokens(index int) *tokenStream {
1405 var tokens []token
1406 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1407 tokens = append(tokens, token{role: operatorToken, value: "AND"})
1408 return getTokenStream(tokens, index)
1409 }
1410
1411 func getDoubleORTokens(index int) *tokenStream {
1412 var tokens []token
1413 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1414 tokens = append(tokens, token{role: operatorToken, value: "OR"})
1415 tokens = append(tokens, token{role: operatorToken, value: "OR"})
1416 tokens = append(tokens, token{role: licenseToken, value: "Apache-2.0"})
1417 return getTokenStream(tokens, index)
1418 }
1419
1420 func getDoubleANDTokens(index int) *tokenStream {
1421 var tokens []token
1422 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1423 tokens = append(tokens, token{role: operatorToken, value: "AND"})
1424 tokens = append(tokens, token{role: operatorToken, value: "AND"})
1425 tokens = append(tokens, token{role: licenseToken, value: "Apache-2.0"})
1426 return getTokenStream(tokens, index)
1427 }
1428
1429 func getMissingStartParenTokens(index int) *tokenStream {
1430 var tokens []token
1431 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1432 tokens = append(tokens, token{role: operatorToken, value: ")"})
1433 return getTokenStream(tokens, index)
1434 }
1435
1436 func getMissingEndParenTokens(index int) *tokenStream {
1437 var tokens []token
1438 tokens = append(tokens, token{role: operatorToken, value: "("})
1439 tokens = append(tokens, token{role: licenseToken, value: "MIT"})
1440 return getTokenStream(tokens, index)
1441 }
1442
1443 func getTokenStream(tokens []token, index int) *tokenStream {
1444 return &tokenStream{
1445 tokens: tokens,
1446 index: index,
1447 err: nil,
1448 }
1449 }