| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.deps; |
| 18 |
|
| 19 |
import com.google.common.collect.ImmutableList; |
| 20 |
import com.google.javascript.jscomp.ErrorManager; |
| 21 |
import com.google.javascript.jscomp.PrintStreamErrorManager; |
| 22 |
|
| 23 |
import junit.framework.TestCase; |
| 24 |
|
| 25 |
import java.util.Collections; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class JsFileParserTest extends TestCase { |
| 33 |
|
| 34 |
JsFileParser parser; |
| 35 |
private ErrorManager errorManager; |
| 36 |
|
| 37 |
private static final String SRC_PATH = "a"; |
| 38 |
private static final String CLOSURE_PATH = "b"; |
| 39 |
|
| 40 |
@Override |
| 41 |
public void setUp() { |
| 42 |
errorManager = new PrintStreamErrorManager(System.err); |
| 43 |
parser = new JsFileParser(errorManager); |
| 44 |
parser.setShortcutMode(true); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public void testParseFile() { |
| 54 |
String contents = "/*" |
| 55 |
+ "goog.provide('no1');*//*\n" |
| 56 |
+ "goog.provide('no2');\n" |
| 57 |
+ "*/goog.provide('yes1');\n" |
| 58 |
+ "/* blah */goog.provide(\"yes2\")/* blah*/\n" |
| 59 |
+ "goog.require('yes3'); // goog.provide('no3');\n" |
| 60 |
+ "// goog.provide('no4');\n" |
| 61 |
+ "goog.require(\"bar.data.SuperstarAddStarThreadActionRequestDelegate\"); " |
| 62 |
+ "//no new line at EOF"; |
| 63 |
|
| 64 |
DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH, |
| 65 |
ImmutableList.of("yes1", "yes2"), |
| 66 |
ImmutableList.of("yes3", "bar.data.SuperstarAddStarThreadActionRequestDelegate")); |
| 67 |
|
| 68 |
DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents); |
| 69 |
|
| 70 |
assertDeps(expected, result); |
| 71 |
} |
| 72 |
|
| 73 |
public void testMultiplePerLine() { |
| 74 |
String contents = "goog.provide('yes1');goog.provide('yes2');/*" |
| 75 |
+ "goog.provide('no1');*/goog.provide('yes3');//goog.provide('no2');"; |
| 76 |
|
| 77 |
DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH, |
| 78 |
ImmutableList.of("yes1", "yes2", "yes3"), Collections.<String>emptyList()); |
| 79 |
|
| 80 |
DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents); |
| 81 |
|
| 82 |
assertDeps(expected, result); |
| 83 |
} |
| 84 |
|
| 85 |
public void testShortcutMode1() { |
| 86 |
|
| 87 |
String contents = " // hi ! \n /* this is a comment */ " |
| 88 |
+ "goog.provide('yes1');\n /* and another comment */ \n" |
| 89 |
+ "goog.provide('yes2'); // include this\n" |
| 90 |
+ "function foo() {}\n" |
| 91 |
+ "goog.provide('no1');"; |
| 92 |
|
| 93 |
DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH, |
| 94 |
ImmutableList.of("yes1", "yes2"), Collections.<String>emptyList()); |
| 95 |
DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents); |
| 96 |
|
| 97 |
assertDeps(expected, result); |
| 98 |
} |
| 99 |
|
| 100 |
public void testShortcutMode2() { |
| 101 |
String contents = "/** goog.provide('no1'); \n" + |
| 102 |
" * goog.provide('no2');\n */\n" |
| 103 |
+ "goog.provide('yes1');\n"; |
| 104 |
|
| 105 |
DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH, |
| 106 |
ImmutableList.of("yes1"), Collections.<String>emptyList()); |
| 107 |
DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents); |
| 108 |
|
| 109 |
assertDeps(expected, result); |
| 110 |
} |
| 111 |
|
| 112 |
public void testShortcutMode3() { |
| 113 |
String contents = "/**\n" + |
| 114 |
" * goog.provide('no1');\n */\n" |
| 115 |
+ "goog.provide('yes1');\n"; |
| 116 |
|
| 117 |
DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH, |
| 118 |
ImmutableList.of("yes1"), Collections.<String>emptyList()); |
| 119 |
DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents); |
| 120 |
|
| 121 |
assertDeps(expected, result); |
| 122 |
} |
| 123 |
|
| 124 |
public void testIncludeGoog1() { |
| 125 |
String contents = "/**\n" + |
| 126 |
" * the first constant in base.js\n" + |
| 127 |
" */\n" + |
| 128 |
"var COMPILED = false;\n"; |
| 129 |
|
| 130 |
DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH, |
| 131 |
ImmutableList.of("goog"), Collections.<String>emptyList()); |
| 132 |
DependencyInfo result = parser.setIncludeGoogBase(true).parseFile( |
| 133 |
SRC_PATH, CLOSURE_PATH, contents); |
| 134 |
assertDeps(expected, result); |
| 135 |
} |
| 136 |
|
| 137 |
public void testIncludeGoog2() { |
| 138 |
String contents = "goog.require('bar');"; |
| 139 |
|
| 140 |
DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH, |
| 141 |
ImmutableList.<String>of(), ImmutableList.of("goog", "bar")); |
| 142 |
DependencyInfo result = parser.setIncludeGoogBase(true).parseFile( |
| 143 |
SRC_PATH, CLOSURE_PATH, contents); |
| 144 |
assertDeps(expected, result); |
| 145 |
} |
| 146 |
|
| 147 |
public void testIncludeGoog3() { |
| 148 |
|
| 149 |
String contents = "goog.provide('x');\n" + |
| 150 |
"/**\n" + |
| 151 |
" * the first constant in base.js\n" + |
| 152 |
" */\n" + |
| 153 |
"var COMPILED = false;\n"; |
| 154 |
|
| 155 |
DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH, |
| 156 |
ImmutableList.of("x"), ImmutableList.of("goog")); |
| 157 |
DependencyInfo result = parser.setIncludeGoogBase(true).parseFile( |
| 158 |
SRC_PATH, CLOSURE_PATH, contents); |
| 159 |
assertDeps(expected, result); |
| 160 |
} |
| 161 |
|
| 162 |
public void testIncludeGoog4() { |
| 163 |
String contents = "goog.addDependency('foo', [], []);\n"; |
| 164 |
|
| 165 |
DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH, |
| 166 |
ImmutableList.<String>of(), ImmutableList.of("goog")); |
| 167 |
DependencyInfo result = parser.setIncludeGoogBase(true).parseFile( |
| 168 |
SRC_PATH, CLOSURE_PATH, contents); |
| 169 |
assertDeps(expected, result); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
private void assertDeps(DependencyInfo expected, DependencyInfo actual) { |
| 174 |
assertEquals(expected, actual); |
| 175 |
assertEquals(0, errorManager.getErrorCount()); |
| 176 |
assertEquals(0, errorManager.getWarningCount()); |
| 177 |
} |
| 178 |
} |