Skip to content

Commit

Permalink
Test Ask methods
Browse files Browse the repository at this point in the history
  • Loading branch information
brbrady committed Dec 1, 2015
1 parent 570d671 commit 2e6372b
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 159 deletions.
124 changes: 65 additions & 59 deletions test/classes/checkbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,79 @@
IOHelper.keys = nil
end

it "finishes rendering with a clear" do
Checkbox.ask "select", ["one","two","three"], response: false
IOHelper.output.must_equal ""
end
[
Checkbox.method(:ask),
Ask.method(:checkbox)
].each do |meth|

it "doesn't render the dialog with 0 items" do
Checkbox.ask "select", [], clear: false, response: false
IOHelper.output.must_equal ""
end
it "finishes rendering with a clear" do
meth.call "select", ["one","two","three"], response: false
IOHelper.output.must_equal ""
end

it "renders the dialog with 3 items" do
Checkbox.ask "select", ["one","two","three"], clear: false, response: false
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m⬡ one\n ⬡ two\n ⬡ three\n"
end
it "doesn't render the dialog with 0 items" do
meth.call "select", [], clear: false, response: false
IOHelper.output.must_equal ""
end

it "renders the dialog with 3 items with defaults" do
Checkbox.ask "select", ["one","two","three"], default: [true, false, true], clear: false, response: false
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m\e[36m⬢\e[0m one\n ⬡ two\n \e[36m⬢\e[0m three\n"
end
it "renders the dialog with 3 items" do
meth.call "select", ["one","two","three"], clear: false, response: false
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m one\n ⬡ two\n three\n"
end

it "it finishes selection on pressing enter" do
IOHelper.keys = "enter"
Checkbox.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal [false,false,false]
end
it "renders the dialog with 3 items with defaults" do
meth.call "select", ["one","two","three"], default: [true, false, true], clear: false, response: false
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m\e[36m⬢\e[0m one\n ⬡ two\n \e[36m⬢\e[0m three\n"
end

it "it finishes selection on pressing enter" do
IOHelper.keys = "enter"
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal [false,false,false]
end

it "it finishes selection on pressing enter with defaults" do
IOHelper.keys = "enter"
Checkbox.ask( "select", ["one","two","three"], default: [true, false, true], clear: false, response: false
).must_equal [true,false,true]
end
IOHelper.keys = "enter"
meth.call( "select", ["one","two","three"], default: [true, false, true], clear: false, response: false
).must_equal [true,false,true]
end

it "selects and renders other items correctly (press down, press up, space, cycle)" do
IOHelper.keys = ["down","space","enter"]
Checkbox.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal [false,true,false]
IOHelper.output.must_equal "select:\n ⬡ one\n\e[36m‣\e[0m\e[36m⬢\e[0m two\n ⬡ three\n"

IOHelper.keys = ["space","down","space","enter"]
Checkbox.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal [true,true,false]
IOHelper.output.must_equal "select:\n \e[36m⬢\e[0m one\n\e[36m‣\e[0m\e[36m⬢\e[0m two\n ⬡ three\n"

IOHelper.keys = ["space","down","space","down","space","enter"]
Checkbox.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal [true,true,true]
IOHelper.output.must_equal "select:\n \e[36m⬢\e[0m one\n \e[36m⬢\e[0m two\n\e[36m‣\e[0m\e[36m⬢\e[0m three\n"

IOHelper.keys = ["down","down","down","space","enter"]
Checkbox.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal [true,false,false]
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m\e[36m⬢\e[0m one\n ⬡ two\n ⬡ three\n"

IOHelper.keys = ["up","space","enter"]
Checkbox.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal [false,false,true]
IOHelper.output.must_equal "select:\n ⬡ one\n ⬡ two\n\e[36m‣\e[0m\e[36m⬢\e[0m three\n"
end
it "selects and renders other items correctly (press down, press up, space, cycle)" do
IOHelper.keys = ["down","space","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal [false,true,false]
IOHelper.output.must_equal "select:\n ⬡ one\n\e[36m‣\e[0m\e[36m⬢\e[0m two\n ⬡ three\n"

it "selects and renders response correctly" do
IOHelper.keys = ["down","space","enter"]
Checkbox.ask( "select", ["one","two","three"])
IOHelper.output.must_equal "select: \e[36mtwo\e[0m\n"
IOHelper.keys = ["space","down","space","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal [true,true,false]
IOHelper.output.must_equal "select:\n \e[36m⬢\e[0m one\n\e[36m‣\e[0m\e[36m⬢\e[0m two\n ⬡ three\n"

IOHelper.keys = ["space","down","space","enter"]
Checkbox.ask( "select", ["one","two","three"])
IOHelper.output.must_equal "select: \e[36mone, two\e[0m\n"
end
IOHelper.keys = ["space","down","space","down","space","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal [true,true,true]
IOHelper.output.must_equal "select:\n \e[36m⬢\e[0m one\n \e[36m⬢\e[0m two\n\e[36m‣\e[0m\e[36m⬢\e[0m three\n"

IOHelper.keys = ["down","down","down","space","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal [true,false,false]
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m\e[36m⬢\e[0m one\n ⬡ two\n ⬡ three\n"

IOHelper.keys = ["up","space","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal [false,false,true]
IOHelper.output.must_equal "select:\n ⬡ one\n ⬡ two\n\e[36m‣\e[0m\e[36m⬢\e[0m three\n"
end

it "selects and renders response correctly" do
IOHelper.keys = ["down","space","enter"]
meth.call( "select", ["one","two","three"])
IOHelper.output.must_equal "select: \e[36mtwo\e[0m\n"

IOHelper.keys = ["space","down","space","enter"]
meth.call( "select", ["one","two","three"])
IOHelper.output.must_equal "select: \e[36mone, two\e[0m\n"
end

end
end
89 changes: 50 additions & 39 deletions test/classes/confirm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,56 @@
IOHelper.output = ""
end

it "should return true for y and Y and enter when default" do
IOHelper.keys = "y"
Confirm.ask("Are you sure?").must_equal true
IOHelper.keys = "Y"
Confirm.ask("Are you sure?").must_equal true
IOHelper.keys = "\r"
Confirm.ask("Are you sure?", default: true).must_equal true
end

it "should return false for n and N and enter when default" do
IOHelper.keys = "n"
Confirm.ask("Are you sure?").must_equal false
IOHelper.keys = "N"
Confirm.ask("Are you sure?").must_equal false
IOHelper.keys = "\r"
Confirm.ask("Are you sure?", default: false).must_equal false
end
[
Confirm.method(:ask),
Ask.method(:confirm)
].each do |meth|

it "should return true for y and Y and enter when default" do
IOHelper.keys = "y"
meth.call("Are you sure?").must_equal true
IOHelper.keys = "Y"
meth.call("Are you sure?").must_equal true
IOHelper.keys = "\r"
meth.call("Are you sure?", default: true).must_equal true
end

it "should return false for n and N and enter when default" do
IOHelper.keys = "n"
meth.call("Are you sure?").must_equal false
IOHelper.keys = "N"
meth.call("Are you sure?").must_equal false
IOHelper.keys = "\r"
meth.call("Are you sure?", default: false).must_equal false
end

it "should return true if not default given" do
IOHelper.keys = "\r"
meth.call("Are you sure?").must_equal true
end

it "accepts and renders response correctly" do
IOHelper.keys = "n"
meth.call("Are you sure?")
IOHelper.output.must_equal "Are you sure?: \e[36mNo\e[0m\n"

IOHelper.keys = "N"
meth.call("Are you sure?")
IOHelper.output.must_equal "Are you sure?: \e[36mNo\e[0m\n"

IOHelper.keys = "y"
meth.call("Are you sure?")
IOHelper.output.must_equal "Are you sure?: \e[36mYes\e[0m\n"

IOHelper.keys = "y"
meth.call("Are you sure?")
IOHelper.output.must_equal "Are you sure?: \e[36mYes\e[0m\n"
end

it "should return default value if given and there is no input" do
IOHelper.keys = "\r"
meth.call("Are you sure?", default: "I'm default").must_equal "I'm default"
end

it "should return true if not default given" do
IOHelper.keys = "\r"
Confirm.ask("Are you sure?").must_equal true
end

it "accepts and renders response correctly" do
IOHelper.keys = "n"
Confirm.ask("Are you sure?")
IOHelper.output.must_equal "Are you sure?: \e[36mNo\e[0m\n"

IOHelper.keys = "N"
Confirm.ask("Are you sure?")
IOHelper.output.must_equal "Are you sure?: \e[36mNo\e[0m\n"

IOHelper.keys = "y"
Confirm.ask("Are you sure?")
IOHelper.output.must_equal "Are you sure?: \e[36mYes\e[0m\n"

IOHelper.keys = "y"
Confirm.ask("Are you sure?")
IOHelper.output.must_equal "Are you sure?: \e[36mYes\e[0m\n"
end

end
29 changes: 15 additions & 14 deletions test/classes/input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
IOHelper.keys = ['t','y','p','e','d',' ','i','n','p','u','t',"\r"]
end

it "should use chars value from the user" do
Input.ask("please type input").must_equal "typed input"
end
[
Input.method(:ask),
Ask.method(:input)
].each do |meth|

it "accepts and renders response correctly" do
Input.ask("please type input")
IOHelper.output.must_equal "please type input: \e[36mtyped input\e[0m\n"
end
it "should use chars value from the user" do
meth.call("please type input").must_equal "typed input"
end

it "should return default value if given and there is no input" do
IOHelper.keys = "\r"
Confirm.ask("Are you sure?", default: "I'm default").must_equal "I'm default"
end
it "accepts and renders response correctly" do
meth.call("please type input")
IOHelper.output.must_equal "please type input: \e[36mtyped input\e[0m\n"
end

it "should provide a password input without displaying the value" do
Input.ask("please type password", password: true)
IOHelper.output.must_equal "please type password: \e[36m***********\e[0m\n"
it "should provide a password input without displaying the value" do
meth.call("please type password", password: true)
IOHelper.output.must_equal "please type password: \e[36m***********\e[0m\n"
end
end

end
100 changes: 53 additions & 47 deletions test/classes/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,68 @@
IOHelper.keys = nil
end

it "finishes rendering with a clear" do
List.ask "select", ["one","two","three"], response: false
IOHelper.output.must_equal ""
end
[
List.method(:ask),
Ask.method(:list)
].each do |meth|

it "doesn't render the dialog with 0 items" do
List.ask "select", [], clear: false, response: false
IOHelper.output.must_equal ""
end
it "finishes rendering with a clear" do
meth.call "select", ["one","two","three"], response: false
IOHelper.output.must_equal ""
end

it "renders the dialog with 3 items" do
List.ask "select", ["one","two","three"], clear: false, response: false
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m \e[36mone\e[0m\n two\n three\n"
end
it "doesn't render the dialog with 0 items" do
meth.call "select", [], clear: false, response: false
IOHelper.output.must_equal ""
end

it "it finishes selection on pressing enter" do
IOHelper.keys = "enter"
List.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal 0
end
it "renders the dialog with 3 items" do
meth.call "select", ["one","two","three"], clear: false, response: false
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m \e[36mone\e[0m\n two\n three\n"
end

it "selects and renders other items correctly (press down, press up, cycle)" do
IOHelper.keys = ["down","enter"]
List.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal 1
IOHelper.output.must_equal "select:\n one\n\e[36m‣\e[0m \e[36mtwo\e[0m\n three\n"
it "it finishes selection on pressing enter" do
IOHelper.keys = "enter"
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal 0
end

IOHelper.keys = ["down","down","enter"]
List.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal 2
IOHelper.output.must_equal "select:\n one\n two\n\e[36m‣\e[0m \e[36mthree\e[0m\n"
it "selects and renders other items correctly (press down, press up, cycle)" do
IOHelper.keys = ["down","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal 1
IOHelper.output.must_equal "select:\n one\n\e[36m‣\e[0m \e[36mtwo\e[0m\n three\n"

IOHelper.keys = ["down","down","down","enter"]
List.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal 0
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m \e[36mone\e[0m\n two\n three\n"
IOHelper.keys = ["down","down","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal 2
IOHelper.output.must_equal "select:\n one\n two\n\e[36m‣\e[0m \e[36mthree\e[0m\n"

IOHelper.keys = ["down","up","enter"]
List.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal 0
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m \e[36mone\e[0m\n two\n three\n"
IOHelper.keys = ["down","down","down","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal 0
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m \e[36mone\e[0m\n two\n three\n"

IOHelper.keys = ["up","enter"]
List.ask( "select", ["one","two","three"], clear: false, response: false
).must_equal 2
IOHelper.output.must_equal "select:\n one\n two\n\e[36m‣\e[0m \e[36mthree\e[0m\n"
end
IOHelper.keys = ["down","up","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal 0
IOHelper.output.must_equal "select:\n\e[36m‣\e[0m \e[36mone\e[0m\n two\n three\n"

it "selects and renders response correctly" do
IOHelper.keys = ["down","enter"]
List.ask( "select", ["one","two","three"])
IOHelper.output.must_equal "select: \e[36mtwo\e[0m\n"
IOHelper.keys = ["up","enter"]
meth.call( "select", ["one","two","three"], clear: false, response: false
).must_equal 2
IOHelper.output.must_equal "select:\n one\n two\n\e[36m‣\e[0m \e[36mthree\e[0m\n"
end

IOHelper.keys = ["down","down","enter"]
List.ask( "select", ["one","two","three"])
IOHelper.output.must_equal "select: \e[36mthree\e[0m\n"
end
it "selects and renders response correctly" do
IOHelper.keys = ["down","enter"]
meth.call( "select", ["one","two","three"])
IOHelper.output.must_equal "select: \e[36mtwo\e[0m\n"

IOHelper.keys = ["down","down","enter"]
meth.call( "select", ["one","two","three"])
IOHelper.output.must_equal "select: \e[36mthree\e[0m\n"
end

end
end

0 comments on commit 2e6372b

Please sign in to comment.