diff options
author | Rocky Madden <git@rockymadden.com> | 2012-10-15 16:24:41 -0600 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2012-10-15 16:24:41 -0600 |
commit | 2085318fcd0f785630c3f2baad09f0d70b481cc6 (patch) | |
tree | a40a554397ea674d659d202d7188fa84c907630e /cli/source | |
parent | 488de0cd595a7a034d706f319ec7cdcacea6eaab (diff) | |
download | stringmetric-2085318fcd0f785630c3f2baad09f0d70b481cc6.tar.gz stringmetric-2085318fcd0f785630c3f2baad09f0d70b481cc6.tar.bz2 stringmetric-2085318fcd0f785630c3f2baad09f0d70b481cc6.zip |
Created SoundexMetric, spec, and command.
Diffstat (limited to 'cli/source')
-rwxr-xr-x | cli/source/core/scala/org/hashtree/stringmetric/cli/command/soundexMetric.scala | 52 | ||||
-rwxr-xr-x | cli/source/test/scala/org/hashtree/stringmetric/cli/command/soundexMetricSpec.scala | 39 |
2 files changed, 91 insertions, 0 deletions
diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/command/soundexMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/soundexMetric.scala new file mode 100755 index 0000000..6f6f9ec --- /dev/null +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/soundexMetric.scala @@ -0,0 +1,52 @@ +package org.hashtree.stringmetric.cli.command + +import org.hashtree.stringmetric.{ SoundexMetric, StringCleanerDelegate } +import org.hashtree.stringmetric.cli._ +import org.hashtree.stringmetric.cli.command._ + +/** + * The soundexMetric [[org.hashtree.stringmetric.cli.command.Command]]. Compares two strings to determine if they are + * pronounced similarly, per the Soundex phonetic algorithm. + */ +object soundexMetric extends Command { + override def main(args: Array[String]): Unit = { + val options = OptionMapUtility.toOptionMap(args) + + try { + // Help. + if (options.contains('h) || options.contains('help)) { + help() + exit(options) + // Execute. + } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 1) { + execute(options) + exit(options) + // Invalid syntax. + } else { + throw new IllegalArgumentException("Expected valid syntax. See --help.") + } + } catch { + case e => error(e)(options) + } + } + + override def help(): Unit = { + val ls = sys.props("line.separator") + val tab = " " + + println( + "Compares two strings to determine if they are pronounced similarly, per the Soundex phonetic algorithm." + ls + ls + + "Syntax:" + ls + + tab + "soundexMetric [Options] string1 string2..." + ls + ls + + "Options:" + ls + + tab + "-h, --help" + ls + + tab + tab + "Outputs description, syntax, and options." + ) + } + + override def execute(options: OptionMap): Unit = { + val strings = options('dashless).split(" ") + + println(SoundexMetric.compare(strings(0), strings(1))(new StringCleanerDelegate).toString) + } +}
\ No newline at end of file diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/command/soundexMetricSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/soundexMetricSpec.scala new file mode 100755 index 0000000..c71b1ea --- /dev/null +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/soundexMetricSpec.scala @@ -0,0 +1,39 @@ +package org.hashtree.stringmetric.cli.command + +import org.hashtree.stringmetric.ScalaTest +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class soundexMetricSpec extends ScalaTest { + "soundexMetric" should provide { + "main method" when passed { + "valid dashless arguments" should executes { + "print if they are a match" in { + val out = new java.io.ByteArrayOutputStream() + + Console.withOut(out)( + soundexMetric.main(Array("--unitTest", "--debug", "aBc", "abc")) + ) + + out.toString should equal ("true\n") + out.reset() + + Console.withOut(out)( + soundexMetric.main(Array("--unitTest", "--debug", "aBc", "xyz")) + ) + + out.toString should equal ("false\n") + out.reset() + } + } + "no dashless arguments" should throws { + "IllegalArgumentException" in { + evaluating { + soundexMetric.main(Array("--unitTest", "--debug")) + } should produce [IllegalArgumentException] + } + } + } + } +}
\ No newline at end of file |