boolean and logging

Discussion in 'Ruby' started by testautomated6@googlemail.com, Mar 28, 2008.

  1. Guest

    Hi,

    I use Ruby and Watir

    I have a method:

    def does_the_html_include(target)
    puts "HTML text of #{target} is being verified"
    if target.kind_of? String
    return $browser.text.include?(target)
    elsif target.kind_of? Regexp
    return $browser.text.match(target)
    end
    end


    I wrap this up in a logger

    def test_results(result, *msg)
    #make true explicit, everything else I am seeing as a failure
    if result == true
    test_status = @test.add_element 'teststatus'
    test_status.text = 'PASS'
    else
    fail = @test.add_element 'teststatus'
    fail.text = 'FAIL'
    fail_msg = @test.add_element 'failmessage'
    fail_msg.text = msg
    end
    end


    However, when I don't want HTML text to appear, eg: an error. I want
    to do something better than this

    boolean = does_the_html_include('You appear to have an active
    account')
    $test.test_results(!boolean, 'already have registered account')

    Any ideas?

    Cheers

    Aidy
     
    , Mar 28, 2008
    #1
    1. Advertising

  2. On 28.03.2008 15:58, wrote:
    > Hi,
    >
    > I use Ruby and Watir
    >
    > I have a method:
    >
    > def does_the_html_include(target)
    > puts "HTML text of #{target} is being verified"
    > if target.kind_of? String
    > return $browser.text.include?(target)
    > elsif target.kind_of? Regexp
    > return $browser.text.match(target)
    > end
    > end
    >
    >
    > I wrap this up in a logger
    >
    > def test_results(result, *msg)
    > #make true explicit, everything else I am seeing as a failure
    > if result == true


    This is a very bad idea, as only false and nil are false. Your code
    above will especially break with the test because the regexp match will
    either return nil or a MatchData object - which both will make "result
    == true" false.

    > test_status = @test.add_element 'teststatus'
    > test_status.text = 'PASS'
    > else
    > fail = @test.add_element 'teststatus'
    > fail.text = 'FAIL'
    > fail_msg = @test.add_element 'failmessage'
    > fail_msg.text = msg
    > end
    > end
    >
    >
    > However, when I don't want HTML text to appear, eg: an error. I want
    > to do something better than this
    >
    > boolean = does_the_html_include('You appear to have an active
    > account')
    > $test.test_results(!boolean, 'already have registered account')


    I am not sure what exactly it is that you want to improve. Do you
    dislike the local variable?

    def test_results(msg)
    if yield
    # test ok
    else
    # not ok
    end
    end

    test_results "foo" { does_the_html_include('You appear to have an active
    account') }

    Cheers

    robert
     
    Robert Klemme, Mar 28, 2008
    #2
    1. Advertising

  3. Guest

    On Mar 28, 9:28 am, Robert Klemme <> wrote:
    > On 28.03.2008 15:58, wrote:
    >
    > > Hi,

    >
    > > I use Ruby and Watir

    >
    > > I have a method:

    >
    > > def does_the_html_include(target)
    > > puts "HTML text of #{target} is being verified"
    > > if target.kind_of? String
    > > return $browser.text.include?(target)
    > > elsif target.kind_of? Regexp
    > > return $browser.text.match(target)
    > > end
    > > end

    >
    > > I wrap this up in a logger

    >
    > > def test_results(result, *msg)
    > > #make true explicit, everything else I am seeing as a failure
    > > if result == true

    >
    > This is a very bad idea, as only false and nil are false. Your code
    > above will especially break with the test because the regexp match will
    > either return nil or a MatchData object - which both will make "result
    > == true" false.
    >


    Good point.

    >
    > > test_status = @test.add_element 'teststatus'
    > > test_status.text = 'PASS'
    > > else
    > > fail = @test.add_element 'teststatus'
    > > fail.text = 'FAIL'
    > > fail_msg = @test.add_element 'failmessage'
    > > fail_msg.text = msg
    > > end
    > > end

    >
    > > However, when I don't want HTML text to appear, eg: an error. I want
    > > to do something better than this

    >
    > > boolean = does_the_html_include('You appear to have an active
    > > account')
    > > $test.test_results(!boolean, 'already have registered account')

    >
    > I am not sure what exactly it is that you want to improve. Do you
    > dislike the local variable?


    Yes. I am writing two lines when I should be writing one.

    >
    > def test_results(msg)
    > if yield
    > # test ok
    > else
    > # not ok
    > end
    > end
    >
    > test_results "foo" { does_the_html_include('You appear to have an active
    > account') }
    >


    Could you give a brief explanation of this code please?

    Aidy
     
    , Mar 31, 2008
    #3
  4. 2008/3/31, <>:
    > On Mar 28, 9:28 am, Robert Klemme <> wrote:
    >
    > > On 28.03.2008 15:58, wrote:
    > >
    > > > Hi,

    > >
    > > > I use Ruby and Watir

    > >
    > > > I have a method:

    > >
    > > > def does_the_html_include(target)
    > > > puts "HTML text of #{target} is being verified"
    > > > if target.kind_of? String
    > > > return $browser.text.include?(target)
    > > > elsif target.kind_of? Regexp
    > > > return $browser.text.match(target)
    > > > end
    > > > end

    > >
    > > > I wrap this up in a logger

    > >
    > > > def test_results(result, *msg)
    > > > #make true explicit, everything else I am seeing as a failure
    > > > if result == true

    > >
    > > This is a very bad idea, as only false and nil are false. Your code
    > > above will especially break with the test because the regexp match will
    > > either return nil or a MatchData object - which both will make "result
    > > == true" false.
    > >

    >
    >
    > Good point.
    >
    >
    > >
    > > > test_status = @test.add_element 'teststatus'
    > > > test_status.text = 'PASS'
    > > > else
    > > > fail = @test.add_element 'teststatus'
    > > > fail.text = 'FAIL'
    > > > fail_msg = @test.add_element 'failmessage'
    > > > fail_msg.text = msg
    > > > end
    > > > end

    > >
    > > > However, when I don't want HTML text to appear, eg: an error. I want
    > > > to do something better than this

    > >
    > > > boolean = does_the_html_include('You appear to have an active
    > > > account')
    > > > $test.test_results(!boolean, 'already have registered account')

    > >
    > > I am not sure what exactly it is that you want to improve. Do you
    > > dislike the local variable?

    >
    > Yes. I am writing two lines when I should be writing one.


    Well, if that's all that bothers you - you can easily inline it:

    $test.test_results(!does_the_html_include('You..'), 'already have
    registered account')

    > > def test_results(msg)
    > > if yield
    > > # test ok
    > > else
    > > # not ok
    > > end
    > > end
    > >
    > > test_results "foo" { does_the_html_include('You appear to have an active
    > > account') }

    >
    > Could you give a brief explanation of this code please?


    The method invokes the block and uses the result to determine what to
    do (if or else).

    Does that help?

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Mar 31, 2008
    #4
  5. aidy Guest

    On 31 Mar, 16:56, Robert Klemme <> wrote:

    > The method invokes the block and uses the result to determine what to
    > do (if or else).
    >
    > Does that help?
    >

    Surely does. Thanks Robert, your a linguist.

    Aidy
     
    aidy, Apr 1, 2008
    #5
  6. 2008/4/1, aidy <>:
    > On 31 Mar, 16:56, Robert Klemme <> wrote:
    >
    > > The method invokes the block and uses the result to determine what to
    > > do (if or else).
    > >
    > > Does that help?

    >
    > Surely does. Thanks Robert, your a linguist.


    Hm, not sure whether that was irony or not. :)

    Maybe this helps:

    irb(main):001:0> def test(yes,no)
    irb(main):002:1> if yield
    irb(main):003:2> puts yes
    irb(main):004:2> else
    irb(main):005:2* puts no
    irb(main):006:2> end
    irb(main):007:1> end
    => nil
    irb(main):008:0> test("ok", "not ok") { 1 > 2 }
    not ok
    => nil
    irb(main):009:0> test("ok", "not ok") { 1 < 2 }
    ok
    => nil
    irb(main):010:0> test("ok", "not ok")
    LocalJumpError: no block given
    from (irb):2:in `test'
    from (irb):10
    from :0
    irb(main):011:0>

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Apr 1, 2008
    #6
  7. aidy Guest

    On 1 Apr, 14:23, Robert Klemme <> wrote:
    > 2008/4/1,aidy<>:
    >
    > > On 31 Mar, 16:56, Robert Klemme <> wrote:

    >
    > >  > The method invokes the block and uses the result to determine what to
    > >  > do (if or else).

    >
    > >  > Does that help?

    >
    > >  Surely does. Thanks Robert, your a linguist.

    >
    > Hm, not sure whether that was irony or not. :)
    >
    > Maybe this helps:
    >
    > irb(main):001:0> def test(yes,no)
    > irb(main):002:1> if yield
    > irb(main):003:2> puts yes
    > irb(main):004:2> else
    > irb(main):005:2* puts no
    > irb(main):006:2> end
    > irb(main):007:1> end
    > => nil
    > irb(main):008:0> test("ok", "not ok") { 1 > 2 }
    > not ok
    > => nil
    > irb(main):009:0> test("ok", "not ok") { 1 < 2 }
    > ok
    > => nil
    > irb(main):010:0> test("ok", "not ok")
    > LocalJumpError: no block given
    >         from (irb):2:in `test'
    >         from (irb):10
    >         from :0
    > irb(main):011:0>
    >
    > Kind regards
    >
    > robert
    >
    > --
    > use.inject do |as, often| as.you_can - without end


    Wasn't ironic Robert, I studied #yield and blocks when you mentioned
    them. It was exactly what I was looking for. Probably one of the most
    powerful assets of Ruby and I missed it.

    Cheers

    Aidy
     
    aidy, Apr 2, 2008
    #7
  8. 2008/4/2, aidy <>:
    > On 1 Apr, 14:23, Robert Klemme <> wrote:
    > > 2008/4/1,aidy<>:


    > > > Surely does. Thanks Robert, your a linguist.

    > >
    > > Hm, not sure whether that was irony or not. :)


    > Wasn't ironic Robert, I studied #yield and blocks when you mentioned
    > them. It was exactly what I was looking for. Probably one of the most
    > powerful assets of Ruby and I missed it.


    Ah! I'm glad I could help. And, yes, blocks (anonymous functions) are
    one of the best features in Ruby - totally agree.

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Apr 2, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christoph Haas
    Replies:
    0
    Views:
    468
    Christoph Haas
    Jun 12, 2006
  2. Christoph Haas
    Replies:
    1
    Views:
    469
    Vinay Sajip
    Jun 14, 2006
  3. johnny
    Replies:
    1
    Views:
    659
    Dennis Lee Bieber
    Dec 12, 2006
  4. J Leonard
    Replies:
    4
    Views:
    12,769
    Mark Space
    Jan 19, 2008
  5. Metre Meter
    Replies:
    7
    Views:
    407
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page