Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/samovar/boolean_flag.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions test/samovar/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down
14 changes: 14 additions & 0 deletions test/samovar/flag.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 4 additions & 8 deletions test/samovar/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 11 additions & 1 deletion test/samovar/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

10 changes: 9 additions & 1 deletion test/samovar/one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)}

Expand All @@ -54,4 +63,3 @@
end
end
end

10 changes: 10 additions & 0 deletions test/samovar/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions test/samovar/output/header.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 0 additions & 20 deletions test/samovar/output.rb → test/samovar/output/rows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

require "samovar"
require "samovar/output/rows"
require "samovar/output/header"

describe Samovar::Output::Rows do
let(:rows) {subject.new}
Expand All @@ -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
Loading