Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 1 | // Package vdltest provides testing utilities for veyron2/vdl/... |
| 2 | package vdltest |
| 3 | |
| 4 | import ( |
| 5 | "io" |
| 6 | "io/ioutil" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
Jiri Simsa | ffceefa | 2015-02-28 11:03:34 -0800 | [diff] [blame] | 11 | "v.io/x/ref/lib/vdl/build" |
| 12 | "v.io/x/ref/lib/vdl/vdlutil" |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | // ExpectPass makes sure errs has no errors. |
| 16 | func ExpectPass(t *testing.T, errs *vdlutil.Errors, testName string) { |
| 17 | if !errs.IsEmpty() { |
| 18 | t.Errorf("%v expected no errors but saw: %v", testName, errs.ToError()) |
| 19 | errs.Reset() |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // ExpectFail makes sure errs has an error that matches all the re regexps. |
| 24 | func ExpectFail(t *testing.T, errs *vdlutil.Errors, testName string, re ...string) { |
| 25 | if errs.IsEmpty() { |
| 26 | t.Errorf("%v expected errors but didn't see any", testName) |
| 27 | return |
| 28 | } |
| 29 | actual := errs.ToError().Error() |
| 30 | errs.Reset() |
| 31 | for index, errRe := range re { |
| 32 | matched, err := regexp.Match(errRe, []byte(actual)) |
| 33 | if err != nil { |
| 34 | t.Errorf("%v bad regexp pattern [%v] %q", testName, index, errRe) |
| 35 | return |
| 36 | } |
| 37 | if !matched { |
| 38 | t.Errorf("%v couldn't match pattern [%v] %q against %q", testName, index, errRe, actual) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // ExpectResult ensures errs has an error that matches all the re regexps, or |
| 44 | // that errs has no errors if no regexps were provided, or only one was provided |
| 45 | // with the empty string. |
| 46 | func ExpectResult(t *testing.T, errs *vdlutil.Errors, testName string, re ...string) { |
| 47 | if len(re) == 0 || len(re) == 1 && re[0] == "" { |
| 48 | ExpectPass(t, errs, testName) |
| 49 | } else { |
| 50 | ExpectFail(t, errs, testName, re...) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // FakeBuildPackage constructs a fake build package for testing, with files |
| 55 | // mapping from file names to file contents. |
| 56 | func FakeBuildPackage(name, path string, files map[string]string) *build.Package { |
| 57 | var fnames []string |
| 58 | for fname, _ := range files { |
| 59 | fnames = append(fnames, fname) |
| 60 | } |
| 61 | return &build.Package{ |
| 62 | Dir: "", |
| 63 | Name: name, |
| 64 | Path: path, |
| 65 | BaseFileNames: fnames, |
| 66 | OpenFilesFunc: FakeOpenFiles(files), |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // FakeOpenFiles returns a function that obeys the build.Package.OpenFilesFunc |
| 71 | // signature, that simply uses the files map to return readers. |
| 72 | func FakeOpenFiles(files map[string]string) func(fnames []string) (map[string]io.ReadCloser, error) { |
| 73 | return func(fnames []string) (map[string]io.ReadCloser, error) { |
| 74 | ret := make(map[string]io.ReadCloser, len(fnames)) |
| 75 | for _, fname := range fnames { |
| 76 | ret[fname] = ioutil.NopCloser(strings.NewReader(files[fname])) |
| 77 | } |
| 78 | return ret, nil |
| 79 | } |
| 80 | } |