Commit 43f64b24 authored by Moul's avatar Moul

[mod] grammars/test_output: import class directly

parent 676b1099
...@@ -2,92 +2,92 @@ import unittest ...@@ -2,92 +2,92 @@ import unittest
import pypeg2 import pypeg2
from duniterpy.grammars import output from duniterpy.grammars.output import SIG, CLTV, CSV, XHX, Operator, Condition
class TestOutputgrammar(unittest.TestCase): class TestOutputgrammar(unittest.TestCase):
def test_sig(self): def test_sig(self):
condition = "SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd)" condition = "SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd)"
result = pypeg2.parse(condition, output.SIG) result = pypeg2.parse(condition, SIG)
self.assertEqual(result.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(result.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(pypeg2.compose(result, output.Condition), condition) self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_xhx(self): def test_xhx(self):
condition = "XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)" condition = "XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)"
result = pypeg2.parse(condition, output.XHX) result = pypeg2.parse(condition, XHX)
self.assertEqual(result.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6") self.assertEqual(result.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6")
def test_sig_condition(self): def test_sig_condition(self):
condition = "SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd)" condition = "SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd)"
result = pypeg2.parse(condition, output.Condition) result = pypeg2.parse(condition, Condition)
self.assertEqual(result.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(result.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(pypeg2.compose(result, output.Condition), condition) self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_xhr_condition(self): def test_xhr_condition(self):
condition = "SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd)" condition = "SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd)"
result = pypeg2.parse(condition, output.Condition) result = pypeg2.parse(condition, Condition)
self.assertEqual(result.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(result.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(pypeg2.compose(result, output.Condition), condition) self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_simple_condition(self): def test_simple_condition(self):
condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd))" condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd))"
result = pypeg2.parse(condition, output.Condition) result = pypeg2.parse(condition, Condition)
self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(pypeg2.compose(result, output.Condition), condition) self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_simple_and_condition(self): def test_simple_and_condition(self):
condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) && XHX(" \ condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) && XHX(" \
"309BC5E644F797F53E5A2065EAF38A173437F2E6))" "309BC5E644F797F53E5A2065EAF38A173437F2E6))"
result = pypeg2.parse(condition, output.Condition) result = pypeg2.parse(condition, Condition)
self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(result.left.op.name, "&&") self.assertEqual(result.left.op.name, "&&")
self.assertEqual(result.left.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6") self.assertEqual(result.left.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6")
self.assertEqual(pypeg2.compose(result, output.Condition), condition) self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_simple_or_condition(self): def test_simple_or_condition(self):
condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || XHX(" \ condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || XHX(" \
"309BC5E644F797F53E5A2065EAF38A173437F2E6))" "309BC5E644F797F53E5A2065EAF38A173437F2E6))"
result = pypeg2.parse(condition, output.Condition) result = pypeg2.parse(condition, Condition)
self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(result.left.op.name, "||") self.assertEqual(result.left.op.name, "||")
self.assertEqual(result.left.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6") self.assertEqual(result.left.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6")
self.assertEqual(pypeg2.compose(result, output.Condition), condition) self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_complex_condition(self): def test_complex_condition(self):
condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || (SIG(" \ condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || (SIG(" \
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)))" "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)))"
result = pypeg2.parse(condition, output.Condition) result = pypeg2.parse(condition, Condition)
self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(result.left.op.name, "||") self.assertEqual(result.left.op.name, "||")
self.assertEqual(result.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV") self.assertEqual(result.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV")
self.assertEqual(result.left.right.op.name, "&&") self.assertEqual(result.left.right.op.name, "&&")
self.assertEqual(result.left.right.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6") self.assertEqual(result.left.right.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6")
self.assertEqual(pypeg2.compose(result, output.Condition), condition) self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_csv_cltv_condition(self): def test_csv_cltv_condition(self):
condition = "(CSV(1654300) || (SIG(DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && CLTV(2594024)))" condition = "(CSV(1654300) || (SIG(DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && CLTV(2594024)))"
result = pypeg2.parse(condition, output.Condition) result = pypeg2.parse(condition, Condition)
self.assertEqual(result.left.left.time, "1654300") self.assertEqual(result.left.left.time, "1654300")
self.assertEqual(result.left.op.name, "||") self.assertEqual(result.left.op.name, "||")
self.assertEqual(result.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV") self.assertEqual(result.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV")
self.assertEqual(result.left.right.op.name, "&&") self.assertEqual(result.left.right.op.name, "&&")
self.assertEqual(result.left.right.right.timestamp, "2594024") self.assertEqual(result.left.right.right.timestamp, "2594024")
self.assertEqual(pypeg2.compose(result, output.Condition), condition) self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_instanciate_condition(self): def test_instanciate_condition(self):
output.Condition.token(output.SIG.token("HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"), Condition.token(SIG.token("HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"),
output.Operator.token("||"), Operator.token("||"),
output.Condition.token( Condition.token(
output.SIG.token("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"), SIG.token("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"),
output.Operator.token("&&"), Operator.token("&&"),
output.XHX.token("309BC5E644F797F53E5A2065EAF38A173437F2E6") XHX.token("309BC5E644F797F53E5A2065EAF38A173437F2E6")
)) ))
condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || (SIG(" \ condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || (SIG(" \
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)))" "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)))"
inst = pypeg2.parse(condition, output.Condition) inst = pypeg2.parse(condition, Condition)
self.assertEqual(inst.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(inst.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(inst.left.op.name, "||") self.assertEqual(inst.left.op.name, "||")
self.assertEqual(inst.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV") self.assertEqual(inst.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV")
self.assertEqual(inst.left.right.op.name, "&&") self.assertEqual(inst.left.right.op.name, "&&")
self.assertEqual(inst.left.right.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6") self.assertEqual(inst.left.right.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6")
self.assertEqual(pypeg2.compose(inst, output.Condition), condition) self.assertEqual(pypeg2.compose(inst, Condition), condition)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment