aboutsummaryrefslogtreecommitdiff
path: root/test/functional/plugin/man_spec.lua
blob: 53384a9d470e61589298859bb1bd9dce7b7594f7 (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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
local helpers = require('test.functional.helpers')(after_each)
local plugin_helpers = require('test.functional.plugin.helpers')

local Screen = require('test.functional.ui.screen')

local buffer, command, eval = helpers.buffer, helpers.command, helpers.eval

before_each(function()
  plugin_helpers.reset()
  helpers.clear()
  command('syntax on')
  command('set filetype=man')
end)

describe('In autoload/man.vim', function()
  describe('function man#highlight_formatted_text', function()
    local screen

    before_each(function()
      command('syntax off') -- Ignore syntax groups
      screen = Screen.new(52, 5)
      screen:attach()
    end)

    after_each(function()
      screen:detach()
    end)

    local function expect(string)
      screen:expect(string,
      {
        b = { bold = true },
        i = { italic = true },
        u = { underline = true },
        bi = { bold = true, italic = true },
        biu = { bold = true, italic = true, underline = true },
      },
      {{ bold = true, foreground = Screen.colors.Blue }})
    end

    local function expect_without_highlights(string)
      screen:expect(string, nil, true)
    end

    local function insert_lines(...)
      buffer('set_lines', 0, 0, 1, false, { ... })
    end

    it('clears backspaces from text', function()
      insert_lines(
        "this i\bis\bs a\ba test",
        "with _\bo_\bv_\be_\br_\bs_\bt_\br_\bu_\bc_\bk text"
      )

      expect_without_highlights([[
      ^this i^His^Hs a^Ha test                             |
      with _^Ho_^Hv_^He_^Hr_^Hs_^Ht_^Hr_^Hu_^Hc_^Hk text  |
      ~                                                   |
      ~                                                   |
                                                          |
      ]])

      eval('man#highlight_formatted_text()')

      expect_without_highlights([[
      ^this is a test                                      |
      with overstruck text                                |
      ~                                                   |
      ~                                                   |
                                                          |
      ]])
    end)

    it('clears escape sequences from text', function()
      insert_lines(
        "this \027[1mis \027[3ma \027[4mtest\027[0m",
        "\027[4mwith\027[24m \027[4mescaped\027[24m \027[4mtext\027[24m"
      )

      expect_without_highlights([[
      ^this ^[[1mis ^[[3ma ^[[4mtest^[[0m                  |
      ^[[4mwith^[[24m ^[[4mescaped^[[24m ^[[4mtext^[[24m  |
      ~                                                   |
      ~                                                   |
                                                          |
      ]])

      eval('man#highlight_formatted_text()')

      expect_without_highlights([[
      ^this is a test                                      |
      with escaped text                                   |
      ~                                                   |
      ~                                                   |
                                                          |
      ]])
    end)

    it('highlights overstruck text', function()
      insert_lines(
        "this i\bis\bs a\ba test",
        "with _\bo_\bv_\be_\br_\bs_\bt_\br_\bu_\bc_\bk text"
      )
      eval('man#highlight_formatted_text()')

      expect([[
      ^this {b:is} {b:a} test                                      |
      with {u:overstruck} text                                |
      ~                                                   |
      ~                                                   |
                                                          |
      ]])
    end)

    it('highlights escape sequences in text', function()
      insert_lines(
        "this \027[1mis \027[3ma \027[4mtest\027[0m",
        "\027[4mwith\027[24m \027[4mescaped\027[24m \027[4mtext\027[24m"
      )
      eval('man#highlight_formatted_text()')

      expect([[
      ^this {b:is }{bi:a }{biu:test}                                      |
      {u:with} {u:escaped} {u:text}                                   |
      ~                                                   |
      ~                                                   |
                                                          |
      ]])
    end)

    it('highlights multibyte text', function()
      insert_lines(
        "this i\bis\bs あ\bあ test",
        "with _\bö_\bv_\be_\br_\bs_\bt_\br_\bu_\bc_\bk te\027[3mxt¶\027[0m"
      )
      eval('man#highlight_formatted_text()')

      expect([[
      ^this {b:is} {b:あ} test                                     |
      with {u:överstruck} te{i:xt¶}                               |
      ~                                                   |
      ~                                                   |
                                                          |
      ]])
    end)

    it('highlights underscores based on context', function()
      insert_lines(
        "_\b_b\bbe\beg\bgi\bin\bns\bs",
        "m\bmi\bid\bd_\b_d\bdl\ble\be",
        "_\bm_\bi_\bd_\b__\bd_\bl_\be"
      )
      eval('man#highlight_formatted_text()')

      expect([[
      {b:^_begins}                                             |
      {b:mid_dle}                                             |
      {u:mid_dle}                                             |
      ~                                                   |
                                                          |
      ]])
    end)

    it('highlights various bullet formats', function()
      insert_lines(
        "· ·\b·",
        "+\bo",
        "+\b+\bo\bo double"
      )
      eval('man#highlight_formatted_text()')

      expect([[
      ^· {b:·}                                                 |
      {b:·}                                                   |
      {b:·} double                                            |
      ~                                                   |
                                                          |
      ]])
    end)
  end)
end)