import pytest
from katana.storage import Node, Pair, prepare
from katana.compound import sequence, group, repeat, option, maybe
from katana.term import term


A = term('a')
B = term('b')
C = term('c')
node = lambda x: Node(x, 'data')


def test_sequence():
    na = node('a')
    nb = node('b')
    s = sequence(A, B)

    given = prepare([na, nb])
    after = Pair([na, nb], [])
    assert s(given) == after


def test_group():
    n = node('a')
    g = group(A)
    given = prepare([n])
    after = Pair([Node(g, [n])], [])
    assert g(given) == after


def test_repeat():
    n = node('a')
    r = repeat(A)
    given = prepare([n]*10)
    after = Pair([n]*10, [])
    assert r(given) == after


def test_option():
    a = node('a')
    b = node('b')
    c = node('c')
    opt = option(A, B, C)
    for item in [a, b]:
        assert opt(prepare([item])) == Pair([item], [])


def test_option_empty():
    c = node('c')
    with pytest.raises(ValueError):
        assert option(A, B)(prepare([c]))


def test_maybe():
    m = maybe(A)
    a = node('a')
    b = node('b')
    assert m(prepare([b])) == Pair([], [b])
    assert m(prepare([a])) == Pair([a], [])

@@ -152,17 +152,18 @@
  term%0A%0A%0A
-A
+Ta
  = term(
@@ -167,17 +167,18 @@
 rm('a')%0A
-B
+Tb
  = term(
@@ -182,17 +182,18 @@
 rm('b')%0A
-C
+Tc
  = term(
@@ -201,31 +201,67 @@
 c')%0A
-node = lambda x:
+Na = Node('a', 'data')%0ANb = Node('b', 'data')%0ANc =
  Node(
-x
+'c'
 , 'd
@@ -293,46 +293,8 @@
 ():%0A
-    na = node('a')%0A    nb = node('b')%0A
     
@@ -306,20 +306,22 @@
 equence(
-A, B
+Ta, Tb
 )%0A%0A    g
@@ -336,21 +336,21 @@
 repare(%5B
-na, n
+Na, N
 b%5D)%0A    
@@ -367,13 +367,13 @@
 ir(%5B
-na, n
+Na, N
 b%5D, 
@@ -425,34 +425,16 @@
 roup():%0A
-    n = node('a')%0A
     g = 
@@ -439,17 +439,18 @@
 = group(
-A
+Ta
 )%0A    gi
@@ -464,17 +464,18 @@
 repare(%5B
-n
+Na
 %5D)%0A    a
@@ -496,17 +496,18 @@
 ode(g, %5B
-n
+Na
 %5D)%5D, %5B%5D)
@@ -561,26 +561,8 @@
 ():%0A
-    n = node('a')%0A
     
@@ -572,17 +572,18 @@
  repeat(
-A
+Ta
 )%0A    gi
@@ -597,17 +597,18 @@
 repare(%5B
-n
+Na
 %5D*10)%0A  
@@ -623,17 +623,18 @@
 = Pair(%5B
-n
+Na
 %5D*10, %5B%5D
@@ -689,62 +689,8 @@
 ():%0A
-    a = node('a')%0A    b = node('b')%0A    c = node('c')%0A
     
@@ -706,15 +706,18 @@
 ion(
-A, B, C
+Ta, Tb, Tc
 )%0A  
@@ -726,20 +726,22 @@
 for 
-item
+node
  in %5B
+N
 a, 
+N
 b%5D:%0A
@@ -768,20 +768,20 @@
 repare(%5B
-item
+node
 %5D)) == P
@@ -789,12 +789,12 @@
 ir(%5B
-item
+node
 %5D, %5B
@@ -827,26 +827,8 @@
 ():%0A
-    c = node('c')%0A
     
@@ -885,12 +885,14 @@
 ion(
-A, B
+Ta, Tb
 )(pr
@@ -898,16 +898,17 @@
 repare(%5B
+N
 c%5D))%0A%0A%0Ad
@@ -942,45 +942,10 @@
 ybe(
-A)%0A    a = node('a')%0A    b = node('b'
+Ta
 )%0A  
@@ -964,16 +964,17 @@
 repare(%5B
+N
 b%5D)) == 
@@ -983,16 +983,17 @@
 ir(%5B%5D, %5B
+N
 b%5D)%0A    
@@ -1010,16 +1010,17 @@
 repare(%5B
+N
 a%5D)) == 
@@ -1025,16 +1025,17 @@
 = Pair(%5B
+N
 a%5D, %5B%5D)%0A

Refactor test case for compound terms