summaryrefslogtreecommitdiff
path: root/src/couch_tests/test/couch_tests_app_tests.erl
blob: 6f9c7e419bd823a2c2757903f940e41e1f1e7782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_tests_app_tests).

-include_lib("eunit/include/eunit.hrl").

setup() ->
    [mock(application)].

teardown(Mocks) ->
    [unmock(Mock) || Mock <- Mocks].

%% ------------------------------------------------------------------
%% Test callbacks definitions
%% ------------------------------------------------------------------

dummy_setup() ->
    couch_tests:new(?MODULE, dummy_setup,
        fun(_Fixture, Ctx) -> Ctx end,
        fun(_Fixture, Ctx) -> Ctx end).


setup1(Arg1) ->
    couch_tests:new(?MODULE, setup1,
        fun(Fixture, Ctx0) ->
           Ctx1 = couch_tests:start_applications([asn1], Ctx0),
           couch_tests:set_state(Fixture, Ctx1, {Arg1})
        end,
        fun(_Fixture, Ctx) ->
           couch_tests:stop_applications([asn1], Ctx)
        end).

setup2(Arg1, Arg2) ->
    couch_tests:new(?MODULE, setup2,
        fun(Fixture, Ctx0) ->
           Ctx1 = couch_tests:start_applications([public_key], Ctx0),
           couch_tests:set_state(Fixture, Ctx1, {Arg1, Arg2})
        end,
        fun(_Fixture, Ctx) ->
           Ctx
        end).


couch_tests_test_() ->
    {
        "couch_tests tests",
        {
            foreach, fun setup/0, fun teardown/1,
            [
                {"chained setup", fun chained_setup/0}
            ]
        }
    }.


chained_setup() ->
    ?assert(meck:validate(application)),
    ?assertEqual([], history(application, start)),
    Ctx0 = couch_tests:setup([
        setup1(foo),
        dummy_setup(),
        setup2(bar, baz)
    ], [], []),

    ?assertEqual([asn1, public_key], history(application, start)),
    ?assertEqual([asn1, public_key], couch_tests:get(started_apps, Ctx0)),
    ?assertEqual([], couch_tests:get(stopped_apps, Ctx0)),

    Ctx1 = couch_tests:teardown(Ctx0),

    ?assertEqual([public_key, asn1], history(application, stop)),
    ?assertEqual([], couch_tests:get(started_apps, Ctx1)),
    ?assertEqual([public_key, asn1], couch_tests:get(stopped_apps, Ctx1)),

    ok.

mock(application) ->
    ok = meck:new(application, [unstick, passthrough]),
    ok = meck:expect(application, start, fun(_) -> ok end),
    ok = meck:expect(application, stop, fun(_) -> ok end),
    meck:validate(application),
    application.

unmock(application) ->
    catch meck:unload(application).

history(Module, Function) ->
    Self = self(),
    [A || {Pid, {M, F, [A]}, _Result} <- meck:history(Module)
        , Pid =:= Self
        , M =:= Module
        , F =:= Function].