Home > gcontracts, releases > GContracts 1.1.2 Released!

GContracts 1.1.2 Released!

I am proud to announce that GContracts 1.1.2 has just been released and is available now in the Central Maven repository [0] and at Github [1].

The release mainly includes internal refactorings, bug fixes and support for Groovy’s power assertions [2].

Therefore, 1.1.2 upwards depends on Groovy 1.7. If there is the demand to maintain a separate Groovy 1.6 branch, I will certainly do that – just let me know.

Groovy Power Assertions & GContracts

Before 1.1.2, GContracts custom handling of assertion messages prevented triggering of Groovy’s power assertion rewriting transformation. Certain parts of GContracts have been rewritten to support power assertions know. The greatest advantage of power assertions are certainly there expressiveness:

import org.gcontracts.annotations.*

@Invariant({ speed >= 0 && speed <= 100 })
class Rocket  {
    int speed
    boolean started

    @Requires({ !started })
    def start() { started = true }

    @Requires({ started })
    @Ensures({ old -> (speed - old.speed) > 0 })
    def accelerate()  { speed += 10 }
}

class BetterRocket extends Rocket {
    @Override
    def accelerate() {
      speed += 20
    }
}

def betterRocket = new BetterRocket()
betterRocket.accelerate()

Output:

Assertion failed: 

<inherited precondition> Rocket.accelerate()

{ started }
  |
  false

	at Rocket.precondition_java_lang_Object_accelerate(ConsoleScript0:11)
	at BetterRocket.super$2$precondition_java_lang_Object_accelerate(ConsoleScript0)

Let’s say we would introduce a bug in BetterRocket‘s implementation that will violate the parent’s postcondition:

class BetterRocket extends Rocket {
    @Override
    def accelerate() {
      speed = 20
    }
}

def betterRocket = new BetterRocket()
betterRocket.start()
betterRocket.accelerate()
// ...
betterRocket.accelerate()

Output:

Assertion failed: 

<inherited postcondition> Rocket.accelerate()

{ old -> (speed - old.speed) > 0 }
          |     | |   |      |
          20    0 |   20     false
                  {speed=20}

	at Rocket.postcondition_java_lang_Object_accelerate(ConsoleScript3:12)
	at BetterRocket.super$2$postcondition_java_lang_Object_accelerate(ConsoleScript3)

The assertion message shows what assertion got violated and has visual pointers to the computed expression values – assertion messages can’t be more readable than that.

Changes

ISSUE-7: use Groovy power asserts as an alternative to AssertionStatement
ISSUE-16: old variables gets cleared on inherited postcondition

[0] GContracts 1.1.2 in Central Maven Repository
[1] GContracts at Github
[2] Groovy Power Assertions

Categories: gcontracts, releases
  1. Andrej
    November 3, 2010 at 10:08 am

    Hallo,
    it is possible to make GContracts configurable?

    I need to throw and catch a exception instead of throw a assertion error.

    For example RequiresException, EnsureException and InvariantException.

    I need to generate pretty error messages for my users if some object don’t exists in the db.

    With your currently implementation I can only catch the org.codehaus.groovy.transform.powerassert.PowerAssertionError

    Thanks for your advice

  1. No trackbacks yet.

Leave a comment