from textwrap import dedent
import unittest

from peru import edit_yaml
from peru.test import shared


yaml_template = dedent("""\
    a:
      b: [1, 2, 3]
      c: {}
    d: blarg
    """)


class EditYamlTest(unittest.TestCase):

    def testReplace(self):
        start_yaml = yaml_template.format("foo")
        new_yaml = edit_yaml.set_module_field(start_yaml, "a", "c", "bar")
        self.assertEqual(yaml_template.format("bar"), new_yaml)

    def testInsert(self):
        start_yaml = dedent("""\
            a:
               b: 5
              """)
        new_yaml = edit_yaml.set_module_field(start_yaml, "a", "c", "9")
        self.assertEqual(start_yaml + "   c: 9\n", new_yaml)

    def testWithFile(self):
        tmp_name = shared.tmp_file()
        start_yaml = yaml_template.format("foo")
        with open(tmp_name, "w") as f:
            f.write(start_yaml)
        edit_yaml.set_module_field_in_file(tmp_name, "a", "c", "bar")
        with open(tmp_name) as f:
            new_yaml = f.read()
        self.assertEqual(yaml_template.format("bar"), new_yaml)

@@ -682,32 +682,527 @@
 %5Cn%22, new_yaml)%0A%0A
+    def testInsertWithLastFieldAsDict(self):%0A        start_yaml = dedent(%22%22%22%5C%0A            a:%0A              b:%0A                foo: bar%0A                baz: bing%0A            x: y%0A            %22%22%22)%0A        end_yaml = dedent(%22%22%22%5C%0A            a:%0A              b:%0A                foo: bar%0A                baz: bing%0A              c: stuff%0A            x: y%0A            %22%22%22)%0A        edited_yaml = edit_yaml.set_module_field(start_yaml, %22a%22, %22c%22, %22stuff%22)%0A        self.assertEqual(end_yaml, edited_yaml)%0A%0A
     def testWith

add a failing test for edit_yaml