Eiffel has a feature Design by Contract. According to this, for any routine, there is a check that assertions such as require and ensure must be true. However, I haven't been able to find out how to enforce the rule through command line that if the assertions are false, there must be an error/exception thrown by the compiler or during run time.
For example, if this is the root class,
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE}
object: TEST1
make
-- Run application.
do
create object
object.function(-1)
print(object.value)
end
end
and this is the TEST1 class
class
TEST1
feature
value: INTEGER
-- value for testing
function(val: INTEGER)
-- Assign
require
val>0
do
value:=val
ensure
value>0
end
end
The program compiles and executes without any error, even though both the assertions are violated.
I am compiling using
ec application.e
However, Eiffel Studio does report a violation of contract.
So, how can one enforce this through command line? Is there a particular flag to use?
It looks like GUI version of EiffelStudio and command-line version of EiffelStudio use different defaults when they create new projects: command-line version turns off assertion monitoring and GUI version turns on assertion monitoring. In fact you can open a project created with a command-line version in GUI, or, conversely, compile a project created in GUI version by a command-line compiler. The project settings are kept in a
.ecffile and are part of a project, i.e. this.ecffile should be distributed together with the project.To address the particular issue you describe, open the
.ecfin GUI (File | Open Project | Add Project | Open), then navigate to Project | Project Settings | Assertions, turn on all assertions and press Save, exit from the GUI version. Now when you recompile your project from the command-line, the assertions will be turned on and you'll get an exception trace during execution as expected.