forked from google-gemini/deprecated-generative-ai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_string_utils.py
More file actions
113 lines (97 loc) · 3.36 KB
/
Copy pathtest_string_utils.py
File metadata and controls
113 lines (97 loc) · 3.36 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
# Copyright 2023 Google LLC
#
# 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
#
# http://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 dataclasses
import pprint
import textwrap
from typing import Any
import unittest
from google.generativeai import string_utils
from absl.testing import parameterized
@string_utils.prettyprint
@dataclasses.dataclass
class MyClass:
a: int
b: float
c: list[int]
d: Any
class OperationsTests(parameterized.TestCase):
def test_simple(self):
m = MyClass(a=1, b=1 / 3, c=[0, 1, 2, 3, 4, 5], d={"a": 1, "b": 2})
result = str(m)
expected = textwrap.dedent(
"""
MyClass(a=1,
b=0.3333333333333333,
c=[0, 1, 2, 3, 4, 5],
d={'a': 1, 'b': 2})"""
)[1:]
self.assertEqual(expected, result)
self.assertEqual(pprint.pformat(m), result)
self.assertEqual(repr(m), result)
def test_long_list(self):
m = MyClass(a=1, b=1 / 3, c=[1, 2, 3] * 10, d={"a": 1, "b": 2})
expected = textwrap.dedent(
"""
MyClass(a=1,
b=0.3333333333333333,
c=[...],
d={'a': 1, 'b': 2})"""
)[1:]
self.assertEqual(expected, str(m))
def test_nested(self):
m1 = MyClass(a=1, b=1 / 3, c=[1, 2, 3], d={"a": 1, "b": 2})
m2 = MyClass(a=1, b=1 / 3, c=[1, 2, 3], d=m1)
expected = textwrap.dedent(
"""
MyClass(a=1,
b=0.3333333333333333,
c=[1, 2, 3],
d=MyClass(a=1,
b=0.3333333333333333,
c=[1, 2, 3],
d={'a': 1, 'b': 2}))"""
)[1:]
result = str(m2)
self.assertEqual(expected, result)
self.assertEqual(pprint.pformat(m2), result)
self.assertEqual(repr(m2), result)
def test_long_obj(self):
m = MyClass(a=1, b=1 / 3, c=[1, 2, 3], d=None)
m = MyClass(a=1, b=1 / 3, c=[1, 2, 3], d=m)
m = MyClass(a=1, b=1 / 3, c=[1, 2, 3], d=m)
m = MyClass(a=1, b=1 / 3, c=[1, 2, 3], d=m)
m = MyClass(a=1, b=1 / 3, c=[1, 2, 3], d=m)
expected = textwrap.dedent(
"""
MyClass(a=1,
b=0.3333333333333333,
c=[1, 2, 3],
d=MyClass(...))"""
)[1:]
self.assertEqual(expected, str(m))
def test_recursive(self):
m = MyClass(a=1, b=1 / 3, c=[1, 2, 3], d=None)
m.d = m
expected = textwrap.dedent(
"""
MyClass(a=1,
b=0.3333333333333333,
c=[1, 2, 3],
d=...)"""
)[1:]
result = str(m)
self.assertEqual(expected, result)
self.assertEqual(pprint.pformat(m), result)
self.assertEqual(repr(m), result)