From 637d1cf76595c19dd27a532ff2388ccd49b2b7c6 Mon Sep 17 00:00:00 2001 From: Vsevolod Kukol Date: Thu, 5 Oct 2017 18:28:53 +0200 Subject: [PATCH] [Gtk] Add TextIndexer tests --- Testing/GtkTestRunner.csproj | 1 + Testing/GtkTests/PangoTests.cs | 87 ++++++++++++++++++++++++++++ Xwt.Gtk/Xwt.GtkBackend/GtkInterop.cs | 2 +- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 Testing/GtkTests/PangoTests.cs diff --git a/Testing/GtkTestRunner.csproj b/Testing/GtkTestRunner.csproj index c9ebd83e..92c71bc0 100644 --- a/Testing/GtkTestRunner.csproj +++ b/Testing/GtkTestRunner.csproj @@ -111,6 +111,7 @@ + diff --git a/Testing/GtkTests/PangoTests.cs b/Testing/GtkTests/PangoTests.cs new file mode 100644 index 00000000..d2e4d575 --- /dev/null +++ b/Testing/GtkTests/PangoTests.cs @@ -0,0 +1,87 @@ +// +// PangoTests.cs +// +// Author: +// Vsevolod Kukol +// +// Copyright (c) 2017 (c) Microsoft Corporation +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using NUnit.Framework; +using Xwt.GtkBackend; + +namespace Xwt +{ + [TestFixture] + public class PangoUtilTests + { + [Test] + public void TextIndexerAscii() + { + var str = "Test"; + var indexer = new TextIndexer(str); + + Assert.AreEqual(0, indexer.IndexToByteIndex(0)); + Assert.AreEqual(0, indexer.ByteIndexToIndex(0)); + Assert.AreEqual(1, indexer.IndexToByteIndex(1)); + Assert.AreEqual(1, indexer.ByteIndexToIndex(1)); + Assert.AreEqual(2, indexer.IndexToByteIndex(2)); + Assert.AreEqual(2, indexer.ByteIndexToIndex(2)); + Assert.AreEqual(3, indexer.IndexToByteIndex(3)); + Assert.AreEqual(3, indexer.ByteIndexToIndex(3)); + } + + [Test] + public void TextIndexerUnicode() + { + var str = "バージョン"; + var indexer = new TextIndexer(str); + + Assert.AreEqual(0, indexer.IndexToByteIndex(0)); + Assert.AreEqual(3, indexer.IndexToByteIndex(1)); + Assert.AreEqual(6, indexer.IndexToByteIndex(2)); + + Assert.AreEqual(0, indexer.ByteIndexToIndex(0)); + Assert.AreEqual(0, indexer.ByteIndexToIndex(1)); + Assert.AreEqual(0, indexer.ByteIndexToIndex(2)); + Assert.AreEqual(1, indexer.ByteIndexToIndex(3)); + Assert.AreEqual(1, indexer.ByteIndexToIndex(4)); + Assert.AreEqual(1, indexer.ByteIndexToIndex(5)); + } + + [Test] + public void TextIndexerMixed() + { + var str = "バAジョン"; + var indexer = new TextIndexer(str); + + Assert.AreEqual(0, indexer.IndexToByteIndex(0)); + Assert.AreEqual(3, indexer.IndexToByteIndex(1)); + Assert.AreEqual(4, indexer.IndexToByteIndex(2)); + + Assert.AreEqual(0, indexer.ByteIndexToIndex(0)); + Assert.AreEqual(0, indexer.ByteIndexToIndex(1)); + Assert.AreEqual(0, indexer.ByteIndexToIndex(2)); + Assert.AreEqual(1, indexer.ByteIndexToIndex(3)); + Assert.AreEqual(2, indexer.ByteIndexToIndex(4)); + Assert.AreEqual(2, indexer.ByteIndexToIndex(5)); + } + } +} + diff --git a/Xwt.Gtk/Xwt.GtkBackend/GtkInterop.cs b/Xwt.Gtk/Xwt.GtkBackend/GtkInterop.cs index e13ab5df..36cdc224 100644 --- a/Xwt.Gtk/Xwt.GtkBackend/GtkInterop.cs +++ b/Xwt.Gtk/Xwt.GtkBackend/GtkInterop.cs @@ -228,7 +228,7 @@ namespace Xwt.GtkBackend } } - internal class TextIndexer + public class TextIndexer { static readonly List emptyList = new List (); static readonly int [] emptyArray = new int [0];