1
1
/*
2
- * Copyright 2012 the original author or authors.
2
+ * Copyright 2012-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -23,6 +23,26 @@ import spock.lang.Specification
23
23
*/
24
24
class GrailsStringUtilsSpec extends Specification {
25
25
26
+ static final String FOO = ' foo'
27
+ static final String TRIMMABLE
28
+ static final String NON_TRIMMABLE
29
+
30
+ static {
31
+ def trimmable = new StringBuilder ()
32
+ def nonTrimmable = new StringBuilder ()
33
+ (0 .. < Character . MAX_VALUE ). each { i ->
34
+ char ch = (char ) i
35
+ if (Character . isWhitespace(ch) && i > 32 ) {
36
+ nonTrimmable. append(ch)
37
+ }
38
+ }
39
+ (0 .. 32 ). each { int i ->
40
+ trimmable. append((char ) i)
41
+ }
42
+ TRIMMABLE = trimmable. toString()
43
+ NON_TRIMMABLE = nonTrimmable. toString()
44
+ }
45
+
26
46
void " Test toBoolean" () {
27
47
expect :
28
48
GrailsStringUtils . toBoolean(" on" ) == true
@@ -63,4 +83,17 @@ class GrailsStringUtilsSpec extends Specification{
63
83
GrailsStringUtils . trimStart(" abc" , " ab" ) == ' c'
64
84
GrailsStringUtils . trimStart(" abc" , " c" ) == ' abc'
65
85
}
86
+
87
+ void " Test trimToNull method" () {
88
+ expect :
89
+ GrailsStringUtils . trimToNull(FOO + " " ) == FOO
90
+ GrailsStringUtils . trimToNull(" " + FOO + " " ) == FOO
91
+ GrailsStringUtils . trimToNull(" " + FOO ) == FOO
92
+ GrailsStringUtils . trimToNull(FOO + " " ) == FOO
93
+ GrailsStringUtils . trimToNull(" \t\r\n\b " ) == null
94
+ GrailsStringUtils . trimToNull(TRIMMABLE ) == null
95
+ GrailsStringUtils . trimToNull(NON_TRIMMABLE ) == NON_TRIMMABLE
96
+ GrailsStringUtils . trimToNull(" " ) == null
97
+ GrailsStringUtils . trimToNull(null ) == null
98
+ }
66
99
}
0 commit comments