Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Block#call_with_errors_logged , #call_with_errors_logged: , #call…

…_with_errors_logged_to: & #call:with_errors_logged_to:
  • Loading branch information...
commit 3ce0c6994f21ce8cbf6530d5f10591ee913be187 1 parent b32758d
Christopher Bertels authored
Showing with 90 additions and 0 deletions.
  1. +47 −0 lib/block.fy
  2. +43 −0 tests/block.fy
47 lib/block.fy
View
@@ -284,4 +284,51 @@ class Block {
self
}
+
+ def call_with_errors_logged {
+ """
+ Calls @self while logging any errors to @*stdout*.
+ """
+
+ call_with_errors_logged_to: *stdout*
+ }
+
+ def call_with_errors_logged: args {
+ """
+ @args @Array@ of arguments to call @self with.
+
+ Calls @self with @args while logging any errors to @*stdout*.
+ """
+
+ call: args with_errors_logged_to: *stdout*
+ }
+
+ def call_with_errors_logged_to: io {
+ """
+ @io @IO@ object to log any errors to.
+
+ Calls @self while logging any errors to @io.
+ """
+
+ try {
+ self call
+ } catch StandardError => e {
+ io println: e
+ }
+ }
+
+ def call: args with_errors_logged_to: io {
+ """
+ @args @Array@ of arguments to call @self with.
+ @io @IO@ object to log any errors to.
+
+ Calls @self with @args while logging any errors to @io.
+ """
+
+ try {
+ self call: args
+ } catch StandardError => e {
+ io println: e
+ }
+ }
}
43 tests/block.fy
View
@@ -340,4 +340,47 @@ FancySpec describe: Block with: {
block call
a is: [2,1,2,1]
}
+
+ it: "calls itself while logging errors to *stdout*" with: 'call_with_errors_logged when: {
+ io = StringIO new
+ let: '*stdout* be: io in: {
+ {
+ "hello" println
+ 2 / 0
+ } call_with_errors_logged
+ }
+ io string is: "hello\ndivided by 0\n"
+ }
+
+ it: "calls itself with arguments while logging errors to *stdout*" with: 'call_with_errors_logged: when: {
+ io = StringIO new
+ let: '*stdout* be: io in: {
+ |x y| {
+ "x: #{x} y: #{y}" println
+ 2 / 0
+ } call_with_errors_logged: [10, 20]
+ }
+ io string is: "x: 10 y: 20\ndivided by 0\n"
+ }
+
+ it: "calls itself while logging errors to a given IO object" with: 'call_with_errors_logged_to: when: {
+ io = StringIO new
+ {
+ 2 / 0
+ } call_with_errors_logged_to: io
+ io string is: "divided by 0\n"
+
+ io = StringIO new
+ { "fail!" raise! } call_with_errors_logged_to: io
+ io string is: "fail!\n"
+ }
+
+ it: "calls itself with arguments while logging errors to a given IO object" with: 'call:with_errors_logged_to: when: {
+ io = StringIO new
+ |x y| {
+ io println: "x: #{x} y: #{y}"
+ 2 / 0
+ } call: [10, 20] with_errors_logged_to: io
+ io string is: "x: 10 y: 20\ndivided by 0\n"
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.