summaryrefslogtreecommitdiffhomepage
path: root/test/workbook
diff options
context:
space:
mode:
authorrikweelvoormedia <[email protected]>2023-08-15 09:30:15 +0200
committerGitHub <[email protected]>2023-08-15 09:30:15 +0200
commitc71a735991b3ab1bf8f5ead894476898e70c7700 (patch)
tree8014880fb5f1ec3835bf847b0af781c7b10336d5 /test/workbook
parent8a8a256602283debc7b116ab11a2cf64ba56222c (diff)
downloadcaxlsx-c71a735991b3ab1bf8f5ead894476898e70c7700.tar.gz
caxlsx-c71a735991b3ab1bf8f5ead894476898e70c7700.zip
Added sorting to the AutoFilter class - add sort conditions to the xml (#286)
--------- Co-authored-by: SarahVanHaute <[email protected]> Co-authored-by: Geremia Taglialatela <[email protected]>
Diffstat (limited to 'test/workbook')
-rw-r--r--test/workbook/worksheet/auto_filter/tc_sort_condition.rb37
-rw-r--r--test/workbook/worksheet/auto_filter/tc_sort_state.rb36
2 files changed, 73 insertions, 0 deletions
diff --git a/test/workbook/worksheet/auto_filter/tc_sort_condition.rb b/test/workbook/worksheet/auto_filter/tc_sort_condition.rb
new file mode 100644
index 00000000..67e4bdac
--- /dev/null
+++ b/test/workbook/worksheet/auto_filter/tc_sort_condition.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+require 'tc_helper'
+
+class TestSortCondition < Test::Unit::TestCase
+ def setup
+ ws = Axlsx::Package.new.workbook.add_worksheet
+ ws.add_row ['first', 'second', 'third']
+ 3.times { |index| ws.add_row [1 * index, 2 * index, 3 * index] }
+ ws.auto_filter = 'A1:C4'
+ @auto_filter = ws.auto_filter
+ @auto_filter.sort_state.add_sort_condition(column_index: 0)
+ @auto_filter.sort_state.add_sort_condition(column_index: 1, order: :desc)
+ @auto_filter.sort_state.add_sort_condition(column_index: 2, custom_list: ['low', 'middle', 'high'])
+ @sort_state = @auto_filter.sort_state
+ @sort_conditions = @sort_state.sort_conditions
+ end
+
+ def test_ref_to_single_column
+ assert_equal('A2:A4', @sort_conditions[0].ref_to_single_column('A2:C4', 0))
+ end
+
+ def test_to_xml_string
+ doc = Nokogiri::XML(@sort_state.to_xml_string)
+
+ assert_equal(3, doc.xpath("sortState//sortCondition").size)
+ assert_equal('A2:A4', doc.xpath("sortState//sortCondition")[0].attribute('ref').value)
+ assert_nil doc.xpath("sortState//sortCondition")[0].attribute('descending')
+ assert_nil doc.xpath("sortState//sortCondition")[0].attribute('customList')
+ assert_equal('1', doc.xpath("sortState//sortCondition")[1].attribute('descending').value)
+ assert_equal('B2:B4', doc.xpath("sortState//sortCondition")[1].attribute('ref').value)
+ assert_nil doc.xpath("sortState//sortCondition")[1].attribute('customList')
+ assert_equal('C2:C4', doc.xpath("sortState//sortCondition")[2].attribute('ref').value)
+ assert_equal('low,middle,high', doc.xpath("sortState//sortCondition")[2].attribute('customList').value)
+ assert_nil doc.xpath("sortState//sortCondition")[2].attribute('descending')
+ end
+end
diff --git a/test/workbook/worksheet/auto_filter/tc_sort_state.rb b/test/workbook/worksheet/auto_filter/tc_sort_state.rb
new file mode 100644
index 00000000..db3d03c0
--- /dev/null
+++ b/test/workbook/worksheet/auto_filter/tc_sort_state.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+require 'tc_helper'
+
+class TestSortState < Test::Unit::TestCase
+ def setup
+ ws = Axlsx::Package.new.workbook.add_worksheet
+ ws.add_row ['first', 'second', 'third']
+ 3.times { |index| ws.add_row [1 * index, 2 * index, 3 * index] }
+ ws.auto_filter = 'A1:C4'
+ @auto_filter = ws.auto_filter
+ @auto_filter.sort_state.add_sort_condition(column_index: 0)
+ @sort_state = @auto_filter.sort_state
+ end
+
+ def test_sort_conditions
+ assert @sort_state.sort_conditions.is_a?(Axlsx::SimpleTypedList)
+ assert_equal @sort_state.sort_conditions.allowed_types, [Axlsx::SortCondition]
+ end
+
+ def test_add_sort_conditions
+ @sort_state.add_sort_condition(column_index: 0) do |condition|
+ assert condition.is_a? SortCondition
+ end
+ end
+
+ def test_increment_cell_value
+ assert_equal('A2', @sort_state.increment_cell_value('A1'))
+ end
+
+ def test_to_xml_string
+ doc = Nokogiri::XML(@sort_state.to_xml_string)
+
+ assert_equal('A2:C4', doc.xpath("sortState")[0].attribute('ref').value)
+ end
+end