import unittest
import ruamel.yaml as yaml
from ruamel.yaml.loader import RoundTripLoader
from mpf.file_interfaces.yaml_interface import YamlInterface, MpfLoader


class TestYamlInterface(unittest.TestCase):

    def test_round_trip(self):

        orig_config = """\
hardware:
    platform: smart_virtual
    driverboards: virtual
    dmd: smartmatrix

config:
- portconfig.yaml
- switches.yaml
- coils.yaml
- devices.yaml
- keyboard.yaml
- virtual.yaml
- images.yaml

dmd:
    physical: false
    width: 128
    height: 32
    type: color

window:
    elements:
    -   type: virtualdmd
        width: 512
        height: 128
        h_pos: center
        v_pos: center
        pixel_color: ff6600
        dark_color: 220000
        pixel_spacing: 1
    -   type: shape
        shape: box
        width: 516
        height: 132
        color: aaaaaa
        thickness: 2

modes:
- base
- airlock_multiball

sound_system:
    buffer: 512
    frequency: 44100
    channels: 1
    initial_volume: 1
    volume_steps: 20
    tracks:
        voice:
            volume: 1
            priority: 2
            simultaneous_sounds: 1
            preload: false
        sfx:
            volume: 1
            priority: 1
            preload: false
            simultaneous_sounds: 3
    stream:
        name: music
        priority: 0
"""
        parsed_config = YamlInterface.process(orig_config, True)
        saved_config = YamlInterface.save_to_str(parsed_config)

        # print(saved_config)

        self.assertEqual(orig_config, saved_config)

    def test_duplicate_key(self):
        yaml_str = '''

a: 1
b: 2
a: 3


'''
        with self.assertRaises(KeyError):
            yaml.load(yaml_str, Loader=MpfLoader)



@@ -40,55 +40,8 @@
 aml%0A
-from ruamel.yaml.loader import RoundTripLoader%0A
 from
@@ -1663,9 +1663,560 @@
 oader)%0A%0A
+    def test_yaml_patches(self):%0A%0A        # tests our patches to the yaml processor%0A%0A        config = %22%22%22%0A%0Astr_1: +1%0Astr_2: 032%0Astr_3: on%0Astr_4: off%0A# str_5: 123e45%0Abool_1: yes%0Abool_2: no%0Abool_3: true%0Abool_4: false%0Astr_6: hi%0Aint_1: 123%0Afloat_1: 1.0%0A%0A        %22%22%22%0A%0A        parsed_config = YamlInterface.process(config, True)%0A%0A        for k, v in parsed_config.items():%0A            if not type(v) is eval(k.split('_')%5B0%5D):%0A                raise AssertionError('YAML value %22%7B%7D%22 is %7B%7D, not %7B%7D'.format(v,%0A                    type(v), eval(k.split('_')%5B0%5D)))
 %0A

Add tests for our YAML patches