You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2018/02/26 21:55:29 UTC

[2/8] groovy git commit: GROOVY-8379: Rework groovy-json FastStringUtils (closes #667)

http://git-wip-us.apache.org/repos/asf/groovy/blob/63bd67ba/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/LazyMapTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/LazyMapTest.groovy b/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/LazyMapTest.groovy
new file mode 100644
index 0000000..607ad73
--- /dev/null
+++ b/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/LazyMapTest.groovy
@@ -0,0 +1,52 @@
+/*
+ *  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
+ *
+ *    http://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.apache.groovy.json.internal
+
+class LazyMapTest extends GroovyTestCase {
+
+    // GROOVY-7302
+    void testSizeWhenNoBackingMapCreated() {
+        def map = new LazyMap()
+        map.someProperty = "1"
+        map.someProperty = "2"
+        map.someProperty = "3"
+        assert map.size() == 1
+        map.someProperty2 = "4"
+        assert map.size() == 2
+    }
+
+    void testSizeWhenLazyCreated() {
+        def map = new LazyMap()
+        map.someProperty1 = '1'
+        assert map.@map == null
+        map.someProperty2 = '2'
+        assert map.@map == null
+        map.someProperty3 = '3'
+        assert map.@map == null
+        map.someProperty4 = '4'
+        assert map.@map == null
+        map.someProperty5 = '5'
+        assert map.@map == null
+        map.someProperty6 = '6'
+        assert map.@map == null
+        map.someProperty7 = '7'
+        assert map.someProperty6 == '6'
+        assert map.@map?.size() == 7
+    }
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/63bd67ba/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/ReaderCharacterSourceTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/ReaderCharacterSourceTest.groovy b/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/ReaderCharacterSourceTest.groovy
new file mode 100644
index 0000000..f45a0ef
--- /dev/null
+++ b/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/ReaderCharacterSourceTest.groovy
@@ -0,0 +1,77 @@
+/*
+ *  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
+ *
+ *    http://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.apache.groovy.json.internal
+
+import org.apache.groovy.json.internal.Exceptions.JsonInternalException
+
+class ReaderCharacterSourceTest extends GroovyTestCase {
+
+    public static final int QUOTE_CHAR = (int)'"'.charAt(0)
+    public static final int BACKSLASH_CHAR = (int)'\\'.charAt(0)
+
+    void testFindNextChar() {
+        def testCases = [
+                [ input: '""', expected: '' ],
+                [ input: '"word"', expected: 'word' ],
+                [ input: '"\\u0026value"', expected: '\\u0026value' ],
+                [ input: '"value\\u0026"', expected: 'value\\u0026' ],
+                [ input: '"double\\"quote"', expected: 'double\\"quote' ],
+                [ input: '"\\"\\"\\"\\""', expected: '\\"\\"\\"\\"' ],
+                [ input: '"\\\\\\\\\\\\"', expected: '\\\\\\\\\\\\' ]
+        ]
+
+        testCases.each {
+            boolean containsEscape = it.input.contains('\\')
+            // Test all possible buffer sizes
+            for (int i = 1; i < it.input.length() + 1; i++) {
+                ReaderCharacterSource rcs = new ReaderCharacterSource(new StringReader(it.input), i)
+                rcs.nextChar() // Read the first double quote as if JSON parsing
+
+                String result = new String(rcs.findNextChar(QUOTE_CHAR, BACKSLASH_CHAR))
+
+                assertEquals("Buffer size ${i}", it.expected, result)
+                assertEquals("Expected escape character in ${it.input}, buffer size ${i}", containsEscape, rcs.hadEscape())
+            }
+        }
+    }
+
+    void testFindNextCharException() {
+        shouldFail(JsonInternalException) {
+            ReaderCharacterSource rcs = new ReaderCharacterSource(new StringReader('"missing end quote'))
+            rcs.nextChar() // Read the first double quote as if JSON parsing
+            rcs.findNextChar(QUOTE_CHAR, BACKSLASH_CHAR)
+        }
+    }
+
+    void testFindNextCharExceptionWithEscapedEnding() {
+        shouldFail(JsonInternalException) {
+            ReaderCharacterSource rcs = new ReaderCharacterSource(new StringReader('"missing end quote ending with escape \\'))
+            rcs.nextChar() // Read the first double quote as if JSON parsing
+            rcs.findNextChar(QUOTE_CHAR, BACKSLASH_CHAR)
+        }
+    }
+
+    void testFindNextCharExceptionWithEscapedQuote() {
+        shouldFail(JsonInternalException) {
+            ReaderCharacterSource rcs = new ReaderCharacterSource(new StringReader('"missing end quote with escaped quote \\"'))
+            rcs.nextChar() // Read the first double quote as if JSON parsing
+            rcs.findNextChar(QUOTE_CHAR, BACKSLASH_CHAR)
+        }
+    }
+}