~sakrecoer/ubuntu-manual-tests/bug-1538228

« back to all changes in this revision

Viewing changes to tools/test_case_format

removeĀ revĀ 320

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/sh
 
2
#description: format file according to https://wiki.ubuntu.com/Testing/TestCaseFormat
 
3
#save as test-case-format
 
4
#Make file executable <code>chmod +x test-case-format
 
5
#move to /urs/local/bin <code>sudo mv test-case-format /usr/local/bin</code>
 
6
#In order to use this script, both vim and tidy need to be installed.
 
7
#usage: test-case-format /path/to/files
 
8
INDENTATION="4"
 
9
 
 
10
_basename()
 
11
{
 
12
    [ -z "${1}" ] && return 1 || _basename_var_name="${1}"
 
13
    [ -z "${2}" ] || _basename_var_suffix="${2}"
 
14
    case "${_basename_var_name}" in
 
15
        /*|*/*) _basename_var_name="$(expr "${_basename_var_name}" : '.*/\([^/]*\)')" ;;
 
16
    esac
 
17
 
 
18
    if [ -n "${_basename_var_suffix}" ] && [ "${#_basename_var_name}" -gt "${#2}" ]; then
 
19
        if [ X"$(printf "%s" "${_basename_var_name}" | cut -c"$((${#_basename_var_name} - ${#_basename_var_suffix} + 1))"-"${#_basename_var_name}")" \
 
20
           = X"$(printf "%s" "${_basename_var_suffix}")" ]; then
 
21
            _basename_var_name="$(printf "%s" "${_basename_var_name}" | cut -c1-"$((${#_basename_var_name} - ${#_basename_var_suffix}))")"
 
22
        fi
 
23
    fi
 
24
 
 
25
    printf "%s\\n" "${_basename_var_name}"
 
26
}
 
27
 
 
28
_usage()
 
29
{
 
30
    printf "%s\\n" "Usage: $(_basename "${0}") testcase ..." >&2
 
31
    printf "%s\\n" "https://wiki.ubuntu.com/Testing/TestCaseFormat" >&2
 
32
    exit 1
 
33
}
 
34
 
 
35
_tmp_files()
 
36
{
 
37
    mkdir -p /tmp/"$(_basename "${0}")"/indent
 
38
    #this file can also be copied to ~/.vim/indent/html.vim so
 
39
    #vim indent html files the testcase way <dl>/<dt>/<dd>
 
40
    cat > /tmp/"$(_basename "${0}")"/indent/html.vim << EOF
 
41
" Description:  html indenter
 
42
" Author:       Johannes Zellner <johannes@zellner.org>
 
43
" Last Change:  Mo, 05 Jun 2006 22:32:41 CEST
 
44
"               Restoring 'cpo' and 'ic' added by Bram 2006 May 5
 
45
" Globals:      g:html_indent_tags         -- indenting tags
 
46
"               g:html_indent_strict       -- inhibit 'O O' elements
 
47
"               g:html_indent_strict_table -- inhibit 'O -' elements
 
48
 
 
49
" Only load this indent file when no other was loaded.
 
50
if exists("b:did_indent")
 
51
    finish
 
52
endif
 
53
let b:did_indent = 1
 
54
 
 
55
 
 
56
" [-- local settings (must come before aborting the script) --]
 
57
setlocal indentexpr=HtmlIndentGet(v:lnum)
 
58
setlocal indentkeys=o,O,*<Return>,<>>,{,}
 
59
 
 
60
 
 
61
if exists('g:html_indent_tags')
 
62
    unlet g:html_indent_tags
 
63
endif
 
64
 
 
65
" [-- helper function to assemble tag list --]
 
66
fun! <SID>HtmlIndentPush(tag)
 
67
    if exists('g:html_indent_tags')
 
68
        let g:html_indent_tags = g:html_indent_tags.'\|'.a:tag
 
69
    else
 
70
        let g:html_indent_tags = a:tag
 
71
    endif
 
72
endfun
 
73
 
 
74
 
 
75
" [-- <ELEMENT ? - - ...> --]
 
76
call <SID>HtmlIndentPush('a')
 
77
call <SID>HtmlIndentPush('abbr')
 
78
call <SID>HtmlIndentPush('acronym')
 
79
call <SID>HtmlIndentPush('address')
 
80
call <SID>HtmlIndentPush('b')
 
81
call <SID>HtmlIndentPush('bdo')
 
82
call <SID>HtmlIndentPush('big')
 
83
call <SID>HtmlIndentPush('blockquote')
 
84
call <SID>HtmlIndentPush('button')
 
85
call <SID>HtmlIndentPush('caption')
 
86
call <SID>HtmlIndentPush('center')
 
87
call <SID>HtmlIndentPush('cite')
 
88
call <SID>HtmlIndentPush('code')
 
89
call <SID>HtmlIndentPush('colgroup')
 
90
call <SID>HtmlIndentPush('dd')
 
91
call <SID>HtmlIndentPush('del')
 
92
call <SID>HtmlIndentPush('dfn')
 
93
call <SID>HtmlIndentPush('dir')
 
94
call <SID>HtmlIndentPush('div')
 
95
call <SID>HtmlIndentPush('dl')
 
96
call <SID>HtmlIndentPush('dt')
 
97
call <SID>HtmlIndentPush('em')
 
98
call <SID>HtmlIndentPush('fieldset')
 
99
call <SID>HtmlIndentPush('font')
 
100
call <SID>HtmlIndentPush('form')
 
101
call <SID>HtmlIndentPush('frameset')
 
102
call <SID>HtmlIndentPush('h1')
 
103
call <SID>HtmlIndentPush('h2')
 
104
call <SID>HtmlIndentPush('h3')
 
105
call <SID>HtmlIndentPush('h4')
 
106
call <SID>HtmlIndentPush('h5')
 
107
call <SID>HtmlIndentPush('h6')
 
108
call <SID>HtmlIndentPush('i')
 
109
call <SID>HtmlIndentPush('iframe')
 
110
call <SID>HtmlIndentPush('ins')
 
111
call <SID>HtmlIndentPush('kbd')
 
112
call <SID>HtmlIndentPush('label')
 
113
call <SID>HtmlIndentPush('legend')
 
114
call <SID>HtmlIndentPush('map')
 
115
call <SID>HtmlIndentPush('menu')
 
116
call <SID>HtmlIndentPush('noframes')
 
117
call <SID>HtmlIndentPush('noscript')
 
118
call <SID>HtmlIndentPush('object')
 
119
call <SID>HtmlIndentPush('ol')
 
120
call <SID>HtmlIndentPush('optgroup')
 
121
" call <SID>HtmlIndentPush('pre')
 
122
call <SID>HtmlIndentPush('q')
 
123
call <SID>HtmlIndentPush('s')
 
124
call <SID>HtmlIndentPush('samp')
 
125
call <SID>HtmlIndentPush('script')
 
126
call <SID>HtmlIndentPush('select')
 
127
call <SID>HtmlIndentPush('small')
 
128
call <SID>HtmlIndentPush('span')
 
129
call <SID>HtmlIndentPush('strong')
 
130
call <SID>HtmlIndentPush('style')
 
131
call <SID>HtmlIndentPush('sub')
 
132
call <SID>HtmlIndentPush('sup')
 
133
call <SID>HtmlIndentPush('table')
 
134
call <SID>HtmlIndentPush('textarea')
 
135
call <SID>HtmlIndentPush('title')
 
136
call <SID>HtmlIndentPush('tt')
 
137
call <SID>HtmlIndentPush('u')
 
138
call <SID>HtmlIndentPush('ul')
 
139
call <SID>HtmlIndentPush('var')
 
140
 
 
141
 
 
142
" [-- <ELEMENT ? O O ...> --]
 
143
if !exists('g:html_indent_strict')
 
144
    call <SID>HtmlIndentPush('body')
 
145
    call <SID>HtmlIndentPush('head')
 
146
    call <SID>HtmlIndentPush('html')
 
147
    call <SID>HtmlIndentPush('tbody')
 
148
endif
 
149
 
 
150
 
 
151
" [-- <ELEMENT ? O - ...> --]
 
152
if !exists('g:html_indent_strict_table')
 
153
    call <SID>HtmlIndentPush('th')
 
154
    call <SID>HtmlIndentPush('td')
 
155
    call <SID>HtmlIndentPush('tr')
 
156
    call <SID>HtmlIndentPush('tfoot')
 
157
    call <SID>HtmlIndentPush('thead')
 
158
endif
 
159
 
 
160
delfun <SID>HtmlIndentPush
 
161
 
 
162
let s:cpo_save = &cpo
 
163
set cpo-=C
 
164
 
 
165
" [-- count indent-increasing tags of line a:lnum --]
 
166
fun! <SID>HtmlIndentOpen(lnum, pattern)
 
167
    let s = substitute('x'.getline(a:lnum),
 
168
    \ '.\{-}\(\(<\)\('.a:pattern.'\)\>\)', "\1", 'g')
 
169
    let s = substitute(s, "[^\1].*$", '', '')
 
170
    return strlen(s)
 
171
endfun
 
172
 
 
173
" [-- count indent-decreasing tags of line a:lnum --]
 
174
fun! <SID>HtmlIndentClose(lnum, pattern)
 
175
    let s = substitute('x'.getline(a:lnum),
 
176
    \ '.\{-}\(\(<\)/\('.a:pattern.'\)\>>\)', "\1", 'g')
 
177
    let s = substitute(s, "[^\1].*$", '', '')
 
178
    return strlen(s)
 
179
endfun
 
180
 
 
181
" [-- count indent-increasing '{' of (java|css) line a:lnum --]
 
182
fun! <SID>HtmlIndentOpenAlt(lnum)
 
183
    return strlen(substitute(getline(a:lnum), '[^{]\+', '', 'g'))
 
184
endfun
 
185
 
 
186
" [-- count indent-decreasing '}' of (java|css) line a:lnum --]
 
187
fun! <SID>HtmlIndentCloseAlt(lnum)
 
188
    return strlen(substitute(getline(a:lnum), '[^}]\+', '', 'g'))
 
189
endfun
 
190
 
 
191
" [-- return the sum of indents respecting the syntax of a:lnum --]
 
192
fun! <SID>HtmlIndentSum(lnum, style)
 
193
    if a:style == match(getline(a:lnum), '^\s*</')
 
194
        if a:style == match(getline(a:lnum), '^\s*</\<\('.g:html_indent_tags.'\)\>')
 
195
            let open = <SID>HtmlIndentOpen(a:lnum, g:html_indent_tags)
 
196
            let close = <SID>HtmlIndentClose(a:lnum, g:html_indent_tags)
 
197
            if 0 != open || 0 != close
 
198
                return open - close
 
199
            endif
 
200
        endif
 
201
    endif
 
202
    if '' != &syntax &&
 
203
        \ synIDattr(synID(a:lnum, 1, 1), 'name') =~ '\(css\|java\).*' &&
 
204
        \ synIDattr(synID(a:lnum, strlen(getline(a:lnum)), 1), 'name')
 
205
        \ =~ '\(css\|java\).*'
 
206
        if a:style == match(getline(a:lnum), '^\s*}')
 
207
            return <SID>HtmlIndentOpenAlt(a:lnum) - <SID>HtmlIndentCloseAlt(a:lnum)
 
208
        endif
 
209
    endif
 
210
    return 0
 
211
endfun
 
212
 
 
213
fun! HtmlIndentGet(lnum)
 
214
    " Find a non-empty line above the current line.
 
215
    let lnum = prevnonblank(a:lnum - 1)
 
216
 
 
217
    " Hit the start of the file, use zero indent.
 
218
    if lnum == 0
 
219
        return 0
 
220
    endif
 
221
 
 
222
    " [-- special handling for <dd> --]
 
223
    if getline(a:lnum-1) =~ '\c</dt>'
 
224
        if getline(a:lnum) =~ '\c<dd>'
 
225
            return indent(lnum) + (&sw)
 
226
        endif
 
227
    endif
 
228
 
 
229
    " [-- special handling for <dd>/<dt>: --]
 
230
    if getline(a:lnum-1) =~ '\c</dd>'
 
231
        if getline(a:lnum) =~ '\c</dd>'
 
232
            return indent(lnum)
 
233
        endif
 
234
        if getline(a:lnum) =~ '\c<dt>'
 
235
            return indent(lnum) - (&sw)
 
236
        endif
 
237
        return -1
 
238
    endif
 
239
 
 
240
    let restore_ic = &ic
 
241
    setlocal ic " ignore case
 
242
 
 
243
    " [-- special handling for <pre>: no indenting --]
 
244
    if getline(a:lnum) =~ '\c</pre>'
 
245
                \ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nWb')
 
246
                \ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nW')
 
247
        " we're in a line with </pre> or inside <pre> ... </pre>
 
248
        if restore_ic == 0
 
249
          setlocal noic
 
250
        endif
 
251
        return -1
 
252
    endif
 
253
 
 
254
    " [-- special handling for <javascript>: use cindent --]
 
255
    let js = '<script.*type\s*=\s*.*java'
 
256
 
 
257
    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 
258
    " by Tye Zdrojewski <zdro@yahoo.com>, 05 Jun 2006
 
259
    " ZDR: This needs to be an AND (we are 'after the start of the pair' AND
 
260
    "      we are 'before the end of the pair').  Otherwise, indentation
 
261
    "      before the start of the script block will be affected; the end of
 
262
    "      the pair will still match if we are before the beginning of the
 
263
    "      pair.
 
264
    "
 
265
    if   0 < searchpair(js, '', '</script>', 'nWb')
 
266
    \ && 0 < searchpair(js, '', '</script>', 'nW')
 
267
        " we're inside javascript
 
268
        if getline(lnum) !~ js && getline(a:lnum) != '</script>'
 
269
            if restore_ic == 0
 
270
              setlocal noic
 
271
            endif
 
272
            return cindent(a:lnum)
 
273
        endif
 
274
    endif
 
275
 
 
276
    if getline(lnum) =~ '\c</pre>'
 
277
        " line before the current line a:lnum contains
 
278
        " a closing </pre>. --> search for line before
 
279
        " starting <pre> to restore the indent.
 
280
        let preline = prevnonblank(search('\c<pre>', 'bW') - 1)
 
281
        if preline > 0
 
282
            if restore_ic == 0
 
283
              setlocal noic
 
284
            endif
 
285
            return indent(preline)
 
286
        endif
 
287
    endif
 
288
 
 
289
    let ind = <SID>HtmlIndentSum(lnum, -1)
 
290
    let ind = ind + <SID>HtmlIndentSum(a:lnum, 0)
 
291
 
 
292
    if restore_ic == 0
 
293
        setlocal noic
 
294
    endif
 
295
 
 
296
    return indent(lnum) + (&sw * ind)
 
297
endfun
 
298
 
 
299
let &cpo = s:cpo_save
 
300
unlet s:cpo_save
 
301
 
 
302
" [-- EOF <runtime>/indent/html.vim --]
 
303
EOF
 
304
 
 
305
    cat > /tmp/"$(_basename "${0}")"/vimrc << EOF
 
306
set runtimepath=/tmp/$(_basename "${0}"),\$VIMRUNTIME
 
307
syntax on
 
308
set ffs=unix,dos
 
309
set encoding=utf-8          "utf is able to represent any character
 
310
set softtabstop=${INDENTATION}
 
311
set shiftwidth=${INDENTATION}
 
312
set expandtab               "tabs mutate into spaces, if you wanna insert "real"
 
313
filetype plugin indent on   "enable filetype-specific plugins
 
314
EOF
 
315
}
 
316
 
 
317
_format()
 
318
{
 
319
    for _format_var_testcase; do
 
320
        printf "%s\\n" "${_format_var_testcase}"
 
321
        tidy -f /tmp/"$(_basename "${0}")"/testcase -xml -i "${_format_var_testcase}" > /dev/null
 
322
        [ "${?}" -eq "2" ] && { cat /tmp/"$(_basename "${0}")"/testcase; exit 1; }
 
323
        vim -es -u /tmp/"$(_basename "${0}")"/vimrc -c "set ft=html" -c "normal gg=G" -c wq "${_format_var_testcase}"
 
324
    done
 
325
}
 
326
 
 
327
if [ ! -t 0 ]; then
 
328
    #there is input comming from pipe or file, add to the end of $@
 
329
    set -- "${@}" $(cat)
 
330
fi
 
331
 
 
332
[ "${#}" -eq "0" ] && _usage
 
333
 
 
334
for arg in "${@}"; do #parse options
 
335
    case "${arg}" in
 
336
        --) shift; break  ;;
 
337
        -h|--help) _usage ;;
 
338
        -*) printf "%s\\n" "$(expr "${0}" : '.*/\([^/]*\)'): unrecognized option \`${arg}'" >&2; _usage ;;
 
339
    esac
 
340
done
 
341
 
 
342
[ -f "$(command -v "vim")" ]  || { printf "%b\\n" "$(_basename "${0}"): You need to install 'vim' & 'tidy' to run this program\n$ sudo apt-get install vim tidy" >&2; exit 1; }
 
343
[ -f "$(command -v "tidy")" ] || { printf "%b\\n" "$(_basename "${0}"): You need to install 'tidy' to run this program\n$ sudo apt-get install tidy" >&2; exit 1; }
 
344
 
 
345
_tmp_files
 
346
_format "${@}"