1#!/usr/bin/python3
2#
3# Copyright 2023 The Khronos Group Inc.
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7# Author(s):    Shahbaz Youssefi <syoussefi@google.com>
8#
9# Purpose:      This file contains tests for reflow.py
10
11import pytest
12
13from collections import namedtuple
14import os
15
16from reflib import loadFile
17from reflow import reflowFile
18
19testsDir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'reflow-tests')
20resultsDir = os.path.join(testsDir, 'results')
21
22Variations = namedtuple('Variations', ['reflow', 'addVUID'])
23
24def makeTestId(variations):
25    separator = ''
26    testid = ''
27
28    if not variations.reflow:
29        testid += separator + 'noreflow'
30        separator = '-'
31
32    if not variations.addVUID:
33        testid += separator + 'novuid'
34        separator = '-'
35
36    if testid == '':
37        testid = 'default'
38
39    return testid
40
41class ReflowArgs:
42    def __init__(self, variations):
43        self.overwrite = False
44        self.nowrite = False
45        self.outDir = os.path.join(resultsDir, makeTestId(variations))
46        self.check = True
47        self.checkVUID = True
48        self.noflow = not variations.reflow
49        self.margin = 76
50        self.suffix = ''
51        self.nextvu = 10000 if variations.addVUID else None
52        self.maxvu = 99999
53        self.warnCount = 0
54        self.vuidDict = {}
55
56        self.variations = variations
57
58variations = [
59    Variations(False, False),
60    Variations(False, True),
61    Variations(True, False),
62    Variations(True, True),
63]
64
65@pytest.fixture(params=variations, ids=makeTestId)
66def args(request):
67    return ReflowArgs(request.param)
68
69def getPath(*names):
70    return os.path.join(testsDir, *names)
71
72def match_with_expected(resultFile, expectation):
73    result, result_newline = loadFile(resultFile)
74    expect, expect_newline = loadFile(expectation)
75
76    assert(result_newline == expect_newline)
77    assert(result == expect)
78
79def run_reflow_test(args, filetag):
80    testid = makeTestId(args.variations)
81
82    source = 'src-' + filetag + '.adoc'
83    expect = 'expect-' + filetag + '-' + testid + '.adoc'
84
85    filename = getPath(source)
86
87    reflowFile(filename, args)
88
89    match_with_expected(getPath(resultsDir, testid, source), getPath(expect))
90
91def match_warn_count(args, expected):
92    assert(args.warnCount == expected)
93
94def match_vuid_dict(args, expectedExisting, expectedNew):
95    expected = expectedExisting
96    if args.nextvu is not None:
97        expected = expected | expectedNew
98
99    assert(sorted(args.vuidDict.keys()) == sorted(expected.keys()))
100
101    for vuid, locations in args.vuidDict.items():
102        for location, expectedLocation in zip(locations, expected[vuid]):
103            filename, tagline = location
104            expectedFilename, expectedTag = expectedLocation
105
106        assert(expectedFilename in filename)
107        assert(expectedTag in tagline)
108
109def test_text(args):
110    """Basic test of text reflow."""
111    run_reflow_test(args, 'text')
112    match_warn_count(args, 0)
113    match_vuid_dict(args, {}, {})
114
115def test_table(args):
116    """Basic test that ensures tables are not reformatted."""
117    run_reflow_test(args, 'table')
118    match_warn_count(args, 0)
119    match_vuid_dict(args, {}, {})
120
121def test_vu(args):
122    """Basic test that VU reflows work."""
123    run_reflow_test(args, 'vu')
124    match_warn_count(args, 0)
125    match_vuid_dict(args, {'01993':
126                           [['scripts/reflow-tests/src-vu.adoc',
127                             '[[VUID-vkCmdClearColorImage-image-01993]]']],
128                           '00002':
129                           [['scripts/reflow-tests/src-vu.adoc',
130                             '[[VUID-vkCmdClearColorImage-image-00002]]']],
131                           '01545':
132                           [['scripts/reflow-tests/src-vu.adoc',
133                             '[[VUID-vkCmdClearColorImage-image-01545]]']],
134                           '00003':
135                           [['scripts/reflow-tests/src-vu.adoc',
136                             '[[VUID-vkCmdClearColorImage-image-00003]]']],
137                           '00004':
138                           [['scripts/reflow-tests/src-vu.adoc',
139                             '[[VUID-vkCmdClearColorImage-imageLayout-00004]]']],
140                           '00005':
141                           [['scripts/reflow-tests/src-vu.adoc',
142                             '[[VUID-vkCmdClearColorImage-imageLayout-00005]]']],
143                           '01394':
144                           [['scripts/reflow-tests/src-vu.adoc',
145                             '[[VUID-vkCmdClearColorImage-imageLayout-01394]]']],
146                           '02498':
147                           [['scripts/reflow-tests/src-vu.adoc',
148                             '[[VUID-vkCmdClearColorImage-aspectMask-02498]]']],
149                           '01470':
150                           [['scripts/reflow-tests/src-vu.adoc',
151                             '[[VUID-vkCmdClearColorImage-baseMipLevel-01470]]']],
152                           '01692':
153                           [['scripts/reflow-tests/src-vu.adoc',
154                             '[[VUID-vkCmdClearColorImage-pRanges-01692]]']],
155                           '01472':
156                           [['scripts/reflow-tests/src-vu.adoc',
157                             '[[VUID-vkCmdClearColorImage-baseArrayLayer-01472]]']],
158                           '01693':
159                           [['scripts/reflow-tests/src-vu.adoc',
160                             '[[VUID-vkCmdClearColorImage-pRanges-01693]]']],
161                           '00007':
162                           [['scripts/reflow-tests/src-vu.adoc',
163                             '[[VUID-vkCmdClearColorImage-image-00007]]']],
164                           '04961':
165                           [['scripts/reflow-tests/src-vu.adoc',
166                             '[[VUID-vkCmdClearColorImage-pColor-04961]]']],
167                           '01805':
168                           [['scripts/reflow-tests/src-vu.adoc',
169                             '[[VUID-vkCmdClearColorImage-commandBuffer-01805]]']],
170                           '01806':
171                           [['scripts/reflow-tests/src-vu.adoc',
172                             '[[VUID-vkCmdClearColorImage-commandBuffer-01806]]']]},
173                    {})
174
175# Commented out now that VU extractor supports this, but may
176# need to refactor through a conventions object enable if
177# OpenXR still needs this.
178# def test_ifdef_in_vu(args):
179#     """Test that ifdef in VUs are warned against."""
180#     run_reflow_test(args, 'ifdef-in-vu')
181#     match_warn_count(args, 1)
182#     match_vuid_dict(args, {'00003':
183#                            [['scripts/reflow-tests/src-ifdef-in-vu.adoc',
184#                              '[[VUID-vkCmdClearColorImage-image-00003]]']],
185#                            '00004':
186#                            [['scripts/reflow-tests/src-ifdef-in-vu.adoc',
187#                              '[[VUID-vkCmdClearColorImage-imageLayout-00004]]']],
188#                            '00005':
189#                            [['scripts/reflow-tests/src-ifdef-in-vu.adoc',
190#                              '[[VUID-vkCmdClearColorImage-imageLayout-00005]]']],
191#                            '04961':
192#                            [['scripts/reflow-tests/src-ifdef-in-vu.adoc',
193#                              '[[VUID-vkCmdClearColorImage-pColor-04961]]']]},
194#                     {})
195
196def test_vuid_repeat(args):
197    """Test that same VUID in multiple VUs is detected."""
198    run_reflow_test(args, 'vuid-repeat')
199    match_warn_count(args, 0)
200    match_vuid_dict(args, {'02498':
201                           [['scripts/reflow-tests/src-vuid-repeat.adoc',
202                             '[[VUID-vkCmdClearColorImage-aspectMask-02498]]'],
203                            ['scripts/reflow-tests/src-vuid-repeat.adoc',
204                             '[[VUID-vkCmdClearColorImage-pRanges-02498]]'],
205                            ['scripts/reflow-tests/src-vuid-repeat.adoc',
206                             '[[VUID-vkCmdClearColorImage-pRanges-02498]]'],
207                            ['scripts/reflow-tests/src-vuid-repeat.adoc',
208                             '[[VUID-vkCmdClearColorImage-pColor-02498]]']],
209                           '01470':
210                           [['scripts/reflow-tests/src-vuid-repeat.adoc',
211                             '[[VUID-vkCmdClearColorImage-baseMipLevel-01470]]']],
212                           '00007':
213                           [['scripts/reflow-tests/src-vuid-repeat.adoc',
214                             '[[VUID-vkCmdClearColorImage-baseArrayLayer-00007]]'],
215                            ['scripts/reflow-tests/src-vuid-repeat.adoc',
216                             '[[VUID-vkCmdClearColorImage-image-00007]]']]},
217                    {})
218
219def test_new_vuid(args):
220    """Test that VUID generation works."""
221    run_reflow_test(args, 'new-vuid')
222    match_warn_count(args, 0)
223    match_vuid_dict(args, {'01993':
224                           [['scripts/reflow-tests/src-new-vuid.adoc',
225                             '[[VUID-vkCmdClearColorImage-image-01993]]']],
226                           '01545':
227                           [['scripts/reflow-tests/src-new-vuid.adoc',
228                             '[[VUID-vkCmdClearColorImage-image-01545]]']],
229                           '00004':
230                           [['scripts/reflow-tests/src-new-vuid.adoc',
231                             '[[VUID-vkCmdClearColorImage-imageLayout-00004]]']],
232                           '00005':
233                           [['scripts/reflow-tests/src-new-vuid.adoc',
234                             '[[VUID-vkCmdClearColorImage-imageLayout-00005]]']],
235                           '02498':
236                           [['scripts/reflow-tests/src-new-vuid.adoc',
237                             '[[VUID-vkCmdClearColorImage-aspectMask-02498]]']],
238                           '01470':
239                           [['scripts/reflow-tests/src-new-vuid.adoc',
240                             '[[VUID-vkCmdClearColorImage-baseMipLevel-01470]]']],
241                           '01472':
242                           [['scripts/reflow-tests/src-new-vuid.adoc',
243                             '[[VUID-vkCmdClearColorImage-baseArrayLayer-01472]]']],
244                           '01693':
245                           [['scripts/reflow-tests/src-new-vuid.adoc',
246                             '[[VUID-vkCmdClearColorImage-pRanges-01693]]']],
247                           '01805':
248                           [['scripts/reflow-tests/src-new-vuid.adoc',
249                             '[[VUID-vkCmdClearColorImage-commandBuffer-01805]]']],
250                           '01806':
251                           [['scripts/reflow-tests/src-new-vuid.adoc',
252                             '[[VUID-vkCmdClearColorImage-commandBuffer-01806]]']]},
253                          {'10000':
254                           [['scripts/reflow-tests/src-new-vuid.adoc',
255                             '[[VUID-vkCmdClearColorImage-image-10000]]']],
256                           '10001':
257                           [['scripts/reflow-tests/src-new-vuid.adoc',
258                             '[[VUID-vkCmdClearColorImage-image-10001]]']],
259                           '10002':
260                           [['scripts/reflow-tests/src-new-vuid.adoc',
261                             '[[VUID-vkCmdClearColorImage-imageLayout-10002]]']],
262                           '10003':
263                           [['scripts/reflow-tests/src-new-vuid.adoc',
264                             '[[VUID-vkCmdClearColorImage-pRanges-10003]]']],
265                           '10004':
266                           [['scripts/reflow-tests/src-new-vuid.adoc',
267                             '[[VUID-vkCmdClearColorImage-image-10004]]']],
268                           '10005':
269                           [['scripts/reflow-tests/src-new-vuid.adoc',
270                             '[[VUID-vkCmdClearColorImage-pColor-10005]]']]})
271
272def test_new_vuid_attribute(args):
273    """Test that VUID generation works and prioritizes attributes for tags."""
274    run_reflow_test(args, 'new-vuid-attribute')
275    match_warn_count(args, 0)
276    match_vuid_dict(args, {}, {'10000':
277                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
278                                 '[[VUID-{refpage}-{imageparam}-10000]]']],
279                               '10001':
280                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
281                                 '[[VUID-{refpage}-{imagesubresource}-10001]]']],
282                               '10002':
283                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
284                                 '[[VUID-{refpage}-{imageparam}-10002]]']],
285                               '10003':
286                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
287                                 '[[VUID-{refpage}-{imageparam}-10003]]']],
288                               '10004':
289                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
290                                 '[[VUID-{refpage}-{imageparam}-10004]]']],
291                               '10005':
292                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
293                                 '[[VUID-{refpage}-{imageparam}-10005]]']],
294                               '10006':
295                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
296                                 '[[VUID-{refpage}-{imageparam}-10006]]']],
297                               '10007':
298                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
299                                 '[[VUID-{refpage}-{imageparam}-10007]]']],
300                               '10008':
301                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
302                                 '[[VUID-{refpage}-{imageparam}-10008]]']],
303                               '10009':
304                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
305                                 '[[VUID-{refpage}-{imageparam}-10009]]']],
306                               '10010':
307                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
308                                 '[[VUID-{refpage}-{imageparam}-10010]]']],
309                               '10011':
310                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
311                                 '[[VUID-{refpage}-{imagesubresource}-10011]]']],
312                               '10012':
313                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
314                                 '[[VUID-{refpage}-{imageparam}-10012]]']],
315                               '10013':
316                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
317                                 '[[VUID-{refpage}-{imageparam}-10013]]']],
318                               '10014':
319                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
320                                 '[[VUID-{refpage}-{imageparam}-10014]]']],
321                               '10015':
322                               [['scripts/reflow-tests/src-new-vuid-attribute.adoc',
323                                 '[[VUID-{refpage}-{imageparam}-10015]]']]})
324
325def test_common_validity(args):
326    """Test that VUID generation works for common validity files."""
327    run_reflow_test(args, 'common-validity')
328    match_warn_count(args, 0)
329    match_vuid_dict(args, {'00171':
330                           [['scripts/reflow-tests/src-common-validity.adoc',
331                             '[[VUID-{refpage}-pRegions-00171]]']],
332                           '00176':
333                           [['scripts/reflow-tests/src-common-validity.adoc',
334                             '[[VUID-{refpage}-srcBuffer-00176]]']],
335                           '00177':
336                           [['scripts/reflow-tests/src-common-validity.adoc',
337                             '[[VUID-{refpage}-dstImage-00177]]']],
338                           '00178':
339                           [['scripts/reflow-tests/src-common-validity.adoc',
340                             '[[VUID-{refpage}-dstImage-00178]]']],
341                           '00181':
342                           [['scripts/reflow-tests/src-common-validity.adoc',
343                             '[[VUID-{refpage}-dstImageLayout-00181]]']]},
344                          {'10000':
345                           [['scripts/reflow-tests/src-common-validity.adoc',
346                             '[[VUID-{refpage}-pRegions-10000]]']],
347                           '10001':
348                           [['scripts/reflow-tests/src-common-validity.adoc',
349                             '[[VUID-{refpage}-srcBuffer-10001]]']],
350                           '10002':
351                           [['scripts/reflow-tests/src-common-validity.adoc',
352                             '[[VUID-{refpage}-dstImage-10002]]']],
353                           '10003':
354                           [['scripts/reflow-tests/src-common-validity.adoc',
355                             '[[VUID-{refpage}-dstImage-10003]]']],
356                           '10004':
357                           [['scripts/reflow-tests/src-common-validity.adoc',
358                             '[[VUID-{refpage}-dstImageLayout-10004]]']]})
359
360def test_nested_lists_in_vu(args):
361    """Test that nested lists in VU work correctly."""
362    run_reflow_test(args, 'nested-lists-in-vu')
363    match_warn_count(args, 0)
364    match_vuid_dict(args, {'07284':
365                           [['scripts/reflow-tests/src-nested-lists-in-vu.adoc',
366                             '[[VUID-{refpage}-multisampledRenderToSingleSampled-07284]]']],
367                           '08971':
368                           [['scripts/reflow-tests/src-nested-lists-in-vu.adoc',
369                             '[[VUID-{refpage}-None-08971]]']]},
370                          {'10000':
371                           [['scripts/reflow-tests/src-nested-lists-in-vu.adoc',
372                             '[[VUID-{refpage}-None-10000]]']]})
373
374
375def test_math_block_in_vu(args):
376    """Test that nested lists in VU work correctly."""
377    run_reflow_test(args, 'math-block-in-vu')
378    match_warn_count(args, 0)
379    match_vuid_dict(args, {'00004':
380                           [['scripts/reflow-tests/src-math-block-in-vu.adoc',
381                             '[[VUID-vkCmdClearColorImage-imageLayout-00004]]']]},
382                          {'10000':
383                           [['scripts/reflow-tests/src-math-block-in-vu.adoc',
384                             '[[VUID-vkCmdClearColorImage-image-10000]]']]})
385