From 43e6f328776ec3d3f048abf62e36a47b3f7f03b7 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 17 Jun 2026 15:57:02 +1200 Subject: [PATCH] Improve test coverage --- test/samovar/boolean_flag.rb | 16 +++++++++++++ test/samovar/completion.rb | 24 ++++++++++++++++++++ test/samovar/flag.rb | 14 ++++++++++++ test/samovar/flags.rb | 12 ++++------ test/samovar/many.rb | 12 +++++++++- test/samovar/one.rb | 10 ++++++++- test/samovar/options.rb | 10 +++++++++ test/samovar/output/header.rb | 26 ++++++++++++++++++++++ test/samovar/{output.rb => output/rows.rb} | 20 ----------------- 9 files changed, 114 insertions(+), 30 deletions(-) create mode 100644 test/samovar/boolean_flag.rb create mode 100644 test/samovar/flag.rb create mode 100644 test/samovar/output/header.rb rename test/samovar/{output.rb => output/rows.rb} (58%) diff --git a/test/samovar/boolean_flag.rb b/test/samovar/boolean_flag.rb new file mode 100644 index 0000000..cbe0ec0 --- /dev/null +++ b/test/samovar/boolean_flag.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2026, by Samuel Williams. + +require "samovar/flags" + +describe Samovar::BooleanFlag do + let(:flag) {Samovar::Flag.parse("--[no]-color")} + + it "can check prefix" do + expect(flag.prefix?("--color")).to be == true + expect(flag.prefix?("--no-color")).to be == true + expect(flag.prefix?("--other")).to be == false + end +end diff --git a/test/samovar/completion.rb b/test/samovar/completion.rb index 4b2b17f..7957af4 100644 --- a/test/samovar/completion.rb +++ b/test/samovar/completion.rb @@ -103,6 +103,12 @@ def complete(input, **options) expect(values(result)).to be == ["json"] end + it "continues completion after consuming option values" do + result = complete(["--configuration", "development", ""]) + + expect(values(result)).to be == ["--configuration", "-c", "--verbose", "-v", "leaf", "list"] + end + it "requests native path completion for option values" do result = complete(["leaf", "--output", "tmp/"]) suggestion = result.first @@ -175,6 +181,24 @@ def complete(input, **options) expect(values(result)).to be == ["--no-color"] end + it "uses default nested command for unmatched command words" do + result = complete(["unknown", ""]) + + expect(values(result)).to be == ["extra-a", "extra-b"] + end + + it "returns collected suggestions for unmatched nested commands without default" do + nested = Samovar::Nested.new(:command, {"list" => CompletionList}) + context = Samovar::Completion::Context.for(CompletionTop, ["unknown", ""]) + collected = [ + Samovar::Completion::Suggestion.new("--verbose", type: :option) + ] + + result = nested.complete(["unknown"], context, collected) + + expect(values(result)).to be == ["--verbose"] + end + it "prints completion results as TSV" do output = StringIO.new result = complete(["le"]) diff --git a/test/samovar/flag.rb b/test/samovar/flag.rb new file mode 100644 index 0000000..4943baa --- /dev/null +++ b/test/samovar/flag.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2026, by Samuel Williams. + +require "samovar/flags" + +describe Samovar::Flag do + let(:flag) {subject.new("--flag", "--flag", ["-f"])} + + it "can check alternatives" do + expect(flag.prefix?("-f")).to be == true + end +end diff --git a/test/samovar/flags.rb b/test/samovar/flags.rb index 80a0ecd..44d36c3 100644 --- a/test/samovar/flags.rb +++ b/test/samovar/flags.rb @@ -16,14 +16,10 @@ result = flags.parse(["--other"]) expect(result).to be_nil end -end - -describe Samovar::BooleanFlag do - let(:flag) {Samovar::Flag.parse("--[no]-color")} - it "can check prefix" do - expect(flag.prefix?("--color")).to be == true - expect(flag.prefix?("--no-color")).to be == true - expect(flag.prefix?("--other")).to be == false + it "matches flag alternatives" do + flag = flags.first + + expect(flag.prefix?("-f")).to be == true end end diff --git a/test/samovar/many.rb b/test/samovar/many.rb index b260e88..3a6068d 100644 --- a/test/samovar/many.rb +++ b/test/samovar/many.rb @@ -62,6 +62,16 @@ expect(many_no_stop.parse(all_input)).to be == ["1", "2", "3", "--also"] expect(all_input).to be(:empty?) end + + it "clears previous input before completing" do + many_no_stop = subject.new(:items, "all items", stop: nil, completions: ["one", "two"]) + context = Samovar::Completion::Context.for(Samovar::Command, ["previous", "t"]) + input = ["previous"] + + result = many_no_stop.complete(input, context, []) + + expect(input).to be(:empty?) + expect(result.collect(&:value)).to be == ["two"] + end end end - diff --git a/test/samovar/one.rb b/test/samovar/one.rb index ceb362f..c534e20 100644 --- a/test/samovar/one.rb +++ b/test/samovar/one.rb @@ -32,6 +32,15 @@ expect(input).to be == ["3", "4"] end + it "completes with no suggestions if input does not match" do + one = subject.new(:thing, "a thing", pattern: /^valid$/, completions: ["valid"]) + context = Samovar::Completion::Context.for(Samovar::Command, ["invalid", ""]) + + result = one.complete(["invalid"], context, []) + + expect(result).to be(:empty?) + end + with "required field" do let(:required_one) {subject.new(:thing, "a thing", required: true)} @@ -54,4 +63,3 @@ end end end - diff --git a/test/samovar/options.rb b/test/samovar/options.rb index fb2ba18..df2fd0c 100644 --- a/test/samovar/options.rb +++ b/test/samovar/options.rb @@ -20,6 +20,16 @@ expect(values).to be == {x: 2} end + it "lists option completions" do + expect(options.completions).to be == ["-x", "-y", "--symbol"] + end + + it "knows whether an option consumes a value" do + option = options.option_for("-x") + + expect(option).to be(:value?) + end + it "should resolve callable defaults" do count = 0 diff --git a/test/samovar/output/header.rb b/test/samovar/output/header.rb new file mode 100644 index 0000000..e1c4082 --- /dev/null +++ b/test/samovar/output/header.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2025, by Samuel Williams. + +require "samovar" +require "samovar/output/header" + +describe Samovar::Output::Header do + let(:command_class) do + Class.new(Samovar::Command) do + self.description = "Test command" + + options do + option "--help", "Show help" + end + end + end + + it "can align header" do + header = Samovar::Output::Header.new("test", command_class) + result = header.align(nil) + + expect(result).to be(:include?, "test") + end +end diff --git a/test/samovar/output.rb b/test/samovar/output/rows.rb similarity index 58% rename from test/samovar/output.rb rename to test/samovar/output/rows.rb index 83ebdd3..2e76df4 100644 --- a/test/samovar/output.rb +++ b/test/samovar/output/rows.rb @@ -5,7 +5,6 @@ require "samovar" require "samovar/output/rows" -require "samovar/output/header" describe Samovar::Output::Rows do let(:rows) {subject.new} @@ -25,22 +24,3 @@ expect(rows.last).not.to be_nil end end - -describe Samovar::Output::Header do - let(:command_class) do - Class.new(Samovar::Command) do - self.description = "Test command" - - options do - option "--help", "Show help" - end - end - end - - it "can align header" do - header = Samovar::Output::Header.new("test", command_class) - result = header.align(nil) - - expect(result).to be(:include?, "test") - end -end