Skip to content

Commit

Permalink
Add test.is_file and test.is_dir tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiojr committed Oct 3, 2024
1 parent 0035a17 commit 8e47b74
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
29 changes: 26 additions & 3 deletions lib/test.risor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import color
import exec
import regexp
import os

__fatal := false
__raises := false
Expand All @@ -13,14 +14,16 @@ failed_is_fatal := func(b) {
}

_last_error := nil
// internal use only
failed_raises := func() {
__raises = true
}
// internal use only
failed_does_not_raise := func() {
__raises = false
}

raised_error := errors.new("error raised")
_raised_error := errors.new("error raised")

func exit_if_fatal() {
if __fatal {
Expand All @@ -41,7 +44,7 @@ func print_ok(desc) {
func print_fail(desc) {
if __raises {
if _last_error == nil {
error(raised_error)
error(_raised_error)
}
error(_last_error)
_last_error = nil
Expand Down Expand Up @@ -75,7 +78,7 @@ func equals(desc, o1, o2) {
// desc: the test description
// fn: the function that should raise an error
// err: the error that should be raised
func raises(desc, fn, err) {
func raises(desc, fn, err="") {
happened := false
try(fn, func(e) {
happened = true
Expand Down Expand Up @@ -146,3 +149,23 @@ func stdout_matches(desc, cmd, reg) {
print_fail(desc + '(output {out} does not match {reg})')
}
}

func is_dir(desc, path) {
ok := try(func() { f := os.stat(path); f.is_dir }, false)
if ok {
print_ok(desc)
} else {
//error(raised_error)
print_fail(desc)
}
}

func is_file(desc, path) {
ok := try(func() { f := os.stat(path); !f.is_dir }, false)
if ok {
print_ok(desc)
} else {
print_fail(desc)
//error(raised_error)
}
}
24 changes: 20 additions & 4 deletions tests/test.risor
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import test
import filepath
import os

test.failed_raises()

Expand All @@ -8,7 +10,7 @@ test.equals("foo == foo", "foo", "foo")
test.equals("comparing integers works", 1, 1)
test.raises("foo == bar should raise", func() {
test.equals("foo == bar", "foo", "bar")
}, test.raised_error)
})


test.nothing_raised("no-op should not raise", func() {
Expand All @@ -18,15 +20,29 @@ test.raises("nothing_raised fails when test raises", func() {
test.nothing_raised("", func() {
error("foo")
})
}, test.raised_error)
})

test.is_true("is_true should not fail with true", true)
test.is_true("is_true should not fail when expresion returns true", "foo" == "foo")
test.raises("is_true with false should false", func() {
test.is_true("false is true", false)
}, test.raised_error)
})

test.stdout_matches("echo foo matches foo", "echo foo", "^foo.*")
test.raises("is_true with false should false", func() {
test.stdout_matches("echo foo does not match bar", "echo foo", "bar")
}, test.raised_error)
})

test.is_dir('{os.temp_dir()} is a directory', os.temp_dir())
tmp_file := uuid()
test.raises('random file {tmp_file} is not a directory', func() {
test.is_dir("", tmp_file)
})

tmp_file = filepath.join(os.temp_dir(), uuid())
f := os.create(tmp_file)
f.close()
test.is_file('{tmp_file} is a file', tmp_file)
test.raises('{os.temp_dir()} is not a file', func() {
test.is_file("", os.temp_dir())
})

0 comments on commit 8e47b74

Please sign in to comment.