From 9ab3cfb7a77b68f873bdcbe9405aeb54cfc61047 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Tue, 28 Feb 2023 17:36:12 -0800 Subject: [PATCH] Add tests --- Library/Homebrew/cask/auditor.rb | 2 +- Library/Homebrew/test/utils_spec.rb | 47 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/auditor.rb b/Library/Homebrew/cask/auditor.rb index 80bdb5f839..caf2fa0ed8 100644 --- a/Library/Homebrew/cask/auditor.rb +++ b/Library/Homebrew/cask/auditor.rb @@ -58,7 +58,7 @@ module Cask sample_languages = if language_blocks.length > LANGUAGE_BLOCK_LIMIT && !@audit_new_cask sample_keys = language_blocks.keys.sample(LANGUAGE_BLOCK_LIMIT) sample_keys_sentence = ::Utils.to_sentence(sample_keys.map { |lang| lang[0].to_s }) - ohai "Auditing a sample of available languages: #{sameple_keys_sentence}" + ohai "Auditing a sample of available languages: #{sample_keys_sentence}" language_blocks.select { |k| sample_keys.include?(k) } else language_blocks diff --git a/Library/Homebrew/test/utils_spec.rb b/Library/Homebrew/test/utils_spec.rb index c75d21bed8..551f7d9c78 100644 --- a/Library/Homebrew/test/utils_spec.rb +++ b/Library/Homebrew/test/utils_spec.rb @@ -58,4 +58,51 @@ describe Utils do expect(described_class.pluralize("foo", 2, singular: "o", plural: "es")).to eq("fooes") end end + + describe ".to_sentence" do + it "converts a plain array to a sentence" do + expect(described_class.to_sentence([])).to eq("") + expect(described_class.to_sentence(["one"])).to eq("one") + expect(described_class.to_sentence(["one", "two"])).to eq("one and two") + expect(described_class.to_sentence(["one", "two", "three"])).to eq("one, two, and three") + end + + it "converts an array to a sentence with a custom connector" do + expect(described_class.to_sentence(["one", "two", "three"], words_connector: " ")).to eq("one two, and three") + expect(described_class.to_sentence(["one", "two", "three"], + words_connector: " & ")).to eq("one & two, and three") + end + + it "converts an array to a sentence with a custom last word connector" do + expect(described_class.to_sentence(["one", "two", "three"], + last_word_connector: ", and also ")).to eq("one, two, and also three") + expect(described_class.to_sentence(["one", "two", "three"], last_word_connector: " ")).to eq("one, two three") + expect(described_class.to_sentence(["one", "two", "three"], + last_word_connector: " and ")).to eq("one, two and three") + end + + it "converts an array to a sentence with a custom two word connector" do + expect(described_class.to_sentence(["one", "two"], two_words_connector: " ")).to eq("one two") + end + + it "creates a new string" do + elements = ["one"] + expect(described_class.to_sentence(elements).object_id).not_to eq(elements[0].object_id) + end + + it "converts a non-String to a sentence" do + expect(described_class.to_sentence([1])).to eq("1") + end + + it "converts an array with blank elements to a sentence" do + expect(described_class.to_sentence([nil, "one", "", "two", "three"])).to eq(", one, , two, and three") + end + + it "does not return a frozen string" do + expect(described_class.to_sentence([])).not_to be_frozen + expect(described_class.to_sentence(["one"])).not_to be_frozen + expect(described_class.to_sentence(["one", "two"])).not_to be_frozen + expect(described_class.to_sentence(["one", "two", "three"])).not_to be_frozen + end + end end