How to fix the Unbound module Graphics in an ocaml project

Image
From ~/pr/gitl/ocaml-gol In a constant effort to learn new programming languages, I'm currently trying to use ocaml , a free and open-source general-purpose, multi-paradigm programming language maintained at the Inria . It's basically an extension of Caml with object-oriented features. I'm mostly interested by its functionnal and pattern matching features but the module part of the language can be a bit difficult to understand for someone with little to none ML (Meta Language) background.   The error When trying to use the graphics module to create a graphical window and go just a little further than the simplest helloworld program, here is the result : If the project uses dune : (executable (name ocaml_project) (libraries lwt.unix graphics) ) with this code : let () = Printf.printf "Hello, world!\n";; Lwt_io.printf "Hello, world!\n";; Graphics.open_graph " 800x600";; The first times I built this project running the du

How to capture ruby console output

While writing unit tests for a ruby project, headers-date, I had to unit test a function that print some useful debugging informations in terminal using IO#puts stdlib function.
This code originally was posted to the following google group.

An emacs/console example screenshot showing ruby unit tests results

The tested class

Let's imagine a to-be-tested class that use puts, print or p IO methods to print some text to stdout (or stderr).
class TestMe
  def print
    puts "Some useful text"
  end
end

The capture trick

To help testing the printed text, we will implement a new function called util_capture. It simply temporarily replace both $stdout and $stderr variables. The dollar sign prefix (sometimes called a sigil)in ruby is used to identify global variables. Here are really sparce official documentation on $stddout and $stderr

The yield keyword will be replaced by your block.

def util_capture
  require 'stringio'
  orig_stdout = $stdout.dup
  orig_stderr = $stderr.dup
  captured_stdout = StringIO.new
  captured_stderr = StringIO.new
  $stdout = captured_stdout
  $stderr = captured_stderr
  yield
  captured_stdout.rewind
  captured_stderr.rewind
  return captured_stdout, captured_stderr
ensure
  $stdout = orig_stdout
  $stderr = orig_stderr
end

Capture and test

The usage is pretty simple. With the ability of ruby of returning multiple variables, the util_capture function will simply return the two captured ones filled by the block output that occured at the yield keyword :
def test_testme
  tm = TestMe.new
  out, err = util_capture do
    tm.print          # Call the function that should use 'puts'
  end
  assert_equal "useful", out.string # Standard output
  assert_equal "", err.string       # Errors
end
So, now, you can test the printed output using asserts in a unit test case :
assert_equal "useful", out.string

You can also use assert_match to test if the printed string match a regex :

assert_match /\d+/, out.string

Conclusion

This trick is very simple to setup and reusable if you have to test the output of a tested code.

Comments

Popular posts from this blog

How to make a map of variant in C++