diff options
author | Mook <marky@activestate.com> | 2012-05-29 12:44:34 -0700 |
---|---|---|
committer | Mook <marky@activestate.com> | 2012-05-29 12:44:34 -0700 |
commit | dd24e21bbeadbd1c7f6954f72ebe479e7ce3dc99 (patch) | |
tree | 52cde4788556622c9e58f09a202df524e3497216 /test | |
parent | 9adc3c5c2f112142780c0cdd892e3ff7e338c3ad (diff) | |
download | scintilla-mirror-dd24e21bbeadbd1c7f6954f72ebe479e7ce3dc99.tar.gz |
add CharClassifier::GetCharsOfClass
add SCI_GETWORDCHARS(<unused>, stringresult chars) command to get word chars
add SCI_GETWHITESPACECHARS(<unused>, stringresult chars) to get whitespace chars
add SCI_GETPUNCTUATIONCHARS(<unused>, stringresult chars) to get punctutation
also add tests for {Set,Get}{Word,Whitespce,Punctuation}Chars, CharClassifier
Diffstat (limited to 'test')
-rw-r--r-- | test/simpleTests.py | 129 | ||||
-rw-r--r-- | test/unit/makefile | 2 | ||||
-rw-r--r-- | test/unit/testCharClassify.cxx | 110 |
3 files changed, 239 insertions, 2 deletions
diff --git a/test/simpleTests.py b/test/simpleTests.py index 8e101b84e..d8abbb651 100644 --- a/test/simpleTests.py +++ b/test/simpleTests.py @@ -3,7 +3,7 @@ from __future__ import with_statement from __future__ import unicode_literals -import ctypes, os, sys, unittest +import codecs, ctypes, os, sys, unittest import XiteWin @@ -1348,6 +1348,133 @@ class TestDirectAccess(unittest.TestCase): cpBuffer = ctypes.c_char_p(rangePointer) self.assertEquals(cpBuffer.value, text[1:]) +class TestWordChars(unittest.TestCase): + def setUp(self): + self.xite = XiteWin.xiteFrame + self.ed = self.xite.ed + self.ed.ClearAll() + self.ed.EmptyUndoBuffer() + + def tearDown(self): + self.ed.SetCharsDefault() + + def _setChars(self, charClass, chars): + """ Wrapper to call self.ed.Set*Chars with the right type + @param charClass {str} the character class, "word", "space", etc. + @param chars {iterable of int} characters to set + """ + if sys.version_info.major == 2: + # Python 2, use latin-1 encoded str + unichars = (unichr(x) for x in chars if x != 0) + # can't use literal u"", that's a syntax error in Py3k + # uncode() doesn't exist in Py3k, but we never run it there + result = unicode("").join(unichars).encode("latin-1") + else: + # Python 3, use bytes() + result = bytes(x for x in chars if x != 0) + meth = getattr(self.ed, "Set%sChars" % (charClass.capitalize())) + return meth(None, result) + + def assertCharSetsEqual(self, first, second, *args, **kwargs): + """ Assert that the two character sets are equal. + If either set are an iterable of numbers, convert them to chars + first. """ + first_set = set() + for c in first: + first_set.add(chr(c) if isinstance(c, int) else c) + second_set = set() + for c in second: + second_set.add(chr(c) if isinstance(c, int) else c) + return self.assertEqual(first_set, second_set, *args, **kwargs) + + def testDefaultWordChars(self): + # check that the default word chars are as expected + import string + dataLen = self.ed.GetWordChars(None, None) + data = b"\0" * dataLen + self.ed.GetWordChars(None, data) + self.assertEquals(dataLen, len(data)) + expected = set(string.digits + string.ascii_letters + '_') | \ + set(chr(x) for x in range(0x80, 0x100)) + self.assertCharSetsEqual(data, expected) + + def testDefaultWhitespaceChars(self): + # check that the default whitespace chars are as expected + import string + dataLen = self.ed.GetWhitespaceChars(None, None) + data = b"\0" * dataLen + self.ed.GetWhitespaceChars(None, data) + self.assertEquals(dataLen, len(data)) + expected = (set(chr(x) for x in (range(0, 0x20))) | set(' ')) - \ + set(['\r', '\n']) + self.assertCharSetsEqual(data, expected) + + def testDefaultPunctuationChars(self): + # check that the default punctuation chars are as expected + import string + dataLen = self.ed.GetPunctuationChars(None, None) + data = b"\0" * dataLen + self.ed.GetPunctuationChars(None, data) + self.assertEquals(dataLen, len(data)) + expected = set(chr(x) for x in range(0x20, 0x80)) - \ + set(string.ascii_letters + string.digits + "\r\n_ ") + self.assertCharSetsEqual(data, expected) + + def testCustomWordChars(self): + # check that setting things to whitespace chars makes them not words + self._setChars("whitespace", range(1, 0x100)) + dataLen = self.ed.GetWordChars(None, None) + data = b"\0" * dataLen + self.ed.GetWordChars(None, data) + self.assertEquals(dataLen, len(data)) + expected = set() + self.assertCharSetsEqual(data, expected) + # and now set something to make sure that works too + expected = set(range(1, 0x100, 2)) + self._setChars("word", expected) + dataLen = self.ed.GetWordChars(None, None) + data = b"\0" * dataLen + self.ed.GetWordChars(None, data) + self.assertEquals(dataLen, len(data)) + self.assertCharSetsEqual(data, expected) + + def testCustomWhitespaceChars(self): + # check setting whitespace chars to non-default values + self._setChars("word", range(1, 0x100)) + # we can't change chr(0) from being anything but whitespace + expected = set([0]) + dataLen = self.ed.GetWhitespaceChars(None, None) + data = b"\0" * dataLen + self.ed.GetWhitespaceChars(None, data) + self.assertEquals(dataLen, len(data)) + self.assertCharSetsEqual(data, expected) + # now try to set it to something custom + expected = set(range(1, 0x100, 2)) | set([0]) + self._setChars("whitespace", expected) + dataLen = self.ed.GetWhitespaceChars(None, None) + data = b"\0" * dataLen + self.ed.GetWhitespaceChars(None, data) + self.assertEquals(dataLen, len(data)) + self.assertCharSetsEqual(data, expected) + + def testCustomPunctuationChars(self): + # check setting punctuation chars to non-default values + self._setChars("word", range(1, 0x100)) + expected = set() + dataLen = self.ed.GetPunctuationChars(None, None) + data = b"\0" * dataLen + self.ed.GetPunctuationChars(None, data) + self.assertEquals(dataLen, len(data)) + self.assertEquals(set(data), expected) + # now try to set it to something custom + expected = set(range(1, 0x100, 1)) + self._setChars("punctuation", expected) + dataLen = self.ed.GetPunctuationChars(None, None) + data = b"\0" * dataLen + self.ed.GetPunctuationChars(None, data) + self.assertEquals(dataLen, len(data)) + self.assertCharSetsEqual(data, expected) + #~ import os #~ for x in os.getenv("PATH").split(";"): #~ n = "scilexer.dll" diff --git a/test/unit/makefile b/test/unit/makefile index 35a6fd0ce..220952c6f 100644 --- a/test/unit/makefile +++ b/test/unit/makefile @@ -42,7 +42,7 @@ CXXFLAGS += -g -Wall -Wextra -Wno-unused-function #~ CXXFLAGS += -g -Wall CASES:=$(addsuffix .o,$(basename $(notdir $(wildcard test*.cxx)))) -TESTEDOBJS=ContractionState.o RunStyles.o +TESTEDOBJS=ContractionState.o RunStyles.o CharClassify.o TESTS=$(EXE) diff --git a/test/unit/testCharClassify.cxx b/test/unit/testCharClassify.cxx new file mode 100644 index 000000000..8ff98b302 --- /dev/null +++ b/test/unit/testCharClassify.cxx @@ -0,0 +1,110 @@ +// Unit Tests for Scintilla internal data structures + +#include <string.h> + +#include "Platform.h" + +#include "CharClassify.h" + +#include <gtest/gtest.h> + +// Test CharClassify. + +class CharClassifyTest : public::testing::Test { +protected: + virtual void SetUp() { + pcc = new CharClassify(); + for (int ch = 0; ch < 256; ch++) { + if (ch == '\r' || ch == '\n') + charClass[ch] = CharClassify::ccNewLine; + else if (ch < 0x20 || ch == ' ') + charClass[ch] = CharClassify::ccSpace; + else if (ch >= 0x80 || isalnum(ch) || ch == '_') + charClass[ch] = CharClassify::ccWord; + else + charClass[ch] = CharClassify::ccPunctuation; + } + } + + virtual void TearDown() { + delete pcc; + pcc = 0; + } + + CharClassify *pcc; + CharClassify::cc charClass[256]; + + static const char* GetClassName(CharClassify::cc charClass) { + switch(charClass) { + #define CASE(c) case CharClassify::c: return #c + CASE(ccSpace); + CASE(ccNewLine); + CASE(ccWord); + CASE(ccPunctuation); + #undef CASE + default: + return "<unknown>"; + } + } +}; + +TEST_F(CharClassifyTest, Defaults) { + for (int i = 0; i < 256; i++) { + EXPECT_EQ(charClass[i], pcc->GetClass(i)) + << "Character " << i + << " should be class " << GetClassName(charClass[i]) + << ", but got " << GetClassName(pcc->GetClass(i)); + } +} + +TEST_F(CharClassifyTest, Custom) { + unsigned char buf[2] = {0, 0}; + for (int i = 0; i < 256; i++) { + CharClassify::cc thisClass = CharClassify::cc(i % 4); + buf[0] = i; + pcc->SetCharClasses(buf, thisClass); + charClass[i] = thisClass; + } + for (int i = 0; i < 256; i++) { + EXPECT_EQ(charClass[i], pcc->GetClass(i)) + << "Character " << i + << " should be class " << GetClassName(charClass[i]) + << ", but got " << GetClassName(pcc->GetClass(i)); + } +} + +TEST_F(CharClassifyTest, CharsOfClass) { + unsigned char buf[2] = {0, 0}; + for (int i = 1; i < 256; i++) { + CharClassify::cc thisClass = CharClassify::cc(i % 4); + buf[0] = i; + pcc->SetCharClasses(buf, thisClass); + charClass[i] = thisClass; + } + for (int classVal = 0; classVal < 4; ++classVal) { + CharClassify::cc thisClass = CharClassify::cc(classVal % 4); + int size = pcc->GetCharsOfClass(thisClass, NULL); + unsigned char* buffer = reinterpret_cast<unsigned char*>(malloc(size + 1)); + ASSERT_TRUE(buffer); + buffer[size] = '\0'; + pcc->GetCharsOfClass(thisClass, buffer); + for (int i = 1; i < 256; i++) { + if (charClass[i] == thisClass) { + EXPECT_TRUE(memchr(reinterpret_cast<char*>(buffer), i, size)) + << "Character " << i + << " should be class " << GetClassName(thisClass) + << ", but was not in GetCharsOfClass;" + << " it is reported to be " + << GetClassName(pcc->GetClass(i)); + } else { + EXPECT_FALSE(memchr(reinterpret_cast<char*>(buffer), i, size)) + << "Character " << i + << " should not be class " << GetClassName(thisClass) + << ", but was in GetCharsOfClass" + << " it is reported to be " + << GetClassName(pcc->GetClass(i)); + } + } + free(buffer); + } +} |