new_contents: import difflib
  
  from ...views.forms import apply_patch
  
  
  def test_empty_patch():
      diff(apply_patch("", ""), "")
  
  
  def test_vellum_xml_patch():
      # Form and patch generated by creating a simple form in stand-alone Vellum
      # and then inspecting a save call in src/core.js with the debugger.
      diff(apply_patch(PATCH, FORM_XML), PATCHED_FORM)
  
  
  def diff(actual, expected):
      if actual != expected:
          diff = difflib.unified_diff(
              expected.splitlines(1),
              actual.splitlines(1),
              fromfile='expected',
              tofile='actual'
          )
          raise AssertionError("unexpected diff:\n" + "".join(diff))
  
  
  FORM_XML = """
  <?xml version=\"1.0\" encoding=\"UTF-8\" ?>
  <h:html xmlns:h=\"http://www.w3.org/1999/xhtml\" xmlns:orx=\"http://openrosa.or\
  g/jr/xforms\" xmlns=\"http://www.w3.org/2002/xforms\" xmlns:xsd=\"http://www.w3\
  .org/2001/XMLSchema\" xmlns:jr=\"http://openrosa.org/javarosa\" xmlns:vellum=\"\
  http://commcarehq.org/xforms/vellum\">
  \t<h:head>
  \t\t<h:title>Untitled Form</h:title>
  \t\t<model>
  \t\t\t<instance>
  \t\t\t\t<data xmlns:jrm=\"http://dev.commcarehq.org/jr/xforms\" xmlns=\"http://\
  openrosa.org/formdesigner/03D5F750-13D0-4F08-A1CF-E5D9E6239E7F\" uiVersion=\"1\
  \" version=\"1\" name=\"Untitled Form\">
  \t\t\t\t\t<test />
  \t\t\t\t</data>
  \t\t\t</instance>
  \t\t\t<bind vellum:nodeset=\"#form/test\" nodeset=\"/data/test\" type=\"xsd:string\" />
  \t\t\t<itext>
  \t\t\t\t<translation lang=\"en\" default=\"\">
  \t\t\t\t\t<text id=\"test-label\">
  \t\t\t\t\t\t<value>test</value>
  \t\t\t\t\t</text>
  \t\t\t\t</translation>
  \t\t\t</itext>
  \t\t</model>
  \t</h:head>
  \t<h:body>
  \t\t<input vellum:ref=\"#form/test\" ref=\"/data/test\">
  \t\t\t<label ref=\"jr:itext('test-label')\" />
  \t\t</input>
  \t</h:body>
  </h:html>
  """.strip()
  
  PATCHED_FORM = FORM_XML.replace("test", "text")
  
  PATCH = """@@ -558,17 +558,17 @@
   %09%09%09%09%09%3Cte
  -s
  +x
   t /%3E%0A%09%09%09
  @@ -624,17 +624,17 @@
   #form/te
  -s
  +x
   t%22 nodes
  @@ -645,17 +645,17 @@
   /data/te
  -s
  +x
   t%22 type=
  @@ -737,17 +737,17 @@
   t id=%22te
  -s
  +x
   t-label%22
  @@ -763,17 +763,17 @@
   value%3Ete
  -s
  +x
   t%3C/value
  @@ -879,17 +879,17 @@
   #form/te
  -s
  +x
   t%22 ref=%22
  @@ -896,17 +896,17 @@
   /data/te
  -s
  +x
   t%22%3E%0A%09%09%09%3C
  @@ -928,17 +928,17 @@
   text('te
  -s
  +x
   t-label'
  """
  
subject: Add tests for apply_patch