PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/silk-core/src/test/scala/xerial/silk/util/OptionParserTest.scala

https://bitbucket.org/xerial/silk
Scala | 174 lines | 110 code | 39 blank | 25 comment | 0 complexity | 970276fe36cb486863f151c63538be49 MD5 | raw file
Possible License(s): Apache-2.0
  1. package xerial.silk
  2. package util
  3. /*
  4. * Copyright 2012 Taro L. Saito
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import org.junit.runner.RunWith
  19. import org.scalatest.junit.JUnitRunner
  20. import lens.ObjectSchema
  21. import xerial.silk.io.SilkTextWriter
  22. //--------------------------------------
  23. //
  24. // OptionParserTest.scala
  25. // Since: 2012/01/10 13:43
  26. //
  27. //--------------------------------------
  28. object OptionParserTest {
  29. private class Config(
  30. @option(symbol = "h", longName = "help", description = "display help messages")
  31. val displayHelp: Boolean = false,
  32. @option(symbol = "c", description = "compression level")
  33. val compressionLevel: Int = 3,
  34. @argument(description = "input files")
  35. val inputFile: Array[String] = Array.empty
  36. )
  37. class ArgConfig(
  38. @option(symbol = "h", longName = "help", description = "display help messages")
  39. val displayHelp: Boolean = false
  40. )
  41. }
  42. trait ConfigTrait {
  43. @option(symbol = "h", longName = "help", description = "display help messages")
  44. var displayHelp: Boolean = false
  45. @option(symbol = "c", description = "compression level")
  46. var compressionLevel: Int = 2
  47. @argument(description = "input files")
  48. var inputFile: Array[String] = Array.empty
  49. }
  50. class ValConfig extends ConfigTrait
  51. /**
  52. * @author leo
  53. */
  54. @RunWith(classOf[JUnitRunner])
  55. class OptionParserTest extends SilkSpec {
  56. import OptionParserTest._
  57. "OptionParser" should {
  58. "create option parsers" in {
  59. val p = OptionParser.of[Config]
  60. }
  61. "read option definitions from class definitions" in {
  62. val config: Config = OptionParser.parse[Config]("-h -c 10 file1 file2")
  63. config.displayHelp should be(true)
  64. config.compressionLevel should be(10)
  65. config.inputFile.size should be(2)
  66. config.inputFile(0) should be("file1")
  67. config.inputFile(1) should be("file2")
  68. }
  69. "create help messages" in {
  70. OptionParser.of[Config].printUsage
  71. }
  72. "detect option defined in extended trait" in {
  73. //pending
  74. val schema = ObjectSchema(classOf[ValConfig])
  75. val p = schema.parameters
  76. debug("params:%s", p.mkString(", "))
  77. p.length must be (3)
  78. val config = OptionParser.parse[ValConfig]("-h -c 3 f1 f2 f3")
  79. config.displayHelp should be(true)
  80. config.compressionLevel should be(3)
  81. config.inputFile.size should be(3)
  82. config.inputFile(0) should be("f1")
  83. config.inputFile(1) should be("f2")
  84. config.inputFile(2) should be("f3")
  85. }
  86. "detect option in constructor args" in {
  87. val config = OptionParser.parse[ArgConfig]("-h")
  88. config.displayHelp should be(true)
  89. }
  90. "be able to configure help message" in {
  91. val opt = OptionParser.of[Config]
  92. val usage = opt.createUsage()
  93. debug {
  94. usage
  95. }
  96. }
  97. }
  98. "TypeUtil" should {
  99. "detect types that can be created from buffer" in {
  100. import TypeUtil._
  101. canBuildFromBuffer(TypeUtil.toClassManifest(java.lang.Integer.TYPE)) must be (false)
  102. val a = Array.empty
  103. canBuildFromBuffer(a.getClass) must be (true)
  104. val b = Array("132", "23")
  105. canBuildFromBuffer(b.getClass) must be (true)
  106. class C(val a:Array[Int])
  107. val c = new C(Array(10))
  108. canBuildFromBuffer(c.a.getClass) must be (true)
  109. }
  110. "report an error when using inner classes" in {
  111. class A
  112. (
  113. @option(symbol = "h", longName = "help", description = "display help messages")
  114. val displayHelp: Boolean = true,
  115. @option(symbol = "c", description = "compression level")
  116. val compressionLevel: Int,
  117. @argument(description = "input files")
  118. val inputFile: Array[String] = Array.empty
  119. )
  120. intercept[IllegalArgumentException] {
  121. val v = TypeUtil.newInstance(classOf[A])
  122. }
  123. }
  124. }
  125. "CommandLine" should {
  126. "tokenize a single string into args" in {
  127. val args = CommandLineTokenizer.tokenize("""-c "hello world!" -f 3.432""")
  128. args.length must be(4)
  129. debug {
  130. args.mkString(", ")
  131. }
  132. args should equal(Array("-c", "hello world!", "-f", "3.432"))
  133. }
  134. }
  135. }