ropes: Fix the behavior of `RopeBuffer.clear` when used with `to_s`.
authorJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Sun, 7 Dec 2014 16:30:50 +0000 (11:30 -0500)
committerJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Sun, 7 Dec 2014 16:30:50 +0000 (11:30 -0500)
Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

lib/standard/ropes.nit
tests/sav/test_ropes_buffer_to_s.res [new file with mode: 0644]
tests/test_ropes_buffer_to_s.nit [new file with mode: 0644]

index df98433..8d2d057 100644 (file)
@@ -241,6 +241,10 @@ class RopeBuffer
                length = 0
                rpos = 0
                dumped = 0
+               if written then
+                       ns = new NativeString(buf_size)
+                       written = false
+               end
        end
 
        redef fun substring(from, count) do
diff --git a/tests/sav/test_ropes_buffer_to_s.res b/tests/sav/test_ropes_buffer_to_s.res
new file mode 100644 (file)
index 0000000..0bf725e
--- /dev/null
@@ -0,0 +1,4 @@
+`clear` and `append`: abcd
+`clear` and `add`: ab
+`add` at `maxlen + 1`: c
+`append` up to `maxlen + 1`: ab
diff --git a/tests/test_ropes_buffer_to_s.nit b/tests/test_ropes_buffer_to_s.nit
new file mode 100644 (file)
index 0000000..4933a1b
--- /dev/null
@@ -0,0 +1,58 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed 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.
+
+# Checks the immutability of the strings returned by `RopeBuffer.to_s`.
+module test_ropes_buffer_to_s
+
+import standard
+
+# Note: In this sort of test, never print the string more than once: the string
+# itself may cache an flatten representation of itself when `print` calls `to_s`
+# on it. For example, the original bug that motivated the writting of the
+# present test got unoticed until we tried to edit the buffer **before**
+# outputting `s`.
+
+var buffer = new RopeBuffer
+var s: String
+
+sys.stdout.write "`clear` and `append`: "
+buffer.append "abcd"
+s = buffer.to_s
+buffer.clear
+buffer.append "ef"
+print s
+
+sys.stdout.write "`clear` and `add`: "
+buffer.clear
+buffer.add 'a'
+buffer.add 'b'
+s = buffer.to_s
+buffer.clear
+buffer.add 'c'
+print s
+
+sys.stdout.write "`add` at `maxlen + 1`: "
+buffer.clear
+buffer.add 'c'
+s = buffer.to_s
+buffer.append("*" * (maxlen -1))
+buffer.add 'x'
+print s
+
+sys.stdout.write "`append` up to `maxlen + 1`: "
+buffer.clear
+buffer.append "ab"
+s = buffer.to_s
+buffer.append("*" * (maxlen -1))
+print s