Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance Gatlin committed May 1, 2016
1 parent e11d022 commit d238dc8
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 34 deletions.
8 changes: 5 additions & 3 deletions src/main/scala/s_mach/i18n/messages/Message.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package s_mach.i18n.messages

import s_mach.i18n.I18NConfig

/**
* A base trait for a message that has a message key
*/
Expand All @@ -28,9 +30,9 @@ trait Message {
/**
* Throw IllegalArgumentException if the message key is missing
*/
def throwIfMissing()(implicit m:Messages) : this.type = {
if(m.contains(key) == false) {
throw new IllegalArgumentException(s"Messages missing key $key")
def throwIfMissing()(implicit i18ncfg:I18NConfig) : this.type = {
if(i18ncfg.messages.contains(key) == false) {
throw new NoSuchElementException(s"Messages missing key $key")
} else {
this
}
Expand Down
24 changes: 23 additions & 1 deletion src/test/resources/conf/messages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,26 @@ m_test=this is a test message format that accepts two arguments {0} and {1}
test_key=testvalue
fmt_test1_key=hello {0} test {1} '{2}'
fmt_test2_key=When {1,time} on {1,date}, there will be {2} on moon {0,number,integer}
fmt_choice_key=There {0,choice,0#are no apples|1#is one apple|1<are {0,number,integer} apples}.
fmt_choice_key=There {0,choice,0#are no apples|1#is one apple|1<are {0,number,integer} apples}.
m_test1=test {0}
m_test2=test {0} {1}
m_test3=test {0} {1} {2}
m_test4=test {0} {1} {2} {3}
m_test5=test {0} {1} {2} {3} {4}
m_test6=test {0} {1} {2} {3} {4} {5}
m_test7=test {0} {1} {2} {3} {4} {5} {6}
m_test8=test {0} {1} {2} {3} {4} {5} {6} {7}
m_test9=test {0} {1} {2} {3} {4} {5} {6} {7} {8}
m_test10=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9}
m_test11=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}
m_test12=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11}
m_test13=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12}
m_test14=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13}
m_test15=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14}
m_test16=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15}
m_test17=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16}
m_test18=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17}
m_test19=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18}
m_test20=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {19}
m_test21=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {19} {20}
m_test22=test {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {19} {20} {21}
44 changes: 44 additions & 0 deletions src/test/scala/s_mach/i18n/codegen/MessageNTestCodeGen.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package s_mach.i18n.codegen

import Header.header
object MessageNTestCodeGen {
def gen(n: Int) = {
s"""
"Message$n.apply" should "resolve interpolation" in {
val m_test$n = 'm_test$n.m[${Seq.fill(n)("Int").mkString(",")}]
m_test$n(${(1 to n).mkString(",")}) should equal ("test ${(1 to n).mkString(" ")}")
}
"""
}


def genMessage(n: Int) = {
s"m_test$n=test ${(0 until n).map(i => s"{$i}").mkString(" ")}"
}

def genToFile(path: String) : Unit = {

val contents =
s"""$header
package s_mach.i18n.test

import java.util.Locale
import org.scalatest.{FlatSpec, Matchers}
import s_mach.i18n._
import s_mach.i18n.messages._

/* WARNING: Generated code. To modify see s_mach.i18n.codegen.MessageNTestCodeGen */

class MessageNTest extends FlatSpec with Matchers {
implicit val i18ncfg = I18NConfig(Locale.ENGLISH)

${(1 to 22).map(i => gen(i)).mkString("\n")}
}
"""

import java.io._
val out = new PrintWriter(new BufferedWriter(new FileWriter(path)))
out.println(contents)
out.close()
}
}
2 changes: 1 addition & 1 deletion src/test/scala/s_mach/i18n/test/CommonTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object CommonTest {
import Interpolation.Part._

locale match {
case l if l == Locale.US =>
case l if l == Locale.ENGLISH =>
Messages(
locale = locale,
m_hello.key -> m_hello_us_value,
Expand Down
8 changes: 4 additions & 4 deletions src/test/scala/s_mach/i18n/test/I18NTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class I18NTest extends FlatSpec with Matchers {


"I18N[Number]" should "format numbers correctly using the US locale" in {
implicit val cfg = I18NConfig(UTF8Messages(Locale.US))
implicit val cfg = I18NConfig(UTF8Messages(Locale.ENGLISH))

1000.i18n should equal(I18NString("1,000"))
(-1000).i18n should equal(I18NString("-1,000"))
Expand Down Expand Up @@ -74,13 +74,13 @@ class I18NTest extends FlatSpec with Matchers {
}

"I18N[I18NString]" should "return the same value" in {
implicit val cfg = I18NConfig(UTF8Messages(Locale.US))
implicit val cfg = I18NConfig(UTF8Messages(Locale.ENGLISH))

I18NString("test").asI18N should equal(I18NString("test"))
}

"I18N[Boolean]" should "format boolean values correctly using the US locale messages" in {
implicit val cfg = I18NConfig(UTF8Messages(Locale.US))
implicit val cfg = I18NConfig(UTF8Messages(Locale.ENGLISH))

true.i18n should equal("true")
false.i18n should equal("false")
Expand All @@ -99,7 +99,7 @@ class I18NTest extends FlatSpec with Matchers {
val b = false

{
implicit val cfg = I18NConfig(UTF8Messages(Locale.US))
implicit val cfg = I18NConfig(UTF8Messages(Locale.ENGLISH))
i18n"$b test $qty ${name.asI18N}" should equal("false test 10,000.1 Gary")
}
{
Expand Down
164 changes: 164 additions & 0 deletions src/test/scala/s_mach/i18n/test/MessageNTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
,i::,
:;;;;;;;
;:,,::;.
1ft1;::;1tL
t1;::;1,
:;::; _____ __ ___ __
fCLff ;:: tfLLC / ___/ / |/ /____ _ _____ / /_
CLft11 :,, i1tffLi \__ \ ____ / /|_/ // __ `// ___// __ \
1t1i .;; .1tf ___/ //___// / / // /_/ // /__ / / / /
CLt1i :,: .1tfL. /____/ /_/ /_/ \__,_/ \___//_/ /_/
Lft1,:;: , 1tfL:
;it1i ,,,:::;;;::1tti s_mach.i18n
.t1i .,::;;; ;1tt Copyright (c) 2016 S-Mach, Inc.
Lft11ii;::;ii1tfL: Author: lance.gatlin@gmail.com
.L1 1tt1ttt,,Li
...1LLLL...
*/
package s_mach.i18n.test

import java.util.Locale
import org.scalatest.{FlatSpec, Matchers}
import s_mach.i18n._
import s_mach.i18n.messages._

/* WARNING: Generated code. To modify see s_mach.i18n.codegen.MessageNTestCodeGen */

class MessageNTest extends FlatSpec with Matchers {
implicit val i18ncfg = I18NConfig(Locale.ENGLISH)


"Message1.apply" should "resolve interpolation" in {
val m_test1 = 'm_test1.m[Int]
m_test1(1) should equal ("test 1")
}


"Message2.apply" should "resolve interpolation" in {
val m_test2 = 'm_test2.m[Int,Int]
m_test2(1,2) should equal ("test 1 2")
}


"Message3.apply" should "resolve interpolation" in {
val m_test3 = 'm_test3.m[Int,Int,Int]
m_test3(1,2,3) should equal ("test 1 2 3")
}


"Message4.apply" should "resolve interpolation" in {
val m_test4 = 'm_test4.m[Int,Int,Int,Int]
m_test4(1,2,3,4) should equal ("test 1 2 3 4")
}


"Message5.apply" should "resolve interpolation" in {
val m_test5 = 'm_test5.m[Int,Int,Int,Int,Int]
m_test5(1,2,3,4,5) should equal ("test 1 2 3 4 5")
}


"Message6.apply" should "resolve interpolation" in {
val m_test6 = 'm_test6.m[Int,Int,Int,Int,Int,Int]
m_test6(1,2,3,4,5,6) should equal ("test 1 2 3 4 5 6")
}


"Message7.apply" should "resolve interpolation" in {
val m_test7 = 'm_test7.m[Int,Int,Int,Int,Int,Int,Int]
m_test7(1,2,3,4,5,6,7) should equal ("test 1 2 3 4 5 6 7")
}


"Message8.apply" should "resolve interpolation" in {
val m_test8 = 'm_test8.m[Int,Int,Int,Int,Int,Int,Int,Int]
m_test8(1,2,3,4,5,6,7,8) should equal ("test 1 2 3 4 5 6 7 8")
}


"Message9.apply" should "resolve interpolation" in {
val m_test9 = 'm_test9.m[Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test9(1,2,3,4,5,6,7,8,9) should equal ("test 1 2 3 4 5 6 7 8 9")
}


"Message10.apply" should "resolve interpolation" in {
val m_test10 = 'm_test10.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test10(1,2,3,4,5,6,7,8,9,10) should equal ("test 1 2 3 4 5 6 7 8 9 10")
}


"Message11.apply" should "resolve interpolation" in {
val m_test11 = 'm_test11.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test11(1,2,3,4,5,6,7,8,9,10,11) should equal ("test 1 2 3 4 5 6 7 8 9 10 11")
}


"Message12.apply" should "resolve interpolation" in {
val m_test12 = 'm_test12.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test12(1,2,3,4,5,6,7,8,9,10,11,12) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12")
}


"Message13.apply" should "resolve interpolation" in {
val m_test13 = 'm_test13.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test13(1,2,3,4,5,6,7,8,9,10,11,12,13) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13")
}


"Message14.apply" should "resolve interpolation" in {
val m_test14 = 'm_test14.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test14(1,2,3,4,5,6,7,8,9,10,11,12,13,14) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13 14")
}


"Message15.apply" should "resolve interpolation" in {
val m_test15 = 'm_test15.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test15(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15")
}


"Message16.apply" should "resolve interpolation" in {
val m_test16 = 'm_test16.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test16(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16")
}


"Message17.apply" should "resolve interpolation" in {
val m_test17 = 'm_test17.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test17(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17")
}


"Message18.apply" should "resolve interpolation" in {
val m_test18 = 'm_test18.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test18(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18")
}


"Message19.apply" should "resolve interpolation" in {
val m_test19 = 'm_test19.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test19(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19")
}


"Message20.apply" should "resolve interpolation" in {
val m_test20 = 'm_test20.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test20(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20")
}


"Message21.apply" should "resolve interpolation" in {
val m_test21 = 'm_test21.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test21(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21")
}


"Message22.apply" should "resolve interpolation" in {
val m_test22 = 'm_test22.m[Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]
m_test22(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22) should equal ("test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22")
}

}

2 changes: 1 addition & 1 deletion src/test/scala/s_mach/i18n/test/MessageResolverTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import s_mach.i18n._
import s_mach.i18n.messages._

class MessageResolverTest extends FlatSpec with Matchers {
implicit val cfg = I18NConfig(Messages(Locale.US))
implicit val cfg = I18NConfig(Messages(Locale.ENGLISH))

"MessageResolver.strict.interpolate" should "throw if key is missing" in {
an[NoSuchElementException] should be thrownBy MessageResolver.strict.resolveInterpolation(
Expand Down
17 changes: 11 additions & 6 deletions src/test/scala/s_mach/i18n/test/MessageTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ package s_mach.i18n.test

import java.util.Locale

import org.scalatest.{Matchers, FlatSpec}
import org.scalatest.{FlatSpec, Matchers}
import s_mach.i18n._
import CommonTest._
import s_mach.i18n.messages.MessageLiteral

class MessageTest extends FlatSpec with Matchers {
val name = "Gary"
val qty = 10000.1


"Message0.apply" should "retrieve the message from Messages for the current locale" in {
"MessageLiteral.apply" should "retrieve the message from Messages for the current locale" in {
{
implicit val cfg = I18NConfig(mkTestMessages(Locale.US))
implicit val cfg = I18NConfig(mkTestMessages(Locale.ENGLISH))
m_hello() should equal("hello")
}
{
Expand All @@ -42,7 +43,7 @@ class MessageTest extends FlatSpec with Matchers {

"Message1.apply" should "retrieve the message from I18NConfig and interpolate arguments" in {
{
implicit val cfg = I18NConfig(mkTestMessages(Locale.US))
implicit val cfg = I18NConfig(mkTestMessages(Locale.ENGLISH))
m_hello_name_qty(name.asI18N,qty) should equal("hello Gary test 10,000.1")
}
{
Expand All @@ -53,7 +54,7 @@ class MessageTest extends FlatSpec with Matchers {

"Message2.apply" should "retrieve the message from I18NConfig and interpolate arguments" in {
{
implicit val cfg = I18NConfig(mkTestMessages(Locale.US))
implicit val cfg = I18NConfig(mkTestMessages(Locale.ENGLISH))
m_hello_name_qty("There are 2 apples".asI18N,qty) should equal("hello There are 2 apples test 10,000.1")
}
{
Expand All @@ -64,7 +65,7 @@ class MessageTest extends FlatSpec with Matchers {

"Choice.apply" should "retrieve the message from I18NConfig and choose the correct message" in {
{
implicit val cfg = I18NConfig(mkTestMessages(Locale.US))
implicit val cfg = I18NConfig(mkTestMessages(Locale.ENGLISH))
m_there_are_qty_apples(0) should equal("There are no apples")
m_there_are_qty_apples(1) should equal("There is one apple")
m_there_are_qty_apples(2) should equal("There are 2 apples")
Expand All @@ -77,5 +78,9 @@ class MessageTest extends FlatSpec with Matchers {
}
}

"Message.throwIfMissing" should "throw an exception if key is missing" in {
implicit val cfg = I18NConfig(mkTestMessages(Locale.ENGLISH))
an [NoSuchElementException] should be thrownBy MessageLiteral('m_missing_key).throwIfMissing()
}

}
Loading

0 comments on commit d238dc8

Please sign in to comment.