Skip to content

Commit

Permalink
- Added the following built-in keyowrds: assert, assert_eq and `a…
Browse files Browse the repository at this point in the history
…ssert_ne` which will stop the program execution when they don't evaluate to a true value.

Example:
	assert(1 == 2);		# assert: not true at script.sf line 1.
	assert_eq(1, 2);	# assert_eq: 1 == 2 is false at script.sf line 2.
	assert_ne(1, 1);	# assert_ne: 1 != 1 is false at script.sf line 3.

Alternatively:
	assert(1 == 1);		# ok
	assert_eq(1, 1);	# ok
	assert_ne(1, 2);	# ok
  • Loading branch information
trizen committed Jul 21, 2015
1 parent 98fc865 commit 354e22e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 22 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ scripts/pi_and_e.sf
scripts/pi_machin_like_formula.sf
scripts/polymorphism.sf
scripts/power_numbers.sf
scripts/prefix_methods.sf
scripts/problem_of_apollonius.sf
scripts/pythagorean_means.sf
scripts/quicksort.sf
Expand Down
3 changes: 2 additions & 1 deletion lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ package Sidef::Parser {
read
die
warn
assert assert_eq assert_ne
my
var
Expand Down Expand Up @@ -1417,7 +1418,7 @@ package Sidef::Parser {
);
}

if (/\G(?:die|warn)\b/gc) {
if (/\G(?:die|warn|assert(?:_(?:eq|ne))?)\b/gc) {
pos($_) = $-[0];
return (Sidef::Sys::Sys->new(line => $self->{line}, file_name => $self->{file_name}), 1);
}
Expand Down
17 changes: 17 additions & 0 deletions lib/Sidef/Sys/Sys.pm
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ package Sidef::Sys::Sys {

*raise = \¨

sub assert {
my ($self, $bool) = @_;
$bool || $self->die("assert: not true");
}

sub assert_eq {
my ($self, $arg1, $arg2) = @_;
state $method = '==';
$arg1->$method($arg2) || $self->die("assert_eq: $arg1 $method $arg2 is false");
}

sub assert_ne {
my ($self, $arg1, $arg2) = @_;
state $method = '!=';
$arg1->$method($arg2) || $self->die("assert_ne: $arg1 $method $arg2 is false");
}

sub warn {
my ($self, @args) = @_;
if (exists $self->{file_name}) {
Expand Down
31 changes: 15 additions & 16 deletions scripts/define_methods_at_runtime.sf
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,46 @@ class Hello(arr is Array) {
#
var obj = Hello(['foo', 'bar']);

obj.foo == "foo" || die "error";
assert_eq(obj.foo, "foo");
obj.add_method('baz');
obj.add_class_method('zoo');
obj.baz == "baz" || die "error";
assert_eq(obj.baz, "baz");
obj.baz;

var name = "zoro";
obj.add_block_method(name, { name });
obj.zoro == name || die "error";
assert_eq(obj.zoro, name);

#
## Object 2
#
var obj2 = Hello();

obj2.foo == "foo" || die "error";
assert_eq(obj2.foo, "foo");

if (obj2.respond_to('baz')) {
die "error";
};

obj2.zoo == "zoo" || die "error";
obj.baz == "baz" || die "error";
obj.foo == "foo" || die "error";
assert_eq(obj2.zoo, "zoo");
assert_eq(obj.baz, "baz");
assert_eq(obj.foo, "foo");

obj.add_method('fox');
obj.fox == 'fox' || die "error";
assert_eq(obj.fox, 'fox');

obj.def_method('foo', {'bar'});
obj.foo == 'bar' || die "error";
obj2.foo == 'foo' || die "error";
assert_eq(obj.foo, 'bar');
assert_eq(obj2.foo, 'foo');

obj.add_method_with_arg('bau');
obj.bau('~') == "~bau" || die "error";
assert_eq(obj.bau('~'), "~bau");

obj2.add_method_with_arg('boo');
obj2.boo('-') == "-boo" || die "error";
obj.bau('/') == "/bau" || die "error";
assert_eq(obj2.boo('-'), "-boo");
assert_eq(obj.bau('/'), "/bau");

obj2.add_method_with_arg('bau');
obj.add_method('bau');

obj2.bau('~') == '~bau' || die "error";
obj.bau == 'bau' || die "error";
assert_eq(obj2.bau('~'), '~bau');
assert_eq(obj.bau, 'bau');
5 changes: 3 additions & 2 deletions scripts/multisplit.sf
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ var chunks = [];
chunks.append(multisplit(%w(== != =), 'a!===b=!=c!=', keep_sep => bool));
}

chunks == [
assert_eq(chunks,
[
["a", "", "b", "", "c", ""],
["a", "!=", "", "==", "b", "=", "", "!=", "c", "!=", ""],
] || die "error!";
]);

say "** Test passed!";
6 changes: 3 additions & 3 deletions scripts/object_copy.sf
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ var obj3 = Sys.copy(obj2); # make a copy to obj2

obj2.value = "foo"; # change the value of obj2

obj1.value == "T" || die "error";
obj2.value == "foo" || die "error";
obj3.value == "S" || die "error";
assert_eq(obj1.value, "T");
assert_eq(obj2.value, "foo");
assert_eq(obj3.value, "S");

obj1.display;
obj2.display;
Expand Down
19 changes: 19 additions & 0 deletions scripts/prefix_methods.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/ruby

# Tests for prefix methods

assert_eq(run { "hi" }, "hi");
assert_eq(run { var run = 42; run }, 42);
assert_eq(run { "hi" }, "hi");

var i = int 12.5;

assert_eq(i, 12);
assert_eq(lc "TeSt", "test");
assert_eq(ceil 12.1.add(2), 15);

var int = 42;
assert_eq(int, 42);
assert_eq(CORE::int 12.5, 12);

say "** Test passed!";

0 comments on commit 354e22e

Please sign in to comment.