Elixir doctest fails for function that returns random values

Alex Fallenstedt

I have a function in Elixir that produces three random RGB tuples in a list.

defmodule Color do



  @doc """
  Create three random r,g,b colors as a list of three tuples

  ## Examples

      iex> colors = Color.pick_color()
      iex> colors
      [{207, 127, 117}, {219, 121, 237}, {109, 101, 206}]

  """
      def pick_color() do
        color = Enum.map((0..2), fn(x)->
          r = Enum.random(0..255)
          g = Enum.random(0..255)
          b = Enum.random(0..255)
          {r, g, b}
        end)
end

When I run my tests, my doctests fail. The resulting list of tuples are different than what is defined in my doctest. How can I write a doctest for a function that returns a random value?

Ronan Boiteau

In order to test a function with doctests, you must be able to predict the output of your function. In this case, you can't predict the output of your function.


However you could test your function with regular tests.

Here is a test that would make sure Color.pick_color() produces a list of 3 tuples, using pattern matching:

test "pick color" do
  [{_, _, _}, {_, _, _}, {_, _, _}] = Color.pick_color()
end

You could also check if each value is between 0 and 255, etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Doctest of function with random output

Recursive function that returns array with random X values

Elixir - Using variables in doctest

Write a function that returns random int

Random function always returns 1

The function returns invalid values

Maxima: define a function that returns a random integer in a range, such that the value is distinct from another value or a list of other values

C : function that generates random Matrix returns last line full of null values

DocTest fails when creating an object

Random function does not return random values

Elixir - How can you use an alias in doctest?

Is there a way to test an IO output using Doctest in Elixir?

SDL_GetWindowFlags() returns seemingly random values

Elixir compiler warning on ignored function return values

How can values be cached in an Elixir function?

Python random.choice() fails to work in a function

Ctypes pass float into function returns random numbers

dialyzer fails to recognise elixir functions with error :0:unknown_function

Powershell function returns intermediate values

Print function returns two values

checkPrime function returns incorrect values

counter function returns wrong values

Function which returns multiple values

My Elixir recursive function returns a list of lists, not a simple list

Elixir anonymous function application in list tail returns arithmetic error

Doctest fails due to unicode leading u

Macro fails to separate sequences when some values are random

Check constraint with function fails on update although function returns correctly

How to generate random values for a predefined function?