1%% Test that a spec containing more items than actually returned
2%% (whether by accident or by benign overspeccing) does not prevent
3%% detection of impossible matches.
4
5-module(extra_range).
6
7-export([t1/2, t2/2, t3/2, t4/2]).
8
9-dialyzer([no_return]).
10
11%% this spec matches the behaviour of the code
12-spec normal(integer()) -> ok | error.
13normal(1) -> ok;
14normal(2) -> error.
15
16t1(X, Y) when is_integer(X), is_integer(Y) ->
17    ok = normal(X),
18    error = normal(Y),
19    ok.
20
21
22%% this spec has a typo, which should cause anyone trying to match on
23%% `ok = typo(X)' to get a warning, because `ok' is not in the spec
24-spec typo(integer()) -> ook | error.
25typo(1) -> ok;
26typo(2) -> error.
27
28t2(X, Y) when is_integer(X), is_integer(Y) ->
29    ok = typo(X),  % warning expected - not allowed according to spec
30    error = typo(Y),
31    ok.
32
33
34%% this is overspecified, and should cause a warning for trying
35%% to match on `no = over(X)', because it cannot succeed and either
36%% the spec should be updated or the code should be extended
37-spec over(integer()) -> yes | no | maybe.
38over(1) -> yes;
39over(_) -> maybe.
40
41t3(X, Y) when is_integer(X), is_integer(Y) ->
42    yes = over(X),
43    no = over(Y),  % warning expected - spec or code needs fixing
44    maybe = over(X + Y),
45    ok.
46
47
48%% this is underspecified, which should cause anyone trying to match on
49%% `maybe = under(X)' to get a warning, because `maybe' is not in the spec
50-spec under(integer()) -> yes | no.
51under(1) -> yes;
52under(2) -> no;
53under(_) -> maybe.
54
55t4(X, Y) when is_integer(X), is_integer(Y) ->
56    yes = under(X),
57    no = under(Y),
58    maybe = under(X + Y), % warning expected - not in spec
59    ok.
60