<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE chapter SYSTEM "chapter.dtd">

<chapter>
  <header>
    <copyright>
      <year>2002</year><year>2009</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.
    
      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.
    
    </legalnotice>

    <title>Examples</title>
    <prepared>Siri Hansen</prepared>
    <docno></docno>
    <date></date>
    <rev></rev>
    <file>example_chapter.xml</file>
  </header>

  <section>
    <title>Test suite</title>
    <code type="none">
-module(my_SUITE).

-export([all/1,
         not_started/1, not_started_func1/1, not_started_func2/1,
         start/1, stop/1,
         func1/1, func2/1 
        ]).

-export([init_per_testcase/2, end_per_testcase/2]).

-include("test_server.hrl").

-define(default_timeout, ?t:minutes(1)).

init_per_testcase(_Case, Config) ->
    ?line Dog=?t:timetrap(?default_timeout),
    [{watchdog, Dog}|Config].
end_per_testcase(_Case, Config) ->
    Dog=?config(watchdog, Config),
    ?t:timetrap_cancel(Dog),
    ok.

all(suite) -> 
    %% Test specification on test suite level
    [not_started,
     {conf, start, [func1, func2], stop}].

not_started(suite) ->
    %% Test specification on test case level
    [not_started_func1, not_started_func2];
not_started(doc) ->
    ["Testing all functions when application is not started"].
%% No execution clause unless the specification clause returns [].


not_started_func1(suite) ->
    [];
not_started_func1(doc) ->
    ["Testing function 1 when application is not started"].
not_started_func1(Config) when list(Config) ->
    ?line {error, not_started} = myapp:func1(dummy_ref,1),
    ?line {error, not_started} = myapp:func1(dummy_ref,2),
    ok.

not_started_func2(suite) ->
    [];
not_started_func2(doc) ->
    ["Testing function 2 when application is not started"].
not_started_func2(Config) when list(Config) ->
    ?line {error, not_started} = myapp:func2(dummy_ref,1),
    ?line {error, not_started} = myapp:func2(dummy_ref,2),
    ok.


%% No specification clause needed for an init function in a conf case!!!
start(doc) ->
    ["Testing start of my application."];
start(Config) when list(Config) ->
    ?line Ref = myapp:start(),
    case erlang:whereis(my_main_process) of
      Pid when pid(Pid) -> 
         [{myapp_ref,Ref}|Config];
      undefined -> 
         %% Since this is the init function in a conf case, the rest of the 
         %% cases in the conf case will be skipped if this case fails.
         ?t:fail("my_main_process did not start")
    end.

func1(suite) ->
    [];
func1(doc) ->
    ["Test that func1 returns ok when argument is 1 and error if argument is 2"];
func1(Config) when list(Config) ->
    ?line Ref = ?config(myapp_ref,Config),
    ?line ok = myapp:func1(Ref,1),
    ?line error = myapp:func1(Ref,2),
    ok.

func2(suite) ->
    [];
func2(doc) ->
    ["Test that func1 returns ok when argument is 3 and error if argument is 4"];
func2(Config) when list(Config) ->
    ?line Ref = ?config(myapp_ref,Config),
    ?line ok = myapp:func2(Ref,3),
    ?line error = myapp:func2(Ref,4),
    ok.

%% No specification clause needed for a cleanup function in a conf case!!!
stop(doc) ->
    ["Testing termination of my application"];
stop(Config) when list(Config) ->
    ?line Ref = ?config(myapp_ref,Config),
    ?line ok = myapp:stop(Ref),
    case erlang:whereis(my_main_process) of
      undefined -> 
         lists:keydelete(myapp_ref,1,Config);
      Pid when pid(Pid) -> 
         ?t:fail("my_main_process did not stop")
    end.
    </code>
  </section>

  <section>
    <title>Test specification file</title>
    <p><em><c>myapp.spec:</c></em></p>
    <code type="none">
{topcase, {dir, "../myapp_test"}}. % Test specification on top level    </code>
    <p><em><c>myapp.spec.vxworks:</c></em></p>
    <code type="none">
{topcase, {dir, "../myapp_test"}}. % Test specification on top level
{skip,{my_SUITE,func2,"Not applicable on VxWorks"}}.    </code>
  </section>
</chapter>


