more functions unit tests v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 24 Dec 2013 14:23:22 +0100
branchv_0
changeset 53eb30ad93ca8b
parent 52 e2ba2af0ef40
child 54 53020d0bd2e4
more functions unit tests
java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java
     1.1 --- a/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java	Tue Dec 24 12:05:05 2013 +0100
     1.2 +++ b/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java	Tue Dec 24 14:23:22 2013 +0100
     1.3 @@ -17,10 +17,10 @@
     1.4   */
     1.5  package info.globalcode.sql.dk;
     1.6  
     1.7 -import java.util.regex.Matcher;
     1.8 -import java.util.regex.Pattern;
     1.9 -import org.testng.annotations.Test;
    1.10 +import java.util.ArrayList;
    1.11 +import java.util.Collection;
    1.12  import static org.testng.Assert.*;
    1.13 +import org.testng.annotations.*;
    1.14  
    1.15  /**
    1.16   *
    1.17 @@ -29,31 +29,49 @@
    1.18  public class FunctionsTest {
    1.19  
    1.20  	@Test
    1.21 -	public void testEscapeRegEx() {
    1.22 -		for (String original : new String[]{"abcd", "1234", "xxx", "\\Eescape\\Q", "\\Qescape\\E", "abc\\Eescape\\Qdef.", ".", ""}) {
    1.23 -			String patternString = Pattern.quote(original);
    1.24 -			System.out.println(original + " → " + patternString);
    1.25 +	public void testNotNull() {
    1.26 +		Collection<String> c = null;
    1.27 +		for (String s : Functions.notNull(c)) {
    1.28 +			fail("Should not iterate through null collection");
    1.29 +		}
    1.30  
    1.31 -			Pattern pattern = Pattern.compile(patternString);
    1.32 +		c = new ArrayList<>();
    1.33 +		c.add("ahoj");
    1.34 +		int count = 0;
    1.35 +		for (String s : Functions.notNull(c)) {
    1.36 +			assertEquals(s, "ahoj", "Wrong item in collection");
    1.37 +			count++;
    1.38 +		}
    1.39 +		assertEquals(count, 1, "Wrong number of iterations");
    1.40 +	}
    1.41  
    1.42 -			String testString;
    1.43 -			Matcher m;
    1.44 +	@Test
    1.45 +	public void testLpad() {
    1.46 +		String original = "abc";
    1.47 +		String padded;
    1.48  
    1.49 -			testString = original;
    1.50 -			m = pattern.matcher(testString);
    1.51 -			assertTrue(m.matches(), "Pattern does not match original string: " + testString);
    1.52 +		padded = Functions.lpad(original, 5);
    1.53 +		assertEquals(padded, "  abc");
    1.54  
    1.55 -			testString = original + "x";
    1.56 -			m = pattern.matcher(testString);
    1.57 -			assertFalse(m.matches(), "Pattern matches wrong string: " + testString);
    1.58 +		padded = Functions.lpad(original, 2);
    1.59 +		assertEquals(padded, original);
    1.60 +	}
    1.61  
    1.62 -			testString = "x" + original;
    1.63 -			m = pattern.matcher(testString);
    1.64 -			assertFalse(m.matches(), "Pattern matches wrong string: " + testString);
    1.65 +	@Test
    1.66 +	public void testRpad() {
    1.67 +		String original = "abc";
    1.68 +		String padded;
    1.69  
    1.70 -			testString = (original + "ab").substring(1);
    1.71 -			m = pattern.matcher(testString);
    1.72 -			assertFalse(m.matches(), "Pattern matches wrong string: " + testString);
    1.73 -		}
    1.74 +		padded = Functions.rpad(original, 5);
    1.75 +		assertEquals(padded, "abc  ");
    1.76 +
    1.77 +		padded = Functions.rpad(original, 2);
    1.78 +		assertEquals(padded, original);
    1.79 +	}
    1.80 +
    1.81 +	@Test
    1.82 +	public void testRepeat() {
    1.83 +		assertEquals(Functions.repeat('f', 0), "");
    1.84 +		assertEquals(Functions.repeat('f', 3), "fff");
    1.85  	}
    1.86  }