string: add more test cases
							parent
							
								
									7705281459
								
							
						
					
					
						commit
						e4de1e1e89
					
				|  | @ -543,7 +543,7 @@ pub fn (s string) substr(start, end int) string { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (s string) index_old(p string) int { | pub fn (s string) index_old(p string) int { | ||||||
| 	if p.len > s.len { | 	if p.len > s.len || p.len == 0 { | ||||||
| 		return -1 | 		return -1 | ||||||
| 	} | 	} | ||||||
| 	mut i := 0 | 	mut i := 0 | ||||||
|  | @ -561,7 +561,7 @@ pub fn (s string) index_old(p string) int { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (s string) index(p string) ?int { | pub fn (s string) index(p string) ?int { | ||||||
| 	if p.len > s.len { | 	if p.len > s.len || p.len == 0 { | ||||||
| 		return none | 		return none | ||||||
| 	} | 	} | ||||||
| 	mut i := 0 | 	mut i := 0 | ||||||
|  | @ -620,7 +620,7 @@ pub fn (s string) index_any(chars string) int { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (s string) last_index(p string) ?int { | pub fn (s string) last_index(p string) ?int { | ||||||
| 	if p.len > s.len { | 	if p.len > s.len || p.len == 0 { | ||||||
| 		return none | 		return none | ||||||
| 	} | 	} | ||||||
| 	mut i := s.len - p.len | 	mut i := s.len - p.len | ||||||
|  | @ -1199,8 +1199,8 @@ pub fn (s []string) join_lines() string { | ||||||
| 
 | 
 | ||||||
| // reverse will return a new reversed string.
 | // reverse will return a new reversed string.
 | ||||||
| pub fn (s string) reverse() string { | pub fn (s string) reverse() string { | ||||||
| 	if s.len == 0 { | 	if s.len == 0 || s.len == 1 { | ||||||
| 		return '' | 		return s | ||||||
| 	} | 	} | ||||||
| 	mut res := string{ | 	mut res := string{ | ||||||
| 		len: s.len | 		len: s.len | ||||||
|  |  | ||||||
|  | @ -355,16 +355,22 @@ fn test_upper() { | ||||||
| 	assert s.to_upper() == 'HAVE A NICE DAY!' | 	assert s.to_upper() == 'HAVE A NICE DAY!' | ||||||
| 	s = 'hi' | 	s = 'hi' | ||||||
| 	assert s.to_upper() == 'HI' | 	assert s.to_upper() == 'HI' | ||||||
| 
 |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn test_left_right() { | fn test_left_right() { | ||||||
| 	s := 'ALOHA' | 	s := 'ALOHA' | ||||||
| 	assert s.left(3) == 'ALO' | 	assert s.left(3) == 'ALO' | ||||||
|  | 	assert s.left(0) == '' | ||||||
|  | 	assert s.left(8) == s | ||||||
|  | 	assert s.right(3) == 'HA' | ||||||
|  | 	assert s.right(6) == '' | ||||||
| 	assert s[3..] == 'HA' | 	assert s[3..] == 'HA' | ||||||
| 	u := s.ustring() | 	u := s.ustring() | ||||||
| 	assert u.left(3) == 'ALO' | 	assert u.left(3) == 'ALO' | ||||||
|  | 	assert u.left(0) == '' | ||||||
|  | 	assert s.left(8) == s | ||||||
| 	assert u.right(3) == 'HA' | 	assert u.right(3) == 'HA' | ||||||
|  | 	assert u.right(6) == '' | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn test_contains() { | fn test_contains() { | ||||||
|  | @ -439,20 +445,33 @@ fn test_trim_right() { | ||||||
| 	assert s.trim_right('na') == 'b' | 	assert s.trim_right('na') == 'b' | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | fn test_all_before() { | ||||||
|  | 	s := 'fn hello fn' | ||||||
|  | 	assert s.all_before(' ') == 'fn' | ||||||
|  | 	assert s.all_before('2') == s | ||||||
|  | 	assert s.all_before('') == s | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn test_all_before_last() { | ||||||
|  | 	s := 'fn hello fn' | ||||||
|  | 	assert s.all_before_last(' ') == 'fn hello' | ||||||
|  | 	assert s.all_before_last('2') == s | ||||||
|  | 	assert s.all_before_last('') == s | ||||||
|  | } | ||||||
|  | 
 | ||||||
| fn test_all_after() { | fn test_all_after() { | ||||||
| 	s := 'fn hello' | 	s := 'fn hello' | ||||||
| 	q := s.all_after('fn ') | 	assert s.all_after('fn ') == 'hello' | ||||||
| 	assert q == 'hello' | 	assert s.all_after('test') == s | ||||||
|  | 	assert s.all_after('') == s | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn test_reverse() { | fn test_reverse() { | ||||||
| 	s := 'hello' | 	assert 'hello'.reverse() == 'olleh' | ||||||
| 	assert s.reverse() == 'olleh' | 	assert ''.reverse() == '' | ||||||
| 	t := '' | 	assert 'a'.reverse() == 'a' | ||||||
| 	assert t.reverse() == t |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| struct Foo { | struct Foo { | ||||||
| 	bar int | 	bar int | ||||||
| mut: | mut: | ||||||
|  | @ -575,9 +594,20 @@ fn test_ustring_count() { | ||||||
| 	assert (a.count('a'.ustring())) == 0 | 	assert (a.count('a'.ustring())) == 0 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | fn test_limit() { | ||||||
|  | 	s := 'hello' | ||||||
|  | 	assert s.limit(2) == 'he' | ||||||
|  | 	assert s.limit(9) == s | ||||||
|  | 	assert s.limit(0) == '' | ||||||
|  | 	// assert s.limit(-1) == ''
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
| fn test_repeat() { | fn test_repeat() { | ||||||
| 	s := 'V! ' | 	s := 'V! ' | ||||||
| 	assert s.repeat(5) == 'V! V! V! V! V! ' | 	assert s.repeat(5) == 'V! V! V! V! V! ' | ||||||
|  | 	assert s.repeat(1) == s | ||||||
|  | 	assert s.repeat(0) == s | ||||||
|  | 	assert s.repeat(-1) == s | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn test_raw() { | fn test_raw() { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue