
package commons;

import org.makagiga.commons.ConfigFile;
import org.makagiga.test.AbstractEnumTest;
import org.makagiga.test.Test;
import org.makagiga.test.TestMethod;
import org.makagiga.test.Tester;

@Test(className = ConfigFile.Format.class)
public final class TestConfigFile_Format extends AbstractEnumTest<ConfigFile.Format> {
	
	// public

	public TestConfigFile_Format() {
		super(
			ConfigFile.Format.values(),
			ConfigFile.Format.DESKTOP, ConfigFile.Format.INI
		);
	}

	@Test
	public void test_commons() {
		for (final ConfigFile.Format i : ConfigFile.Format.values()) {
			assertIllegalArgumentException(new Tester.Code() {
				public void run() throws Throwable {
					i.validateGroup(null);
				}
			} );

			assertIllegalArgumentException(new Tester.Code() {
				public void run() throws Throwable {
					i.validateGroup("");
				}
			} );

			assertIllegalArgumentException(new Tester.Code() {
				public void run() throws Throwable {
					i.validateKey(null);
				}
			} );

			assertIllegalArgumentException(new Tester.Code() {
				public void run() throws Throwable {
					i.validateKey("");
				}
			} );
		}
		
		final String LONG_VALUE = "AZaz09-";
		final String SHORT_VALUE = "X";
		
		// DESKTOP
				
		ConfigFile.Format f = ConfigFile.Format.DESKTOP;
		
		assertIllegalArgumentException(new Tester.Code() {
			public void run() throws Throwable {
				ConfigFile.Format.DESKTOP.validateGroup("[");
			}
		} );
		assertIllegalArgumentException(new Tester.Code() {
			public void run() throws Throwable {
				ConfigFile.Format.DESKTOP.validateGroup("]");
			}
		} );
		assert f.validateGroup(SHORT_VALUE) == SHORT_VALUE;
		assert f.validateGroup(LONG_VALUE) == LONG_VALUE;

		assertIllegalArgumentException(new Tester.Code() {
			public void run() throws Throwable {
				ConfigFile.Format.DESKTOP.validateKey("=");
			}
		} );
		assert f.validateKey(SHORT_VALUE) == SHORT_VALUE;
		assert f.validateKey(LONG_VALUE) == LONG_VALUE;
		
		f.validateGroup(" ");
		f.validateGroup("Foo Bar");

		// INI
		
		f = ConfigFile.Format.INI;
		
		assert f.validateGroup(SHORT_VALUE) == SHORT_VALUE;
		assert f.validateGroup(LONG_VALUE) == LONG_VALUE;

		assert f.validateKey(SHORT_VALUE) == SHORT_VALUE;
		assert f.validateKey(LONG_VALUE) == LONG_VALUE;
	}

	@Test(
		methods = @TestMethod(name = "equals", parameters = "String, String")
	)
	public void test_equals() {
		ConfigFile.Format f;

		f = ConfigFile.Format.DESKTOP;
		assert f.equals("foo", "foo");
		assert !f.equals("foo", "FOO");

		f = ConfigFile.Format.INI;
		assert f.equals("foo", "foo");
		assert f.equals("foo", "FOO");
	}
	
	@Test(
		methods = @TestMethod(name = "escape", parameters = "String")
	)
	public void test_escape() {
		assertNull(ConfigFile.Format.escape(null));
		assertEmpty(ConfigFile.Format.escape(""));
		assertEquals("\\tFoo\\sBar\\r\\nBaz\\\\", ConfigFile.Format.escape("\tFoo Bar\r\nBaz\\"));
	}

	@Test(
		methods = @TestMethod(name = "unescape", parameters = "String")
	)
	public void test_unescape() {
		assertNull(ConfigFile.Format.unescape(null));
		assertEmpty(ConfigFile.Format.unescape(""));
		assertEquals("Foo Bar", ConfigFile.Format.unescape("Foo Bar"));
		assertEquals("\tFoo Bar\r\nBaz\\", ConfigFile.Format.unescape("\\tFoo\\sBar\\r\\nBaz\\\\"));
		assertEquals("\n\n  \\\\", ConfigFile.Format.unescape("\\n\\n\\s\\s\\\\\\\\"));
	}

	@Test(
		methods = @TestMethod(name = "getComment")
	)
	public void test_getComment() {
		assert ConfigFile.Format.DESKTOP.getComment().equals("#");
		assert ConfigFile.Format.INI.getComment().equals(";");
	}

	@Test(
		methods = @TestMethod(name = "getEOL")
	)
	public void test_getEOL() {
		assert ConfigFile.Format.DESKTOP.getEOL().equals("\n");
		assert ConfigFile.Format.INI.getEOL().equals("\r\n");
	}

	@Test(
		methods = @TestMethod(name = "getSuffix")
	)
	public void test_getSuffix() {
		assert ConfigFile.Format.DESKTOP.getSuffix().equals(".desktop");
		assert ConfigFile.Format.INI.getSuffix().equals(".ini");
	}

	@Test(
		methods = @TestMethod(name = "isCaseSensitive")
	)
	public void test_isCaseSensitive() {
		assert ConfigFile.Format.DESKTOP.isCaseSensitive();
		assert !ConfigFile.Format.INI.isCaseSensitive();
	}

}
