Skip to content

Commit

Permalink
Semver version (#272)
Browse files Browse the repository at this point in the history
* semver version

* add test
  • Loading branch information
xufulin1994 authored Apr 1, 2024
1 parent 7e98d1f commit f8af0db
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/grammar/base.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ description = "\"" _ inner:(!"\"" i:. {return i})* "\"" {
}

// version
semver = semver:([0-9]+ "." [0-9]+ "." [0-9]+ ("-" [a-zA-Z0-9.-]+)?) { return semver.flat(2).join(""); }
semver = semver:([0-9]+ "." [0-9]+ "." [0-9]+ preRelease) { return semver.flat(Infinity).join(""); }
preRelease = ('-' preReleaseIdentifiers)?;
preReleaseIdentifiers = identifier ('.' [0-9a-zA-Z-]+)*;
identifier = [a-zA-Z-] ([a-zA-Z-] / [0-9])*;

// whitespace or comment
_ = ([ \t\r\n]+ / comment)*
Expand Down
50 changes: 50 additions & 0 deletions src/tests/semver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {readFile} from "../parse";
import peg from "pegjs";
import * as path from "path";

const grammar = readFile(path.resolve(__dirname, '..'), "grammar", "base.pegjs")

const parser = peg.generate(grammar, {allowedStartRules: ["semver"]});


describe("semver", () => {
it("should parse a semantic version with a pre-release identifier", () => {
const input_1 = "1.2.3-alpha";
const expected_1 = "1.2.3-alpha";

const input_2 = "1.0.0-alpha.beta";
const expected_2 = "1.0.0-alpha.beta";

const input_3 = "1.2.3----RC-SNAPSHOT.12.9.1--.12";
const expected_3 = "1.2.3----RC-SNAPSHOT.12.9.1--.12";
// Parse the input string
const result_1 = parser.parse(input_1);
const result_2 = parser.parse(input_2);
const result_3 = parser.parse(input_3);

// Check the result
expect(result_1).toBe(expected_1);
expect(result_2).toBe(expected_2);
expect(result_3).toBe(expected_3);
});

it("should parse a semantic version with no pre-release identifier", () => {
const input = "1.2.3";
const expected = "1.2.3";

// Parse the input string
const result = parser.parse(input);

// Check the result
expect(result).toBe(expected);
});

it("parse a invalid semantic version throw error", () => {
const input_1 = "1.2.3-0123";
const input_2 = "alpha_beta";

// Parsing an invalid semantic version should throw an error
expect(() => parser.parse(input_1)).toThrow();
expect(() => parser.parse(input_2)).toThrow();
});
});

0 comments on commit f8af0db

Please sign in to comment.