| OLD | NEW |
| (Empty) |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import io | |
| 6 import os.path | |
| 7 import unittest | |
| 8 | |
| 9 from fakes import FakeMojomFile | |
| 10 from fetcher.mojom_directory import MojomDirectory | |
| 11 from fetcher.repository import Repository | |
| 12 from mojom_gn import BuildGNGenerator | |
| 13 | |
| 14 class FakeRepository(Repository): | |
| 15 def get_all_external_mojom_directories(self): | |
| 16 mojom = FakeMojomFile( | |
| 17 self, os.path.join(self.get_external_directory(), | |
| 18 "domokit.org/bar/baz/foo.mojom")) | |
| 19 mojom.add_dependency("example.com/dir/example.mojom") | |
| 20 mojom.add_dependency("example.com/dir/dir.mojom") | |
| 21 mojom.add_dependency("buzz.mojom") | |
| 22 mojom.add_dependency("foo/bar.mojom") | |
| 23 mojom.add_dependency( | |
| 24 "mojo/public/interfaces/application/shell.mojom") | |
| 25 directory = MojomDirectory( | |
| 26 os.path.join(self.get_external_directory(), | |
| 27 "domokit.org/bar/baz")) | |
| 28 directory.add_mojom(mojom) | |
| 29 return [directory] | |
| 30 | |
| 31 | |
| 32 class FakeBuildGNGenerator(BuildGNGenerator): | |
| 33 def __init__(self, *args, **kwargs): | |
| 34 self.opened_files = {} | |
| 35 BuildGNGenerator.__init__(self, *args, **kwargs) | |
| 36 | |
| 37 def _open(self, filepath, mode): | |
| 38 if mode != "w": | |
| 39 raise Exception("Invalid mode " + str(mode)) | |
| 40 self.opened_files[filepath] = io.StringIO() | |
| 41 return self.opened_files[filepath] | |
| 42 | |
| 43 | |
| 44 class TestBuildGNGenerator(unittest.TestCase): | |
| 45 BAZ_BUILD_GN = u"""import("//build/module_args/mojo.gni") | |
| 46 import("$mojo_sdk_root/mojo/public/tools/bindings/mojom.gni") | |
| 47 | |
| 48 mojom("baz") { | |
| 49 deps = [ | |
| 50 ":foo", | |
| 51 ] | |
| 52 } | |
| 53 | |
| 54 mojom("foo") { | |
| 55 sources = [ | |
| 56 "foo.mojom", | |
| 57 ] | |
| 58 import_dirs = [ | |
| 59 get_path_info("..", "abspath"), | |
| 60 ] | |
| 61 mojo_sdk_deps = [ | |
| 62 "mojo/public/interfaces/application", | |
| 63 ] | |
| 64 deps = [ | |
| 65 "//third_party/external/example.com/dir:example", | |
| 66 "//third_party/external/example.com/dir:dir_mojom", | |
| 67 ":buzz", | |
| 68 "../foo:bar", | |
| 69 ] | |
| 70 }""" | |
| 71 def test_generate(self): | |
| 72 self.maxDiff = None | |
| 73 repository = FakeRepository("/base/repo", "third_party/external") | |
| 74 gn_generator = FakeBuildGNGenerator(repository, os.path.abspath( | |
| 75 os.path.join(os.path.dirname(__file__), | |
| 76 "../../../public/tools/mojom_fetcher"))) | |
| 77 gn_generator.generate() | |
| 78 output_stream = gn_generator.opened_files[ | |
| 79 "/base/repo/third_party/external/domokit.org/bar/baz/BUILD.gn"] | |
| 80 self.assertEquals(prepare_string(self.BAZ_BUILD_GN), | |
| 81 prepare_string(output_stream.getvalue())) | |
| 82 | |
| 83 def prepare_string(value): | |
| 84 lines = value.split("\n") | |
| 85 lines = map(lambda l: l.strip().replace(" ", ""), lines) | |
| 86 lines = filter(lambda l: not l.startswith("#"), lines) | |
| 87 return ''.join(lines) | |
| 88 | |
| OLD | NEW |