Skip to content

Commit 2b8baea

Browse files
committed
Nicer exception handling and testing
1 parent a9750e2 commit 2b8baea

File tree

7 files changed

+29
-22
lines changed

7 files changed

+29
-22
lines changed

spaghetti-core/src/main/groovy/com/prezi/spaghetti/ast/parser/AstParserException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ public AstParserException(ModuleDefinitionSource source, String message) {
88
}
99

1010
public AstParserException(ModuleDefinitionSource source, String message, Throwable cause) {
11-
super("Parse error in " + source.getLocation() + " " + message, cause);
11+
super("Parse error in " + source.getLocation() + message, cause);
1212
}
1313
}

spaghetti-core/src/main/groovy/com/prezi/spaghetti/ast/parser/InternalAstParserException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private InternalAstParserException(Token start, Token stop, String message, Thro
1515

1616
private static String createMessage(Token start, Token stop, String message)
1717
{
18-
return "at line " + start.getLine() + ":" + start.getCharPositionInLine() + ": " + message;
18+
return " at line " + start.getLine() + ":" + start.getCharPositionInLine() + ": " + message;
1919
}
2020

2121
public InternalAstParserException(Token token, String message, Throwable cause)

spaghetti-core/src/main/groovy/com/prezi/spaghetti/internal/DependencyTreeResolver.groovy

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ class DependencyTreeResolver {
1414
[ module, dependencies.toSet() ]
1515
}
1616

17+
// Check for non-existent modules
18+
remainingModules.each { module, dependencies ->
19+
dependencies.each {
20+
if (!remainingModules.containsKey(it)) {
21+
throw new IllegalArgumentException("Module found: ${it} (dependency of module ${module})")
22+
}
23+
}
24+
}
25+
1726
Map<M, I> moduleInstances = [:]
1827

1928
while (remainingModules) {

spaghetti-core/src/test/groovy/com/prezi/spaghetti/ast/FQNameTest.groovy

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class FQNameTest extends Specification {
1111
FQName.fromString(what)
1212

1313
then:
14-
thrown IllegalArgumentException
14+
def ex = thrown IllegalArgumentException
15+
ex.message == "Qualified name cannot be empty"
1516

1617
where:
1718
what | _

spaghetti-core/src/test/groovy/com/prezi/spaghetti/ast/internal/NodeSetTest.groovy

+9-14
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,18 @@ class NodeSetTest extends Specification {
7878
def "Duplicate: #value (#key)"() {
7979
set.add value
8080

81-
def exception = null
82-
try {
83-
set.add value
84-
assert false
85-
} catch (InternalAstParserException ex) {
86-
exception = ex
87-
}
81+
when:
82+
set.add value
8883

89-
expect:
90-
exception instanceof InternalAstParserException
91-
exception.message == "Test with the same name already exists: " + key
84+
then:
85+
def ex = thrown InternalAstParserException
86+
ex.message == message
9287

9388
where:
94-
key | value | set
95-
"" | new TestNode("") | new DefaultNamedNodeSet<TestNode>("test")
96-
"lajos" | new TestNode("lajos") | new DefaultNamedNodeSet<TestNode>("test")
97-
"alma.lajos" | new TestQNode(qn("alma.lajos")) | new DefaultQualifiedNodeSet<TestQNode>("test")
89+
message | value | set
90+
"Test with the same name already exists: " | new TestNode("") | new DefaultNamedNodeSet<TestNode>("test")
91+
"Test with the same name already exists: lajos" | new TestNode("lajos") | new DefaultNamedNodeSet<TestNode>("test")
92+
"Test with the same name already exists: alma.lajos" | new TestQNode(qn("alma.lajos")) | new DefaultQualifiedNodeSet<TestQNode>("test")
9893
}
9994

10095

spaghetti-core/src/test/groovy/com/prezi/spaghetti/bundle/ModuleBundleTest.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ModuleBundleTest extends Specification {
5959
then:
6060
1 * source.hasFile("META-INF/MANIFEST.MF") >> false
6161
0 * _
62-
IllegalArgumentException ex = thrown()
62+
def ex = thrown IllegalArgumentException
6363
ex.message.contains "Not a module, missing manifest"
6464
}
6565

spaghetti-core/src/test/groovy/com/prezi/spaghetti/packaging/DependencyTreeResolverTest.groovy

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class DependencyTreeResolverTest extends Specification {
4242

4343
then:
4444
0 * _
45-
thrown IllegalStateException
45+
def ex = thrown IllegalStateException
46+
ex.message == "Cyclic dependency detected among modules: [a, b, c]"
4647
}
4748

4849
def "cyclic dependency 2"() {
@@ -58,7 +59,8 @@ class DependencyTreeResolverTest extends Specification {
5859
then:
5960
1 * processor.processDependency("b", [])
6061
0 * _
61-
thrown IllegalStateException
62+
def ex = thrown IllegalStateException
63+
ex.message == "Cyclic dependency detected among modules: [a, c]"
6264
}
6365

6466
def "non-existent module"() {
@@ -72,8 +74,8 @@ class DependencyTreeResolverTest extends Specification {
7274
], processor)
7375

7476
then:
75-
1 * processor.processDependency("b", [])
7677
0 * _
77-
thrown IllegalStateException
78+
def ex = thrown IllegalArgumentException
79+
ex.message == "Module found: d (dependency of module a)"
7880
}
7981
}

0 commit comments

Comments
 (0)