diff --git a/operators/ltnp_operator.cr b/operators/ltnp_operator.cr index 88cada8..41f69bf 100644 --- a/operators/ltnp_operator.cr +++ b/operators/ltnp_operator.cr @@ -22,8 +22,8 @@ class LtnpOperator @ltnp_records.find { |r| r.port == port } end - def search_p_name(p_name : String) : LtnpRecord | Nil - @ltnp_records.find { |r| r.p_name == p_name } + def search_p_name(p_name : String) : Array(LtnpRecord) + @ltnp_records.select { |r| r.p_name.includes?(p_name) } end # Actions @@ -43,12 +43,12 @@ class LtnpOperator # -p, --find-process def find_process(p_name : String) - record = search_p_name p_name + records = search_p_name p_name - unless record.nil? - result = record.output_s + unless records.empty? + result = records.map { |record| record.output_s }.join("\n") else - result = "No process found by the name %s" % p_name + result = "No processes found that match the name %s" % p_name end result diff --git a/spec/operators/ltnp_operator_spec.cr b/spec/operators/ltnp_operator_spec.cr index c26cd7d..6139dd8 100644 --- a/spec/operators/ltnp_operator_spec.cr +++ b/spec/operators/ltnp_operator_spec.cr @@ -8,6 +8,8 @@ describe LtnpOperator do nonsense_port = "1234" existing_p_name = "polymer" + partial_existing_p_name = "poly" + multiple_exist_p_name = "spotify" nonsense_p_name = "thisisnotathing" describe "#get_port_record" do @@ -22,11 +24,11 @@ describe LtnpOperator do describe "#search_p_name" do it "should find record when process exists" do - sut.search_p_name(existing_p_name).nil?.should be_false + sut.search_p_name(existing_p_name).empty?.should be_false end it "should not find record when process doesn't exist" do - sut.search_p_name(nonsense_p_name).nil?.should be_true + sut.search_p_name(nonsense_p_name).empty?.should be_true end end @@ -41,10 +43,18 @@ describe LtnpOperator do end describe "#find_process" do - it "should output successful message if found" do + it "should output successful message if full match found" do sut.find_process(existing_p_name).should contain("Process:") end + it "should output successful message if partial match found" do + sut.find_process(partial_existing_p_name).should contain("Process:") + end + + it "should output every found process" do + sut.find_process(multiple_exist_p_name).each_line.size.should eq 2 + end + it "should output failure message if not found" do sut.find_process(nonsense_p_name).should contain("No") end