Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. package com.dominicsayers.isemail;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import org.junit.Test;
  6.  
  7. public class PHPFunctionsTest {
  8.  
  9.         @Test
  10.         public void preg_match_allTest() {
  11.                 String[][] a = PHPFunctions.preg_match_all("hell(.)w(.)rld",
  12.                                 "helloworld, hellawirld, hellaworle, ...");
  13.                
  14.                 assertEquals(3, a.length);
  15.                 assertEquals(2, a[0].length);
  16.                 assertEquals("helloworld", a[0][0]);
  17.                 assertEquals("hellawirld", a[0][1]);
  18.                 assertEquals(2, a[1].length);
  19.                 assertEquals("o", a[1][0]);
  20.                 assertEquals("a", a[1][1]);
  21.                 assertEquals(2, a[2].length);
  22.                 assertEquals("o", a[2][0]);
  23.                 assertEquals("i", a[2][1]);
  24.         }
  25.        
  26.         @Test
  27.         public void preg_matchTest() {
  28.                
  29.                 assertEquals(1, PHPFunctions.preg_match("(h|e)(l|o)", "hl"));
  30.                 assertEquals(1, PHPFunctions.preg_match("(h|e)(l|o)", "eo"));
  31.                 assertEquals(1, PHPFunctions.preg_match("(h|e)(l|o)", "eol"));
  32.                 assertEquals(1, PHPFunctions.preg_match("(h|e)(l|o)", "hol"));
  33.                 assertEquals(1, PHPFunctions.preg_match("(h|e)(l|o)", "hoho"));
  34.                 assertEquals(0, PHPFunctions.preg_match("(h|e)(l|o)", "iee"));
  35.                
  36.         }
  37.        
  38.         @Test
  39.         public void preg_splitTest() {
  40.                 String[] s = PHPFunctions.preg_split("[\\s,]+", "hypertext language, programming");
  41.                
  42.                 assertEquals(3, s.length);
  43.                 assertEquals("hypertext", s[0]);
  44.                 assertEquals("language", s[1]);
  45.                 assertEquals("programming", s[2]);
  46.                
  47.                 s = PHPFunctions.preg_split("\\.", ".hello.");
  48.                
  49.                 assertEquals(3, s.length);
  50.                 assertEquals("", s[0]);
  51.                 assertEquals("hello", s[1]);
  52.                 assertEquals("", s[2]);
  53.  
  54.         }
  55.        
  56.         @Test
  57.         public void preg_match_to_arrayTest() {
  58.                 String[] s = PHPFunctions.preg_match_to_array("(?i)^(?:http://)?([^/]+)", "http://www.php.net/index.html");
  59.                
  60.                 assertEquals(2, s.length);
  61.                 assertEquals("http://www.php.net", s[0]);
  62.                 assertEquals("www.php.net", s[1]);
  63.                
  64.                 s = PHPFunctions.preg_match_to_array("t(.st)", "test tost taste toast");
  65.                
  66.                 assertEquals(2, s.length);
  67.                 assertEquals("test", s[0]);
  68.                 assertEquals("est", s[1]);
  69.         }
  70.        
  71.         @Test
  72.         public void substr1Test() {
  73.                 assertEquals("3456789", PHPFunctions.substr("123456789", 2));
  74.         }
  75.  
  76.         @Test
  77.         public void substr2Test() {
  78.                 assertEquals("345", PHPFunctions.substr("123456789", 2, 3));
  79.         }
  80.        
  81.         @Test
  82.         public void preg_repalceTest() {               
  83.                 String zeichenkette = "Der schnelle braune Fuchs sprang über den faulen Hund.";
  84.                
  85.                 zeichenkette = PHPFunctions.preg_replace("schnelle", "langsame", zeichenkette);
  86.                 zeichenkette = PHPFunctions.preg_replace("braune", "schwarze", zeichenkette);
  87.                 zeichenkette = PHPFunctions.preg_replace("Fuchs", "Bär", zeichenkette);
  88.                
  89.                 assertEquals("Der langsame schwarze Bär sprang über den faulen Hund.", zeichenkette);
  90.         }
  91.  
  92. }
  93.