-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsemver_test.c
736 lines (589 loc) · 14.5 KB
/
semver_test.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
* semver_test.c
*
* Copyright (c) 2015-2017 Tomas Aparicio
* MIT licensed
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "semver.h"
#define test_start(x) \
printf("\n# Test: %s\n", x) \
#define test_end() \
printf("OK\n") \
struct test_case {
char * x;
char * y;
int expected;
};
struct test_case_match {
char * x;
char * y;
char * op;
int expected;
};
typedef int (*fn)(semver_t, semver_t);
static void
compare_helper (char *a, char *b, int expected, fn test_fn) {
semver_t verX = {0};
semver_t verY = {0};
semver_parse(a, &verX);
semver_parse(b, &verY);
int resolution = test_fn(verX, verY);
assert(resolution == expected);
semver_free(&verX);
semver_free(&verY);
}
static void
suite_runner(struct test_case cases[], int len, fn test_fn) {
int i;
for (i = 0; i < len; i++) {
struct test_case args = cases[i];
compare_helper(args.x, args.y, args.expected, test_fn);
}
}
void
test_parse_simple() {
test_start("parse_simple");
char buf[] = "1.2.12";
semver_t ver;
int error = semver_parse(buf, &ver);
assert(error == 0);
assert(ver.major == 1);
assert(ver.minor == 2);
assert(ver.patch == 12);
semver_free(&ver);
test_end();
}
void
test_parse_major() {
test_start("parse_major");
char buf[] = "2";
semver_t ver = {0};
int error = semver_parse(buf, &ver);
assert(error == 0);
assert(ver.major == 2);
assert(ver.minor == 0);
assert(ver.patch == 0);
semver_free(&ver);
test_end();
}
void
test_parse_minor() {
test_start("parse_minor");
char buf[] = "1.2";
semver_t ver = {0};
int error = semver_parse(buf, &ver);
assert(error == 0);
assert(ver.major == 1);
assert(ver.minor == 2);
assert(ver.patch == 0);
semver_free(&ver);
test_end();
}
void
test_parse_prerelease() {
test_start("parse_prerelease");
char buf[] = "1.2.12-beta.alpha.1.1";
semver_t ver = {0};
int error = semver_parse(buf, &ver);
assert(error == 0);
assert(ver.major == 1);
assert(ver.minor == 2);
assert(ver.patch == 12);
assert(strcmp(ver.prerelease, "beta.alpha.1.1") == 0);
semver_free(&ver);
test_end();
}
void
test_parse_metadata() {
test_start("parse_metadata");
char buf[] = "1.2.12+20130313144700";
semver_t ver = {0};
int error = semver_parse(buf, &ver);
assert(error == 0);
assert(ver.major == 1);
assert(ver.minor == 2);
assert(ver.patch == 12);
assert(strcmp(ver.metadata, "20130313144700") == 0);
semver_free(&ver);
test_end();
}
void
test_parse_prerelerease_metadata() {
test_start("parse_prerelease_metadata");
char buf[] = "1.2.12-alpha.1+20130313144700";
semver_t ver = {0};
int error = semver_parse(buf, &ver);
assert(error == 0);
assert(ver.major == 1);
assert(ver.minor == 2);
assert(ver.patch == 12);
assert(strcmp(ver.prerelease, "alpha.1") == 0);
assert(strcmp(ver.metadata, "20130313144700") == 0);
semver_free(&ver);
test_end();
}
void
test_compare() {
test_start("semver_compare");
struct test_case cases[] = {
{"1", "0", 1},
{"1", "1", 0},
{"1", "3", -1},
{"1.5", "0.8", 1},
{"1.5", "1.3", 1},
{"1.2", "2.2", -1},
{"3.0", "1.5", 1},
{"1.5", "1.5", 0},
{"1.0.9", "1.0.0", 1},
{"1.0.9", "1.0.9", 0},
{"1.1.5", "1.1.9", -1},
{"1.2.2", "1.1.9", 1},
{"1.2.2", "1.2.9", -1},
};
suite_runner(cases, 13, &semver_compare);
test_end();
}
void
test_compare_full() {
test_start("semver_compare_full");
struct test_case cases[] = {
{"1.5.1", "1.5.1-beta", 1},
{"1.5.1-beta", "1.5.1", -1},
{"1.5.1-beta", "1.5.1-beta", 0},
{"1.5.1-beta", "1.5.1-alpha", 1},
{"1.5.1-beta.1", "1.5.1-alpha.1", 1},
{"1.5.1-beta.1", "1.5.1-beta.0", 1},
{"1.5.1-beta.1.5", "1.5.1-beta.1.5", 0},
{"1.5.1-beta.1.5", "1.5.1-beta.1.4", 1},
{"1.5.1-beta.1.0", "1.5.1-beta.1.4", -1},
{"1.5.1-beta.1.0", "1.5.1-alpha.1.0", 1},
{"1.5.1-beta.1.100", "1.5.1-alpha.1.99", 1},
{"1.5.1-beta.1.123456789", "1.5.1-alpha.1.12345678", 1},
{"1.5.1-beta.alpha.1", "1.5.1-beta.alpha.1.12345678", -1},
{"1.5.1-beta.alpha.1", "1.5.1-beta.alpha.1+123", 0},
{"1.5.1-beta.1+20130313144700", "1.5.1-beta.1+20120313144700", 0},
{"1.5.1-beta.1+20130313144700", "1.5.1-beta.1+20130313144700", 0},
{"1.5.1-beta.1+20130313144700", "1.5.1-beta.1+exp.sha.5114f85", 0},
{"1.5.1-beta.1+exp.sha.5114f85", "1.5.1-beta.1+exp.sha.5114f84", 0},
{"1.5.1-beta.1+exp.sha.5114f85", "1.5.1-beta.1+exp.sha1.5114f84", 0},
{"1.5.1-beta.1+exp.sha", "1.5.1-beta.1+exp.sha256", 0},
{"1.5.1-alpha.beta", "1.5.1-1.beta", 1},
};
suite_runner(cases, 21, &semver_compare);
test_end();
}
void
test_compare_spec() {
test_start("semver_compare_spec");
/* from: http://semver.org/spec/v2.0.0.html#spec-item-11 */
struct test_case cases[] = {
{"1.0.0-alpha", "1.0.0-alpha.1", -1},
{"1.0.0-alpha.1", "1.0.0-alpha.beta", -1},
{"1.0.0-alpha.beta", "1.0.0-beta", -1},
{"1.0.0-beta", "1.0.0-beta.2", -1},
{"1.0.0-beta.2", "1.0.0-beta.11", -1},
{"1.0.0-beta.11", "1.0.0-rc.1", -1},
{"1.0.0-rc.1", "1.0.0", -1},
};
suite_runner(cases, 7, &semver_compare);
test_end();
}
void
test_compare_gt() {
test_start("semver_gt");
struct test_case cases[] = {
{"1", "0", 1},
{"1", "3", 0},
{"3.0", "1", 1},
{"1.0", "3", 0},
{"1.0", "1", 0},
{"1.5", "0.8", 1},
{"1.2", "2.2", 0},
{"3.0", "1.5", 1},
{"1.1", "1.0.9", 1},
{"1.0", "1.0.0", 0},
{"1.1.9", "1.2", 0},
{"1.0.9", "1.0.0", 1},
{"1.0.9", "1.0.9", 0},
{"1.1.5", "1.1.9", 0},
{"1.2.2", "1.2.9", 0},
};
suite_runner(cases, 15, &semver_gt);
test_end();
}
void
test_compare_lt() {
test_start("semver_lt");
struct test_case cases[] = {
{"1", "0", 0},
{"1", "3", 1},
{"1.5", "0.8", 0},
{"1.2", "2.2", 1},
{"3.0", "1.5", 0},
{"1.0.9", "1.0.0", 0},
{"1.0.9", "1.0.9", 0},
{"1.1.5", "1.1.9", 1},
{"1.2.2", "1.2.9", 1},
};
suite_runner(cases, 9, &semver_lt);
test_end();
}
void
test_compare_eq() {
test_start("semver_eq");
struct test_case cases[] = {
{"1", "0", 0},
{"1", "3", 0},
{"1", "1", 1},
{"1.5", "0.8", 0},
{"1.2", "2.2", 0},
{"3.0", "1.5", 0},
{"1.0", "1.0", 1},
{"1.0.9", "1.0.0", 0},
{"1.1.5", "1.1.9", 0},
{"1.2.2", "1.2.9", 0},
{"1.0.0", "1.0.0", 1},
};
suite_runner(cases, 11, &semver_eq);
test_end();
}
void
test_compare_neq() {
test_start("semver_neq");
struct test_case cases[] = {
{"1", "0", 1},
{"1", "3", 1},
{"1", "1", 0},
{"1.5", "0.8", 1},
{"1.2", "2.2", 1},
{"3.0", "1.5", 1},
{"1.0", "1.0", 0},
{"1.0.9", "1.0.0", 1},
{"1.1.5", "1.1.9", 1},
{"1.2.2", "1.2.9", 1},
{"1.0.0", "1.0.0", 0},
};
suite_runner(cases, 11, &semver_neq);
test_end();
}
void
test_compare_gte() {
test_start("semver_gte");
struct test_case cases[] = {
{"1", "0", 1},
{"1", "3", 0},
{"1", "1", 1},
{"1.5", "0.8", 1},
{"1.2", "2.2", 0},
{"3.0", "1.5", 1},
{"1.0", "1.0", 1},
{"1.0.9", "1.0.0", 1},
{"1.1.5", "1.1.9", 0},
{"1.2.2", "1.2.9", 0},
{"1.0.0", "1.0.0", 1},
};
suite_runner(cases, 11, &semver_gte);
test_end();
}
void
test_compare_lte() {
test_start("semver_lte");
struct test_case cases[] = {
{"1", "0", 0},
{"1", "3", 1},
{"1", "1", 1},
{"1.5", "0.8", 0},
{"1.2", "2.2", 1},
{"3.0", "1.5", 0},
{"1.0", "1.0", 1},
{"1.0.9", "1.0.0", 0},
{"1.1.5", "1.1.9", 1},
{"1.2.2", "1.2.9", 1},
{"1.0.0", "1.0.0", 1},
};
suite_runner(cases, 11, &semver_lte);
test_end();
}
void
test_satisfies() {
test_start("semver_satisfies");
struct test_case_match cases[] = {
{"1", "0", ">=", 1},
{"1", "3", ">=", 0},
{"1", "1", ">=", 1},
{"1.5", "0.8", ">=", 1},
{"1.2", "2.2", ">=", 0},
{"3.0", "1.5", ">=", 1},
{"1.0", "1.0", ">=", 1},
{"1.0.9", "1.0.0", ">=", 1},
{"1.1.5", "1.1.9", ">=", 0},
{"1.2.2", "1.2.9", ">=", 0},
{"1.0.0", "1.0.0", ">=", 1},
{"1", "0", "<=", 0},
{"1", "3", "<=", 1},
{"1", "1", "<=", 1},
{"1.5", "0.8", "<=", 0},
{"1.2", "2.2", "<=", 1},
{"3.0", "1.5", "<=", 0},
{"1.0", "1.0", "<=", 1},
{"1.0.9", "1.0.0", "<=", 0},
{"1.1.5", "1.1.9", "<=", 1},
{"1.2.2", "1.2.9", "<=", 1},
{"1.0.0", "1.0.0", "<=", 1},
{"1", "0", "=", 0},
{"1", "3", "=", 0},
{"1", "1", "=", 1},
{"1.5", "0.8", "=", 0},
{"1.2", "2.2", "=", 0},
{"3.0", "1.5", "=", 0},
{"1.0", "1.0", "=", 1},
{"1.0.9", "1.0.0", "=", 0},
{"1.1.5", "1.1.9", "=", 0},
{"1.2.2", "1.2.9", "=", 0},
{"1.0.0", "1.0.0", "=", 1},
{"1", "0", ">", 1},
{"1", "3", ">", 0},
{"1", "1", ">", 0},
{"1.5", "0.8", ">", 1},
{"1.2", "2.2", ">", 0},
{"3.0", "1.5", ">", 1},
{"1.0", "1.0", ">", 0},
{"1.0.9", "1.0.0", ">", 1},
{"1.1.5", "1.1.9", ">", 0},
{"1.2.2", "1.2.9", ">", 0},
{"1.0.0", "1.0.0", ">", 0},
{"1", "0", "<", 0},
{"1", "3", "<", 1},
{"1", "1", "<", 0},
{"1.5", "0.8", "<", 0},
{"1.2", "2.2", "<", 1},
{"3.0", "1.5", "<", 0},
{"1.0", "1.0", "<", 0},
{"1.0.9", "1.0.0", "<", 0},
{"1.1.5", "1.1.9", "<", 1},
{"1.2.2", "1.2.9", "<", 1},
{"1.0.0", "1.0.0", "<", 0},
{"1", "0", "^", 0},
{"1", "3", "^", 0},
{"1", "1", "^", 1},
{"1.5", "0.8", "^", 0},
{"1.2", "2.2", "^", 0},
{"3.0", "1.5", "^", 0},
{"1.0", "1.0", "^", 1},
{"1.0.9", "1.0.0", "^", 1},
{"1.1.5", "1.1.9", "^", 0},
{"1.3.2", "1.1.9", "^", 1},
{"1.1.2", "1.5.9", "^", 0},
{"0.1.2", "1.5.9", "^", 0},
{"0.1.2", "0.2.9", "^", 0},
{"1.2.2", "1.2.9", "^", 0},
{"1.0.0", "1.0.0", "^", 1},
{"1", "0", "~", 0},
{"1", "3", "~", 0},
{"1", "1", "~", 1},
{"1.5", "0.8", "~", 0},
{"1.2", "2.2", "~", 0},
{"3.0", "1.5", "~", 0},
{"1.0", "1.0", "~", 1},
{"1.0.9", "1.0.0", "~", 1},
{"1.1.5", "1.1.9", "~", 1},
{"1.1.9", "1.1.3", "~", 1},
{"1.2.2", "1.3.9", "~", 0},
{"1.0.0", "1.0.0", "~", 1},
};
size_t i;
for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
struct test_case_match args = cases[i];
semver_t verX = {0};
semver_t verY = {0};
semver_parse(args.x, &verX);
semver_parse(args.y, &verY);
int resolution = semver_satisfies(verX, verY, args.op);
assert(resolution == args.expected);
semver_free(&verX);
semver_free(&verY);
}
test_end();
}
/**
* Renders
*/
void
test_render() {
test_start("render");
semver_t ver = {1, 5, 8, NULL, NULL};
char * str[10] = {0};
semver_render(&ver, (char *) str);
assert(strcmp((char *) str, "1.5.8") == 0);
semver_t ver2 = {1, 5, 8, NULL, NULL};
ver2.prerelease = "alpha.1";
ver2.metadata = "1232323";
char * str2[22] = {0};
semver_render(&ver2, (char *) str2);
assert(strcmp((char *) str2, "1.5.8-alpha.1+1232323") == 0);
semver_t ver3 = {1, 0, 8, NULL, NULL};
char * str3[10] = {0};
semver_render(&ver3, (char *) str3);
assert(strcmp((char *) str3, "1.0.8") == 0);
test_end();
}
void
test_numeric() {
test_start("numeric");
semver_t v0 = {0, 5, 9, NULL, NULL};
assert(semver_numeric(&v0) == 59);
semver_t v01 = {0, 0, 9, NULL, NULL};
assert(semver_numeric(&v01) == 9);
semver_t v02 = {0, 0, 0, NULL, NULL};
assert(semver_numeric(&v02) == 0);
semver_t v03 = {1, 0, 2, NULL, NULL};
assert(semver_numeric(&v03) == 102);
semver_t v04 = {1, 2, 0, NULL, NULL};
assert(semver_numeric(&v04) == 120);
semver_t v05 = {4, 0, 0, NULL, NULL};
assert(semver_numeric(&v05) == 400);
semver_t v1 = {1, 3, 6, NULL, NULL};
assert(semver_numeric(&v1) == 136);
semver_t v2 = {1, 3, 6, "beta", "123456789"};
assert(semver_numeric(&v2) == 1025);
semver_t v3 = {1, 3, 6, "be$ta", "12&345@67(89"};
assert(semver_numeric(&v3) == 1025);
test_end();
}
/**
* Modifiers
*/
void
test_bump() {
test_start("bump");
semver_t ver = {1, 5, 8, NULL, NULL};
semver_bump(&ver);
assert(ver.major == 2);
semver_free(&ver);
semver_t ver2 = {1, 0, 0, NULL, NULL};
semver_bump(&ver2);
assert(ver2.major == 2);
semver_free(&ver2);
semver_t ver3 = {0, 0, 0, NULL, NULL};
semver_bump(&ver3);
assert(ver3.major == 1);
semver_free(&ver3);
test_end();
}
void
test_bump_minor() {
test_start("bump_minor");
semver_t ver = {1, 5, 8, NULL, NULL};
semver_bump_minor(&ver);
assert(ver.minor == 6);
semver_free(&ver);
semver_t ver2 = {1, 0, 0, NULL, NULL};
semver_bump_minor(&ver2);
assert(ver2.minor == 1);
semver_free(&ver2);
test_end();
}
void
test_bump_patch() {
test_start("bump_patch");
semver_t ver = {1, 5, 8, NULL, NULL};
semver_bump_patch(&ver);
assert(ver.patch == 9);
semver_free(&ver);
semver_t ver2 = {1, 5, 0, NULL, NULL};
semver_bump_patch(&ver2);
assert(ver2.patch == 1);
semver_free(&ver2);
test_end();
}
/**
* Clean up
*/
void
test_free () {
test_start("free");
semver_t ver = {0};
char * str = "1.5.6";
semver_parse(str, &ver);
assert(ver.major == 1);
assert(ver.patch == 6);
semver_free(&ver);
semver_t ver2 = {0};
char * str2 = "1.5.6-beta.1+12345";
semver_parse(str2, &ver2);
assert(ver2.prerelease != NULL);
semver_free(&ver2);
assert(ver2.prerelease == NULL);
test_end();
}
/**
* Helper functions
*/
void
test_valid_chars() {
test_start("valid_chars");
int valid;
valid = semver_is_valid("1");
assert(valid == 1);
valid = semver_is_valid("159");
assert(valid == 1);
valid = semver_is_valid("1b3");
assert(valid == 1);
valid = semver_is_valid("3 0 1");
assert(valid == 0);
valid = semver_is_valid("&3@(");
assert(valid == 0);
test_end();
}
void
test_clean() {
test_start("clean");
int error;
char str[] = "1.2.3";
error = semver_clean(str);
assert(strcmp(str, "1.2.3") == 0);
assert(error == 0);
char str2[] = " [email protected] #.alpha+12@34 ";
error = semver_clean(str2);
assert(strcmp(str2, "1.2.3-beta.alpha+1234") == 0);
assert(error == 0);
test_end();
}
int
main() {
/* Parser */
test_parse_simple();
test_parse_major();
test_parse_minor();
test_parse_prerelease();
test_parse_metadata();
test_parse_prerelerease_metadata();
/* Comparison */
test_compare();
test_compare_full();
test_compare_spec();
test_compare_gt();
test_compare_lt();
test_compare_eq();
test_compare_neq();
test_compare_gte();
test_compare_lte();
test_satisfies();
/* Renders */
test_render();
test_numeric();
/* Modifiers */
test_bump();
test_bump_minor();
test_bump_patch();
/* Clean up */
test_free();
/* Helpers */
test_valid_chars();
test_clean();
return 0;
}