Skip to content

Commit

Permalink
Test Enso Date/java.time.LocalDate interop
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Apr 14, 2022
1 parent 0ea5dc2 commit 413179e
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
- [Fixed issues related to constructors' default arguments][3330]
- [Fixed compiler issue related to module cache.][3367]
- [Fixed execution of defaulted arguments of Atom Constructors][3358]
- [Converting Enso Date to java.time.LocalDate and back][3374]

[3227]: https://github.com/enso-org/enso/pull/3227
[3248]: https://github.com/enso-org/enso/pull/3248
Expand All @@ -179,6 +180,7 @@
[3358]: https://github.com/enso-org/enso/pull/3358
[3360]: https://github.com/enso-org/enso/pull/3360
[3367]: https://github.com/enso-org/enso/pull/3367
[3374]: https://github.com/enso-org/enso/pull/3374

# Enso 2.0.0-alpha.18 (2021-10-12)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.enso.interpreter.test.semantic

import org.enso.interpreter.test.{InterpreterContext, InterpreterTest}

class DateTest extends InterpreterTest {
override def subject: String = "LocalDate"

override def specify(implicit
interpreterContext: InterpreterContext
): Unit = {
"evaluate a date expression" in {
val code =
s"""from Standard.Builtins import all
|
|import Standard.Base.Data.Time.Date
|
|main =
| IO.println (Date.new 2022 04 01)
|""".stripMargin
eval(code)
consumeOut shouldEqual List("2022-04-01")
}

"print out java date" in {
val code =
s"""from Standard.Builtins import all
|polyglot java import java.time.LocalDate
|
|main =
| IO.println (LocalDate.of 2022 04 01)
|""".stripMargin
eval(code)
consumeOut shouldEqual List("2022-04-01")
}

"send enso date into java" in {
val code =
s"""from Standard.Builtins import all
|polyglot java import java.time.LocalTime
|import Standard.Base.Data.Time.Date
|
|main =
| ensodate = Date.new 2022 04 01
| javatime = LocalTime.of 10 26
| javatimedate = javatime.atDate ensodate
| javadate = javatimedate . toLocalDate
| IO.println javadate
|""".stripMargin
eval(code)
consumeOut shouldEqual List("2022-04-01")
}

"check java date has enso methods" in {
val code =
s"""from Standard.Builtins import all
|polyglot java import java.time.LocalDate
|import Standard.Base.Data.Time.Date
|
|main =
| javadate = LocalDate.of 2022 4 1
| ensoyear = javadate.year
| ensomonth = javadate.month
| ensoday = javadate.day
| ensotext = javadate.to_text
| IO.println ensoyear
| IO.println ensomonth
| IO.println ensoday
| IO.println ensotext
|""".stripMargin
eval(code)
consumeOut shouldEqual List("2022", "4", "1", "2022-04-01")
}

"check enso date has enso methods" in {
val code =
s"""from Standard.Builtins import all
|import Standard.Base.Data.Time.Date
|
|main =
| ensodate = Date.new 2022 4 1
| ensoyear = ensodate.year
| ensomonth = ensodate.month
| ensoday = ensodate.day
| ensotext = ensodate.to_text
| IO.println ensoyear
| IO.println ensomonth
| IO.println ensoday
| IO.println ensotext
|""".stripMargin
eval(code)
consumeOut shouldEqual List("2022", "4", "1", "2022-04-01")
}
}
}
62 changes: 43 additions & 19 deletions test/Tests/src/Semantic/Java_Interop_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,46 @@ polyglot java import java.lang.String
polyglot java import java.lang.StringBuilder as Java_String_Builder
polyglot java import java.util.ArrayList

spec = Test.group "Java FFI" <|
Test.specify "should call methods imported from Java" <|
Long.sum 1 2 . should_equal 3

Test.specify "should call constructors imported from Java" <|
list = ArrayList.new
list.add 432
list.get 0 . should_equal 432
Test.specify "should auto-convert numeric types across the polyglot boundary" <|
(Float.valueOf "123.3" + 5).should_equal 128.3 epsilon=0.0001
(Integer.sum 1 2 + 3) . should_equal 6
Test.specify "should auto-convert strings across the polyglot boundary" <|
(String.format "%s bar %s" "baz" "quux" + " foo").should_equal "baz bar quux foo"
Test.specify "should support Java import renaming" <|
builder = Java_String_Builder.new
builder.append "foo"
builder.append "bar"
str = builder.toString
str.should_equal "foobar"
import Standard.Base.Data.Time.Date
polyglot java import java.time.LocalDate
polyglot java import java.time.LocalTime

spec =
Test.group "Java FFI" <|
Test.specify "should call methods imported from Java" <|
Long.sum 1 2 . should_equal 3

Test.specify "should call constructors imported from Java" <|
list = ArrayList.new
list.add 432
list.get 0 . should_equal 432
Test.specify "should auto-convert numeric types across the polyglot boundary" <|
(Float.valueOf "123.3" + 5).should_equal 128.3 epsilon=0.0001
(Integer.sum 1 2 + 3) . should_equal 6
Test.specify "should auto-convert strings across the polyglot boundary" <|
(String.format "%s bar %s" "baz" "quux" + " foo").should_equal "baz bar quux foo"
Test.specify "should support Java import renaming" <|
builder = Java_String_Builder.new
builder.append "foo"
builder.append "bar"
str = builder.toString
str.should_equal "foobar"

Test.group "Java/Enso Date" <|
Test.specify "Java date has Enso properties" <|
april1st = LocalDate.of 2022 04 01
april1st.year.should_equal 2022
april1st.month.should_equal 4
april1st.day.should_equal 1

Test.specify "send Enso date into Java" <|
ensodate = Date.new 2022 04 01
javatime = LocalTime.of 10 26
javatimedate = javatime.atDate ensodate
april1st = javatimedate . toLocalDate
april1st.year.should_equal 2022
april1st.month.should_equal 4
april1st.day.should_equal 1


main = Test.Suite.run_main here.spec

0 comments on commit 413179e

Please sign in to comment.