Example #1
0
def test_CS_push(tmpdir):
    labels = copy.deepcopy(TEST_LABELS)
    tf = make_corr_file(tmpdir)

    cs = eved.EditStack(labels=labels, ops_file=tf.name, load=True)
    assert cs.labels[1]['name'] == "b"
    assert cs.labels[2]['stop'] == 4.5
    assert cs.labels[0]['name'] == "q"

    new_cmd = """(set-name #:target (interval #:index 1 #:name "b") #:new-name "z")"""
    cs.push(eved.parse(new_cmd))  # push adds new_cmd to head of stack
    assert cs.labels[1]['name'] == "z"
    assert cs.labels[2]['stop'] == 4.5
    assert cs.labels[0]['name'] == "q"

    cs.undo()
    cs.undo()
    cs.push(eved.parse(
        new_cmd))  # TEST_OPS[1] is gone; new_cmd now at head of stack
    assert len(cs.undo_stack) == 2
    assert cs.labels[1]['name'] == "z"
    assert cs.labels[2]['stop'] == 4.2
    assert cs.labels[0]['name'] == "q"

    os.remove(tf.name)
Example #2
0
def test_invert():
    cmd = '(merge-next #:target (interval-pair #:index 0 #:name null #:sep null #:next-name null) #:new-name "b" #:new-sep 1.5 #:new-next-name "c")'
    hand_inv = '(split #:target (interval-pair #:index 0 #:name "b" #:sep 1.5 #:next-name "c") #:new-name null #:new-sep null #:new-next-name null)'
    inv = eved.invert(eved.parse(cmd))
    assert inv == eved.parse(hand_inv)

    ident = eved.invert(eved.invert(eved.parse(cmd)))
    assert cmd == eved.deparse(ident)
Example #3
0
def test_CS_peek(tmpdir):
    labels = copy.deepcopy(TEST_LABELS)
    tf = make_corr_file(tmpdir)

    cs = eved.EditStack(labels=labels, ops_file=tf.name, load=True)
    p = cs.peek()  # default: show op at top of undo_stack
    assert p == eved.parse(TEST_OPS[1])

    with pytest.raises(IndexError):
        p = cs.peek(len(cs.undo_stack) + 10)

    with pytest.raises(IndexError):
        p = cs.peek(-10)

    p = cs.peek(0)
    assert p == eved.parse(TEST_OPS[0])

    os.remove(tf.name)
Example #4
0
def test_CS_write_to_file(tmpdir):
    labels = copy.deepcopy(TEST_LABELS)
    tf = make_corr_file(tmpdir)

    cs = eved.EditStack(labels=labels, ops_file=tf.name, load=True)
    new_cmd = """(set-name #:target (interval #:index 1 #:name "b") #:new-name "z")"""
    cs.push(eved.parse(new_cmd))
    os.remove(tf.name)
    cs.write_to_file()
    assert os.path.exists(cs.file)
    assert os.path.exists(cs.file + '.yaml')

    labels = copy.deepcopy(TEST_LABELS)
    cs_new = eved.EditStack(labels=labels, ops_file=tf.name, load=True)
    assert len(cs_new.undo_stack) == 3
    assert cs_new.undo_stack == cs.undo_stack
    assert cs_new.undo_stack[-1] == eved.parse(new_cmd)
    assert cs_new.hash_pre == cs.hash_pre

    os.remove(tf.name)
Example #5
0
def test_CS_read_from_file(tmpdir):
    labels = copy.deepcopy(TEST_LABELS)
    tf = make_corr_file(tmpdir)

    cs = eved.EditStack(labels=labels, ops_file=tf.name, load=False)
    cs.read_from_file(tf.name)
    assert cs.labels[0]['name'] == 'q'
    assert cs.labels[2]['stop'] == 4.5
    assert list(cs.undo_stack) == [eved.parse(op) for op in TEST_OPS]

    os.remove(tf.name)
Example #6
0
def test_CS_init(tmpdir):
    labels = copy.deepcopy(TEST_LABELS)
    tf = make_corr_file(tmpdir)

    cs = eved.EditStack(labels=labels, ops_file=tf.name, load=False)
    assert cs.labels == TEST_LABELS
    assert cs.file == tf.name
    assert len(cs.undo_stack) == 0

    cs = eved.EditStack(labels=labels, ops_file=tf.name, load=True)
    assert cs.file == tf.name
    assert len(cs.undo_stack) == 2
    assert cs.undo_stack[0] == eved.parse(TEST_OPS[0])
    assert cs.undo_stack[1] == eved.parse(TEST_OPS[1])
    assert cs.labels[1] == TEST_LABELS[1]
    assert cs.labels[3] == TEST_LABELS[3]
    assert cs.labels[0]['name'] == 'q'
    assert cs.labels[2]['stop'] == 4.5

    os.remove(tf.name)
Example #7
0
def test_CS__apply(tmpdir):
    labels = copy.deepcopy(TEST_LABELS)
    tf = make_corr_file(tmpdir)

    cs = eved.EditStack(labels=labels, ops_file=tf.name, load=True)
    new_cmd = """(set-name #:target (interval #:index 1 #:name "b") #:new-name "z")"""
    cs._apply(eved.parse(new_cmd))
    # the stack is now in an undefined state
    # but we can still check that _apply performed the new_cmd operation
    assert cs.labels[1]['name'] == 'z'

    os.remove(tf.name)
Example #8
0
def test_whole_stack():
    labels = copy.deepcopy(TEST_LABELS)
    test_env = eved.make_env(labels=labels)

    cmd = """(set-name #:target (interval #:index 0 #:name "a") #:new-name "b")"""
    eved.evaluate(eved.parse(cmd), test_env)
    assert labels[0]['name'] == 'b'

    cmd = """(set-start #:target (interval #:index 1 #:start 3.141) #:new-start 2.2)"""
    eved.evaluate(eved.parse(cmd), test_env)
    assert labels[1]['start'] == 2.2

    cmd = """(merge-next #:target (interval #:index 1 #:name "b" #:stop 3.240 #:next-start 3.240 #:next-name "silence") #:new_stop null #:new_next_start null)"""
    eved.evaluate(eved.parse(cmd), test_env)
    assert len(labels) == len(TEST_LABELS) - 1
    assert labels[1]['stop'] == TEST_LABELS[2]['stop']

    cmd = """(split #:target (interval #:index 1 #:name "b" #:stop null #:next-start null #:next-name "b") #:new_stop 3.5 #:new_next_start 3.5)"""
    eved.evaluate(eved.parse(cmd), test_env)
    assert len(labels) == len(TEST_LABELS)
    assert labels[1]['stop'] == TEST_LABELS[1]['stop']
Example #9
0
def test_parse_and_read_from_tokens():
    with pytest.raises(SyntaxError):
        eved.read_from_tokens([])

    with pytest.raises(SyntaxError):
        eved.read_from_tokens([')'])

    nested_list = eved.parse(TEST_COMMAND)
    assert len(nested_list) == 3
    assert len(nested_list[1]) == 6
    assert nested_list[0] == 'set_name'
    assert nested_list[1][0] == 'interval'
    assert nested_list[1][5] == 'focus_bird'
Example #10
0
def test_CS_undo_and_redo(tmpdir):
    labels = copy.deepcopy(TEST_LABELS)
    tf = make_corr_file(tmpdir)

    cs = eved.EditStack(labels=labels, ops_file=tf.name, load=True)
    new_cmd = """(set-name #:target (interval #:index 1 #:name "b") #:new-name "z")"""
    cs.push(eved.parse(new_cmd))
    assert len(cs.undo_stack) == 3
    assert len(cs.redo_stack) == 0
    assert cs.labels[1]['name'] == "z"
    assert cs.labels[2]['stop'] == 4.5
    assert cs.labels[0]['name'] == "q"

    cs.undo()  # undo new_cmd
    assert len(cs.undo_stack) == 2
    assert len(cs.redo_stack) == 1
    assert cs.labels[1]['name'] == "b"
    assert cs.labels[2]['stop'] == 4.5
    assert cs.labels[0]['name'] == "q"

    cs.undo()  # undo TEST_OPS[1]
    assert len(cs.undo_stack) == 1
    assert len(cs.redo_stack) == 2
    assert cs.labels[1]['name'] == "b"
    assert cs.labels[2]['stop'] == 4.2
    assert cs.labels[0]['name'] == "q"

    cs.undo()  # undo TEST_OPS[0]
    assert len(cs.undo_stack) == 0
    assert len(cs.redo_stack) == 3
    assert cs.labels[1]['name'] == "b"
    assert cs.labels[2]['stop'] == 4.2
    assert cs.labels[0]['name'] == "a"

    # undo on an empty undo_stack raises exception
    with pytest.raises(IndexError):
        cs.undo()

    cs.redo()  # redo TEST_OPS[0]
    assert len(cs.undo_stack) == 1
    assert len(cs.redo_stack) == 2
    assert cs.labels[1]['name'] == "b"
    assert cs.labels[2]['stop'] == 4.2
    assert cs.labels[0]['name'] == "q"

    cs.redo()  # redo TEST_OPS[1]
    assert len(cs.undo_stack) == 2
    assert len(cs.redo_stack) == 1
    assert cs.labels[1]['name'] == "b"
    assert cs.labels[2]['stop'] == 4.5
    assert cs.labels[0]['name'] == "q"

    cs.redo()  # redo new_cmd
    assert len(cs.undo_stack) == 3
    assert len(cs.redo_stack) == 0
    assert cs.labels[1]['name'] == "z"
    assert cs.labels[2]['stop'] == 4.5
    assert cs.labels[0]['name'] == "q"

    # redo on an empty redo_stack raises exception
    with pytest.raises(IndexError):
        cs.redo()

    os.remove(tf.name)
Example #11
0
def test_deparse():
    s_exprs = [eved.parse(op) for op in TEST_OPS]
    deparsed = [eved.deparse(e) for e in s_exprs]
    assert deparsed == TEST_OPS