[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Testing a written file?
From:       Robert Klemme <shortcutter () googlemail ! com>
Date:       2017-04-16 7:01:09
Message-ID: CAM9pMnM6az6i31T5zq=hXvLx7OupKH8c75w_z2g-CnsxQRMYVQ () mail ! gmail ! com
[Download RAW message or body]

On Fri, Apr 14, 2017 at 3:16 PM, leam hall <leamhall@gmail.com> wrote:
> This is the meat of a test I'm writing. The end state is that the test will
> read a file another method has written. The goal is to test the writing
> method. is there a better way? Should testing the nameserver IP addresses go
> in a different test?
>
> Yes, I'm probably doing a lot more testing than necessary. Right now one of
> my big goals is to get better at testing and understanding how and what to
> test.
>
> Thanks!
>
> Leam
>
> ####
>   def test_resolver_file_strings
>     resolver_starts = ["search", "domain", "nameserver", ";", "#"]
>     resolver_file = File.open("data/test_resolv.conf", "r")
>     resolver_file.each_line do |line|
>       line_start = line.split()[0]
>       assert(resolver_starts.include?(line_start))
>     end
>     resolver_file.close
>   end

There are a few Rubyism that would make the code more robust and rubyish:

  def test_resolver_file_strings
    File.foreach "data/test_resolv.conf" do |line|
      assert(/^(?:search|domain|nameserver|;|#)\s/ =~ line)
    end
  end

:-)

This represents the exact same test but I believe you do not need to
require a character after ";" and "#". Then the test can be changed
slightly

      assert(/^(?:(?:search|domain|nameserver)\s|;|#)/ =~ line)

You would probably want to have more tests, e.g.

    File.foreach "data/test_resolv.conf" do |line|
      case line
      when /^search\s/
        # test remainder of the line
      when /^domain\s/
        # test remainder of the line
...
      else
        assert nil
      end
    end

Kind regards

robert

-- 
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic