aboutsummaryrefslogtreecommitdiff
path: root/test/elixir-config/attr_test.exs
blob: c5cab5bd81483db4e407cf57d3cda40a167d6eb5 (plain) (blame)
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
defmodule Ejabberd.Config.AttrTest do
  use ExUnit.Case, async: true

  alias Ejabberd.Config.Attr

  test "extract attrs from single line block" do
    block = quote do
      @active false
    end

    block_res = Attr.extract_attrs_from_block_with_defaults(block)
    assert {:active, false} in block_res
  end

  test "extract attrs from multi line block" do
    block = quote do
      @active false
      @opts [http: true]
    end

    block_res = Attr.extract_attrs_from_block_with_defaults(block)
    assert {:active, false} in block_res
    assert {:opts, [http: true]} in block_res
  end

  test "inserts correctly defaults attr when missing in block" do
    block = quote do
      @active false
      @opts [http: true]
    end

    block_res = Attr.extract_attrs_from_block_with_defaults(block)

    assert {:active, false} in block_res
    assert {:git, ""} in block_res
    assert {:name, ""} in block_res
    assert {:opts, [http: true]} in block_res
    assert {:dependency, []} in block_res
  end

  test "inserts all defaults attr when passed an empty block" do
    block = quote do
    end

    block_res = Attr.extract_attrs_from_block_with_defaults(block)

    assert {:active, true} in block_res
    assert {:git, ""} in block_res
    assert {:name, ""} in block_res
    assert {:opts, []} in block_res
    assert {:dependency, []} in block_res
  end

  test "validates attrs and returns errors, if any" do
    block = quote do
      @not_supported_attr true
      @active "false"
      @opts [http: true]
    end

    block_res =
      block
      |> Attr.extract_attrs_from_block_with_defaults
      |> Attr.validate

    assert {:ok, {:opts, [http: true]}} in block_res
    assert {:ok, {:git, ""}} in block_res
    assert {:error, {:not_supported_attr, true}, :attr_not_supported} in block_res
    assert {:error, {:active, "false"}, :type_not_supported} in block_res
  end

  test "returns the correct type for an attribute" do
    assert :boolean == Attr.get_type_for_attr(:active)
    assert :string == Attr.get_type_for_attr(:git)
    assert :string == Attr.get_type_for_attr(:name)
    assert :list == Attr.get_type_for_attr(:opts)
    assert :list == Attr.get_type_for_attr(:dependency)
  end

  test "returns the correct default for an attribute" do
    assert true == Attr.get_default_for_attr(:active)
    assert "" == Attr.get_default_for_attr(:git)
    assert "" == Attr.get_default_for_attr(:name)
    assert [] == Attr.get_default_for_attr(:opts)
    assert [] == Attr.get_default_for_attr(:dependency)
  end
end