From 2e6372bd2e4e407ac9360d6e812c0377bcbef500 Mon Sep 17 00:00:00 2001 From: Ben Brady Date: Mon, 30 Nov 2015 16:03:35 -0800 Subject: [PATCH] Test Ask methods --- test/classes/checkbox_spec.rb | 124 ++++++++++++++++++---------------- test/classes/confirm_spec.rb | 89 +++++++++++++----------- test/classes/input_spec.rb | 29 ++++---- test/classes/list_spec.rb | 100 ++++++++++++++------------- 4 files changed, 183 insertions(+), 159 deletions(-) diff --git a/test/classes/checkbox_spec.rb b/test/classes/checkbox_spec.rb index e3d8bd9..80335a5 100644 --- a/test/classes/checkbox_spec.rb +++ b/test/classes/checkbox_spec.rb @@ -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 diff --git a/test/classes/confirm_spec.rb b/test/classes/confirm_spec.rb index 08dce68..4bfbc7f 100644 --- a/test/classes/confirm_spec.rb +++ b/test/classes/confirm_spec.rb @@ -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 diff --git a/test/classes/input_spec.rb b/test/classes/input_spec.rb index 262ceba..02e71ae 100644 --- a/test/classes/input_spec.rb +++ b/test/classes/input_spec.rb @@ -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 diff --git a/test/classes/list_spec.rb b/test/classes/list_spec.rb index 8772711..87fddff 100644 --- a/test/classes/list_spec.rb +++ b/test/classes/list_spec.rb @@ -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