package main
import "testing"
func TestParseSection(t *testing.T) {
md := `# Changelog
## 1.2.9
- **BREAKING:** trust_policy déménage sous federation.trust_policy.
- Vue Fédération dans gitfed-web.
## 1.2.8
- Badge de confiance en attente dans la nav admin.
`
bullets := parseSection(md, "1.2.9")
if len(bullets) != 2 {
t.Fatalf("expected 2 bullets, got %d", len(bullets))
}
if !bullets[0].Breaking {
t.Errorf("expected first bullet to be flagged breaking, got %+v", bullets[0])
}
if bullets[1].Breaking {
t.Errorf("expected second bullet not to be flagged breaking, got %+v", bullets[1])
}
older := parseSection(md, "1.2.8")
if len(older) != 1 || older[0].Breaking {
t.Errorf("expected 1.2.8 to have 1 non-breaking bullet, got %+v", older)
}
if got := parseSection(md, "9.9.9"); got != nil {
t.Errorf("expected nil for a version not present, got %+v", got)
}
}
func TestParseSectionDoesNotBleedIntoNextVersion(t *testing.T) {
md := `# Changelog
## 2.0.0
- **BREAKING:** something big.
## 1.9.0
- **BREAKING:** this must not be counted for 2.0.0.
`
bullets := parseSection(md, "2.0.0")
if len(bullets) != 1 {
t.Fatalf("expected exactly 1 bullet for 2.0.0, got %+v", bullets)
}
}
func TestCompareVersions(t *testing.T) {
cases := []struct {
a, b string
want int
}{
{"1.2.9", "1.2.9", 0},
{"1.2.8", "1.2.9", -1},
{"1.2.10", "1.2.9", 1}, // numeric, not lexicographic
{"2.0.0", "1.9.9", 1},
{"1.2.0", "1.10.0", -1},
}
for _, c := range cases {
if got := compareVersions(c.a, c.b); got != c.want {
t.Errorf("compareVersions(%q, %q) = %d, want %d", c.a, c.b, got, c.want)
}
}
}
func TestFlattenBreaking(t *testing.T) {
versions := []versionChangelog{
{Version: "1.2.8", Bullets: []bullet{{Text: "a"}, {Text: "b", Breaking: true}}},
{Version: "1.2.9", Bullets: []bullet{{Text: "c", Breaking: true}}},
}
flat := flattenBreaking(versions)
if len(flat) != 2 {
t.Fatalf("expected 2 breaking bullets, got %d: %+v", len(flat), flat)
}
if flat[0].Text != "b" || flat[1].Text != "c" {
t.Errorf("unexpected order/content: %+v", flat)
}
}
func TestSortVersions(t *testing.T) {
versions := []versionChangelog{
{Version: "1.2.10"},
{Version: "1.2.9"},
{Version: "1.3.0"},
}
sortVersions(versions)
want := []string{"1.2.9", "1.2.10", "1.3.0"}
for i, w := range want {
if versions[i].Version != w {
t.Errorf("index %d: got %s, want %s", i, versions[i].Version, w)
}
}
}