aboutsummaryrefslogtreecommitdiff
path: root/plugins/editorconfig/tests/init.lua
blob: 654067b1f021c483840a486af0eb55d0a64e1d17 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
local Parser = require "plugins.editorconfig.parser"

local tests = {}

---@class tests.test
---@field name string Name of test
---@field config string Path to config file
---@field in_match string A path to test against the config
---@field out_match string A regex to match against the result

---Registered tests
---@type tests.test[]
tests.list = {}

--- parsers cache
---@type table<string,plugins.editorconfig.parser>
local parsers = {}
setmetatable(parsers, {
  __index = function(t, k)
    local v = rawget(t, k)
    if v == nil then
      v = Parser.new(k)
      rawset(t, k, v)
    end
    return v
  end
})

---Adds color to given text on non windows systems.
---@param text string
---@param color "red" | "green" | "yellow"
---@return string colorized_text
local function colorize(text, color)
  if PLATFORM ~= "Windows" then
    if color == "green" then
      return "\27[92m"..text.."\27[0m"
    elseif color == "red" then
      return "\27[91m"..text.."\27[0m"
    elseif color == "yellow" then
      return "\27[93m"..text.."\27[0m"
    end
  end
  return text
end

local PASSED = colorize("PASSED", "green")
local FAILED = colorize("FAILED", "red")

---Runs an individual test (executed by tests.run())
---@param name string Test name
---@param config_path string Relative path to tests diretory for a [config].in
---@param in_match string Filename to match
---@param out_match string | table Result to match regex
function tests.check_config(name, config_path, in_match, out_match, pos, total)
  if type(out_match) == "string" then
    out_match = { out_match }
  end
  local parser = parsers[USERDIR .. "/plugins/editorconfig/tests/" .. config_path]
  local config = parser:getConfigString(in_match)
  local passed = true
  for _, match in ipairs(out_match) do
    if not regex.match(match, config) then
      passed = false
      break
    end
  end
  if pos then
    pos = "[" .. pos .. "/" .. total .. "] "
  else
    pos = ""
  end
  if passed then
    print(pos .. string.format("%s - %s - '%s': %s", name, in_match, config_path, PASSED))
  else
    print(pos .. string.format("%s - %s - '%s': %s", name, in_match, config_path, FAILED))
    print(config)
  end
  return passed
end

---Register a new test to be run later.
---@param name string Test name
---@param config_path string Relative path to tests diretory for a [config].in
---@param in_match string Filename to match
---@param out_match string | table Result to match regex
function tests.add(name, config_path, in_match, out_match)
  table.insert(tests.list, {
    name = name,
    config = config_path,
    in_match = in_match,
    out_match = out_match
  })
end

---Runs all registered tests and outputs the results to terminal.
function tests.run()
  print "========================================================="
  print "Running Tests"
  print "========================================================="
  local failed = 0
  local passed = 0
  local total = #tests.list
  for i, test in ipairs(tests.list) do
    local res = tests.check_config(
      test.name, test.config, test.in_match, test.out_match, i, total
    )
    if res then passed = passed + 1 else failed = failed + 1 end
  end
  print "========================================================="
  print (
    string.format(
      "%s %s %s",
      colorize("Total tests: " .. #tests.list, "yellow"),
      colorize("Passed: " .. passed, "green"),
      colorize("Failed: " .. failed, "red")
    )
  )
  print "========================================================="
end

function tests.add_parser(config_path)
  return parsers[config_path]
end

function tests.run_parsers()
  print "========================================================="
  print "Running Parsers"
  print "========================================================="

  for config, parser in pairs(parsers) do
    print "---------------------------------------------------------"
    print(string.format("%s results:", config))
    for _, section in ipairs(parser.sections) do
      print(string.format("\nPath expression: %s", section.rule.expression))
      print(string.format("Regex: %s", section.rule.regex))
      print(string.format("Negation: %s", section.rule.negation and "true" or "false"))
      print(string.format("Ranges: %s\n", section.rule.ranges and #section.rule.ranges or "0"))
    end
    print "---------------------------------------------------------"
  end
end

return tests