import py
from song import parse, ParseError

class TestSongParser(object):

    def test_chords_without_section(self):
        try:
            parse('C: Chords without section')
            py.test.fail('No error found!')
        except ParseError, e:
            assert e.line_number == 1

    def test_invalid_chord(self):
        try:
            parse('[]\n'+
                  'C: .')
            py.test.fail('No error found!')
        except ParseError, e:
            assert e.line_number == 2
            
    def test_lyrics_wo_chord(self):
        try:
            parse('[]\n'+
                  'No chord yet')
            py.test.fail('No error found!')
        except ParseError, e:
            assert e.line_number == 2

    def test_bad_tu(self):
        try:
            parse('U: ooga')
            py.test.fail('No error found!')
        except ParseError, e:
            assert e.line_number == 1

    def test_simple_Song(self):
        song = parse('U: 4\n'+
                     '[]\n'+
                     'C: A    B  C        D\n'+
                     '   This is a simple song')
        assert len(song.all_sections) == 0
        assert song.timing_unit == 4
        assert len(song.play_sections) == 1
        assert song.play_sections[0].name == ""
        assert len(song.play_sections[0].lines) == 1
        assert len(song.play_sections[0].lines[0].chords) == 4
        for idx, (name, beats) in enumerate(
            [('A', 1), ('B', 1), ('C', 1), ('D', 1)]):
            assert song.play_sections[0].lines[0].chords[idx].name == name
            assert song.play_sections[0].lines[0].chords[idx].beats == beats
        assert len(song.play_sections[0].lines[0].lyrics) == 1
        assert song.play_sections[0].lines[0].lyrics[0] == \
               '   This is a simple song'
            
    def test_repeat(self):
        song = parse('[Chorus]\n'+
                     '[Verse]\n'+
                     '[Chorus]')
        assert len(song.play_sections) == 3
        assert song.play_sections[0] == song.play_sections[2]
        assert song.play_sections[0] != song.play_sections[1]

    def test_beats(self):
        song = parse('[Chorus]\n'+
                     'C: G...\n')
        assert song.play_sections[0].lines[0].chords[0].name == 'G'
        assert song.play_sections[0].lines[0].chords[0].beats == 4


                    


            
        
