From a38e8816239c8ca9216f028cfb585b4d2e71edd3 Mon Sep 17 00:00:00 2001 From: 0xfandom Date: Wed, 1 Jul 2026 17:16:41 +0530 Subject: [PATCH] test: add coverage for normalizeVerString normalizeVerString strips characters that aren't valid in a semantic version pre-release/build-metadata string (i.e. not in semanticAlphabet). Add a table-driven test covering pass-through of valid strings and the stripping of spaces, disallowed punctuation, non-ASCII runes, and fully-invalid input. Signed-off-by: 0xfandom --- version_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 version_test.go diff --git a/version_test.go b/version_test.go new file mode 100644 index 00000000..20f76aae --- /dev/null +++ b/version_test.go @@ -0,0 +1,65 @@ +package terminal + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +// TestNormalizeVerString ensures that normalizeVerString keeps only the +// characters that are valid according to the semantic versioning guidelines +// (those contained in semanticAlphabet) and strips everything else. +func TestNormalizeVerString(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + input string + expected string + }{{ + name: "empty string", + input: "", + expected: "", + }, { + name: "already valid alphanumeric", + input: "alpha1", + expected: "alpha1", + }, { + name: "valid with hyphen and dot", + input: "beta-rc.2", + expected: "beta-rc.2", + }, { + name: "spaces are stripped", + input: "hello world", + expected: "helloworld", + }, { + name: "disallowed punctuation is stripped", + // Underscore, plus and slash are not part of + // semanticAlphabet. + input: "a_b+c/d", + expected: "abcd", + }, { + name: "non-ascii runes are stripped", + input: "café-αβ", + expected: "caf-", + }, { + name: "all characters invalid", + input: "@#$%^&*()", + expected: "", + }, { + name: "full semver pre-release passes through", + input: "0.17.0-alpha.rc1", + expected: "0.17.0-alpha.rc1", + }} + + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + require.Equal( + t, tc.expected, normalizeVerString(tc.input), + ) + }) + } +}