package utm import "testing" func TestTagger(t *testing.T) { tg := NewTagger([]string{"https://www.acme.example/", "acme.dev", "", "https://acme.example/pricing"}) p := Params{Source: "x", Medium: "social", Campaign: "partner", Content: "jitsi-12"} for in, want := range map[string]string{ "https://acme.example/blog/post": "https://acme.example/blog/post?utm_campaign=partner&utm_content=jitsi-12&utm_medium=social&utm_source=x", "https://docs.acme.dev/x?a=1": "https://docs.acme.dev/x?a=1&utm_campaign=partner&utm_content=jitsi-12&utm_medium=social&utm_source=x", "https://www.acme.example/?utm_source=newsletter#top": "https://www.acme.example/?utm_campaign=partner&utm_content=jitsi-12&utm_medium=social&utm_source=newsletter#top", "https://jitsi.org/news": "https://jitsi.org/news", "https://notacme.example/": "https://notacme.example/", "https://x.com/acme/status/1": "https://x.com/acme/status/1", "": "", } { if got := tg.URL(in, p); got != want { t.Errorf("URL(%q) = %q, want %q", in, got, want) } } text := "Read it (https://acme.example/a). Or https://jitsi.org/b, or https://acme.dev!" want := "Read it (https://acme.example/a?utm_campaign=partner&utm_content=jitsi-12&utm_medium=social&utm_source=x). " + "Or https://jitsi.org/b, or https://acme.dev?utm_campaign=partner&utm_content=jitsi-12&utm_medium=social&utm_source=x!" if got := tg.Text(text, p); got != want { t.Errorf("Text =\n%s\nwant\n%s", got, want) } var none *Tagger if none.URL("https://acme.example", p) != "https://acme.example" || NewTagger(nil).Text(text, p) != text { t.Error("a tagger without sites must change nothing") } } func TestSlug(t *testing.T) { for in, want := range map[string]string{"Jitsi Meet": "jitsi-meet", " Acme, Inc. ": "acme-inc", "Ünïcode 2": "n-code-2", "": ""} { if got := Slug(in); got != want { t.Errorf("Slug(%q) = %q, want %q", in, got, want) } } }