Loading ...
Sorry, an error occurred while loading the content.

8748Re: "ocaml_beginners"::[] A way to remove this duplication?

Expand Messages
  • Martin Jambon
    Oct 2, 2007
    • 0 Attachment
      On Tue, 2 Oct 2007, Michael Feathers wrote:

      > let assert_ints_equal expected actual =
      > if expected = actual then
      > ()
      > else
      > raise (TestFailure (sprintf "expected: %d but was: %d" expected actual))
      >
      > let assert_strings_equal expected actual =
      > if expected = actual then
      > ()
      > else
      > raise (TestFailure (sprintf "expected: %s but was: %s" expected actual))
      >
      > assert_ints_equal 20 20;
      > assert_strings_equal "1" "1"
      >
      > Can anyone tell me how do I make this nicer? If I understand the type
      > system correctly, I do need two separate methods because of the
      > sprintf.

      Right.

      > Is there a better way?

      What you can do is pass a to_string function as a parameter:

      let assert_equal to_string expected actual =
      if expected = actual then
      ()
      else
      raise (TestFailure (sprintf "expected: %s but was: %s"
      (to_string expected) (to_string actual)))

      let assert_strings_equal e a = assert_equal (fun s -> s) e a
      let assert_ints_equal e a = assert_equal string_of_int e a
      ...

      Or even better:

      let assert_strings_equal = assert_equal (fun s -> s)
      let assert_ints_equal = assert_equal string_of_int



      Martin

      --
      http://martin.jambon.free.fr
    • Show all 18 messages in this topic