typestar

Una prueba en Go en Go

Un archivo que termina en _test.go, una función que toma *testing.T y t.Errorf.

func Slugify(title string) string {
    return strings.ReplaceAll(strings.ToLower(title), " ", "-")
}

func TestSlugify(t *testing.T) {
    got := Slugify("Language Basics")
    want := "language-basics"
    if got != want {
        t.Errorf("Slugify() = %q, want %q", got, want)
    }
}

func TestSlugifyEmpty(t *testing.T) {
    if got := Slugify(""); got != "" {
        t.Fatalf("expected empty, got %q", got)
    }
}

Cómo funciona

  1. El nombre debe empezar con Test y tomar *testing.T.
  2. t.Errorf registra una falla y sigue; t.Fatalf se detiene.
  3. No hay biblioteca de aserciones: la comparación la escribes tú.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
17
Caracteres a escribir
377
Tokens
93
Ritmo de tres estrellas
100 tpm

Al ritmo de tres estrellas de 100 tokens por minuto, este intento toma unos 56 segundos.

Escribe este fragmento

Paso 1 de 4 en Escribir pruebas; paso 1 de 13 en Pruebas.

Siguiente →