-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
72 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 26 additions & 62 deletions
88
src/test/scala/tscfg/generators/java/JavaGeneratorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,46 @@ | ||
package tscfg.generators.java | ||
|
||
import com.typesafe.config.ConfigFactory | ||
import org.specs2.mutable.Specification | ||
import tscfg._ | ||
import tscfg.generators.{GenOpts, GenResult, Generator} | ||
|
||
import tscfg.generators.{GenOpts, GenResult} | ||
import tscfg.specs.types.{INTEGER, STRING} | ||
import tscfg.specs.{AtomicSpec, ObjSpec} | ||
|
||
class JavaGeneratorSpec extends Specification { | ||
|
||
def generate(configString: String, showOutput: Boolean = false) | ||
(implicit genOpts: GenOpts): GenResult = { | ||
|
||
val config = ConfigFactory.parseString(configString).resolve() | ||
|
||
val objSpec = new SpecBuilder(Key(genOpts.className)).fromConfig(config) | ||
//println("\nobjSpec:\n |" + objSpec.format().replaceAll("\n", "\n |")) | ||
|
||
val generator: Generator = new JavaGenerator(genOpts) | ||
|
||
val results = generator.generate(objSpec) | ||
private val genOpts = GenOpts( | ||
packageName = "pkg", | ||
className = "CFG") | ||
|
||
if (showOutput) | ||
println("Output:\n " + results.code.replace("\n", "\n ")) | ||
private val showOutput = false | ||
|
||
private def generate(objSpec: ObjSpec): GenResult = { | ||
val results = new JavaGenerator(genOpts).generate(objSpec) | ||
if (showOutput) println("Output:\n " + results.code.replace("\n", "\n ")) | ||
results | ||
} | ||
|
||
implicit val genOpts = GenOpts( | ||
language = "java", | ||
packageName = "pkg", | ||
className = "CFG") | ||
|
||
"with good input" should { | ||
val result = generate( | ||
""" | ||
|foo { | ||
| bar = string | ||
| baz = int | ||
|} | ||
""".stripMargin) | ||
"with direct ObjSpec" should { | ||
val objSpec = ObjSpec("foo", | ||
"bar" → AtomicSpec(STRING), | ||
"baz" → AtomicSpec(INTEGER) | ||
) | ||
val result = generate(objSpec) | ||
//println(result.code) | ||
|
||
"generate expected classes " in { | ||
result.classNames must contain("CFG", "Foo") | ||
result.classNames.size must beEqualTo(2) | ||
} | ||
|
||
"generate expected fields" in { | ||
result.fieldNames must contain("foo", "bar", "baz") | ||
result.fieldNames.size must beEqualTo(3) | ||
result.classNames === Set("Foo") | ||
result.fieldNames === Set("bar", "baz") | ||
} | ||
} | ||
|
||
"with empty input" should { | ||
val results = generate("") | ||
"only generate CFG class" in { | ||
results.classNames must contain("CFG") | ||
results.classNames.size must beEqualTo(1) | ||
results.fieldNames must beEmpty | ||
} | ||
} | ||
"with direct empty ObjSpec" should { | ||
val objSpec = ObjSpec("foo") | ||
val result = generate(objSpec) | ||
//println(result.code) | ||
|
||
"handle issue14" should { | ||
val result = generate( | ||
""" | ||
|0 = { | ||
| 1 = bar | ||
|} | ||
|2 = foo | ||
""".stripMargin) | ||
|
||
"generate expected class " in { | ||
result.classNames must contain("CFG", "_0") | ||
result.classNames.size must beEqualTo(2) | ||
} | ||
|
||
"generate expected fields" in { | ||
result.fieldNames must contain("_0", "_1", "_2") | ||
result.fieldNames.size must beEqualTo(3) | ||
"generate expected classes " in { | ||
result.classNames === Set("Foo") | ||
result.fieldNames === Set.empty | ||
} | ||
} | ||
} |
87 changes: 26 additions & 61 deletions
87
src/test/scala/tscfg/generators/scala/ScalaGeneratorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,46 @@ | ||
package tscfg.generators.scala | ||
|
||
import com.typesafe.config.ConfigFactory | ||
import org.specs2.mutable.Specification | ||
import tscfg.{Key, SpecBuilder} | ||
import tscfg.generators.{GenOpts, GenResult, Generator} | ||
import tscfg.generators.{GenOpts, GenResult} | ||
import tscfg.specs.types.{INTEGER, STRING} | ||
import tscfg.specs.{AtomicSpec, ObjSpec} | ||
|
||
class ScalaGeneratorSpec extends Specification { | ||
|
||
def generate(configString: String, showOutput: Boolean = false) | ||
(implicit genOpts: GenOpts): GenResult = { | ||
|
||
val config = ConfigFactory.parseString(configString).resolve() | ||
|
||
val objSpec = new SpecBuilder(Key(genOpts.className)).fromConfig(config) | ||
//println("\nobjSpec:\n |" + objSpec.format().replaceAll("\n", "\n |")) | ||
|
||
val generator: Generator = new ScalaGenerator(genOpts) | ||
|
||
val results = generator.generate(objSpec) | ||
private val genOpts = GenOpts( | ||
packageName = "pkg", | ||
className = "CFG") | ||
|
||
if (showOutput) | ||
println("Output:\n " + results.code.replace("\n", "\n ")) | ||
private val showOutput = false | ||
|
||
private def generate(objSpec: ObjSpec): GenResult = { | ||
val results = new ScalaGenerator(genOpts).generate(objSpec) | ||
if (showOutput) println("Output:\n " + results.code.replace("\n", "\n ")) | ||
results | ||
} | ||
|
||
implicit val genOpts = GenOpts( | ||
language = "scala", | ||
packageName = "pkg", | ||
className = "CFG") | ||
|
||
"with good input" should { | ||
val result = generate( | ||
""" | ||
|foo { | ||
| bar = string | ||
| baz = int | ||
|} | ||
""".stripMargin) | ||
"with direct ObjSpec" should { | ||
val objSpec = ObjSpec("foo", | ||
"bar" → AtomicSpec(STRING), | ||
"baz" → AtomicSpec(INTEGER) | ||
) | ||
val result = generate(objSpec) | ||
//println(result.code) | ||
|
||
"generate expected classes " in { | ||
result.classNames must contain("CFG", "Foo") | ||
result.classNames.size must beEqualTo(2) | ||
} | ||
|
||
"generate expected fields" in { | ||
result.fieldNames must contain("foo", "bar", "baz") | ||
result.fieldNames.size must beEqualTo(3) | ||
} | ||
} | ||
|
||
"with empty input" should { | ||
val results = generate("") | ||
"only generate CFG class" in { | ||
results.classNames must contain("CFG") | ||
results.classNames.size must beEqualTo(1) | ||
results.fieldNames must beEmpty | ||
result.classNames === Set("Foo") | ||
result.fieldNames === Set("bar", "baz") | ||
} | ||
} | ||
|
||
"handle issue14" should { | ||
val result = generate( | ||
""" | ||
|0 = { | ||
| 1 = bar | ||
|} | ||
|2 = foo | ||
""".stripMargin) | ||
|
||
"generate expected class " in { | ||
result.classNames must contain("CFG", "_0") | ||
result.classNames.size must beEqualTo(2) | ||
} | ||
"with direct empty ObjSpec" should { | ||
val objSpec = ObjSpec("foo") | ||
val result = generate(objSpec) | ||
//println(result.code) | ||
|
||
"generate expected fields" in { | ||
result.fieldNames must contain("_0", "_1", "_2") | ||
result.fieldNames.size must beEqualTo(3) | ||
"generate expected classes " in { | ||
result.classNames === Set("Foo") | ||
result.fieldNames === Set.empty | ||
} | ||
} | ||
} |