From dfa1b11cf4d2269f5661763b2b8e0298f5216a55 Mon Sep 17 00:00:00 2001 From: Rocky Madden Date: Tue, 16 Oct 2012 17:33:23 -0600 Subject: Created LevenshteinMetric, spec, and command. --- .../cli/command/levenshteinMetric.scala | 57 ++++++++++++++++++++++ .../cli/command/levenshteinMetricSpec.scala | 39 +++++++++++++++ 2 files changed, 96 insertions(+) create mode 100755 cli/source/core/scala/org/hashtree/stringmetric/cli/command/levenshteinMetric.scala create mode 100755 cli/source/test/scala/org/hashtree/stringmetric/cli/command/levenshteinMetricSpec.scala (limited to 'cli/source') diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/command/levenshteinMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/levenshteinMetric.scala new file mode 100755 index 0000000..5a2d2d6 --- /dev/null +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/levenshteinMetric.scala @@ -0,0 +1,57 @@ +package org.hashtree.stringmetric.cli.command + +import org.hashtree.stringmetric.{ CaseStringCleaner, LevenshteinMetric, StringCleanerDelegate } +import org.hashtree.stringmetric.cli._ +import org.hashtree.stringmetric.cli.command._ + +/** + * The levenshteinMetric [[org.hashtree.stringmetric.cli.command.Command]]. Compares the number of characters that two + * strings are different from one another via insertion, deletion, and substitution. + */ +object levenshteinMetric 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 the number of characters that two strings are different from one another via insertion, deletion, " + + "and substitution." + ls + ls + + "Syntax:" + ls + + tab + "levenshteinMetric [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( + LevenshteinMetric.compare(strings(0), + strings(1))(new StringCleanerDelegate with CaseStringCleaner + ).getOrElse("not comparable").toString + ) + } +} \ No newline at end of file diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/command/levenshteinMetricSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/levenshteinMetricSpec.scala new file mode 100755 index 0000000..77d7187 --- /dev/null +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/levenshteinMetricSpec.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 levenshteinMetricSpec extends ScalaTest { + "levenshteinMetric" 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)( + levenshteinMetric.main(Array("--unitTest", "--debug", "aBc", "abc")) + ) + + out.toString should equal ("0\n") + out.reset() + + Console.withOut(out)( + levenshteinMetric.main(Array("--unitTest", "--debug", "aBc", "xyz")) + ) + + out.toString should equal ("3\n") + out.reset() + } + } + "no dashless arguments" should throws { + "IllegalArgumentException" in { + evaluating { + levenshteinMetric.main(Array("--unitTest", "--debug")) + } should produce [IllegalArgumentException] + } + } + } + } +} \ No newline at end of file -- cgit v1.2.3