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