From 147a5dbd2a2a848cad58b1ed9cee75321578582a Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Mon, 4 Nov 2024 12:18:52 +0530 Subject: [PATCH] chore: include test suites for library --- tests/cmp.luau | 111 +++++++++++++++++++++++++++++++++++ tests/parse_invalid.luau | 122 +++++++++++++++++++++++++++++++++++++++ tests/parse_valid.luau | 75 ++++++++++++++++++++++++ 3 files changed, 308 insertions(+) create mode 100644 tests/cmp.luau create mode 100644 tests/parse_invalid.luau create mode 100644 tests/parse_valid.luau diff --git a/tests/cmp.luau b/tests/cmp.luau new file mode 100644 index 0000000..84355a4 --- /dev/null +++ b/tests/cmp.luau @@ -0,0 +1,111 @@ +local frktest = require("../lune_packages/frktest").core +local test = frktest.test +local check = frktest.assert.check + +local Option = require("../luau_packages/option") +type Option = Option.Option + +local Semver = require("../lib") + +local function t(this: { [any]: any } | T, that: { [any]: any } | T) end +t(Option.Some("x"), Option.Some("x")) + +t({ + str = 1, +}, { + [true] = 1, +}) + +return function() + test.suite("Semver comparison tests", function() + test.case("Basic version comparisons", function() + local v1 = Semver.parse("1.2.3"):unwrap() + local v2 = Semver.parse("1.2.4"):unwrap() + local v3 = Semver.parse("1.3.0"):unwrap() + local v4 = Semver.parse("2.0.0"):unwrap() + local v5 = Semver.parse("2.1.0"):unwrap() + local v6 = Semver.parse("3.0.0"):unwrap() + + check.is_true(v1 < v2) + check.is_true(v2 < v3) + check.is_true(v3 < v4) + check.is_true(v4 < v5) + check.is_true(v5 < v6) + check.is_true(v1 <= v2) + check.is_true(v2 > v1) + check.is_true(v4 >= v3) + check.is_true(v6 > v1) + end) + + test.case("Equal version comparisons", function() + local v1 = Semver.parse("1.2.3"):unwrap() + local v2 = Semver.parse("1.2.3"):unwrap() + local v3 = Semver.parse("1.2.3"):unwrap() + local v4 = Semver.parse("1.2.3"):unwrap() + + check.is_true(v1 == v2) + check.is_true(v2 == v3) + check.is_true(v3 == v4) + check.is_true(v1 <= v2) + check.is_true(v1 >= v2) + check.is_false(v1 < v2) + check.is_false(v1 > v2) + check.is_false(v3 > v4) + end) + + test.case("Prerelease version comparisons", function() + local v1 = Semver.parse("1.2.3-alpha.1"):unwrap() + local v2 = Semver.parse("1.2.3-alpha.2"):unwrap() + local v3 = Semver.parse("1.2.3-beta.1"):unwrap() + local v4 = Semver.parse("1.2.3"):unwrap() + local v5 = Semver.parse("1.2.3-rc.1"):unwrap() + local v6 = Semver.parse("1.2.3-rc.2"):unwrap() + + check.is_true(v1 < v2) + check.is_true(v2 < v3) + check.is_true(v3 < v4) + check.is_true(v3 < v5) + check.is_true(v5 < v6) + check.is_true(v6 < v4) + check.is_false(v4 < v1) + check.is_true(v4 > v3) + end) + + test.case("Build metadata comparisons", function() + local v1 = Semver.parse("1.2.3+build.1"):unwrap() + local v2 = Semver.parse("1.2.3+build.2"):unwrap() + local v3 = Semver.parse("1.2.3+build.123"):unwrap() + local v4 = Semver.parse("1.2.3+20230615"):unwrap() + local v5 = Semver.parse("1.2.3+exp.sha.5114f85"):unwrap() + + -- Build metadata should be ignored in comparisons + check.is_true(v1 == v2) + check.is_true(v2 == v3) + check.is_true(v3 == v4) + check.is_true(v4 == v5) + check.is_false(v1 < v2) + check.is_false(v2 > v3) + check.is_false(v4 > v5) + end) + + test.case("Complex version comparisons", function() + local v1 = Semver.parse("2.0.0-alpha.1+build.123"):unwrap() + local v2 = Semver.parse("2.0.0-beta.1+build.123"):unwrap() + local v3 = Semver.parse("2.0.0+build.123"):unwrap() + local v4 = Semver.parse("2.1.0-alpha.1"):unwrap() + local v5 = Semver.parse("2.1.0"):unwrap() + local v6 = Semver.parse("2.1.0-rc.1+build.999"):unwrap() + local v7 = Semver.parse("2.1.1-alpha.1+sha.xyz"):unwrap() + + check.is_true(v1 < v2) + check.is_true(v2 < v3) + check.is_true(v3 < v4) + check.is_true(v4 < v5) + check.is_true(v4 < v6) + check.is_true(v6 < v5) + check.is_true(v5 < v7) + check.is_false(v5 < v1) + check.is_false(v7 < v6) + end) + end) +end diff --git a/tests/parse_invalid.luau b/tests/parse_invalid.luau new file mode 100644 index 0000000..6c2258a --- /dev/null +++ b/tests/parse_invalid.luau @@ -0,0 +1,122 @@ +local frktest = require("../lune_packages/frktest").core +local test = frktest.test +local check = frktest.assert.check + +local Semver = require("../lib") + +return function() + test.suite("Invalid semver parsing tests", function() + test.case("Rejects missing components", function() + local res = Semver.parse("1.2") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "MandatoryComponentMissing", + components = { "1", "2" }, + }, + }) + end) + + test.case("Rejects invalid component types", function() + -- Test invalid major + local res = Semver.parse("a.2.3") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "InvalidComponentType", + component = "major", + got = "char", + }, + }) + + -- Test invalid minor with symbols + res = Semver.parse("1.$.3") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "InvalidComponentType", + component = "minor", + got = "symbol", + }, + }) + + -- Test invalid patch + res = Semver.parse("1.2.x") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "InvalidComponentType", + component = "patch", + got = "char", + }, + }) + end) + + test.case("Rejects leading zeros", function() + -- Test leading zeros in major + local res = Semver.parse("01.2.3") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "LeadingZerosPresent", + component = "major", + }, + }) + + -- Test leading zeros in minor + res = Semver.parse("1.02.3") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "LeadingZerosPresent", + component = "minor", + }, + }) + + -- Test leading zeros in patch + res = Semver.parse("1.2.03") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "LeadingZerosPresent", + component = "patch", + }, + }) + end) + + test.case("Rejects invalid prerelease types", function() + local res = Semver.parse("1.2.3-gamma.1") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "InvalidPrereleaseType", + type = "gamma", + }, + }) + end) + + test.case("Rejects invalid prerelease ordinals", function() + -- Test with character ordinal + local res = Semver.parse("1.2.3-beta.a") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "InvalidPrereleaseOrdinalType", + expected = "number", + got = "char", + }, + }) + + -- Test with symbol ordinal + res = Semver.parse("1.2.3-beta.$") + check.is_true(res:isErr()) + check.table.contains(res:unwrapErr(), { + kind = { + id = "InvalidPrereleaseOrdinalType", + expected = "number", + got = "symbol", + }, + }) + end) + end) +end diff --git a/tests/parse_valid.luau b/tests/parse_valid.luau new file mode 100644 index 0000000..7167054 --- /dev/null +++ b/tests/parse_valid.luau @@ -0,0 +1,75 @@ +local frktest = require("../lune_packages/frktest").core +local test = frktest.test +local check = frktest.assert.check + +local Option = require("../luau_packages/option") +type Option = Option.Option + +local Semver = require("../lib") + +-- FIXME: Any casts all over the place, because of frktest not being correctly typed +-- See https://github.com/itsfrank/frktest/issues/2 + +return function() + test.suite("Basic tests", function() + test.case("Semver creates valid version objects", function() + local res = Semver.parse("1.2.3-beta.1") + check.is_true(res:isOk()) + + local version = res:unwrap() + check.equal(version.major, 1) + check.equal(version.minor, 2) + check.equal(version.patch, 3) + end) + + test.case("Semver handles prerelease versions", function() + local res = Semver.parse("1.2.3-beta.1") + check.is_true(res:isOk()) + + local version = res:unwrap() + check.equal(version.major, 1) + check.equal(version.minor, 2) + check.equal(version.patch, 3) + + check.table.equal( + version.prerelease :: any, + Option.Some({ + type = "beta", + ordinal = Option.Some(1), + }) :: any + ) + end) + + test.case("Semver handles build metadata", function() + local res = Semver.parse("1.2.3+build.123") + check.is_true(res:isOk()) + + local version = res:unwrap() + + check.equal(version.major, 1) + check.equal(version.minor, 2) + check.equal(version.patch, 3) + check.table.equal(version.buildMetadata :: any, Option.Some("build.123") :: any) + end) + + test.case("Semver handles prerelease versions with build metadata", function() + local res = Semver.parse("1.2.3-beta.1+build.123") + check.is_true(res:isOk()) + + local version = res:unwrap() + check.equal(version.major, 1) + check.equal(version.minor, 2) + check.equal(version.patch, 3) + + check.table.equal( + version.prerelease :: any, + Option.Some({ + type = "beta", + ordinal = Option.Some(1), + }) :: any + ) + + check.table.equal(version.buildMetadata :: any, Option.Some("build.123") :: any) + end) + end) +end