Browse Source

Fixed MeasureUnit guess function base condition and improved test coverage.

Thomas Flucke 2 năm trước cách đây
mục cha
commit
14b0eedea4

+ 9 - 8
shared/shared/src/main/scala/com/weEat/shared/models/MeasureUnit.scala

@@ -32,22 +32,22 @@ sealed trait MeasureUnit {
     case (MASS, VOLUME) => food.density match {
       case Some(density) => VOLUME.defaultUnit
           .convert(food, amount / density, to)
-      case None => Failure(new IncompleteDataException("density"))
+      case None => Failure(new IncompleteDataException(food, "density"))
     }
     case (MASS, NUMBER) => food.massPerUnit match {
       case Some(massPerUnit) => NUMBER.defaultUnit
           .convert(food, amount / massPerUnit, to)
-      case None => Failure(new IncompleteDataException("mass/unit"))
+      case None => Failure(new IncompleteDataException(food, "mass/unit"))
     }
     case (VOLUME, _) => food.density match {
       case Some(density) => MASS.defaultUnit
           .convert(food, amount * density, to)
-      case None => Failure(new IncompleteDataException("density"))
+      case None => Failure(new IncompleteDataException(food, "density"))
     }
     case (NUMBER, _) => food.massPerUnit match {
       case Some(massPerUnit) => MASS.defaultUnit.
           convert(food, amount * massPerUnit, to)
-      case None => Failure(new IncompleteDataException("mass/unit"))
+      case None => Failure(new IncompleteDataException(food, "mass/unit"))
     }
   }).map(_ * conversionRatio)
 }
@@ -70,7 +70,9 @@ object MeasureUnit {
     GallonUS,
     // DryPintUS,
     // DryQuartUS,
-    Dozen
+    Dozen,
+    InCube,
+    CmCube
   )
 
   def fromString(str: String) = units.find(_.abr == str)
@@ -78,10 +80,8 @@ object MeasureUnit {
   def apply(i: Int) = units(i)
 
   private def matchDegree(haystack: String)(needle: String) = {
-    val startsWithNeedle = s"$needle[^\\s]".r
-    val containsNeedle = s"[^\\s]$needle[^\\s]".r
     if (needle == haystack || haystack.startsWith(s"1 $needle") || haystack.startsWith(s"${needle}s")) Some(100000)
-    if (needle.size > 1 && haystack.startsWith(s"${needle}")) Some(10000 - Math.abs(haystack.length - needle.length))
+    else if (needle.size > 1 && haystack.startsWith(s"${needle}")) Some(10000 - Math.abs(haystack.length - needle.length))
     // else if (containsNeedle.findFirstIn(haystack).nonEmpty)
     //   Some(10000 - Math.abs(haystack.length - needle.length))
     else None
@@ -235,6 +235,7 @@ object InCube extends MeasureUnit {
   override val conversionRatio = 16.38706
   override val name = "Cubic inch"
   override val abr = "in^3"
+  override val altNames: Seq[String] = Seq("cubic in")
 }
 
 object FluidOunceUS extends MeasureUnit {

+ 3 - 0
shared/shared/src/test/scala/com/weEat/shared/models/MeasureUnitTest.scala

@@ -20,6 +20,9 @@ class MeasureUnitTest() extends WordSpec with MustMatchers {
         ("Teaspoons" -> TeaspoonUS),
         ("Whole" -> Count),
         ("individual school container" -> Count),
+        ("1 cup" -> CupUS),
+        ("1 fl oz" -> FluidOunceUS),
+        ("1 cubic inch" -> InCube),
       )
 
       for ((str, unit) <- units)

+ 0 - 1
webClient/src/main/scala/com/weEat/views/UsdaImporter.scala

@@ -108,7 +108,6 @@ object UsdaImporter extends View[Option[String]] {
       PaginatedTable[SearchResultFood](Seq(
         ("", 1, { (x) => button(cls := "btn btn-light",
           onClick --> {(e: Event) =>
-            // defaultUnit = if (num().nonEmpty) NUMBER else MASS
             val editor = USDAEditor(USDANodeNoId.fromSearchResult(x), true)
             Overlay.confirm(editor.render) { () =>
               import com.weEat.Main.headers