1#!/usr/bin/env python
2#===- test.py -  ---------------------------------------------*- python -*--===#
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8#===------------------------------------------------------------------------===#
9
10from cppreference_parser import _ParseSymbolPage, _ParseIndexPage
11
12import unittest
13
14class TestStdGen(unittest.TestCase):
15
16  def testParseIndexPage(self):
17    html = """
18 <a href="abs.html" title="abs"><tt>abs()</tt></a> (int) <br>
19 <a href="complex/abs.html" title="abs"><tt>abs&lt;&gt;()</tt></a> (std::complex) <br>
20 <a href="acos.html" title="acos"><tt>acos()</tt></a> <br>
21 <a href="acosh.html" title="acosh"><tt>acosh()</tt></a> <span class="t-mark-rev">(since C++11)</span> <br>
22 <a href="as_bytes.html" title="as bytes"><tt>as_bytes&lt;&gt;()</tt></a> <span class="t-mark-rev t-since-cxx20">(since C++20)</span> <br>
23 """
24
25    actual = _ParseIndexPage(html)
26    expected = [
27      ("abs", "abs.html", True),
28      ("abs", "complex/abs.html", True),
29      ("acos", "acos.html", False),
30      ("acosh", "acosh.html", False),
31      ("as_bytes", "as_bytes.html", False),
32    ]
33    self.assertEqual(len(actual), len(expected))
34    for i in range(0, len(actual)):
35      self.assertEqual(expected[i][0], actual[i][0])
36      self.assertTrue(actual[i][1].endswith(expected[i][1]))
37      self.assertEqual(expected[i][2], actual[i][2])
38
39
40  def testParseSymbolPage_SingleHeader(self):
41    # Defined in header <cmath>
42    html = """
43 <table class="t-dcl-begin"><tbody>
44  <tr class="t-dsc-header">
45  <td> <div>Defined in header <code><a href="cmath.html" title="cmath">&lt;cmath&gt;</a></code>
46   </div></td>
47  <td></td>
48  <td></td>
49  </tr>
50  <tr class="t-dcl">
51    <td>void foo()</td>
52    <td>this is matched</td>
53  </tr>
54</tbody></table>
55"""
56    self.assertEqual(_ParseSymbolPage(html, 'foo'), set(['<cmath>']))
57
58
59  def testParseSymbolPage_MulHeaders(self):
60    #  Defined in header <cstddef>
61    #  Defined in header <cstdio>
62    #  Defined in header <cstdlib>
63    html = """
64<table class="t-dcl-begin"><tbody>
65  <tr class="t-dsc-header">
66    <td> <div>Defined in header <code><a href="cstddef.html" title="cstddef">&lt;cstddef&gt;</a></code>
67     </div></td>
68     <td></td>
69    <td></td>
70  </tr>
71  <tr class="t-dcl">
72    <td>void bar()</td>
73    <td>this mentions foo, but isn't matched</td>
74  </tr>
75  <tr class="t-dsc-header">
76    <td> <div>Defined in header <code><a href="cstdio.html" title="cstdio">&lt;cstdio&gt;</a></code>
77     </div></td>
78    <td></td>
79    <td></td>
80  </tr>
81  <tr class="t-dsc-header">
82    <td> <div>Defined in header <code><a href=".cstdlib.html" title="ccstdlib">&lt;cstdlib&gt;</a></code>
83     </div></td>
84    <td></td>
85    <td></td>
86  </tr>
87  <tr class="t-dcl">
88    <td>
89      <span>void</span>
90      foo
91      <span>()</span>
92    </td>
93    <td>this is matched</td>
94  </tr>
95</tbody></table>
96"""
97    self.assertEqual(_ParseSymbolPage(html, "foo"),
98                     set(['<cstdio>', '<cstdlib>']))
99
100
101  def testParseSymbolPage_MulHeadersInSameDiv(self):
102    # Multile <code> blocks in a Div.
103    # Defined in header <algorithm>
104    # Defined in header <utility>
105    html = """
106<table class="t-dcl-begin"><tbody>
107<tr class="t-dsc-header">
108<td><div>
109     Defined in header <code><a href="../header/algorithm.html" title="cpp/header/algorithm">&lt;algorithm&gt;</a></code><br>
110     Defined in header <code><a href="../header/utility.html" title="cpp/header/utility">&lt;utility&gt;</a></code>
111</div></td>
112<td></td>
113</tr>
114<tr class="t-dcl">
115  <td>
116    <span>void</span>
117    foo
118    <span>()</span>
119  </td>
120  <td>this is matched</td>
121</tr>
122</tbody></table>
123"""
124    self.assertEqual(_ParseSymbolPage(html, "foo"),
125                     set(['<algorithm>', '<utility>']))
126
127  def testParseSymbolPage_MulSymbolsInSameTd(self):
128    # defined in header <cstdint>
129    #   int8_t
130    #   int16_t
131    html = """
132<table class="t-dcl-begin"><tbody>
133<tr class="t-dsc-header">
134<td><div>
135     Defined in header <code><a href="cstdint.html" title="cstdint">&lt;cstdint&gt;</a></code><br>
136</div></td>
137<td></td>
138</tr>
139<tr class="t-dcl">
140  <td>
141    <span>int8_t</span>
142    <span>int16_t</span>
143  </td>
144  <td>this is matched</td>
145</tr>
146</tbody></table>
147"""
148    self.assertEqual(_ParseSymbolPage(html, "int8_t"),
149                     set(['<cstdint>']))
150    self.assertEqual(_ParseSymbolPage(html, "int16_t"),
151                     set(['<cstdint>']))
152
153
154if __name__ == '__main__':
155  unittest.main()
156