TestParse takes the *.tokenizer.json and *.parser.json files in ../testdata, runs the tokenizer files as input through the parser and compares the result with the parser files.
(t *testing.T)
| 17 | // TestParse takes the *.tokenizer.json and *.parser.json files in ../testdata, runs the tokenizer files as input |
| 18 | // through the parser and compares the result with the parser files. |
| 19 | func TestParse(t *testing.T) { |
| 20 | t.Logf("Locating testdata directory...") |
| 21 | tryDirectories := []string{ |
| 22 | "./testdata", |
| 23 | "../testdata", |
| 24 | } |
| 25 | |
| 26 | foundDir := "" |
| 27 | for _, dir := range tryDirectories { |
| 28 | if _, err := os.Stat(dir); err == nil { |
| 29 | foundDir = dir |
| 30 | } |
| 31 | } |
| 32 | if foundDir == "" { |
| 33 | t.Fatalf("failed to find testdata directory in %v", tryDirectories) |
| 34 | } |
| 35 | t.Logf("Testdata directory is located at %s.", foundDir) |
| 36 | |
| 37 | if e := filepath.Walk(foundDir, func(path string, info fs.FileInfo, _ error) error { |
| 38 | if info.IsDir() { |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | if !strings.HasSuffix(path, ".tokenizer.json") { |
| 43 | return nil |
| 44 | } |
| 45 | base := strings.Replace(path2.Base(filepath.ToSlash(path)), ".tokenizer.json", "", 1) |
| 46 | t.Run( |
| 47 | base, |
| 48 | func(t *testing.T) { |
| 49 | sourceFile := path |
| 50 | expectedFile := strings.Replace(path, ".tokenizer.json", ".parser.json", 1) |
| 51 | |
| 52 | t.Logf("Parsing %s and comparing with %s...", sourceFile, expectedFile) |
| 53 | |
| 54 | var input []tokenizer.Event |
| 55 | inputFh, err := os.Open(sourceFile) |
| 56 | if err != nil { |
| 57 | t.Fatalf("Failed to open test input: %s (%v)", sourceFile, err) |
| 58 | } |
| 59 | defer func() { |
| 60 | _ = inputFh.Close() |
| 61 | }() |
| 62 | inputDecoder := json.NewDecoder(inputFh) |
| 63 | if err := inputDecoder.Decode(&input); err != nil { |
| 64 | t.Skipf("Failed to decode test input: %s (%v)", sourceFile, err) |
| 65 | } |
| 66 | |
| 67 | parserInput := make(chan tokenizer.Event) |
| 68 | parserResult := parser.ParseResult{} |
| 69 | prefixes, downloads, packages := parser.Parse(parserInput) |
| 70 | readerDone := make(chan struct{}) |
| 71 | go func() { |
| 72 | for { |
| 73 | prefix, ok := <-prefixes |
| 74 | if !ok { |
| 75 | break |
| 76 | } |