diff --git a/CHANGELOG.md b/CHANGELOG.md index 33dd7250e32d..c35ebfe1c29b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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) diff --git a/engine/runtime/src/test/scala/org/enso/interpreter/test/semantic/DateTest.scala b/engine/runtime/src/test/scala/org/enso/interpreter/test/semantic/DateTest.scala new file mode 100644 index 000000000000..8d72279b2e91 --- /dev/null +++ b/engine/runtime/src/test/scala/org/enso/interpreter/test/semantic/DateTest.scala @@ -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") + } + } +} diff --git a/test/Tests/src/Semantic/Java_Interop_Spec.enso b/test/Tests/src/Semantic/Java_Interop_Spec.enso index 19c0c98b0524..b4e84e42fd4c 100644 --- a/test/Tests/src/Semantic/Java_Interop_Spec.enso +++ b/test/Tests/src/Semantic/Java_Interop_Spec.enso @@ -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