| 1 | package card |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "image/png" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestExcerptAndLines(t *testing.T) { |
| 11 | got := Excerpt("# Titel\n\nEen **widget** die [telt](http://x).\n\nTweede alinea.\n", 80) |
| 12 | if got != "Een widget die telt." { |
| 13 | t.Fatalf("excerpt %q", got) |
| 14 | } |
| 15 | if !strings.Contains(Excerpt("```\ngeheim\n```\n\nZichtbaar", 40), "Zichtbaar") || strings.Contains(Excerpt("```\ngeheim\n```\n\nZichtbaar", 40), "geheim") { |
| 16 | t.Fatal("codeblok") |
| 17 | } |
| 18 | from, to, ok := ParseLines("#L10-20") |
| 19 | if !ok || from != 10 || to != 20 { |
| 20 | t.Fatalf("bereik %d %d %v", from, to, ok) |
| 21 | } |
| 22 | from, to, ok = ParseLines("L4") |
| 23 | if !ok || from != 4 || to != 4 { |
| 24 | t.Fatal(from, to) |
| 25 | } |
| 26 | lines := make([]string, 25) |
| 27 | for i := range lines { |
| 28 | lines[i] = "regel-" + string(rune('a'+i)) |
| 29 | } |
| 30 | src := strings.Join(lines, "\n") + "\n" |
| 31 | snip := Snippet(src, 10, 12, true) |
| 32 | if snip != "regel-j\nregel-k\nregel-l" { |
| 33 | t.Fatalf("snippet %q", snip) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestLockedImageOmitsSecret(t *testing.T) { |
| 38 | secret := "ZXQGEHEIM" |
| 39 | locked := Locked("https://git.example/", "https://git.example/-/card.png?locked=1", "https://git.example/api/v1/oembed?locked=1") |
| 40 | svg := SVG(locked) |
| 41 | if strings.Contains(svg, secret) || !strings.Contains(svg, LockedTitle) { |
| 42 | t.Fatalf("svg:\n%s", svg) |
| 43 | } |
| 44 | pngBytes, err := Raster(svg) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | cfg, err := png.DecodeConfig(bytes.NewReader(pngBytes)) |
| 49 | if err != nil || cfg.Width != Width || cfg.Height != Height { |
| 50 | t.Fatalf("png %v %v", cfg, err) |
| 51 | } |
| 52 | pub := locked |
| 53 | pub.Title = secret |
| 54 | pub.Description = secret |
| 55 | pub.Kind = "repository" |
| 56 | other, err := PNG(pub) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | if bytes.Equal(pngBytes, other) { |
| 61 | t.Fatal("verschillende kaarten hebben dezelfde png") |
| 62 | } |
| 63 | c := NewCache(2) |
| 64 | c.Put(svg, pngBytes) |
| 65 | if _, ok := c.Get(svg); !ok || c.Hits() != 1 { |
| 66 | t.Fatal("cache") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestBlurb(t *testing.T) { |
| 71 | m := Meta{Description: "regel-a\nregel-b", Facts: []string{"Go", "1 star"}} |
| 72 | if m.Blurb() != "regel-a / regel-b · Go · 1 star" { |
| 73 | t.Fatalf("blurb %q", m.Blurb()) |
| 74 | } |
| 75 | } |