Skip to content

Commit

Permalink
Restructure tests (#15)
Browse files Browse the repository at this point in the history
* Restructure tests

* Enhance tests

* Allow empty b32 and b64 byte strings

* Fix test name typo

Note: compiled base32 and base64 byte strings have changed from the form `byte base content` to `byte base(content)`. This allows empty byte strings of the form `byte base()` to be created.
  • Loading branch information
jasonpaulos committed Aug 13, 2020
1 parent 4d37cec commit a67fd6e
Show file tree
Hide file tree
Showing 19 changed files with 1,110 additions and 319 deletions.
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[mypy]

[mypy-pytest.*]
ignore_missing_imports = True
19 changes: 19 additions & 0 deletions pyteal/ast/addr_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from .. import *

def test_addr():
expr = Addr("NJUWK3DJNZTWU2LFNRUW4Z3KNFSWY2LOM5VGSZLMNFXGO2TJMVWGS3THMF")
assert expr.__teal__() == [
["addr", "NJUWK3DJNZTWU2LFNRUW4Z3KNFSWY2LOM5VGSZLMNFXGO2TJMVWGS3THMF"]
]

def test_addr_invalid():
with pytest.raises(TealInputError):
Addr("NJUWK3DJNZTWU2LFNRUW4Z3KNFSWY2LOM5VGSZLMNFXGO2TJMVWGS3TH")

with pytest.raises(TealInputError):
Addr("000000000000000000000000000000000000000000000000000000000")

with pytest.raises(TealInputError):
Addr(2)
19 changes: 19 additions & 0 deletions pyteal/ast/arg_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from .. import *

def test_arg():
expr = Arg(0)
assert expr.__teal__() == [
["arg", "0"]
]

def test_arg_invalid():
with pytest.raises(TealInputError):
Arg("k")

with pytest.raises(TealInputError):
Arg(-1)

with pytest.raises(TealInputError):
Arg(256)
263 changes: 263 additions & 0 deletions pyteal/ast/binaryexpr_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
import pytest

from .. import *

def test_add():
expr = Add(Int(2), Int(3))
assert expr.__teal__() == [
["int", "2"],
["int", "3"],
["+"]
]

def test_add_overload():
expr = Int(2) + Int(3) + Int(4)
assert expr.__teal__() == [
["int", "2"],
["int", "3"],
["+"],
["int", "4"],
["+"]
]

def test_add_invalid():
with pytest.raises(TealTypeError):
Add(Int(2), Txn.receiver())

with pytest.raises(TealTypeError):
Add(Txn.sender(), Int(2))

def test_minus():
expr = Minus(Int(5), Int(6))
assert expr.__teal__() == [
["int", "5"],
["int", "6"],
["-"]
]

def test_minus_overload():
expr = Int(10) - Int(1) - Int(2)
assert expr.__teal__() == [
["int", "10"],
["int", "1"],
["-"],
["int", "2"],
["-"]
]

def test_minus_invalid():
with pytest.raises(TealTypeError):
Minus(Int(2), Txn.receiver())

with pytest.raises(TealTypeError):
Minus(Txn.sender(), Int(2))

def test_mul():
expr = Mul(Int(3), Int(8))
assert expr.__teal__() == [
["int", "3"],
["int", "8"],
["*"]
]

def test_mul_overload():
expr = Int(3) * Int(8) * Int(10)
assert expr.__teal__() == [
["int", "3"],
["int", "8"],
["*"],
["int", "10"],
["*"]
]

def test_mul_invalid():
with pytest.raises(TealTypeError):
Mul(Int(2), Txn.receiver())

with pytest.raises(TealTypeError):
Mul(Txn.sender(), Int(2))

def test_div():
expr = Div(Int(9), Int(3))
assert expr.__teal__() == [
["int", "9"],
["int", "3"],
["/"]
]

def test_div_overload():
expr = Int(9) / Int(3) / Int(3)
assert expr.__teal__() == [
["int", "9"],
["int", "3"],
["/"],
["int", "3"],
["/"],
]

def test_div_invalid():
with pytest.raises(TealTypeError):
Div(Int(2), Txn.receiver())

with pytest.raises(TealTypeError):
Div(Txn.sender(), Int(2))

def test_mod():
expr = Mod(Int(10), Int(9))
assert expr.__teal__() == [
["int", "10"],
["int", "9"],
["%"]
]

def test_mod_overload():
expr = Int(10) % Int(9) % Int(100)
assert expr.__teal__() == [
["int", "10"],
["int", "9"],
["%"],
["int", "100"],
["%"]
]

def test_mod_invalid():
with pytest.raises(TealTypeError):
Mod(Txn.receiver(), Int(2))

with pytest.raises(TealTypeError):
Mod(Int(2), Txn.sender())

def test_arithmetic():
v = ((Int(2) + Int(3))/((Int(5) - Int(6)) * Int(8))) % Int(9)
assert v.__teal__() == \
[['int', '2'], ['int', '3'], ['+'], ['int', '5'], ['int', '6']] + \
[['-'], ['int', '8'], ['*'], ['/'], ['int', '9'], ['%']]

def test_eq():
expr_int = Eq(Int(2), Int(3))
assert expr_int.__teal__() == [
["int", "2"],
["int", "3"],
["=="]
]

expr_bytes = Eq(Txn.receiver(), Txn.sender())
assert expr_bytes.__teal__() == [
["txn", "Receiver"],
["txn", "Sender"],
["=="]
]

def test_eq_overload():
expr_int = Int(2) == Int(3)
assert expr_int.__teal__() == [
["int", "2"],
["int", "3"],
["=="]
]

expr_bytes = Txn.receiver() == Txn.sender()
assert expr_bytes.__teal__() == [
["txn", "Receiver"],
["txn", "Sender"],
["=="]
]

def test_eq_invalid():
with pytest.raises(TealTypeError):
Eq(Txn.fee(), Txn.receiver())

with pytest.raises(TealTypeError):
Eq(Txn.sender(), Int(7))

def test_lt():
expr = Lt(Int(2), Int(3))
assert expr.__teal__() == [
["int", "2"],
["int", "3"],
["<"]
]

def test_lt_overload():
expr = Int(2) < Int(3)
assert expr.__teal__() == [
["int", "2"],
["int", "3"],
["<"]
]

def test_lt_invalid():
with pytest.raises(TealTypeError):
Lt(Int(7), Txn.receiver())

with pytest.raises(TealTypeError):
Lt(Txn.sender(), Int(7))

def test_le():
expr = Le(Int(1), Int(2))
assert expr.__teal__() == [
["int", "1"],
["int", "2"],
["<="]
]

def test_le_overload():
expr = Int(1) <= Int(2)
assert expr.__teal__() == [
["int", "1"],
["int", "2"],
["<="]
]

def test_le_invalid():
with pytest.raises(TealTypeError):
Le(Int(1), Txn.receiver())

with pytest.raises(TealTypeError):
Le(Txn.sender(), Int(1))

def test_gt():
expr = Gt(Int(2), Int(3))
assert expr.__teal__() == [
["int", "2"],
["int", "3"],
[">"]
]

def test_gt_overload():
expr = Int(2) > Int(3)
assert expr.__teal__() == [
["int", "2"],
["int", "3"],
[">"]
]

def test_gt_invalid():
with pytest.raises(TealTypeError):
Gt(Int(1), Txn.receiver())

with pytest.raises(TealTypeError):
Gt(Txn.receiver(), Int(1))

def test_ge():
expr = Ge(Int(1), Int(10))
assert expr.__teal__() == [
["int", "1"],
["int", "10"],
[">="]
]

def test_ge_overload():
expr = Int(1) >= Int(10)
assert expr.__teal__() == [
["int", "1"],
["int", "10"],
[">="]
]

def test_ge_invalid():
with pytest.raises(TealTypeError):
Ge(Int(1), Txn.receiver())

with pytest.raises(TealTypeError):
Ge(Txn.receiver(), Int(1))
9 changes: 5 additions & 4 deletions pyteal/ast/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ def __init__(self, base: str, byte_str: str) -> None:
raise TealInputError("invalid base {}, need to be base32, base64, or base16.".format(base))

def __teal__(self):
if self.base != "base16":
return [["byte", self.base, self.byte_str]]
if self.base == "base16":
payload = "0x" + self.byte_str
else:
return [["byte", "0x" + self.byte_str]]

payload = "{}({})".format(self.base, self.byte_str)
return [["byte", payload]]

def __str__(self):
return "({} bytes: {})".format(self.base, self.byte_str)

Expand Down
58 changes: 58 additions & 0 deletions pyteal/ast/bytes_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest

from .. import *

def test_bytes_base32():
expr = Bytes("base32", "7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M")
assert expr.__teal__() == [
["byte", "base32(7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M)"]
]

def test_bytes_base32_empty():
expr = Bytes("base32", "")
assert expr.__teal__() == [
["byte", "base32()"]
]

def test_bytes_base64():
expr = Bytes("base64", "Zm9vYmE=")
assert expr.__teal__() == [
["byte", "base64(Zm9vYmE=)"]
]

def test_bytes_base64_empty():
expr = Bytes("base64", "")
assert expr.__teal__() == [
["byte", "base64()"]
]

def test_bytes_base16():
expr = Bytes("base16", "A21212EF")
assert expr.__teal__() == [
["byte", "0xA21212EF"]
]

def test_bytes_base16_prefix():
b16_2 = Bytes("base16", "0xA21212EF")
assert b16_2.__teal__() == [
["byte", "0xA21212EF"]
]

def test_bytes_base16_empty():
expr = Bytes("base16", "")
assert expr.__teal__() == [
["byte", "0x"]
]

def test_bytes_invalid():
with pytest.raises(TealInputError):
Bytes("base23", "")

with pytest.raises(TealInputError):
Bytes("base32", "Zm9vYmE=")

with pytest.raises(TealInputError):
Bytes("base64", "?????")

with pytest.raises(TealInputError):
Bytes("base16", "7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M")

0 comments on commit a67fd6e

Please sign in to comment.