1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 from io
import StringIO
9 from incremental_dafsa
import Dafsa
, Node
12 def _node_to_string(node
: Node
, prefix
, buffer, cache
):
13 if not node
.is_end_node
:
15 str(ord(node
.character
)) if ord(node
.character
) < 10 else node
.character
19 cached
= cache
.get(id(node
))
20 buffer.write("{}{}".format(prefix
, "=" if cached
else "").strip() + "\n")
23 cache
[id(node
)] = node
25 for node
in sorted(node
.children
.values(), key
=lambda n
: n
.character
):
26 _node_to_string(node
, prefix
, buffer, cache
)
29 def _dafsa_to_string(dafsa
: Dafsa
):
30 """Encodes the dafsa into a string notation.
32 Each node is printed on its own line with all the nodes that precede it.
33 The end node is designated with the "$" character.
34 If it joins into an existing node, the end of the line is adorned with a "=".
35 Though this doesn't carry information about which other prefix it has joined with,
36 it has seemed to be precise enough for testing.
38 For example, with the input data of:
43 [root] --- a ---- 1 --- [end]
50 a1$ <- end node was found
52 ac1= <- joins with the "a1" prefix
54 bc= <- joins with the "ac" prefix
59 for node
in sorted(dafsa
.root_node
.children
.values(), key
=lambda n
: n
.character
):
60 _node_to_string(node
, "", buffer, cache
)
61 return buffer.getvalue().strip()
65 return [line
.strip() for line
in data
.strip().split("\n")]
68 def _assert_dafsa(data
, expected
):
69 words
= _to_words(data
)
70 dafsa
= Dafsa
.from_tld_data(words
)
72 expected
= expected
.strip()
73 expected
= "\n".join([line
.strip() for line
in expected
.split("\n")])
74 as_string
= _dafsa_to_string(dafsa
)
75 assert as_string
== expected
78 class TestDafsa(unittest
.TestCase
):
545 if __name__
== "__main__":