|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// Tests [validateInput] func |
| 9 | +func TestValidateInput(t *testing.T) { |
| 10 | + entries := []struct { |
| 11 | + input string |
| 12 | + expected bool |
| 13 | + }{ |
| 14 | + {"192.180.33.25", true}, |
| 15 | + {"example.com", true}, |
| 16 | + {"http://192.180.33.25", true}, |
| 17 | + {"https://example.org/path?query=123", true}, |
| 18 | + {"test-domain.org", true}, |
| 19 | + {"256.256.256.256", true}, |
| 20 | + {"invalid@domain", false}, |
| 21 | + {"not-a-domain", false}, |
| 22 | + } |
| 23 | + for _, entry := range entries { |
| 24 | + t.Run(entry.input, func(t *testing.T) { |
| 25 | + result := validateInput(entry.input) |
| 26 | + if result != entry.expected { |
| 27 | + t.Errorf( |
| 28 | + "validateInput(%q) = %v; want %v", |
| 29 | + entry.input, |
| 30 | + result, |
| 31 | + entry.expected, |
| 32 | + ) |
| 33 | + } |
| 34 | + }) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Tests [ReadStdin] and [ProcessInputs] func |
| 39 | +func TestReadAndProcessInputs(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + input string |
| 43 | + wantInputs []string |
| 44 | + wantErr bool |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "single valid url", |
| 48 | + input: "http://192.168.1.1\n", |
| 49 | + wantInputs: []string{"http://192.168.1.1"}, |
| 50 | + wantErr: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "multiple urls with whitespace", |
| 54 | + input: " http://192.168.1.1 https://google.com \n", |
| 55 | + wantInputs: []string{"http://192.168.1.1", "https://google.com"}, |
| 56 | + wantErr: false, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "mixed valid and invalid", |
| 60 | + input: "http://192.168.1.1 not_valid example.com\n", |
| 61 | + wantInputs: []string{"http://192.168.1.1"}, |
| 62 | + wantErr: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "empty input", |
| 66 | + input: "", |
| 67 | + wantInputs: []string{}, |
| 68 | + wantErr: false, |
| 69 | + }, |
| 70 | + } |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + r, w, _ := os.Pipe() |
| 74 | + defer r.Close() |
| 75 | + os.Stdin = r |
| 76 | + w.Write([]byte(tt.input)) |
| 77 | + w.Close() |
| 78 | + got, readErr := ReadStdin() |
| 79 | + if readErr != nil { |
| 80 | + t.Errorf("ReadStdin() error = %v", readErr) |
| 81 | + return |
| 82 | + } |
| 83 | + inputs, processErr := ProcessInputs(got) |
| 84 | + if (processErr != nil) != tt.wantErr { |
| 85 | + t.Errorf( |
| 86 | + "ProcessInputs() error = %v, wantErr %v", |
| 87 | + processErr, |
| 88 | + tt.wantErr, |
| 89 | + ) |
| 90 | + return |
| 91 | + } |
| 92 | + if len(inputs) != len(tt.wantInputs) { |
| 93 | + t.Errorf("got %v, want %v", inputs, tt.wantInputs) |
| 94 | + return |
| 95 | + } |
| 96 | + for i := range inputs { |
| 97 | + if inputs[i] != tt.wantInputs[i] { |
| 98 | + t.Errorf("got %v, want %v", inputs, tt.wantInputs) |
| 99 | + return |
| 100 | + } |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
0 commit comments