• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

christhekeele / matcha / c6283b1b9bb7327327b0d71ceeefc0a6376715f6

21 Nov 2023 06:40PM UTC coverage: 63.884% (+0.1%) from 63.782%
c6283b1b9bb7327327b0d71ceeefc0a6376715f6

push

github

christhekeele
Change 'source' accessor to 'raw'; emphasize 'raw' terminology in docs.

3 of 18 new or added lines in 7 files covered. (16.67%)

2 existing lines in 2 files now uncovered.

398 of 623 relevant lines covered (63.88%)

216.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.55
/lib/matcha/source.ex
1
defmodule Matcha.Source do
2
  @moduledoc """
3
  Functions that work with the raw Erlang terms representing a match spec.
4

5
  The raw "source" code of a match specification is what Matcha calls data that fits the Erlang
6
  [match specification](https://www.erlang.org/doc/apps/erts/match_spec.html) grammar.
7

8
  Matcha compiles Elixir code into such data, and wraps that data in structs.
9
  This module is the bridge between those structs and the Erlang functions that
10
  know how to operate on them.
11
  """
12

13
  @match_all :"$_"
14
  @all_matches :"$$"
15

16
  @type match_all :: unquote(@match_all)
17
  @type all_matches :: unquote(@all_matches)
18

19
  @type pattern :: tuple | atom
20
  @type conditions :: [condition]
21
  @type condition :: tuple
22
  @type filter :: {pattern, conditions}
23
  @type body :: [expression]
24
  @type expression :: atom | match_all | all_matches | tuple | term
25
  @type clause :: {pattern, conditions, body}
26
  @type spec :: [clause]
27
  @type uncompiled :: spec
28

29
  @type type :: :table | :trace
30

31
  @type trace_flags :: list()
32
  @type trace_message :: charlist()
33

34
  @type match_target :: tuple() | list(tuple()) | term()
35
  @type match_result ::
36
          {:ok, any, trace_flags, [{:error | :warning, charlist}]}
37
          | {:error, [{:error | :warning, charlist}]}
38

39
  @type table_match_result ::
40
          {:ok, any, [], [{:error | :warning, charlist}]}
41
          | {:error, [{:error | :warning, charlist}]}
42
  @type trace_match_result ::
43
          {:ok, boolean | trace_message, trace_flags, [{:error | :warning, charlist}]}
44
          | {:error, [{:error | :warning, charlist}]}
45

46
  @type compiled :: :ets.comp_match_spec()
47

48
  def __match_all__, do: @match_all
9✔
UNCOV
49
  def __all_matches__, do: @all_matches
×
50

51
  @spec compile(source :: uncompiled) :: compiled
52
  @doc """
53
  Compiles raw match spec `source` into an opaque, more efficient internal representation.
54
  """
55
  def compile(source) do
56
    :ets.match_spec_compile(source)
365✔
57
  end
58

59
  @spec compiled?(any) :: boolean
60
  @doc """
61
  Checks if provided `value` is a compiled match spec source.
62
  """
63
  def compiled?(value) do
64
    :ets.is_compiled_ms(value)
365✔
65
  end
66

67
  @spec ensure_compiled(source :: uncompiled | compiled) :: compiled
68
  @doc """
69
  Ensures provided raw match spec `source` is compiled.
70
  """
71
  def ensure_compiled(source) do
72
    if :ets.is_compiled_ms(source) do
×
73
      source
×
74
    else
75
      compile(source)
×
76
    end
77
  end
78

79
  @spec run(source :: uncompiled | compiled, list) :: list
80
  @doc """
81
  Runs a raw match spec `source` against a list of values.
82
  """
83
  def run(source, list) do
84
    if compiled?(source) do
365✔
85
      :ets.match_spec_run(list, source)
365✔
86
    else
87
      :ets.match_spec_run(list, compile(source))
×
88
    end
89
  end
90

91
  @spec test(source :: uncompiled, type, match_target) :: match_result
92
  @doc """
93
  Validates raw match spec `source` of variant `type` and tries to match it against `match_target`.
94
  """
95
  def test(source, type, match_target) do
96
    :erlang.match_spec_test(match_target, source, type)
1,301✔
97
  end
98
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc