Skip to content

Commit ed7bd1e

Browse files
committed
GRAILS-11513 add more tests to cover all code paths in JSONObject.writeQuoted
1 parent bc2242e commit ed7bd1e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/converters/JSONConverterTests.groovy

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import grails.persistence.Entity
77
import org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerTests
88
import org.codehaus.groovy.grails.web.servlet.mvc.HibernateProxy
99
import org.codehaus.groovy.grails.web.servlet.mvc.LazyInitializer
10+
import org.codehaus.groovy.grails.web.util.StreamCharBuffer
1011
import org.springframework.core.JdkVersion
1112
import org.springframework.validation.BeanPropertyBindingResult
1213
import org.springframework.validation.Errors
@@ -104,6 +105,25 @@ class JSONConverterTests extends AbstractGrailsControllerTests {
104105
assertEquals('{"quotedString":"I contain a \\"Quote\\"!","nonquotedString":"I don\'t!"}', json.toString())
105106
}
106107

108+
void testGStringsWithQuotes() {
109+
def json = [quotedString: "I contain a \"${'Quote'}\"!", nonquotedString: "I ${'don'}'t!"] as JSON
110+
assertEquals('{"quotedString":"I contain a \\"Quote\\"!","nonquotedString":"I don\'t!"}', json.toString())
111+
}
112+
113+
void testStreamCharBufferWithQuotes() {
114+
def quotedBuffer = new StreamCharBuffer()
115+
quotedBuffer.writer << "I contain a \"Quote\"!"
116+
def nonquotedBuffer = new StreamCharBuffer()
117+
nonquotedBuffer.writer << "I don't!"
118+
def json = [quotedString: quotedBuffer, nonquotedString: nonquotedBuffer] as JSON
119+
assertEquals('{"quotedString":"I contain a \\"Quote\\"!","nonquotedString":"I don\'t!"}', json.toString())
120+
}
121+
122+
void testObjectWithQuotes() {
123+
def json = [quotedString: new CustomCharSequence("I contain a \"Quote\"!"), nonquotedString: new CustomCharSequence("I don't!")] as JSON
124+
assertEquals('{"quotedString":"I contain a \\"Quote\\"!","nonquotedString":"I don\'t!"}', json.toString())
125+
}
126+
107127
// GRAILS-11515
108128
void testJsonMultilineSerialization() {
109129
String multiLine = "first line \n second line"
@@ -160,3 +180,31 @@ class Book {
160180
String title
161181
String author
162182
}
183+
184+
class CustomCharSequence implements CharSequence {
185+
String source
186+
187+
CustomCharSequence(String source) {
188+
this.source = source
189+
}
190+
191+
@Override
192+
public int length() {
193+
source.length()
194+
}
195+
196+
@Override
197+
public char charAt(int index) {
198+
source.charAt(index)
199+
}
200+
201+
@Override
202+
public CharSequence subSequence(int start, int end) {
203+
source.subSequence(start, end)
204+
}
205+
206+
@Override
207+
public String toString() {
208+
source
209+
}
210+
}

0 commit comments

Comments
 (0)