From 10e096d7c98be5e3a2593375dbae4d31b80e8432 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Tue, 19 Jan 2021 22:37:15 +0900 Subject: [PATCH] Add test count_values_non_tally --- lib/youplot/backends/processing.rb | 4 ++-- test/youplot/backends/processing_test.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/youplot/backends/processing.rb b/lib/youplot/backends/processing.rb index 9b7bcb2..5ce6a8f 100644 --- a/lib/youplot/backends/processing.rb +++ b/lib/youplot/backends/processing.rb @@ -6,9 +6,9 @@ module YouPlot module Processing module_function - def count_values(arr) + def count_values(arr, tally: true) # tally was added in Ruby 2.7 - if Enumerable.method_defined? :tally + if tally && Enumerable.method_defined?(:tally) arr.tally else # https://github.com/marcandre/backports diff --git a/test/youplot/backends/processing_test.rb b/test/youplot/backends/processing_test.rb index 94275f8..88954b5 100644 --- a/test/youplot/backends/processing_test.rb +++ b/test/youplot/backends/processing_test.rb @@ -8,4 +8,10 @@ class YouPlotCommandTest < Test::Unit::TestCase assert_equal([%i[a b c], [3, 2, 1]], @m.count_values(%i[a a a b b c])) assert_equal([%i[c b a], [3, 2, 1]], @m.count_values(%i[a b b c c c])) end + + test :count_values_non_tally do + @m = YouPlot::Backends::Processing + assert_equal([%i[a b c], [3, 2, 1]], @m.count_values(%i[a a a b b c], tally: false)) + assert_equal([%i[c b a], [3, 2, 1]], @m.count_values(%i[a b b c c c], tally: false)) + end end