forked from quantumlib/Cirq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget_test.py
75 lines (57 loc) · 2.28 KB
/
widget_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright 2021 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from pathlib import Path
from unittest import mock
import cirq_web
class FakeWidget(cirq_web.Widget):
def __init__(self):
super().__init__()
def get_client_code(self) -> str:
return "This is the test client code."
def get_widget_bundle_name(self) -> str:
return "testfile.txt"
def remove_whitespace(string: str) -> str:
return "".join(string.split())
def test_repr_html(tmpdir):
# # Reset the path so the files are accessible
cirq_web.widget._DIST_PATH = Path(tmpdir) / "dir"
path = tmpdir.mkdir('dir').join('testfile.txt')
path.write("This is a test bundle")
test_widget = FakeWidget()
actual = test_widget._repr_html_()
expected = f"""
<meta charset="UTF-8">
<div id="{test_widget.id}"></div>
<script>This is a test bundle</script>
This is the test client code.
"""
assert remove_whitespace(expected) == remove_whitespace(actual)
@mock.patch.dict(os.environ, {"BROWSER": "true"})
def test_generate_html_file_with_browser(tmpdir):
# # Reset the path so the files are accessible
cirq_web.widget._DIST_PATH = Path(tmpdir) / "dir"
path = tmpdir.mkdir('dir')
testfile_path = path.join('testfile.txt')
testfile_path.write("This is a test bundle")
test_widget = FakeWidget()
test_html_path = test_widget.generate_html_file(str(path), 'test.html', open_in_browser=True)
actual = open(test_html_path, 'r', encoding='utf-8').read()
expected = f"""
<meta charset="UTF-8">
<div id="{test_widget.id}"></div>
<script>This is a test bundle</script>
This is the test client code.
"""
assert remove_whitespace(expected) == remove_whitespace(actual)