Search⌘ K
AI Features

Trying Out the Core

Explore how to test and validate a functional core in Elixir by creating a quiz template with addition questions, generating correct and incorrect responses, and tracking user answers using OTP design principles and iex interactive shell.

Running our tools

To check our code as we go, iex is an excellent tool. We’re not going to run an exhaustive test—we’ll save that work for the next chapter. We’ll use iex to do a quick integration check to ensure our tools work together as expected.

Making our template

To do any meaningful integration test, we need a quiz. We’ll need a template before building one, though. Our quiz will use a single template for addition that generates questions of the form x + y. Open up the codebase interactively and key in the following commands:

Executable

C++
# Aliases
alias Mastery.Core.{Template, Quiz, Response}
C++
# Declaring the generator
generator = %{ left: [1, 2], right: [1, 2] }
C++
checker = fn(sub, answer) ->
sub[:left] + sub[:right] == String.to_integer(answer)
end

Output

iex(1)> alias Mastery.Core.{Template, Quiz, Response} 
[Mastery.Core.Template, Mastery.Core.Quiz, Mastery.Core.Response] 

iex(2)> generator = %{ left: [1, 2], right: [1, 2] }
%{left: [1, 2], right: [1, 2]}

iex(3)> checker = fn(sub, answer) ->
...(3)> sub[:left] + sub[:right] == String.to_integer(answer) 
...(3)> end
#Function<12.99386804/2 in :erl_eval.expr/5>
  • We get ...