package config import ( "bytes" "strings" "testing" ) func TestInterrogate(t *testing.T) { cfg := Config{ Title: "Old title", Description: "Old description", Privacy: "private", CategoryID: "22", } in := strings.NewReader("New title\nNew description\npublic\n3\n") var out bytes.Buffer if err := Interrogate(&cfg, in, &out); err != nil { t.Fatalf("Interrogate() error = %v", err) } if cfg.Title != "New title" { t.Errorf("Title = %q, want %q", cfg.Title, "New title") } if cfg.Description != "New description" { t.Errorf("Description = %q, want %q", cfg.Description, "New description") } if cfg.Privacy != "public" { t.Errorf("Privacy = %q, want %q", cfg.Privacy, "public") } if cfg.CategoryID != "10" { t.Errorf("CategoryID = %q, want %q (Music)", cfg.CategoryID, "10") } } func TestInterrogateKeepsDefaults(t *testing.T) { cfg := Config{ Title: "Title", Description: "Description", Privacy: "unlisted", CategoryID: "22", } if err := Interrogate(&cfg, strings.NewReader("\n\n\n\n"), &bytes.Buffer{}); err != nil { t.Fatalf("Interrogate() error = %v", err) } if cfg.Title != "Title" || cfg.Description != "Description" || cfg.Privacy != "unlisted" || cfg.CategoryID != "22" { t.Errorf("defaults changed: %#v", cfg) } } func TestInterrogateRetriesInvalidInput(t *testing.T) { cfg := Config{ Title: "Title", Privacy: "private", CategoryID: "22", } in := strings.NewReader("\n\nbogus\npublic\n99\n3\n") var out bytes.Buffer if err := Interrogate(&cfg, in, &out); err != nil { t.Fatalf("Interrogate() error = %v", err) } if cfg.Privacy != "public" { t.Errorf("Privacy = %q, want %q", cfg.Privacy, "public") } if cfg.CategoryID != "10" { t.Errorf("CategoryID = %q, want %q (Music)", cfg.CategoryID, "10") } if !strings.Contains(out.String(), "Invalid visibility") { t.Errorf("output does not mention invalid visibility: %q", out.String()) } if !strings.Contains(out.String(), "Please enter a number between") { t.Errorf("output does not mention invalid category: %q", out.String()) } } func TestInterrogateUnexpectedEOF(t *testing.T) { if err := Interrogate(&Config{}, strings.NewReader(""), &bytes.Buffer{}); err == nil { t.Fatal("expected an error on end of input") } }