|
| 1 | +package opt |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestMain_parse(t *testing.T) { |
| 10 | + cases := map[string]struct { |
| 11 | + args []string |
| 12 | + expectedParallelism int |
| 13 | + expectedOutput string |
| 14 | + expectedURLString string |
| 15 | + }{ |
| 16 | + "no options": {args: []string{"http://example.com/foo.png"}, expectedParallelism: 8, expectedOutput: "foo.png", expectedURLString: "http://example.com/foo.png"}, |
| 17 | + "-p=2": {args: []string{"-p=2", "http://example.com/foo.png"}, expectedParallelism: 2, expectedOutput: "foo.png", expectedURLString: "http://example.com/foo.png"}, |
| 18 | + "-o=bar.png": {args: []string{"-o=bar.png", "http://example.com/foo.png"}, expectedParallelism: 8, expectedOutput: "bar.png", expectedURLString: "http://example.com/foo.png"}, |
| 19 | + "index.html": {args: []string{"http://example.com/"}, expectedParallelism: 8, expectedOutput: "index.html", expectedURLString: "http://example.com/"}, |
| 20 | + } |
| 21 | + |
| 22 | + for n, c := range cases { |
| 23 | + c := c |
| 24 | + t.Run(n, func(t *testing.T) { |
| 25 | + args := c.args |
| 26 | + expectedParallelism := c.expectedParallelism |
| 27 | + expectedOutput := c.expectedOutput |
| 28 | + expectedURL, err := url.ParseRequestURI(c.expectedURLString) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("err %s", err) |
| 31 | + } |
| 32 | + |
| 33 | + opts, err := Parse(args...) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("err %s", err) |
| 36 | + } |
| 37 | + |
| 38 | + actualParallelism := opts.Parallelism |
| 39 | + actualOutput := opts.Output |
| 40 | + actualURL := opts.URL |
| 41 | + |
| 42 | + if actualParallelism != expectedParallelism { |
| 43 | + t.Errorf(`unexpected parallelism: expected: %d actual: %d`, expectedParallelism, actualParallelism) |
| 44 | + } |
| 45 | + |
| 46 | + if actualOutput != expectedOutput { |
| 47 | + t.Errorf(`unexpected output: expected: "%s" actual: "%s"`, expectedOutput, actualOutput) |
| 48 | + } |
| 49 | + |
| 50 | + if !reflect.DeepEqual(actualURL, expectedURL) { |
| 51 | + t.Errorf(`unexpected URL: expected: "%s" actual: "%s"`, expectedURL, actualURL) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestMain_parse_InvalidURL(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + expected := "parse %: invalid URI for request" |
| 61 | + |
| 62 | + _, err := Parse([]string{"%"}...) |
| 63 | + if err == nil { |
| 64 | + t.Fatal("Unexpectedly err was nil") |
| 65 | + } |
| 66 | + |
| 67 | + actual := err.Error() |
| 68 | + if actual != expected { |
| 69 | + t.Errorf(`unexpected error: expected: "%s" actual: "%s"`, expected, actual) |
| 70 | + } |
| 71 | +} |
0 commit comments