<commit_before>package grails.web

import org.codehaus.groovy.grails.web.converters.configuration.ConvertersConfigurationInitializer

/**
 * @author <NAME>
 * @since 1.2
 */

public class JSONBuilderTests extends GroovyTestCase{

    void testSimple() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            rootprop ="something"
        }

        assertEquals '{"rootprop":"something"}', result.toString()
    }


    void testArrays() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            categories = ['a', 'b', 'c']
            rootprop ="something"
        }

        assertEquals '{"categories":["a","b","c"],"rootprop":"something"}', result.toString()        
    }

    void testSubObjects() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            categories = ['a', 'b', 'c']
            rootprop ="something"
            test {
                subprop = 10
            }
        }

        assertEquals '{"categories":["a","b","c"],"rootprop":"something","test":{"subprop":10}}', result.toString()
    }

    void testAssignedObjects() {

        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            categories = ['a', 'b', 'c']
            rootprop ="something"
            test = {
                subprop = 10
            }
        }

        assertEquals '{"categories":["a","b","c"],"rootprop":"something","test":{"subprop":10}}', result.toString()
    }

    void testNamedArgumentHandling() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            categories = ['a', 'b', 'c']
            rootprop ="something"
            test subprop:10, three:[1,2,3]

        }

        assertEquals '{"categories":["a","b","c"],"rootprop":"something","test":{"subprop":10,"three":[1,2,3]}}', result.toString()
    }


    void testArrayOfClosures() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

       
        def result = builder.build {
            foo = [ { bar = "hello" } ]
        }

        assertEquals '{"foo":[{"bar":"hello"}]}', result.toString()
    }

    void testRootElementList() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def results = ['one', 'two', 'three']

        def result = builder.build {

           for(b in results) {
               element b
           }
        }

        assertEquals '["one","two","three"]', result.toString()

        result = builder.build {
            results
        }

        assertEquals '["one","two","three"]', result.toString()

    }

    void testExampleFromReferenceGuide() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def results = ['one', 'two', 'three']

        def result = builder.build {
           for(b in results) {
               element title:b
           }
        }

        assertEquals '[{"title":"one"},{"title":"two"},{"title":"three"}]', result.toString()


        result = builder.build {
           books = results.collect {
               [title:it]
           }
        }

        assertEquals '{"books":[{"title":"one"},{"title":"two"},{"title":"three"}]}', result.toString()

        result = builder.build {
           books = array {
                for(b in results) {
                    book title:b
                }
           }
        }

        assertEquals '{"books":[{"title":"one"},{"title":"two"},{"title":"three"}]}', result.toString()
    }
}<commit_msg>test for appending to JSON list<commit_after>package grails.web

import org.codehaus.groovy.grails.web.converters.configuration.ConvertersConfigurationInitializer

/**
 * @author <NAME>
 * @since 1.2
 */

public class JSONBuilderTests extends GroovyTestCase{

    void testSimple() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            rootprop ="something"
        }

        assertEquals '{"rootprop":"something"}', result.toString()
    }


    void testArrays() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            categories = ['a', 'b', 'c']
            rootprop ="something"
        }

        assertEquals '{"categories":["a","b","c"],"rootprop":"something"}', result.toString()        
    }

    void testSubObjects() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            categories = ['a', 'b', 'c']
            rootprop ="something"
            test {
                subprop = 10
            }
        }

        assertEquals '{"categories":["a","b","c"],"rootprop":"something","test":{"subprop":10}}', result.toString()
    }

    void testAssignedObjects() {

        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            categories = ['a', 'b', 'c']
            rootprop ="something"
            test = {
                subprop = 10
            }
        }

        assertEquals '{"categories":["a","b","c"],"rootprop":"something","test":{"subprop":10}}', result.toString()
    }

    void testNamedArgumentHandling() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def result = builder.build {
            categories = ['a', 'b', 'c']
            rootprop ="something"
            test subprop:10, three:[1,2,3]

        }

        assertEquals '{"categories":["a","b","c"],"rootprop":"something","test":{"subprop":10,"three":[1,2,3]}}', result.toString()
    }


    void testArrayOfClosures() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

       
        def result = builder.build {
            foo = [ { bar = "hello" } ]
        }

        assertEquals '{"foo":[{"bar":"hello"}]}', result.toString()
    }

    void testRootElementList() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def results = ['one', 'two', 'three']

        def result = builder.build {

           for(b in results) {
               element b
           }
        }

        assertEquals '["one","two","three"]', result.toString()

        result = builder.build {
            results
        }

        assertEquals '["one","two","three"]', result.toString()

    }

    void testExampleFromReferenceGuide() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def results = ['one', 'two', 'three']

        def result = builder.build {
           for(b in results) {
               element title:b
           }
        }

        assertEquals '[{"title":"one"},{"title":"two"},{"title":"three"}]', result.toString()


        result = builder.build {
           books = results.collect {
               [title:it]
           }
        }

        assertEquals '{"books":[{"title":"one"},{"title":"two"},{"title":"three"}]}', result.toString()

        result = builder.build {
           books = array {
                for(b in results) {
                    book title:b
                }
           }
        }

        assertEquals '{"books":[{"title":"one"},{"title":"two"},{"title":"three"}]}', result.toString()
    }

    void testAppendToArray() {
        def initializer = new ConvertersConfigurationInitializer()
        initializer.initialize()
        def builder = new JSONBuilder()

        def results = ['one', 'two', 'three']

        def result = builder.build {
           books = array { list ->
                for(b in results) {
                    list << [title:b]
                }
           }
        }

        assertEquals '{"books":[{"title":"one"},{"title":"two"},{"title":"three"}]}', result.toString()

    }
}
{'fraction_non_alphanumeric': 0.14378940382701028, 'fraction_numerical': 0.004424289348523394, 'mean_word_length': 2.0547297297297296, 'pattern_counts': {'":': 62, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6709541', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2275, 'n_words': 501}