| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package parser | 5 package parser |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "mojom/mojom_parser/mojom" | 8 "mojom/mojom_parser/mojom" |
| 9 "reflect" | 9 "reflect" |
| 10 "testing" | 10 "testing" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 // FakeFileProvider implements FileProvider. | 13 // FakeFileProvider implements FileProvider. |
| 14 type FakeFileProvider struct { | 14 type FakeFileProvider struct { |
| 15 // This field records the names of the files whose contents were request
ed. | 15 // This field records the names of the files whose contents were request
ed. |
| 16 requestedFileNames []string | 16 requestedFileNames []string |
| 17 } | 17 } |
| 18 | 18 |
| 19 // FakeFileProvider implements provideContents by recording the name of the file | 19 // FakeFileProvider implements provideContents by recording the name of the file |
| 20 // whose contents are being requested. | 20 // whose contents are being requested and then returning the empty string. |
| 21 func (f *FakeFileProvider) provideContents(fileRef *FileReference) (contents str
ing, fileReadError error) { | 21 func (f *FakeFileProvider) provideContents(fileRef *FileReference) (contents str
ing, fileReadError error) { |
| 22 f.requestedFileNames = append(f.requestedFileNames, fileRef.specifiedPat
h) | 22 f.requestedFileNames = append(f.requestedFileNames, fileRef.specifiedPat
h) |
| 23 return "", nil | 23 return "", nil |
| 24 } | 24 } |
| 25 | 25 |
| 26 // FakeFileProvider implements findFile by always succeeding to find the | 26 // FakeFileProvider implements findFile by always succeeding to find the |
| 27 // file and reporting that the |specifiedPath| is the |absolutePath|. | 27 // file and reporting that the |specifiedPath| is the |absolutePath|. |
| 28 func (f *FakeFileProvider) findFile(fileRef *FileReference) error { | 28 func (f *FakeFileProvider) findFile(fileRef *FileReference) error { |
| 29 fileRef.absolutePath = fileRef.specifiedPath | 29 fileRef.absolutePath = fileRef.specifiedPath |
| 30 return nil | 30 return nil |
| (...skipping 22 matching lines...) Expand all Loading... |
| 53 if imports != nil { | 53 if imports != nil { |
| 54 f.importNames[fileName] = append(f.importNames[fileName], import
s...) | 54 f.importNames[fileName] = append(f.importNames[fileName], import
s...) |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 | 57 |
| 58 // FakeFileExtractor implements extractMojomFile() by returning an instance | 58 // FakeFileExtractor implements extractMojomFile() by returning an instance |
| 59 // of MojomFile that has had imports added to it according to the values | 59 // of MojomFile that has had imports added to it according to the values |
| 60 // in the map |importNames|. | 60 // in the map |importNames|. |
| 61 func (f *FakeFileExtractor) extractMojomFile(parser *Parser) *mojom.MojomFile { | 61 func (f *FakeFileExtractor) extractMojomFile(parser *Parser) *mojom.MojomFile { |
| 62 file := parser.GetMojomFile() | 62 file := parser.GetMojomFile() |
| 63 » for _, importName := range f.importNames[file.FileName] { | 63 » for _, importName := range f.importNames[file.CanonicalFileName] { |
| 64 file.AddImport(importName) | 64 file.AddImport(importName) |
| 65 } | 65 } |
| 66 return file | 66 return file |
| 67 } | 67 } |
| 68 | 68 |
| 69 // TestExpectedFilesParsed tests the logic in function ParseDriver.ParseFiles() | 69 // TestExpectedFilesParsed tests the logic in function ParseDriver.ParseFiles() |
| 70 // for determining which files should be parsed. | 70 // for determining which files should be parsed. |
| 71 func TestExpectedFilesParsed(t *testing.T) { | 71 func TestExpectedFilesParsed(t *testing.T) { |
| 72 // Construct our fake objects. | 72 // Construct our fake objects. |
| 73 fakeFileProvider := FakeFileProvider{} | 73 fakeFileProvider := FakeFileProvider{} |
| 74 fakeFileExtractor := makeFakeFileExtractor() | 74 fakeFileExtractor := makeFakeFileExtractor() |
| 75 // Our fake file1 will import file3, file4, file5 | 75 // Our fake file1 will import file3, file4, file5 |
| 76 fakeFileExtractor.appendImportsToFile("file1", "file3", "file4", "file5"
) | 76 fakeFileExtractor.appendImportsToFile("file1", "file3", "file4", "file5"
) |
| 77 » // Our fake file5 will import file1 and file5 | 77 » // Our fake file5 will import file1 and file6 |
| 78 fakeFileExtractor.appendImportsToFile("file5", "file1", "file6") | 78 fakeFileExtractor.appendImportsToFile("file5", "file1", "file6") |
| 79 | 79 |
| 80 // Construct the driver under test | 80 // Construct the driver under test |
| 81 driver := newDriver([]string{}, false, &fakeFileProvider, &fakeFileExtra
ctor, NoOpParseInvoker(0)) | 81 driver := newDriver([]string{}, false, &fakeFileProvider, &fakeFileExtra
ctor, NoOpParseInvoker(0)) |
| 82 | 82 |
| 83 // Invoke ParseFiles | 83 // Invoke ParseFiles |
| 84 driver.ParseFiles([]string{"file1", "file2", "file3"}) | 84 driver.ParseFiles([]string{"file1", "file2", "file3"}) |
| 85 | 85 |
| 86 // Check that the correct files had their contents requested in the expe
cted order. | 86 // Check that the correct files had their contents requested in the expe
cted order. |
| 87 expectedFileRefs := []string{"file1", "file2", "file3", "file4", "file5"
, "file6"} | 87 expectedFileRefs := []string{"file1", "file2", "file3", "file4", "file5"
, "file6"} |
| 88 if !reflect.DeepEqual(expectedFileRefs, fakeFileProvider.requestedFileNa
mes) { | 88 if !reflect.DeepEqual(expectedFileRefs, fakeFileProvider.requestedFileNa
mes) { |
| 89 t.Errorf("%v != %v", expectedFileRefs, fakeFileProvider.requeste
dFileNames) | 89 t.Errorf("%v != %v", expectedFileRefs, fakeFileProvider.requeste
dFileNames) |
| 90 } | 90 } |
| 91 } | 91 } |
| OLD | NEW |