Copilot commented on code in PR #16636:
URL: https://github.com/apache/grails-core/pull/16636#discussion_r4264432269


##########
grails-web-common/src/test/groovy/org/grails/web/json/PathCapturingJSONWriterWrapperApiSpec.groovy:
##########
@@ -0,0 +1,163 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.json
+
+import java.io.StringWriter
+
+import groovy.transform.CompileStatic
+import spock.lang.Specification
+
+class PathCapturingJSONWriterWrapperApiSpec extends Specification {
+
+    void "capture nested object properties and array indices as values are 
written"() {
+        given:
+        def output = new StringWriter()
+        def writer = new PathCapturingJSONWriterWrapper(new JSONWriter(output))
+
+        expect:
+        writer.getCurrentStrackReference() == ''
+        writer.getStackReference(0) == ''
+        writer.object().key('items').is(writer)
+        writer.getCurrentStrackReference() == '.items'
+        writer.array().is(writer)
+        writer.getCurrentStrackReference() == '.items[-1]'
+        writer.object().key('name').is(writer)
+        writer.getCurrentStrackReference() == '.items[0].name'

Review Comment:
   The method name 'getCurrentStrackReference' appears to have a typo ('Strack' 
vs 'Stack'), which can be confusing and, if the production API is actually 
'getCurrentStackReference', would cause these tests to not compile. Consider 
renaming usages to the correct method name (and, if needed for compatibility, 
keeping a deprecated alias in production code) so tests reflect the intended 
API.



##########
grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterApiSpec.groovy:
##########
@@ -0,0 +1,169 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.json
+
+import java.io.IOException
+import java.io.StringWriter
+import java.io.Writer
+import java.util.Date
+
+import groovy.lang.Writable
+import groovy.transform.CompileStatic
+import spock.lang.Specification
+
+class JSONWriterApiSpec extends Specification {
+
+    void "write nested objects arrays and every value overload"() {
+        given:
+        def output = new StringWriter()
+        def writer = new JSONWriter(output)
+
+        when:
+        def result = 
writer.object().key('items').array().object().key('name').value((Object) 
'Grails')
+            
.endObject().array().value(true).value(false).value(2.5d).value(4000000000L)
+            .value((Number) 1.25G).value((Number) null).value((Object) 
null).valueNull()
+            .value((Object) new JSONObject([nested: true])).value((Object) new 
JSONArray([1, 2]))
+            
.endArray().endArray().key('empty').object().endObject().endObject()
+
+        then:
+        result.is(writer)
+        output.toString() == 
'{"items":[{"name":"Grails"},[true,false,2.5,4000000000,1.25,null,null,null,{"nested":true},[1,2]]],"empty":{}}'
+    }
+
+    void "object values quote strings string builders and ordinary objects"() {
+        given:
+        def output = new StringWriter()
+        def writer = new JSONWriter(output)
+
+        when:
+        writer.array().value((Object) 'quote"\n\\').value((Object) new 
StringBuilder('builder'))
+            .value((Object) new StringBuffer('buffer')).value((Object) new 
Date(123L))
+            .value((Object) true).value((Object) 3).endArray()
+
+        then:
+        new JSONArray(output.toString()) == new JSONArray(['quote"\n\\', 
'builder', 'buffer', new Date(123L), true, 3])
+    }
+
+    void "write a Writable string representation as escaped JSON"() {
+        given:
+        def output = new StringWriter()
+        Writable value = new Writable() {
+            @Override
+            Writer writeTo(Writer out) throws IOException {
+                out.write('line\nquote"')
+                out
+            }
+
+            @Override
+            String toString() {
+                'line\nquote"'
+            }
+        }
+
+        when:
+        new JSONWriter(output).array().value((Object) value).endArray()
+
+        then:
+        new JSONArray(output.toString()).getString(0) == 'line\nquote"'
+    }
+
+    void "invalid writer sequences report #message in case #iterationIndex"() {
+        given:
+        def writer = new JSONWriter(new StringWriter())
+        setup(writer)
+
+        when:
+        operation(writer)
+
+        then:
+        def error = thrown(JSONException)
+        error.message.contains(message)
+
+        where:
+        setup                                      | operation                 
        | message
+        ({ JSONWriter w -> })                         | ({ w -> w.value(true) 
})          | 'Value out of sequence'
+        ({ JSONWriter w -> w.object() })              | ({ w -> 
w.value((Object) 'bad') }) | 'Value out of sequence'
+        ({ JSONWriter w -> })                         | ({ w -> w.key('key') 
})           | 'Misplaced key'
+        ({ JSONWriter w -> w.array() })               | ({ w -> w.key('key') 
})           | 'Misplaced key'
+        ({ JSONWriter w -> w.object() })              | ({ w -> w.key(null) }) 
           | 'Null key'
+        ({ JSONWriter w -> w.object() })              | ({ w -> w.array() })   
           | 'Misplaced array'
+        ({ JSONWriter w -> w.object() })              | ({ w -> w.object() })  
           | 'Misplaced object'
+        ({ JSONWriter w -> })                         | ({ w -> w.endArray() 
})           | 'Misplaced endArray'
+        ({ JSONWriter w -> })                         | ({ w -> w.endObject() 
})          | 'Misplaced endArray'
+        ({ JSONWriter w -> w.object().key('key') })   | ({ w -> w.endObject() 
})          | 'Misplaced endArray'

Review Comment:
   The table expects 'Misplaced endArray' even when calling endObject(). If 
JSONWriter distinguishes these cases (commonly 'Misplaced endObject'), these 
assertions will fail and also lock in a potentially incorrect error contract. 
Consider asserting the correct endObject message for endObject() rows, or 
loosening the assertion to a shared invariant (e.g., contains 'Misplaced end').



##########
grails-web-common/src/test/groovy/org/grails/web/json/PathCapturingJSONWriterWrapperApiSpec.groovy:
##########
@@ -0,0 +1,163 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.json
+
+import java.io.StringWriter
+
+import groovy.transform.CompileStatic
+import spock.lang.Specification
+
+class PathCapturingJSONWriterWrapperApiSpec extends Specification {
+
+    void "capture nested object properties and array indices as values are 
written"() {
+        given:
+        def output = new StringWriter()
+        def writer = new PathCapturingJSONWriterWrapper(new JSONWriter(output))
+
+        expect:
+        writer.getCurrentStrackReference() == ''
+        writer.getStackReference(0) == ''
+        writer.object().key('items').is(writer)
+        writer.getCurrentStrackReference() == '.items'
+        writer.array().is(writer)
+        writer.getCurrentStrackReference() == '.items[-1]'
+        writer.object().key('name').is(writer)
+        writer.getCurrentStrackReference() == '.items[0].name'
+        writer.getStackReference(1) == '.items'
+        writer.getStackReference(2) == '.items[0]'
+        writeObjectValue(writer, 'Grails').is(writer)
+        writer.getCurrentStrackReference() == '.items[0]'
+        writer.endObject().is(writer)
+        writer.object().key('active').is(writer)
+        writer.getCurrentStrackReference() == '.items[1].active'
+        writeBoolean(writer, true).is(writer)
+        writer.getCurrentStrackReference() == '.items[1]'
+        writer.endObject().endArray().is(writer)
+        writer.getCurrentStrackReference() == ''
+        writer.key('nested').object().key('count').is(writer)
+        writer.getCurrentStrackReference() == '.nested.count'
+        writeLong(writer, 2L).is(writer)
+        writer.getCurrentStrackReference() == '.nested'
+        writer.endObject().endObject().is(writer)
+        writer.getCurrentStrackReference() == ''
+        output.toString() == 
'{"items":[{"name":"Grails"},{"active":true}],"nested":{"count":2}}'
+    }
+
+    void "each scalar overload advances array indices and removes object 
properties"() {
+        given:
+        def arrayOutput = new StringWriter()
+        def arrayWriter = new PathCapturingJSONWriterWrapper(new 
JSONWriter(arrayOutput))
+        def objectOutput = new StringWriter()
+        def objectWriter = new PathCapturingJSONWriterWrapper(new 
JSONWriter(objectOutput))
+
+        expect:
+        arrayWriter.array().is(arrayWriter)
+        writeBoolean(arrayWriter, false).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[0]'
+        writeDouble(arrayWriter, 2.5d).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[1]'
+        writeLong(arrayWriter, 7L).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[2]'
+        writeObjectValue(arrayWriter, null).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[3]'
+        arrayWriter.endArray().is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == ''

Review Comment:
   The method name 'getCurrentStrackReference' appears to have a typo ('Strack' 
vs 'Stack'), which can be confusing and, if the production API is actually 
'getCurrentStackReference', would cause these tests to not compile. Consider 
renaming usages to the correct method name (and, if needed for compatibility, 
keeping a deprecated alias in production code) so tests reflect the intended 
API.



##########
grails-web-common/src/test/groovy/org/grails/web/json/PathCapturingJSONWriterWrapperApiSpec.groovy:
##########
@@ -0,0 +1,163 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.json
+
+import java.io.StringWriter
+
+import groovy.transform.CompileStatic
+import spock.lang.Specification
+
+class PathCapturingJSONWriterWrapperApiSpec extends Specification {
+
+    void "capture nested object properties and array indices as values are 
written"() {
+        given:
+        def output = new StringWriter()
+        def writer = new PathCapturingJSONWriterWrapper(new JSONWriter(output))
+
+        expect:
+        writer.getCurrentStrackReference() == ''
+        writer.getStackReference(0) == ''
+        writer.object().key('items').is(writer)
+        writer.getCurrentStrackReference() == '.items'
+        writer.array().is(writer)
+        writer.getCurrentStrackReference() == '.items[-1]'
+        writer.object().key('name').is(writer)
+        writer.getCurrentStrackReference() == '.items[0].name'
+        writer.getStackReference(1) == '.items'
+        writer.getStackReference(2) == '.items[0]'
+        writeObjectValue(writer, 'Grails').is(writer)
+        writer.getCurrentStrackReference() == '.items[0]'
+        writer.endObject().is(writer)
+        writer.object().key('active').is(writer)
+        writer.getCurrentStrackReference() == '.items[1].active'
+        writeBoolean(writer, true).is(writer)
+        writer.getCurrentStrackReference() == '.items[1]'
+        writer.endObject().endArray().is(writer)
+        writer.getCurrentStrackReference() == ''
+        writer.key('nested').object().key('count').is(writer)
+        writer.getCurrentStrackReference() == '.nested.count'
+        writeLong(writer, 2L).is(writer)
+        writer.getCurrentStrackReference() == '.nested'
+        writer.endObject().endObject().is(writer)
+        writer.getCurrentStrackReference() == ''

Review Comment:
   The method name 'getCurrentStrackReference' appears to have a typo ('Strack' 
vs 'Stack'), which can be confusing and, if the production API is actually 
'getCurrentStackReference', would cause these tests to not compile. Consider 
renaming usages to the correct method name (and, if needed for compatibility, 
keeping a deprecated alias in production code) so tests reflect the intended 
API.



##########
grails-web-common/src/test/groovy/org/grails/web/json/PathCapturingJSONWriterWrapperApiSpec.groovy:
##########
@@ -0,0 +1,163 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.json
+
+import java.io.StringWriter
+
+import groovy.transform.CompileStatic
+import spock.lang.Specification
+
+class PathCapturingJSONWriterWrapperApiSpec extends Specification {
+
+    void "capture nested object properties and array indices as values are 
written"() {
+        given:
+        def output = new StringWriter()
+        def writer = new PathCapturingJSONWriterWrapper(new JSONWriter(output))
+
+        expect:
+        writer.getCurrentStrackReference() == ''
+        writer.getStackReference(0) == ''
+        writer.object().key('items').is(writer)
+        writer.getCurrentStrackReference() == '.items'
+        writer.array().is(writer)
+        writer.getCurrentStrackReference() == '.items[-1]'
+        writer.object().key('name').is(writer)
+        writer.getCurrentStrackReference() == '.items[0].name'
+        writer.getStackReference(1) == '.items'
+        writer.getStackReference(2) == '.items[0]'
+        writeObjectValue(writer, 'Grails').is(writer)
+        writer.getCurrentStrackReference() == '.items[0]'
+        writer.endObject().is(writer)
+        writer.object().key('active').is(writer)
+        writer.getCurrentStrackReference() == '.items[1].active'
+        writeBoolean(writer, true).is(writer)
+        writer.getCurrentStrackReference() == '.items[1]'
+        writer.endObject().endArray().is(writer)
+        writer.getCurrentStrackReference() == ''
+        writer.key('nested').object().key('count').is(writer)
+        writer.getCurrentStrackReference() == '.nested.count'
+        writeLong(writer, 2L).is(writer)
+        writer.getCurrentStrackReference() == '.nested'
+        writer.endObject().endObject().is(writer)
+        writer.getCurrentStrackReference() == ''
+        output.toString() == 
'{"items":[{"name":"Grails"},{"active":true}],"nested":{"count":2}}'
+    }
+
+    void "each scalar overload advances array indices and removes object 
properties"() {
+        given:
+        def arrayOutput = new StringWriter()
+        def arrayWriter = new PathCapturingJSONWriterWrapper(new 
JSONWriter(arrayOutput))
+        def objectOutput = new StringWriter()
+        def objectWriter = new PathCapturingJSONWriterWrapper(new 
JSONWriter(objectOutput))
+
+        expect:
+        arrayWriter.array().is(arrayWriter)
+        writeBoolean(arrayWriter, false).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[0]'
+        writeDouble(arrayWriter, 2.5d).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[1]'
+        writeLong(arrayWriter, 7L).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[2]'
+        writeObjectValue(arrayWriter, null).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[3]'
+        arrayWriter.endArray().is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == ''
+        arrayOutput.toString() == '[false,2.5,7,null]'
+        objectWriter.object().key('boolean').is(objectWriter)
+        writeBoolean(objectWriter, true).is(objectWriter)
+        objectWriter.getCurrentStrackReference() == ''
+        objectWriter.key('double').is(objectWriter)
+        writeDouble(objectWriter, 2.5d).is(objectWriter)
+        objectWriter.getCurrentStrackReference() == ''
+        objectWriter.key('long').is(objectWriter)
+        writeLong(objectWriter, 7L).is(objectWriter)
+        objectWriter.getCurrentStrackReference() == ''

Review Comment:
   The method name 'getCurrentStrackReference' appears to have a typo ('Strack' 
vs 'Stack'), which can be confusing and, if the production API is actually 
'getCurrentStackReference', would cause these tests to not compile. Consider 
renaming usages to the correct method name (and, if needed for compatibility, 
keeping a deprecated alias in production code) so tests reflect the intended 
API.



##########
grails-web-common/src/test/groovy/org/grails/web/json/PathCapturingJSONWriterWrapperApiSpec.groovy:
##########
@@ -0,0 +1,163 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.json
+
+import java.io.StringWriter
+
+import groovy.transform.CompileStatic
+import spock.lang.Specification
+
+class PathCapturingJSONWriterWrapperApiSpec extends Specification {
+
+    void "capture nested object properties and array indices as values are 
written"() {
+        given:
+        def output = new StringWriter()
+        def writer = new PathCapturingJSONWriterWrapper(new JSONWriter(output))
+
+        expect:
+        writer.getCurrentStrackReference() == ''
+        writer.getStackReference(0) == ''
+        writer.object().key('items').is(writer)
+        writer.getCurrentStrackReference() == '.items'
+        writer.array().is(writer)
+        writer.getCurrentStrackReference() == '.items[-1]'
+        writer.object().key('name').is(writer)
+        writer.getCurrentStrackReference() == '.items[0].name'
+        writer.getStackReference(1) == '.items'
+        writer.getStackReference(2) == '.items[0]'
+        writeObjectValue(writer, 'Grails').is(writer)
+        writer.getCurrentStrackReference() == '.items[0]'
+        writer.endObject().is(writer)
+        writer.object().key('active').is(writer)
+        writer.getCurrentStrackReference() == '.items[1].active'
+        writeBoolean(writer, true).is(writer)
+        writer.getCurrentStrackReference() == '.items[1]'
+        writer.endObject().endArray().is(writer)
+        writer.getCurrentStrackReference() == ''
+        writer.key('nested').object().key('count').is(writer)
+        writer.getCurrentStrackReference() == '.nested.count'
+        writeLong(writer, 2L).is(writer)
+        writer.getCurrentStrackReference() == '.nested'
+        writer.endObject().endObject().is(writer)
+        writer.getCurrentStrackReference() == ''
+        output.toString() == 
'{"items":[{"name":"Grails"},{"active":true}],"nested":{"count":2}}'
+    }
+
+    void "each scalar overload advances array indices and removes object 
properties"() {
+        given:
+        def arrayOutput = new StringWriter()
+        def arrayWriter = new PathCapturingJSONWriterWrapper(new 
JSONWriter(arrayOutput))
+        def objectOutput = new StringWriter()
+        def objectWriter = new PathCapturingJSONWriterWrapper(new 
JSONWriter(objectOutput))
+
+        expect:
+        arrayWriter.array().is(arrayWriter)
+        writeBoolean(arrayWriter, false).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[0]'
+        writeDouble(arrayWriter, 2.5d).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[1]'
+        writeLong(arrayWriter, 7L).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[2]'
+        writeObjectValue(arrayWriter, null).is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == '[3]'
+        arrayWriter.endArray().is(arrayWriter)
+        arrayWriter.getCurrentStrackReference() == ''
+        arrayOutput.toString() == '[false,2.5,7,null]'
+        objectWriter.object().key('boolean').is(objectWriter)
+        writeBoolean(objectWriter, true).is(objectWriter)
+        objectWriter.getCurrentStrackReference() == ''
+        objectWriter.key('double').is(objectWriter)
+        writeDouble(objectWriter, 2.5d).is(objectWriter)
+        objectWriter.getCurrentStrackReference() == ''
+        objectWriter.key('long').is(objectWriter)
+        writeLong(objectWriter, 7L).is(objectWriter)
+        objectWriter.getCurrentStrackReference() == ''
+        objectWriter.key('null').is(objectWriter)
+        writeObjectValue(objectWriter, null).is(objectWriter)
+        objectWriter.endObject().is(objectWriter)
+        objectOutput.toString() == 
'{"boolean":true,"double":2.5,"long":7,"null":null}'
+    }
+
+    void "public raw append and comma delegate to the wrapped writer"() {
+        given:
+        def output = new StringWriter()
+        def writer = new PathCapturingJSONWriterWrapper(new JSONWriter(output))
+        def commaOutput = new StringWriter()
+        def commaWriter = new PathCapturingJSONWriterWrapper(new 
JSONWriter(commaOutput))
+
+        when:
+        writer.array()
+        def result = writer.append('true')
+        writer.endArray()
+        commaWriter.comma()
+
+        then:
+        result.is(writer)
+        output.toString() == '[true]'
+        commaOutput.toString() == ','
+        writer.getCurrentStrackReference() == ''

Review Comment:
   The method name 'getCurrentStrackReference' appears to have a typo ('Strack' 
vs 'Stack'), which can be confusing and, if the production API is actually 
'getCurrentStackReference', would cause these tests to not compile. Consider 
renaming usages to the correct method name (and, if needed for compatibility, 
keeping a deprecated alias in production code) so tests reflect the intended 
API.



##########
grails-web-common/src/test/groovy/org/grails/web/json/PrettyPrintJSONWriterApiSpec.groovy:
##########
@@ -0,0 +1,108 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.json
+
+import java.io.IOException
+import java.io.StringWriter
+import java.io.Writer
+
+import spock.lang.Specification
+
+class PrettyPrintJSONWriterApiSpec extends Specification {
+
+    void "indent nested objects and arrays with the default indentation"() {
+        given:
+        def output = new StringWriter()
+        def writer = new PrettyPrintJSONWriter(output)
+        def newline = PrettyPrintJSONWriter.NEWLINE
+
+        when:
+        def result = 
writer.object().key('items').array().object().key('name').value((Object) 
'Grails')
+            
.endObject().value(true).valueNull().endArray().key('count').value(2L).endObject()
+
+        then:
+        result.is(writer)
+        PrettyPrintJSONWriter.DEFAULT_INDENT_STR == '  '
+        output.toString() == ['{', '  "items": ', '  [', '    {', '      
"name": "Grails"', '    },', '    true,', '    null', '  ],', '  "count": 2', 
'}'].join(newline)
+        new JSONObject(output.toString()).getJSONArray('items').length() == 3
+    }
+
+    void "custom indentation is used at each nesting level including empty 
containers"() {
+        given:
+        def output = new StringWriter()
+        def newline = PrettyPrintJSONWriter.NEWLINE
+
+        when:
+        new PrettyPrintJSONWriter(output, '    
').object().key('empty').object().endObject()
+            .key('array').array().endArray().endObject()
+
+        then:
+        output.toString() == ['{', '    "empty": {', '    },', '    "array": 
', '    [', '    ]', '}'].join(newline)
+    }
+
+    void "pretty writer rejects invalid key and value sequences"() {
+        given:
+        def writer = new PrettyPrintJSONWriter(new StringWriter())
+
+        when:
+        operation(writer)
+
+        then:
+        def error = thrown(JSONException)
+        error.message.contains(message)
+
+        where:
+        operation                           | message
+        ({ w -> w.key(null) })                 | 'Null key'
+        ({ w -> w.key('key') })                | 'Misplaced key'
+        ({ w -> w.value(true) })               | 'Value out of sequence'
+        ({ w -> w.object().valueNull() })      | 'Value out of sequence'
+        ({ w -> w.array().endObject() })       | 'Misplaced endArray'

Review Comment:
   The endObject() misuse case is asserted to contain 'Misplaced endArray'. If 
PrettyPrintJSONWriter reports a distinct 'Misplaced endObject' message 
(typical), this expectation will fail and/or encode an incorrect error message 
contract. Consider updating the expected message for the endObject() case, or 
asserting a more general message fragment that’s stable across implementations.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to