Skip to content

Commit

Permalink
Merge branch 'dev' into fix-type-system-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PalumboN committed Mar 18, 2021
2 parents a3d57c8 + 1b57de0 commit bc376c3
Show file tree
Hide file tree
Showing 21 changed files with 404 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.uqbar.project.wollok.interpreter.listeners

import org.eclipse.emf.ecore.EObject
import org.uqbar.project.wollok.interpreter.api.XInterpreterListener
import org.uqbar.project.wollok.sdk.WollokSDK
import org.uqbar.project.wollok.wollokDsl.WBlockExpression
import org.uqbar.project.wollok.wollokDsl.WMemberFeatureCall

import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*

class WollokTestListener implements XInterpreterListener {
int messagesToAssert = 0

override started() {
}

override terminated() {
}

override aboutToEvaluate(EObject element) {
}

override evaluated(EObject element) {
if (element.hasMessageToAssert) messagesToAssert++
}

def dispatch static boolean hasMessageToAssert(EObject e) { false }

def dispatch static boolean hasMessageToAssert(WMemberFeatureCall call) {
call.memberCallTarget.receiver.equals(WollokSDK.ASSERT_WKO)
}

def dispatch static boolean hasMessageToAssert(WBlockExpression block) {
block.expressions.exists [ hasMessageToAssert ]
}

def hasNoAssertions() {
messagesToAssert === 0
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.uqbar.project.wollok.launch.messages"; //$NON-NLS-1$

public static String TEST_RESULTS;
public static String TEST_NO_MESSAGE_TO_ASSERT;
public static String ALL_TEST_IN_PROJECT;
public static String ALL_TEST_IN_FOLDER;
public static String LINE;
public static String REPL_WELCOME;
public static String REPL_END;

// ****************************
// ** Launcher messages
// ****************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.eclipse.xtend.lib.annotations.Accessors
import org.uqbar.project.wollok.interpreter.WollokInterpreter
import org.uqbar.project.wollok.interpreter.WollokInterpreterEvaluator
import org.uqbar.project.wollok.interpreter.core.WollokObject
import org.uqbar.project.wollok.interpreter.core.WollokProgramExceptionWrapper
import org.uqbar.project.wollok.interpreter.listeners.WollokTestListener
import org.uqbar.project.wollok.launch.tests.WollokTestsReporter
import org.uqbar.project.wollok.wollokDsl.WFile
import org.uqbar.project.wollok.wollokDsl.WSuite
Expand Down Expand Up @@ -106,11 +108,16 @@ class WollokLauncherInterpreterEvaluator extends WollokInterpreterEvaluator {
}

override dispatch evaluate(WTest test) {
val testListener = new WollokTestListener
try {
wollokTestsReporter.testStarted(test)
interpreter.addInterpreterListener(testListener)
test.elements.forEach [ expr |
interpreter.performOnStack(expr, currentContext) [ | expr.eval ]
]
if (testListener.hasNoAssertions) {
throw new WollokProgramExceptionWrapper(newWollokAssertionException(Messages.TEST_NO_MESSAGE_TO_ASSERT))
}
wollokTestsReporter.reportTestOk(test)
null
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#
# Unit testing
TEST_RESULTS=Tests results
TEST_NO_MESSAGE_TO_ASSERT = No message to 'assert' object was sent
ALL_TEST_IN_PROJECT=All tests in project
ALL_TEST_IN_FOLDER=All tests in folder {0}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# Unit testing
TEST_RESULTS=Resultado de los tests
TEST_NO_MESSAGE_TO_ASSERT = No se envi\u00F3 ning\u00FAn mensaje al objeto 'assert'
ALL_TEST_IN_PROJECT=Todos los tests del proyecto
ALL_TEST_IN_FOLDER=Todos los tests de la carpeta {0}

Expand Down Expand Up @@ -32,7 +33,7 @@ WollokLauncherOptions_SPECIFIC_FOLDER = Definir una carpeta espec\u00EDfica
WollokLauncherOptions_NUMBER_DECIMALS = Cantidad de decimales a usar para imprimir y redondear un n\u00FAmero
WollokLauncherOptions_DECIMAL_PRINTING_STRATEGY = Criterio a usar para imprimir un n\u00FAmero
WollokLauncherOptions_DECIMAL_CONVERSION_STRATEGY = Criterio a usar para convertir n\u00FAmeros con muchos decimales
WollokLauncherOptions_JAR_LIBRARIES = Librer\u00EDas JAR extras
WollokLauncherOptions_JAR_LIBRARIES = Bibliotecas JAR extras
WollokLauncherOptions_WOLLOK_FILES = Archivos Wollok a ejecutar
WollokLauncherOptions_DONT_VALIDATE = Ejecutar el programa sin utilizar el validador de Wollok
WollokLauncherOptions_EXIT_ON_BUILD_FAILURE = Validar el archivo y no ejecutarlo en caso de encontrar errores
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,16 @@ class WollokConsoleTestsReporter extends DefaultWollokTestsReporter {
println(ansi
.a(" ").fg(YELLOW).a(test.name).a(": ✗ FAILED (").a(test.totalTime).a("ms) => ").reset
.fg(YELLOW).a(assertionError.message).reset
.fg(YELLOW).a(" (").a(resource.trimFragment).a(":").a(lineNumber).a(")").reset
.showStackTrace(assertionError, lineNumber, resource)
)
}

def showStackTrace(Ansi ansi, AssertionException assertionError, int lineNumber, URI resource) {
if ((resource === null) || lineNumber === 0) return ansi.reset
return ansi.fg(YELLOW).a(" (").a(resource.trimFragment).a(":").a(lineNumber).a(")").reset
.a("\n ")
.fg(YELLOW).a(assertionError.wollokException?.convertStackTrace.join("\n ")).reset
.a("\n")
)
}

private def printException(WTest test, Exception exception, int lineNumber, URI resource) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.uqbar.project.wollok.tests.assertions

import org.junit.Test
import org.uqbar.project.wollok.tests.interpreter.AbstractWollokInterpreterTestCase

/**
*
* @author dodain
*/
class AssertionTest extends AbstractWollokInterpreterTestCase {

def shouldFailTestForAssertObject(CharSequence test) {
try {
test.interpretPropagatingErrorsWithoutStaticChecks
fail("Test should have failed")
} catch (Exception e) {
assertEquals(e.cause.message, "No message to 'assert' object was sent")
}
}

@Test
def void testWithNoAssertShouldFail() {
'''
test "should fail a test with no assert" {
}
'''.shouldFailTestForAssertObject
}

@Test
def void testUsingAssertButNotSendingAnyMessageShouldFail() {
'''
test "should fail a test using assert but calling no method" {
assert
}
'''.shouldFailTestForAssertObject
}

@Test
def void testNotCallingAssertInElseShouldFail() {
'''
test "should fail avoid calling assert inside if" {
var a = 5
if (a > 1) a = 10 else assert.equals(1, 1)
}
'''.shouldFailTestForAssertObject
}

@Test
def void testNotCallingAssertInIfShouldFail() {
'''
test "should fail avoid calling assert inside else" {
var a = 5
if (a < 5) assert.equals(1, 1) else a = 10
}
'''.shouldFailTestForAssertObject
}

@Test
def void testNotCallingAssertInTryShouldFail() {
'''
test "should fail avoid calling assert inside try" {
try {
} catch e : Exception {
assert.equals(1, 1)
}
}
'''.shouldFailTestForAssertObject
}

def void testNotCallingAssertInCatchShouldPass() {
'''
test "should fail avoid calling assert inside try" {
try {
1 / 0
assert.equals(1, 1)
} catch e : Exception {
}
}
'''.interpretPropagatingErrors
}

@Test
def void testCallingAssertInThenAlwaysShouldNotFail() {
'''
test "should not fail calling assert in then always only" {
try {
1 / 0
} catch e : Exception {
} then always {
assert.equals(1, 1)
}
}
'''.interpretPropagatingErrorsWithoutStaticChecks
}

@Test
def void testCallingAssertInBlockShouldNotFail() {
'''
test "should not fail calling assert inside block" {
{ value => assert.equals(value, 1) }.apply(1)
}
'''.interpretPropagatingErrorsWithoutStaticChecks
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ class TestTestCase extends AbstractWollokInterpreterTestCase {
Assert.assertEquals("Couldn't resolve reference to pepa", e.originalCause.message)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,14 @@ object assertsTest {
method m11(clos, comparer) {
assert.throwsExceptionByComparing(clos, comparer)
}


// XPECT methodType at m12 --> () => Void
method m12() {
assert.doesNotThrowException({ 1 + 1 })
}

// XPECT methodType at m13 --> ({() => Any}) => Void
method m13(clos) {
assert.doesNotThrowException(clos)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ describe "without fixture" {
test "test in suite" {
// XPECT type at y --> String
var y = s
assert.that(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
test "solitary number" {
// XPECT type at x --> Number
var x = 1
assert.that(true)
}

test "solitary string" {
// XPECT type at x --> String
var x = ""
assert.that(true)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* XPECT_SETUP org.uqbar.project.wollok.tests.xpect.WollokXPectTest END_SETUP */
import wollok.lib.assert

// XPECT warnings --> "Tests must send at least one message to assert object" at ""testWithBinaryOperation""
test "testWithBinaryOperation" {
1 + 1
}

test "okTest" {
assert.equals(1, 1.0)
}

// XPECT warnings --> "Tests must send at least one message to assert object" at ""testForVariable""
test "testForVariable" {
assert
}

// XPECT warnings --> "Tests must send at least one message to assert object" at ""testForBinaryOperationForAssert""
test "testForBinaryOperationForAssert" {
assert + 1
}

test "testOkTryAndCatchWithAssert" {
try {
assert.equals(1, 1)
} catch e : Exception {
assert.equals(2, 2)
}
}

test "testOkThenAlwaysWithAssert" {
var a = 0
try {
a = 1
} catch e : Exception {
a = 2
} then always {
assert.equals(1, a)
}
}

test "testOkTryCatchWithAssertAndThenAlwaysWithoutAssert" {
try {
assert.equals(2, 2)
} catch e : Exception {
assert.equals(1, 1)
} then always {

}
}

// XPECT warnings --> "Tests must send at least one message to assert object" at ""testNotOkIfWithAssertOnlyInThen""
test "testNotOkIfWithAssertOnlyInThen" {
const a = 1
if (a > 0) assert.equals(a, 1)
}

// XPECT warnings --> "Tests must send at least one message to assert object" at ""testNotOkIfWithAssertOnlyInElse""
test "testNotOkIfWithAssertOnlyInElse" {
var a = 1
if (a > 0) { a = a + 1 } else assert.equals(a, 1)
}

// XPECT warnings --> "Tests must send at least one message to assert object" at ""testTryCatchWithoutAssert""
test "testTryCatchWithoutAssert" {
try {
1 / 0
} catch e : Exception {
1 + 1
}
}

test "testCatchWithoutAssert" {
try {
1 / 0
} catch e : Exception {
assert.equals(1, 1)
1 + 1
}
}

test "testTryWithoutAssert" {
try {
assert.equals(1, 1)
1 / 0
} catch e : Exception {
1 + 1
}
}

// XPECT warnings --> "Tests must send at least one message to assert object" at ""testTryCatchThenAlwaysWithoutAssert""
test "testTryCatchThenAlwaysWithoutAssert" {
var a = 0
try {
a = 1
} catch e : Exception {
a = 2
} then always {
a = 3
}
}

test "testOkIfWithAssert" {
const a = 1
if (a > 0) assert.equals(a, 1) else assert.equals(2, 2)
}

// XPECT warnings --> "Tests must send at least one message to assert object" at ""testElseWithoutAssert""
test "testElseWithoutAssert" {
const a = 1
if (a > 0) assert.equals(a, 1)
}

// XPECT warnings --> "Tests must send at least one message to assert object" at ""testIfWithoutAssert""
test "testIfWithoutAssert" {
var a = 1
if (a > 0) a = 2 else assert.equals(a, 1)
}
Loading

0 comments on commit bc376c3

Please sign in to comment.