OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:async' show | |
6 Future; | |
7 | |
8 import 'dart:io' show | |
9 Directory, | |
10 File, | |
11 FileSystemEntity, | |
12 Platform; | |
13 | |
14 import 'dart:math' show | |
15 min; | |
16 | |
17 import 'package:path/path.dart' show join; | |
18 | |
19 import 'package:expect/expect.dart'; | |
20 import 'package:servicec/compiler.dart' as servicec; | |
21 import 'package:servicec/errors.dart' show | |
22 CompilationError, | |
23 ErrorTag, | |
24 compilerErrorTypes; | |
25 | |
26 import 'package:servicec/targets.dart' show | |
27 Target; | |
28 | |
29 import 'scanner_tests.dart' show | |
30 SCANNER_TESTS; | |
31 | |
32 import 'test.dart' show | |
33 Test; | |
34 | |
35 import 'camelize_test.dart' show | |
36 CamelizeTest; | |
37 | |
38 /// Absolute path to the build directory used by test.py. | |
39 const String buildDirectory = | |
40 const String.fromEnvironment('test.dart.build-dir'); | |
41 | |
42 /// Relative path to the directory containing input files. | |
43 const String filesDirectory = "tests/servicec/input_files"; | |
44 | |
45 /// Temporary directory for test output. | |
46 const String tempTestOutputDirectory = | |
47 const String.fromEnvironment("test.dart.temp-dir"); | |
48 | |
49 final String generatedDirectory = '$tempTestOutputDirectory/servicec_tests'; | |
50 | |
51 final String servicecDirectory = | |
52 const String.fromEnvironment('test.dart.servicec-dir'); | |
53 | |
54 final String resourcesDirectory = join(servicecDirectory, 'lib', 'src', | |
55 'resources'); | |
56 | |
57 class FileTest extends Test { | |
58 final Target target; | |
59 final String outputDirectory; | |
60 | |
61 FileTest(String name, {this.target: Target.ALL}) | |
62 : outputDirectory = "$generatedDirectory/$name", | |
63 super(name); | |
64 | |
65 Future perform() async { | |
66 String input = new File("$filesDirectory/$name.idl").readAsStringSync(); | |
67 List<ErrorTag> expectedErrors = extractExpectedErrors(input); | |
68 List<CompilationError> actualErrors = | |
69 (await servicec.compileInput(input, | |
70 name, | |
71 resourcesDirectory, | |
72 outputDirectory)).toList(); | |
73 | |
74 int length = min(expectedErrors.length, actualErrors.length); | |
75 for (int i = 0; i < length; ++i) { | |
76 Expect.equals(expectedErrors[i], actualErrors[i].tag); | |
77 } | |
78 Expect.equals(expectedErrors.length, actualErrors.length, | |
79 "Expected a different amount of errors"); | |
80 if (actualErrors.length == 0) { | |
81 try { | |
82 await checkOutputDirectoryStructure(outputDirectory, target); | |
83 } finally { | |
84 nukeDirectory(outputDirectory); | |
85 } | |
86 } | |
87 } | |
88 | |
89 List<ErrorTag> extractExpectedErrors(String input) { | |
90 List<ErrorTag> result = <ErrorTag>[]; | |
91 | |
92 List<String> lines = input.split("\n"); | |
93 for (String line in lines) { | |
94 List<String> split = line.split("//"); | |
95 if (split.length > 1) { | |
96 List<String> words = split[1].trim().split(" "); | |
97 if (words.length == 1) { | |
98 addWordIfError(words[0], result); | |
99 } | |
100 } | |
101 } | |
102 | |
103 return result; | |
104 } | |
105 | |
106 void addWordIfError(String word, List<ErrorTag> errors) { | |
107 ErrorTag error = compilerErrorTypes["ErrorTag.$word"]; | |
108 if (null != error) errors.add(error); | |
109 } | |
110 } | |
111 | |
112 // Helpers for Success. | |
113 | |
114 Future checkOutputDirectoryStructure(String outputDirectory, Target target) | |
115 async { | |
116 // If the root out dir does not exist there is no point in checking the | |
117 // children dirs. | |
118 await checkDirectoryExists(outputDirectory); | |
119 | |
120 if (target.includes(Target.JAVA)) { | |
121 await checkDirectoryExists(outputDirectory + '/java'); | |
122 } | |
123 if (target.includes(Target.CC)) { | |
124 await checkDirectoryExists(outputDirectory + '/cc'); | |
125 } | |
126 } | |
127 | |
128 Future checkDirectoryExists(String dirName) async { | |
129 var dir = new Directory(dirName); | |
130 Expect.isTrue(await dir.exists(), "Directory $dirName does not exist"); | |
131 } | |
132 | |
133 // TODO(stanm): Move cleanup logic to dartino_tests setup | |
134 Future nukeDirectory(String dirName) async { | |
135 var dir = new Directory(dirName); | |
136 await dir.delete(recursive: true); | |
137 } | |
138 | |
139 // Test entry point. | |
140 | |
141 typedef Future NoArgFuture(); | |
142 | |
143 Future<Map<String, NoArgFuture>> listTests() async { | |
144 var tests = <String, NoArgFuture>{}; | |
145 List<FileSystemEntity> files = new Directory(filesDirectory).listSync(); | |
146 for (File file in files) { | |
147 String filename = file.path.split("/").last; | |
148 String testname = filename.substring(0, filename.indexOf('.idl')); | |
149 tests['servicec/$testname'] = new FileTest(testname).perform; | |
150 } | |
151 | |
152 for (Test test in SCANNER_TESTS) { | |
153 tests['servicec/scanner/${test.name}'] = test.perform; | |
154 } | |
155 | |
156 tests['servicec/camelize'] = new CamelizeTest().perform; | |
157 return tests; | |
158 } | |
OLD | NEW |