package org.robolectric.res;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.*;

import static org.assertj.core.api.Assertions.*;

@RunWith(JUnit4.class)
public class StringResourcesTest {
  @Test
  public void escape_shouldEscapeStrings() {
    assertThat(StringResources.escape("\"This'll work\"")).isEqualTo("This'll work");
    assertThat(StringResources.escape("This\\'ll also work")).isEqualTo("This'll also work");

    assertThat(StringResources.escape("This is a \\\"good string\\\".")).isEqualTo("This is a \"good string\".");
    assertThat(StringResources.escape("This is a \"bad string with unescaped double quotes\"."))
        .isEqualTo("This is a bad string with unescaped double quotes.");

    assertThat(StringResources.escape("Text with escaped backslash followed by an \\\\\"unescaped double quote."))
        .isEqualTo("Text with escaped backslash followed by an \\unescaped double quote.");
  }

  @Test
  public void escape_shouldEscapeCodePoints() {
    Map<String, String> tests = new HashMap<>();
    tests.put("\\u0031", "1");
    tests.put("1\\u0032", "12");
    tests.put("\\u00312", "12");
    tests.put("1\\u00323", "123");
    tests.put("\\u005A", "Z");
    tests.put("\\u005a", "Z");

    for (Map.Entry<String, String> t : tests.entrySet()) {
      assertThat(StringResources.processStringResources(t.getKey())).isEqualTo(t.getValue());
    }
  }

  // Unsupported escape codes should be ignored.
  @Test
  public void escape_shouldIgnoreUnsupportedEscapeCodes() {
    assertThat(StringResources.processStringResources("\\ \\a\\b\\c\\d\\e\\ ")).isEqualTo("");
  }

  @Test
  public void escape_shouldSupport() {
    Map<String, String> tests = new HashMap<>();
    tests.put("\\\\", "\\");
    tests.put("domain\\\\username", "domain\\username");
    for (Map.Entry<String, String> t : tests.entrySet()) {
      assertThat(StringResources.processStringResources(t.getKey())).isEqualTo(t.getValue());
    }
  }

  @Test
  public void testInvalidCodePoints() {
    List<String> tests = new LinkedList<>();
    tests.add("\\u");
    tests.add("\\u0");
    tests.add("\\u00");
    tests.add("\\u004");
    tests.add("\\uzzzz");
    tests.add("\\u0zzz");
    tests.add("\\u00zz");
    tests.add("\\u000z");
    for (String t : tests) {
      try {
        StringResources.processStringResources(t);
        fail("expected IllegalArgumentException with test '" + t + "'");
      } catch (IllegalArgumentException expected) {
        // cool
      }
    }
  }
}

@@ -1438,16 +1438,445 @@
  %7D%0A  %7D%0A%0A
+  @Test%0A  public void escape_shouldRemoveUnescapedDoubleQuotes() %7B%0A    Map%3CString, String%3E tests = new HashMap%3C%3E();%0A    tests.put(%22a%5C%5C%5C%22b%22, %22a%5C%22b%22);%0A    tests.put(%22a%5C%5C%5C%5C%5C%22b%22, %22a%5C%5Cb%22);%0A    tests.put(%22a%5C%5C%5C%5C%5C%5C%5C%22b%22, %22a%5C%5C%5C%22b%22);%0A    tests.put(%22a%5C%5C%5C%5C%5C%5C%5C%5C%5C%22b%22, %22a%5C%5C%5C%5Cb%22);%0A%0A    for (Map.Entry%3CString, String%3E t : tests.entrySet()) %7B%0A      assertThat(StringResources.processStringResources(t.getKey())).isEqualTo(t.getValue());%0A    %7D%0A  %7D%0A%0A
   // Uns

Add a more comprehensive test for removing unescaped double quotes