package org.fedorahosted.tennera.jgettext;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.junit.Test;

public class TestPoWriter {

	private static String serializeMsg(Message msg) throws IOException {
		Catalog catalog = new Catalog(true);
		catalog.addMessage(msg);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		Charset charset = Charset.forName("utf-8");
		new PoWriter().write(catalog, bos, charset);
		bos.close();
		return new String(bos.toByteArray(), charset);
	}

	@Test
	public void testDefaultMsgStrShouldBeEmpty() throws IOException {
		Message msg = new Message();
		msg.setMsgid("id");
		
		String str = serializeMsg(msg);
		Pattern pattern = Pattern.compile("^msgstr\\s+\"\"$", Pattern.MULTILINE);
		Matcher matcher = pattern.matcher(str);
		Assert.assertTrue(matcher.find());
		Assert.assertFalse(matcher.find());
	}

	@Test
	public void testDefaultPluralStrShouldBeEmpty() throws IOException {
		Message msg = new Message();
		msg.setMsgid("id");
		msg.setMsgidPlural("ids");

		String str = serializeMsg(msg);
		Pattern pattern = Pattern.compile("^msgstr\\[0\\]\\s+\"\"$", Pattern.MULTILINE);
		Matcher matcher = pattern.matcher(str);
		Assert.assertTrue(matcher.find());
		Assert.assertFalse(matcher.find());
	}
}

@@ -253,35 +253,129 @@
 t;%0A%0A
-public class TestPoWriter %7B
+import static org.junit.matchers.JUnitMatchers.containsString;%0A%0Apublic class TestPoWriter %7B%0A   boolean encodeTabs = true;
 %0A%0A%09p
@@ -381,23 +381,16 @@
 private 
-static 
 String s
@@ -621,16 +621,26 @@
 oWriter(
+encodeTabs
 ).write(
@@ -1488,11 +1488,803 @@
 d());%0A%09%7D
+%0A%0A   @Test%0A   public void tabShouldNotBeEncoded() throws IOException %7B%0A      Message msg = new Message();%0A      msg.setMsgid(%22msgid%5Ctmsgid%22);%0A      msg.setMsgstr(%22msgstr%5Ctmsgstr%22);%0A%0A      encodeTabs = false;%0A      String str = serializeMsg(msg);%0A      Assert.assertThat(str, containsString(%22msgid %5C%22msgid%5Ctmsgid%5C%22%22));%0A      Assert.assertThat(str, containsString(%22msgstr %5C%22msgstr%5Ctmsgstr%5C%22%22));%0A   %7D%0A%0A   @Test%0A   public void tabShouldBeEncoded() throws IOException %7B%0A      Message msg = new Message();%0A      msg.setMsgid(%22msgid%5Ctmsgid%22);%0A      msg.setMsgstr(%22msgstr%5Ctmsgstr%22);%0A%0A      encodeTabs = true;%0A      String str = serializeMsg(msg);%0A      Assert.assertThat(str, containsString(%22msgid %5C%22msgid%5C%5Ctmsgid%5C%22%22));%0A      Assert.assertThat(str, containsString(%22msgstr %5C%22msgstr%5C%5Ctmsgstr%5C%22%22));%0A   %7D
 %0A%7D%0A

Add unit test for tab encoding