Skip to content

Commit

Permalink
Merge pull request #1715 from uqbar-project/feature-#1692-change-toSt…
Browse files Browse the repository at this point in the history
…ring-of-Date

Feature #1692 change to string of date
  • Loading branch information
fdodino committed Jul 31, 2019
2 parents cb598e4 + 127d513 commit 402ca6f
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 36 deletions.
35 changes: 19 additions & 16 deletions org.uqbar.project.wollok.lib/src/wollok/lang.wlk
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ class Pair {
}
method key() = x
method value() = y

/** String representation of a Pair */
override method toString() = x.toString() + " -> " + y.toString()
}

/**
Expand Down Expand Up @@ -2506,10 +2509,10 @@ class Date {
*
* Example:
* new Date(day = 12, month = 5, year = 2018).plusDays(1)
* ==> Answers a Date[day = 13, month = 5, year = 2018], a day forward
* ==> Answers 13/5/2018, a day forward
*
* new Date(day = 12, month = 5, year = 2018).plusDays(-1)
* ==> Answers a Date[day = 11, month = 5, year = 2018], a day back
* ==> Answers 11/5/2018, a day back
*/
method plusDays(_days) native

Expand All @@ -2520,10 +2523,10 @@ class Date {
*
* Example:
* new Date(day = 31, month = 1, year = 2018).plusMonths(1)
* ==> Answers a Date[day = 28, month = 2, year = 2018], a month forward
* ==> Answers 28/2/2018, a month forward
*
* new Date(day = 12, month = 5, year = 2018).plusMonths(-1)
* ==> Answers a Date[day = 12, month = 4, year = 2018], a month back
* ==> Answers 12/4/2018, a month back
*/
method plusMonths(_months) native

Expand All @@ -2534,10 +2537,10 @@ class Date {
*
* Example:
* new Date(day = 31, month = 1, year = 2018).plusYears(1)
* ==> Answers a Date[day = 31, month = 1, year = 2019], a year forward
* ==> Answers 31/1/2019, a year forward
*
* new Date(day = 12, month = 5, year = 2018).plusYears(-1)
* ==> Answers a Date[day = 12, month = 5, year = 2017], a year back
* ==> Answers 12/5/2017, a year back
*/
method plusYears(_years) native

Expand Down Expand Up @@ -2586,10 +2589,10 @@ class Date {
*
* Examples:
* new Date(day = 1, month = 1, year = 2009).minusDays(1)
* ==> Answers a Date[day = 31, month = 12, year = 2008], a day back
* ==> Answers 31/12/2008, a day back
*
* new Date(day = 1, month = 1, year = 2009).minusDays(-1)
* ==> Answers a Date[day = 2, month = 1, year = 2009], a day forward
* ==> Answers 2/1/2009, a day forward
*/
method minusDays(_days) native

Expand All @@ -2600,10 +2603,10 @@ class Date {
*
* Examples:
* new Date(day = 1, month = 1, year = 2009).minusMonths(1)
* ==> Answers a Date[day = 1, month = 12, year = 2008], a month back
* ==> Answers 1/12/2008, a month back
*
* new Date(day = 1, month = 1, year = 2009).minusMonths(-1)
* ==> Answers a Date[day = 1, month = 2, year = 2009], a month forward
* ==> Answers 1/2/2009, a month forward
*/
method minusMonths(_months) native

Expand All @@ -2614,10 +2617,10 @@ class Date {
*
* Examples:
* new Date(day = 1, month = 1, year = 2009).minusYears(1)
* ==> Answers a Date[day = 1, month = 1, year = 2008], a year back
* ==> Answers 1/1/2008, a year back
*
* new Date(day = 1, month = 1, year = 2009).minusYears(-1)
* ==> Answers a Date[day = 1, month = 1, year = 2010], a year forward
* ==> Answers 1/1/2010, a year forward
*/
method minusYears(_years) native

Expand All @@ -2637,16 +2640,16 @@ class Date {

/** Shows nicely an internal representation of a date **/
override method toSmartString(alreadyShown) =
"a Date[day = " + day + ", month = " + month + ", year = " + year + "]"
self.shortDescription()

/**
* Shows a short, internal representation of a date
* (the result varies depending on user's locale)
*
* Example:
* new Date(day = 2, month = 4, year = 2018).shortDescription()
* ==> Answers "2/4/2018"
* ==> Answers 2/4/2018
*/
override method shortDescription() =
"" + day + "/" + month + "/" + year
override method shortDescription() native

}
19 changes: 17 additions & 2 deletions org.uqbar.project.wollok.lib/src/wollok/lang/WDate.xtend
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package wollok.lang

import java.math.BigDecimal

import java.time.LocalDate
import java.time.chrono.IsoChronology
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder
import java.time.format.FormatStyle
import java.util.Locale
import org.uqbar.project.wollok.interpreter.WollokInterpreter
import org.uqbar.project.wollok.interpreter.core.WollokObject
import org.uqbar.project.wollok.interpreter.nativeobj.NativeMessage

import static extension org.uqbar.project.wollok.utils.WollokObjectUtils.*
import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*
import static extension org.uqbar.project.wollok.utils.WollokObjectUtils.*

/**
* Native implementation of the date wollok class
Expand All @@ -17,6 +21,8 @@ import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJav
* @author dodain
*/
class WDate extends AbstractJavaWrapper<LocalDate> {

var public static FORMATTER = DateTimeFormatter.ofPattern(WDate.getLocalizedFormat(Locale.^default))

new(WollokObject obj, WollokInterpreter interpreter) {
super(obj, interpreter)
Expand Down Expand Up @@ -109,4 +115,13 @@ class WDate extends AbstractJavaWrapper<LocalDate> {
wDate !== null && getWrapped == wDate.getWrapped
}

def shortDescription() {
getWrapped.format(FORMATTER)
}

def static getLocalizedFormat(Locale locale) {
return DateTimeFormatterBuilder
.getLocalizedDateTimePattern(FormatStyle.SHORT, null, IsoChronology.INSTANCE, locale)
.replaceFirst("/yy$", "/yyyy")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package org.uqbar.project.wollok.tests.base
import org.junit.Before
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.uqbar.project.wollok.tests.interpreter.AbstractWollokInterpreterTestCase
import org.uqbar.project.wollok.tests.injectors.WollokTestInjectorProvider
import org.uqbar.project.wollok.tests.interpreter.AbstractWollokInterpreterTestCase

/**
* Helper class to allow parameterized interpreter tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.uqbar.project.wollok.tests.interpreter
import com.google.inject.Inject
import java.io.File
import java.io.FileInputStream
import java.time.format.DateTimeFormatter
import org.eclipse.emf.common.util.URI
import org.eclipse.xtext.resource.XtextResourceSet
import org.eclipse.xtext.testing.InjectWith
Expand All @@ -15,6 +16,7 @@ import org.junit.runner.RunWith
import org.uqbar.project.wollok.interpreter.WollokInterpreter
import org.uqbar.project.wollok.interpreter.core.WollokProgramExceptionWrapper
import org.uqbar.project.wollok.tests.injectors.WollokTestInjectorProvider
import wollok.lang.WDate

/**
* Abstract base class for all interpreter tests cases.
Expand All @@ -36,6 +38,9 @@ abstract class AbstractWollokInterpreterTestCase extends Assert {
def void setUp() {
interpreter.classLoader = AbstractWollokInterpreterTestCase.classLoader

// This makes the Date string representation independent of the current user's locale
WDate.FORMATTER = DateTimeFormatter.ofPattern("d/M/yy")

new File("target/test-files").mkdirs
new File("target/test-files/WOLLOK.ROOT").createNewFile
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,17 @@ class DateTestCase extends AbstractWollokInterpreterTestCase {
def void toStringDefaultTest() {
'''
const aDay = new Date(day = 28, month = 12, year = 2016)
assert.equals("a Date[day = 28, month = 12, year = 2016]", aDay.toString())
assert.equals("a Date[day = 28, month = 12, year = 2016]", aDay.toSmartString(false))
assert.equals("28/12/16", aDay.toString())
assert.equals("28/12/16", aDay.toSmartString(false))
'''.test
}

@Test
def void toStringWithA1DigitMonthTest() {
'''
const aDay = new Date(day = 28, month = 2, year = 2016)
assert.equals("a Date[day = 28, month = 2, year = 2016]", aDay.toString())
assert.equals("a Date[day = 28, month = 2, year = 2016]", aDay.toSmartString(false))
assert.equals("28/2/16", aDay.toString())
assert.equals("28/2/16", aDay.toSmartString(false))
'''.test
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NumberTestCase extends AbstractWollokInterpreterTestCase {
@Test
def void addWithFailingParameters() {
'''
assert.throwsExceptionWithMessage("Operation doesn't support parameter a Date[day = 18, month = 12, year = 2017]", { 3 + new Date(day = 18, month = 12, year = 2017) })
assert.throwsExceptionWithMessage("Operation doesn't support parameter 18/12/17", { 3 + new Date(day = 18, month = 12, year = 2017) })
assert.throwsExceptionWithMessage("Operation doesn't support parameter pepe", { 3 + "pepe" })
'''.test
}
Expand All @@ -57,7 +57,7 @@ class NumberTestCase extends AbstractWollokInterpreterTestCase {
@Test
def void maxFailingParameter() {
'''
assert.throwsExceptionWithMessage("Operation doesn't support parameter a Date[day = 21, month = 12, year = 2017]", { 4.min(new Date(day = 21, month = 12, year = 2017)) })
assert.throwsExceptionWithMessage("Operation doesn't support parameter 21/12/17", { 4.min(new Date(day = 21, month = 12, year = 2017)) })
'''.test
}

Expand All @@ -78,7 +78,7 @@ class NumberTestCase extends AbstractWollokInterpreterTestCase {
@Test
def void minFailingParameter() {
'''
assert.throwsExceptionWithMessage("Operation doesn't support parameter a Date[day = 21, month = 12, year = 2017]", { 4.min(new Date(day = 21, month = 12, year = 2017)) })
assert.throwsExceptionWithMessage("Operation doesn't support parameter 21/12/17", { 4.min(new Date(day = 21, month = 12, year = 2017)) })
'''.test
}

Expand Down Expand Up @@ -418,7 +418,7 @@ class NumberTestCase extends AbstractWollokInterpreterTestCase {
@Test
def void gcdForInvalidArguments() {
'''
assert.throwsExceptionWithMessage("Operation doesn't support parameter a Date[day = 1, month = 1, year = 2018]", { 4.gcd(new Date(day = 1, month = 1, year = 2018)) })
assert.throwsExceptionWithMessage("Operation doesn't support parameter 1/1/18", { 4.gcd(new Date(day = 1, month = 1, year = 2018)) })
'''.test
}

Expand Down Expand Up @@ -472,7 +472,7 @@ class NumberTestCase extends AbstractWollokInterpreterTestCase {
@Test
def void lcmForInvalidArguments() {
'''
assert.throwsExceptionWithMessage("Operation doesn't support parameter a Date[day = 1, month = 1, year = 2018]", { 4.lcm(new Date(day = 1, month = 1, year = 2018)) })
assert.throwsExceptionWithMessage("Operation doesn't support parameter 1/1/18", { 4.lcm(new Date(day = 1, month = 1, year = 2018)) })
'''.test
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.uqbar.project.wollok.tests.interpreter

import org.junit.Test

class PairTestCase extends AbstractWollokInterpreterTestCase {

@Test
def void smokeTest() {
'''
const pair = new Pair(1, 1)
assert.equals(pair, pair)
'''.test
}

@Test
def void testNewContructor() {
'''
const pair = new Pair(1, 2)
assert.equals(1, pair.x())
assert.equals(2, pair.y())
'''.test
}

@Test
def void testArrowContructor() {
'''
const pair = 1->2
assert.equals(1, pair.x())
assert.equals(2, pair.y())
'''.test
}

@Test
def void testXGetter() {
'''
const pair = "1"->2
assert.equals("1", pair.x())
'''.test
}

@Test
def void testYGetter() {
'''
const pair = 1->"2"
assert.equals("2", pair.y())
'''.test
}

@Test
def void testXSetterThrowsCannotModifyConstantPropertyException() {
'''
const pair = 1->2
assert.throwsExceptionWithMessage("Cannot modify constant property x", { pair.x(10) })
'''.test
}

@Test
def void testYSetterThrowsCannotModifyConstantPropertyException() {
'''
const pair = 1->2
assert.throwsExceptionWithMessage("Cannot modify constant property y", { pair.y(20) })
'''.test
}

@Test
def void testKeyGetter() {
'''
const pair = 10->20
assert.equals(10, pair.key())
'''.test
}

@Test
def void testValueGetter() {
'''
const pair = 10->20
assert.equals(20, pair.value())
'''.test
}

@Test
def void testKeySetterThrowsMessageNotUnderstoodException() {
'''
const pair = 1->2
assert.throwsExceptionWithType(new MessageNotUnderstoodException(), { pair.key(10) })
'''.test
}

@Test
def void testValueSetterThrowsMessageNotUnderstoodException() {
'''
const pair = 1->2
assert.throwsExceptionWithType(new MessageNotUnderstoodException(), { pair.value(20) })
'''.test
}

@Test
def void testToStringRepresentation() {
'''
const pair = 10->20
assert.equals("10 -> 20", pair.toString())
'''.test
}

@Test
def void testShortDescriptionRepresentation() {
'''
const pair = 10->20
assert.equals("10 -> 20", pair.shortDescription())
'''.test
}
}
Loading

0 comments on commit 402ca6f

Please sign in to comment.