Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMatheus Santos <MatheusSantos>2022-04-07 07:32:21 +0300
committerCampbell Barton <campbell@blender.org>2022-04-07 08:17:04 +0300
commitf49a736ff4023231483c7e535ca2a7f2869d641d (patch)
tree506407e67e86034329fed5c71c49435a29ec2b31 /tests
parent7cd6bda206800218da47ee35c53066a71d25ef22 (diff)
Text Editor: Get/Set region text API
Add the ability to get/set the selected text. **Calling the new methods:** - `bpy.data.texts["Text"].region_as_string()` - `bpy.data.texts["Text"].region_from_string("Replacement")`
Diffstat (limited to 'tests')
-rw-r--r--tests/python/CMakeLists.txt5
-rw-r--r--tests/python/bl_pyapi_text.py65
2 files changed, 70 insertions, 0 deletions
diff --git a/tests/python/CMakeLists.txt b/tests/python/CMakeLists.txt
index fabac659555..a0b2e6207bf 100644
--- a/tests/python/CMakeLists.txt
+++ b/tests/python/CMakeLists.txt
@@ -116,6 +116,11 @@ add_blender_test(
--python ${CMAKE_CURRENT_LIST_DIR}/bl_pyapi_prop_array.py
)
+add_blender_test(
+ script_pyapi_text
+ --python ${CMAKE_CURRENT_LIST_DIR}/bl_pyapi_text.py
+)
+
# ------------------------------------------------------------------------------
# DATA MANAGEMENT TESTS
diff --git a/tests/python/bl_pyapi_text.py b/tests/python/bl_pyapi_text.py
new file mode 100644
index 00000000000..67e07e7d907
--- /dev/null
+++ b/tests/python/bl_pyapi_text.py
@@ -0,0 +1,65 @@
+# SPDX-License-Identifier: Apache-2.0
+
+# ./blender.bin --background -noaudio --python tests/python/bl_pyapi_text.py -- --verbose
+import bpy
+import unittest
+
+
+class TestText(unittest.TestCase):
+
+ def setUp(self):
+ self.text = bpy.data.texts.new("test_text")
+
+ def tearDown(self):
+ bpy.data.texts.remove(self.text)
+ del self.text
+
+ def test_text_new(self):
+ self.assertEqual(len(bpy.data.texts), 1)
+ self.assertEqual(self.text.name, "test_text")
+ self.assertEqual(self.text.as_string(), "\n")
+
+ def test_text_clear(self):
+ self.text.clear()
+ self.assertEqual(self.text.as_string(), "\n")
+
+ def test_text_fill(self):
+ tmp_text = (
+ "Line 1: Test line 1\n"
+ "Line 2: test line 2\n"
+ "Line 3: test line 3"
+ )
+ self.text.write(tmp_text)
+ self.assertEqual(self.text.as_string(), tmp_text + "\n")
+
+ def test_text_region_as_string(self):
+ tmp_text = (
+ "Line 1: Test line 1\n"
+ "Line 2: test line 2\n"
+ "Line 3: test line 3"
+ )
+ self.text.write(tmp_text)
+ # Get string in the middle of the text.
+ self.assertEqual(self.text.region_as_string(((1, 0), (1, -1))), "Line 2: test line 2")
+ # Big range test.
+ self.assertEqual(self.text.region_as_string(((-10000, -10000), (10000, 10000))), tmp_text)
+
+ def test_text_region_from_string(self):
+ tmp_text = (
+ "Line 1: Test line 1\n"
+ "Line 2: test line 2\n"
+ "Line 3: test line 3"
+ )
+ self.text.write(tmp_text)
+ # Set string in the middle of the text.
+ self.text.region_from_string("line 2", ((1, 0), (1, -1)))
+ self.assertEqual(self.text.as_string(), tmp_text.replace("Line 2: test line 2", "line 2") + "\n")
+ # Large range test.
+ self.text.region_from_string("New Text", ((-10000, -10000), (10000, 10000)))
+ self.assertEqual(self.text.as_string(), "New Text\n")
+
+
+if __name__ == "__main__":
+ import sys
+ sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
+ unittest.main()