ajhahn.de
← eeco
Go 66 lines
package projecttype

import "testing"

func TestLoadCatalog(t *testing.T) {
	cat, err := LoadCatalog()
	if err != nil {
		t.Fatalf("LoadCatalog: %v", err)
	}
	want := []Category{CLI, Library, WebApp, WebAPI, Fullstack, Mobile, Embedded, GameDev, ML, Infra, Generic}
	if got := len(cat.Categories()); got != len(want) {
		t.Fatalf("category count = %d, want %d (%v)", got, len(want), cat.Categories())
	}
	for _, c := range want {
		e, ok := cat.Get(c)
		if !ok {
			t.Errorf("missing category %q", c)
			continue
		}
		if e.Category != c {
			t.Errorf("%q: entry.Category = %q", c, e.Category)
		}
		if e.Description == "" {
			t.Errorf("%q: empty description", c)
		}
		if e.PickWhen == "" {
			t.Errorf("%q: empty pick_when", c)
		}
		if len(e.Dirs) == 0 {
			t.Errorf("%q: no dirs", c)
		}
	}
}

func TestCategoriesSorted(t *testing.T) {
	cat, err := LoadCatalog()
	if err != nil {
		t.Fatalf("LoadCatalog: %v", err)
	}
	got := cat.Categories()
	for i := 1; i < len(got); i++ {
		if got[i-1] > got[i] {
			t.Fatalf("categories not sorted: %v", got)
		}
	}
}

func TestDirsForCopiesAndUnknown(t *testing.T) {
	cat, err := LoadCatalog()
	if err != nil {
		t.Fatalf("LoadCatalog: %v", err)
	}
	if got := cat.DirsFor(Category("nope")); got != nil {
		t.Errorf("DirsFor(unknown) = %v, want nil", got)
	}
	d1 := cat.DirsFor(CLI)
	if len(d1) == 0 {
		t.Fatal("DirsFor(cli) empty")
	}
	d1[0] = "mutated"
	d2 := cat.DirsFor(CLI)
	if d2[0] == "mutated" {
		t.Error("DirsFor did not return a defensive copy")
	}
}