h2non/semver.c

semver_satisfies 'caret' corner cases

weber-martin opened this issue · 0 comments

From https://docs.npmjs.com/misc/semver#caret-ranges-123-025-004

^1.2.3 := >=1.2.3 <2.0.0

Note >= 1.2.3, i.e., !(< 1.2.3). 1.2.3-alpha < 1.2.3 per semver, but semver_satisfies(1.2.3, 1.2.3-alpha, ^) == 1.

Note that < 2.0.0 and 2.0.0-alpha < 2.0.0, but semver_satisfies(1.2.3, 2.0.0-alpha, ^) == 0

#include <assert.h>
#include <stdio.h>
#include "semver.h"

int satisfies(const char *sv1, const char *sv2, const char *op) {
	semver_t v1, v2;
	int rc;

	rc = semver_parse(sv1, &v1);
	assert(!rc);
	rc = semver_parse(sv2, &v2);
	assert(!rc);
	rc = semver_satisfies(v1, v2, op);
	semver_free(&v1);
	semver_free(&v2);
	return rc;
}

int main(int argc, char *argv[]) {
	struct bundle {
		const char *sv1;
		const char *sv2;
	} cases [] = {
		{"1.2.3", "1.2.3"},
		{"1.2.3", "1.2.3-alpha"}, /* 1.2.3 > 1.2.3-alpha ! */
		{"1.2.3", "2.0.0"},
		{"1.2.3", "2.0.0-alpha"}, /* 2.0.0-alpha < 2.0.0 ! */
	};
	size_t num_cases = sizeof(cases) / sizeof(struct bundle), i;

	for (i=0; i < num_cases; ++i)
		printf("%s ^ %s = %d\n", cases[i].sv1, cases[i].sv2, 
			satisfies(cases[i].sv1, cases[i].sv2, "^"));
	return 0;
}

outputs

1.2.3 ^ 1.2.3 = 1
1.2.3 ^ 1.2.3-alpha = 1 # should be 0
1.2.3 ^ 2.0.0 = 0
1.2.3 ^ 2.0.0-alpha = 0 # should be 1