1import smartpy as sp
2
3
4@sp.module
5def M():
6 class C(sp.Contract):
7 def __init__(self):
8 pass
9
10 @sp.private
11 def test_drop_first(self):
12 assert string_utils.drop_first((0, "abc")) == sp.Some("abc")
13 assert string_utils.drop_first((1, "abc")) == sp.Some("bc")
14 assert string_utils.drop_first((2, "abc")) == sp.Some("c")
15 assert string_utils.drop_first((3, "abc")) == sp.Some("")
16 assert string_utils.drop_first((4, "abc")) == None
17 assert string_utils.drop_first((5, "abc")) == None
18
19 @sp.private
20 def test_split(self):
21 assert sp.pack(string_utils.split((" ", " "))) == sp.pack(
22 sp.cast([], sp.list[sp.string])
23 )
24 assert sp.pack(string_utils.split(("", " "))) == sp.pack(
25 sp.cast([], sp.list[sp.string])
26 )
27 assert sp.pack(string_utils.split(("abc def", "-"))) == sp.pack(["abc def"])
28 assert sp.pack(string_utils.split(("abc def", " "))) == sp.pack(
29 ["abc", "def"]
30 )
31 assert sp.pack(string_utils.split(("abc def ", " "))) == sp.pack(
32 ["abc", "def"]
33 )
34 assert sp.pack(
35 string_utils.split((" abc def ", " "))
36 ) == sp.pack(["abc", "def"])
37 assert sp.pack(string_utils.split(("abc def ghi", " "))) == sp.pack(
38 ["abc", "def", "ghi"]
39 )
40 assert sp.pack(string_utils.split(("abc def ghi ", " "))) == sp.pack(
41 ["abc", "def", "ghi"]
42 )
43 assert sp.pack(string_utils.split(("abc def ghi ", " "))) == sp.pack(
44 ["abc", "def", "ghi"]
45 )
46 assert sp.pack(string_utils.split(("abc,def,ghi", ","))) == sp.pack(
47 ["abc", "def", "ghi"]
48 )
49 assert sp.pack(string_utils.split(("abc##def##ghi##", "##"))) == sp.pack(
50 ["abc", "def", "ghi"]
51 )
52
53 @sp.private
54 def test_starts_with(self):
55 assert not string_utils.starts_with(("abc", ""))
56 assert not string_utils.starts_with(("abc", "ab"))
57 assert string_utils.starts_with(("abc", "abc"))
58 assert string_utils.starts_with(("abc", "abcd"))
59 assert string_utils.starts_with(("abc", "abcdefg"))
60
61 @sp.private
62 def test_find_first(self):
63 assert string_utils.find_first((".", "abc")) == None
64 assert string_utils.find_first((".", ".abc")) == sp.Some(0)
65 assert string_utils.find_first((".", "a.bc")) == sp.Some(1)
66 assert string_utils.find_first((".", "ab.c")) == sp.Some(2)
67 assert string_utils.find_first((".", "abc.")) == sp.Some(3)
68 assert string_utils.find_first(("/", "4/6")) == sp.Some(1)
69
70 @sp.private
71 def test_from_digit(self):
72 assert string_utils.from_digit(0) == "0"
73 assert string_utils.from_digit(1) == "1"
74 assert string_utils.from_digit(2) == "2"
75 assert string_utils.from_digit(3) == "3"
76 assert string_utils.from_digit(4) == "4"
77 assert string_utils.from_digit(5) == "5"
78 assert string_utils.from_digit(6) == "6"
79 assert string_utils.from_digit(7) == "7"
80 assert string_utils.from_digit(8) == "8"
81 assert string_utils.from_digit(9) == "9"
82
83 @sp.private
84 def test_from_int(self):
85 assert string_utils.from_int(1) == "1"
86 assert string_utils.from_int(2) == "2"
87 assert string_utils.from_int(3) == "3"
88 assert string_utils.from_int(123) == "123"
89 assert string_utils.from_int(-123) == "-123"
90
91 @sp.private
92 def test_from_rational(self):
93 assert string_utils.from_rational(rational.mk((4, 3))) == "4/3"
94 assert string_utils.from_rational(rational.mk((-4, 3))) == "-4/3"
95 assert string_utils.from_rational(rational.mk((2, 6))) == "1/3"
96
97 @sp.private
98 def test_from_fixed_point(self):
99 assert string_utils.from_fixed_point(fp.mk((123, 2))) == "1.23"
100
101 @sp.private
102 def test_to_digit(self):
103 assert string_utils.to_digit("1") == 1
104 assert string_utils.to_digit("2") == 2
105 assert string_utils.to_digit("3") == 3
106 assert string_utils.to_digit("4") == 4
107 assert string_utils.to_digit("5") == 5
108 assert string_utils.to_digit("6") == 6
109 assert string_utils.to_digit("7") == 7
110 assert string_utils.to_digit("8") == 8
111 assert string_utils.to_digit("9") == 9
112
113 @sp.private
114 def test_to_int(self):
115 assert string_utils.to_int("1") == 1
116 assert string_utils.to_int("12") == 12
117 assert string_utils.to_int("123") == 123
118 assert string_utils.to_int("1234") == 1234
119
120 @sp.private
121 def test_to_rational(self):
122 assert string_utils.to_rational("4/6") == rational.mk((2, 3))
123 assert string_utils.to_rational("4/60") == rational.mk((2, 30))
124 assert string_utils.to_rational("04/60") == rational.mk((2, 30))
125 assert string_utils.to_rational("0/3") == rational.mk((0, 3))
126
127 @sp.entrypoint
128 def ep(self):
129 self.test_drop_first()
130 self.test_split()
131 self.test_starts_with()
132 self.test_find_first()
133 self.test_from_digit()
134 self.test_from_int()
135 self.test_from_rational()
136 self.test_from_fixed_point()
137 self.test_to_digit()
138 self.test_to_int()
139 self.test_to_rational()
140
141
142@sp.add_test()
143def test():
144 s = sp.test_scenario("Test", [sp.math, sp.rational, sp.string_utils, sp.fp, M])
145 c = M.C()
146 s += c
147 c.ep()