Skip to content

Commit

Permalink
Refactoring code and added i18n messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Aug 29, 2017
1 parent 860a60d commit 5c2a628
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class NullTestCase extends AbstractWollokInterpreterTestCase {
'''
«defineAssertWithMessage»
program a {
extendedAssert.assertException({ null.sayHi() }, "Cannot send message sayHi() to null")
extendedAssert.assertException({ null.toString() }, "Cannot send message toString() to null")
extendedAssert.assertException({ null.sayHi() }, "Wrong message sayHi() sent to null")
extendedAssert.assertException({ null.toString() }, "Wrong message toString() sent to null")
}
'''.interpretPropagatingErrors
}
Expand All @@ -38,10 +38,10 @@ class NullTestCase extends AbstractWollokInterpreterTestCase {
'''
«defineAssertWithMessage»
program a {
extendedAssert.assertException({ null + 3 }, "Cannot send message + to null")
extendedAssert.assertException({ null - 3 }, "Cannot send message - to null")
extendedAssert.assertException({ null * 3 }, "Cannot send message * to null")
extendedAssert.assertException({ null / 3 }, "Cannot send message / to null")
extendedAssert.assertException({ null + 3 }, "Wrong message + sent to null")
extendedAssert.assertException({ null - 3 }, "Wrong message - sent to null")
extendedAssert.assertException({ null * 3 }, "Wrong message * sent to null")
extendedAssert.assertException({ null / 3 }, "Wrong message / sent to null")
}
'''.interpretPropagatingErrors
}
Expand All @@ -51,8 +51,8 @@ class NullTestCase extends AbstractWollokInterpreterTestCase {
'''
«defineAssertWithMessage»
program a {
extendedAssert.assertException({ null || null }, "Cannot send message || to null")
extendedAssert.assertException({ null && null }, "Cannot send message && to null")
extendedAssert.assertException({ null || null }, "Wrong message || sent to null")
extendedAssert.assertException({ null && null }, "Wrong message && sent to null")
}
'''.interpretPropagatingErrorsWithoutStaticChecks
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class Persona {
// XPECT errors --> "Wrong message fooBar() sent to pepita" at "fooBar"
pepita.fooBar()

// XPECT errors --> "Wrong message fooBar(param1, param2) sent to pepita" at "fooBar"
// XPECT errors --> "Wrong message fooBar(2, "hola") sent to pepita" at "fooBar"
pepita.fooBar(2, "hola")

// XPECT errors --> "Wrong argument count in message fooWithParam() sent to pepita, however similar definitions exist: fooWithParam(p)" at "fooWithParam"
pepita.fooWithParam()

// XPECT errors --> "Wrong argument count in message fooWithParam(param1, param2, param3) sent to pepita, however similar definitions exist: fooWithParam(p)" at "fooWithParam"
// XPECT errors --> "Wrong argument count in message fooWithParam(new Date(), 2, 4) sent to pepita, however similar definitions exist: fooWithParam(p)" at "fooWithParam"
pepita.fooWithParam(new Date(), 2, 4)

// XPECT errors --> "Wrong case-sensitive message foO() sent to pepita. Use foo()" at "foO"
Expand All @@ -23,7 +23,7 @@ class Persona {
// XPECT errors --> "Wrong argument count in message severalDef() sent to pepita, however similar definitions exist: severalDef(a), severalDef(a, b)" at "severalDef"
pepita.severalDef()

// XPECT errors --> "Wrong argument count in message severalDef(param1, param2, param3, param4) sent to pepita, however similar definitions exist: severalDef(a), severalDef(a, b)" at "severalDef"
// XPECT errors --> "Wrong argument count in message severalDef(1, 2, 3, 4) sent to pepita, however similar definitions exist: severalDef(a), severalDef(a, b)" at "severalDef"
pepita.severalDef(1, 2, 3, 4)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.uqbar.project.wollok.messages"; //$NON-NLS-1$

public static String LINKING_COULD_NOT_RESOLVE_REFERENCE;

public static String WollokInterpreter_cannot_use_null_in_if;
public static String WollokInterpreter_expression_in_if_must_evaluate_to_boolean;

public static String WollokNativeLobby_cannot_use_self_in_program;

public static String WollokDslValidator_CLASS_NAME_MUST_START_UPPERCASE;
public static String WollokDslValidator_REFERENCIABLE_NAME_MUST_START_LOWERCASE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import java.util.List
import java.util.Map
import org.eclipse.emf.common.util.EList
import org.eclipse.emf.ecore.EObject
import org.eclipse.osgi.util.NLS
import org.uqbar.project.wollok.Messages
import org.uqbar.project.wollok.interpreter.api.XInterpreterEvaluator
import org.uqbar.project.wollok.interpreter.context.UnresolvableReference
import org.uqbar.project.wollok.interpreter.core.CallableSuper
Expand Down Expand Up @@ -64,7 +66,6 @@ import static extension org.uqbar.project.wollok.interpreter.context.EvaluationC
import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*
import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*
import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*
import static extension org.uqbar.project.xtext.utils.XTextExtensions.sourceCode

/**
* It's the real "interpreter".
Expand Down Expand Up @@ -153,11 +154,11 @@ class WollokInterpreterEvaluator implements XInterpreterEvaluator<WollokObject>

// I18N !
if (cond === null) {
throw newWollokExceptionAsJava('''Cannot use null in 'if' expression''')
throw newWollokExceptionAsJava(NLS.bind(Messages.WollokInterpreter_cannot_use_null_in_if, NULL))
}
if (!(cond.isWBoolean))
throw new WollokInterpreterException('''Expression in 'if' must evaluate to a boolean. Instead got: «cond» («cond?.class.name»)''',
it)
throw new WollokInterpreterException(NLS.bind(Messages.WollokInterpreter_expression_in_if_must_evaluate_to_boolean, cond, cond?.class.name), it)

if (wollokToJava(cond, Boolean) == Boolean.TRUE)
then.eval
else
Expand Down Expand Up @@ -394,7 +395,7 @@ class WollokInterpreterEvaluator implements XInterpreterEvaluator<WollokObject>

private def validateNullOperand(WollokObject leftOperand, String operation) {
if (leftOperand === null && !#["==", "!=", "===", "!=="].contains(operation)) {
throw newWollokExceptionAsJava('''Cannot send message «operation» to null''')
throw newWollokExceptionAsJava(NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST, NULL, operation))
}
}

Expand Down Expand Up @@ -433,7 +434,7 @@ class WollokInterpreterEvaluator implements XInterpreterEvaluator<WollokObject>
def dispatch evaluate(WFeatureCall call) {
val target = call.evaluateTarget
if (target === null)
throw newWollokExceptionAsJava('''Cannot send message «call.feature»(«call.memberCallArguments.map[sourceCode].join(',')») to null''')
throw newWollokExceptionAsJava(NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST, NULL, call.fullMessage))
target.call(call.feature, call.memberCallArguments.evalEach)
}

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

import java.util.Map
import org.eclipse.xtend.lib.annotations.Accessors
import org.uqbar.project.wollok.Messages
import org.uqbar.project.wollok.interpreter.WollokInterpreter
import org.uqbar.project.wollok.interpreter.WollokInterpreterConsole
import org.uqbar.project.wollok.interpreter.context.EvaluationContext
Expand Down Expand Up @@ -29,7 +29,7 @@ class WollokNativeLobby extends AbstractWollokDeclarativeNativeObject implements
this.console = console
}

override getThisObject() { throw new UnsupportedOperationException("Cannot use reference to self in a program's code !")}
override getThisObject() { throw new UnsupportedOperationException(Messages.WollokNativeLobby_cannot_use_self_in_program)}

override allReferenceNames() {
localProgramVariables.keySet.map[new WVariable(it, true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,27 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
method.call(parameters)
}

def throwMessageNotUnderstood(String name, Object... parameters) {
// hack because objectliterals are not inheriting base methods from wollok.lang.Object
if (this.behavior instanceof WObjectLiteral || name == "messageNotUnderstood" || name == "toString") {
val fullMessage = name + "(" + parameters.join(",") + ")"
val similarMethods = this.behavior.findMethodsByName(name)
def throwMessageNotUnderstood(String methodName, Object... parameters) {
// hack because object literals are not inheriting base methods from wollok.lang.Object
if (this.behavior instanceof WObjectLiteral || methodName == "messageNotUnderstood" || methodName == "toString") {
val fullMessage = methodName + "(" + parameters.join(",") + ")"
val similarMethods = this.behavior.findMethodsByName(methodName)
if (similarMethods.empty) {
throw messageNotUnderstood(NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST, behavior.name, fullMessage))
val caseSensitiveMethod = this.behavior.allMethods.findMethodIgnoreCase(methodName, parameters.size)
if (caseSensitiveMethod !== null) {
throw messageNotUnderstood(NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST_CASE_SENSITIVE,
#[behavior.name, fullMessage, #[caseSensitiveMethod].convertToString]))
} else {
throw messageNotUnderstood(NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST, behavior.name, fullMessage))
}
} else {
val similarDefinitions = similarMethods.map [ messageName ].join(', ')
throw messageNotUnderstood(NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST_BUT_SIMILAR_FOUND, #[behavior.name, fullMessage, similarDefinitions]))
}
}

try {
call("messageNotUnderstood", name.javaToWollok, parameters.map[javaToWollok].javaToWollok)
call("messageNotUnderstood", methodName.javaToWollok, parameters.map[javaToWollok].javaToWollok)
}
catch (WollokProgramExceptionWrapper e) {
// this one is ok because calling messageNotUnderstood actually throws the exception!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,4 @@ class WollokJavaConversions {

def static getEvaluator() { (WollokInterpreter.getInstance.evaluator as WollokInterpreterEvaluator) }


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ CheckSeverity_ERROR = ERROR
CheckSeverity_INFO = INFO
CheckSeverity_WARN = WARN

#
#
# Interpreter
#
# you must repeat quote in order to appear
WollokInterpreter_cannot_use_null_in_if = Cannot use {0} in ''if'' expression
WollokInterpreter_expression_in_if_must_evaluate_to_boolean = Expression in ''if'' must evaluate to a boolean. Instead got: {0} ({1})

# Native Lobby
WollokNativeLobby_cannot_use_self_in_program = Cannot use reference to self in a program''s code !


# Ifs
WollokDslValidator_BAD_USAGE_OF_IF_AS_BOOLEAN_EXPRESSION = Bad usage of if ! You must return the condition itself without using if. Use:
WollokDslValidator_IF_USED_IN_AN_EXPRESSION_SHOULD_HAVE_AN_ELSE_STATEMENT = If-statements without else cannot be considered valid expressions as they cannot always yield a value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ CheckSeverity_ERROR = ERROR
CheckSeverity_INFO = INFO
CheckSeverity_WARN = ADVERTENCIA

#
#
# Interpreter
#
WollokInterpreter_cannot_use_null_in_if = No puede utilizar {0} en una sentencia 'if'
WollokInterpreter_expression_in_if_must_evaluate_to_boolean = La sentencia 'if' debe evaluar a un valor booleano. Se obtuvo: {0} ({1})

# Native Lobby
WollokNativeLobby_cannot_use_self_in_program = No puede utilizar una referencia a self dentro de un programa.

# Ifs
WollokDslValidator_BAD_USAGE_OF_IF_AS_BOOLEAN_EXPRESSION = ¡Mal uso del if! En lugar de usar if debe devolver directamente:
WollokDslValidator_IF_USED_IN_AN_EXPRESSION_SHOULD_HAVE_AN_ELSE_STATEMENT = Las sentencias "if" sin un "else" no pueden considerarse expresiones v\u00E1lidas ya que no siempre devuelven un valor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.uqbar.project.wollok.model

import java.util.Collection
import java.util.List
import org.eclipse.core.resources.IFile
import org.eclipse.core.resources.ResourcesPlugin
Expand Down Expand Up @@ -30,6 +31,7 @@ import org.uqbar.project.wollok.wollokDsl.WCollectionLiteral
import org.uqbar.project.wollok.wollokDsl.WConstructor
import org.uqbar.project.wollok.wollokDsl.WConstructorCall
import org.uqbar.project.wollok.wollokDsl.WExpression
import org.uqbar.project.wollok.wollokDsl.WFeatureCall
import org.uqbar.project.wollok.wollokDsl.WFile
import org.uqbar.project.wollok.wollokDsl.WFixture
import org.uqbar.project.wollok.wollokDsl.WIfExpression
Expand Down Expand Up @@ -66,6 +68,7 @@ import wollok.lang.Exception
import static org.uqbar.project.wollok.scoping.root.WollokRootLocator.*

import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*
import static extension org.uqbar.project.wollok.utils.XTextExtensions.*

/**
* Extension methods to Wollok semantic model.
Expand Down Expand Up @@ -540,10 +543,6 @@ class WollokModelExtensions {
def static dispatch matchesParam(WParameter p, WVariableReference ref) { p === ref.getRef }
def static dispatch matchesParam(WParameter p, WParameter p2) { p === p2 }

def static fullMessage(WMemberFeatureCall c) {
fullMessage(c.feature, c.memberCallArguments.size)
}

def static fullMessage(String methodName, int argumentsSize) {
var args = ""
val argsSize = argumentsSize
Expand All @@ -553,4 +552,8 @@ class WollokModelExtensions {
methodName + "(" + args + ")"
}

def static fullMessage(WFeatureCall call) {
'''«call.feature»(«call.memberCallArguments.map[sourceCode].join(', ')»)'''
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,16 @@ class XTendUtilExtensions {
def static String createMessage(Object target, String message, Object... args) {
val fullMessage = message + "(" + args.join(",") + ")"
val wollokTarget = target.javaToWollok
val targetName = '''«target» («target.class.simpleName»)''' // wollokTarget.invoke("toString", #[])
val targetName = '''«target» («target.class.simpleName»)'''
val similarMethods = wollokTarget.findMethodsByName(message)
if (similarMethods.empty) {
return NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST, targetName, fullMessage)
val caseSensitiveMethod = wollokTarget.allMethods.findMethodIgnoreCase(message, args.size)
if (caseSensitiveMethod !== null) {
return NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST_CASE_SENSITIVE,
#[target, fullMessage, #[caseSensitiveMethod].convertToString])
} else {
return NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST, targetName, fullMessage)
}
}
val similarDefinitions = similarMethods.map [ messageName ].join(', ')
NLS.bind(Messages.WollokDslValidator_METHOD_DOESNT_EXIST_BUT_SIMILAR_FOUND, #[targetName, fullMessage, similarDefinitions])
Expand Down

0 comments on commit 5c2a628

Please sign in to comment.