Changeset 234

Show
Ignore:
Timestamp:
08/31/08 22:10:43 (4 months ago)
Author:
pwh
Message:
  • application_helper_test.rb: Added TODOs for link_to_controller,
    device_information_box, and wrote tests for ipv4_network_name,
    generate_title_line_1, generate_title_line_2, generate_title_tag,
    test_display_snmptable_value, test_display_yn,
    test_format_interface_name, test_format_l2_interface,
    test_format_l2_interface_type, format_l3_interface and format_site.
  • application_helper.rb: Refactored code based on 'heckle' mutation tests.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nethorus/app/helpers/application_helper.rb

    r220 r234  
    4444  def format_l2_interface(address) 
    4545 
    46     if address.blank? 
     46    address.blank? ? "none" : address 
     47 
     48  end 
     49 
     50 
     51  # Format a Layer 3 interface address list 
     52 
     53  def format_l3_interface(l2_interface) 
     54 
     55 
     56    # Check we've really been passed an L2Interface object 
     57 
     58    raise "Object passed was not an L2Interface" unless l2_interface.is_a? L2Interface 
     59     
     60 
     61    l3_interfaces = l2_interface.l3_interfaces 
     62 
     63    if l3_interfaces.empty? 
    4764      "none" 
    4865    else 
    49       address 
    50     end 
    51  
    52   end 
    53  
    54  
    55   # Format a Layer 3 interface address list 
    56  
    57   def format_l3_interface(addresses) 
    58  
    59     if addresses.blank? 
    60       "none" 
    61     else 
    62       addresses.collect { |l3| l3.ipv4_address + " (" + ipv4_network_name(l3.ipv4_address) + ")" }.join(", ") 
     66      l3_interfaces.collect { |l3| l3.ipv4_address + " (" + ipv4_network_name(l3.ipv4_address) + ")" }.join(", ") 
    6367    end 
    6468 
     
    7175  def format_interface_name(interface_name, description) 
    7276 
    73     if description.blank? 
    74       interface_name 
    75     else 
    76       interface_name << "(" << description << ")" 
     77    if interface_name.blank? 
     78      return "" 
     79    elsif description.blank? 
     80      return interface_name 
     81    else 
     82      return interface_name << " (" << description << ")" 
    7783    end 
    7884 
     
    8591  def format_site(site) 
    8692 
    87     if site.nil? 
    88       "Unknown" 
    89     els
    90       site.description 
    91     end 
     93    # Check we've really been passed an Site object 
     94         
     95    raise "Object passed was not an Site" unless site.is_a? Sit
     96 
     97    site.description.nil? || site.description.blank?  ? "Unknown" : site.description 
    9298 
    9399  end 
     
    99105  def format_l2_interface_type(iftype) 
    100106 
    101     if iftype.nil? 
    102       "Unknown" 
    103     else 
    104       iftype 
    105     end 
     107    iftype.blank? ? "Unknown" : iftype 
    106108 
    107109  end 
     
    157159      @subtitle 
    158160 
    159     elsif params[:action] == "index" || params[:action].match("_"
     161    elsif params && (params[:action] == "index" || params[:action].match("_")
    160162 
    161163      "#{params[:controller].capitalize}" 
     
    174176  def device_information_box(device) 
    175177 
    176     if device.device_type 
    177       image = "/images/device_types/small/" + device.device_type + ".png" 
    178     end 
    179  
    180     render :partial => "common/device_information_box", :locals => { :device => device, :image => image } 
     178    render :partial => "common/device_information_box", :locals => { :device => device } 
    181179 
    182180  end 
     
    187185  def display_snmptable_value(table, index, value) 
    188186 
    189     if !table || table[index].nil? 
     187    if !table.nil? && table.has_key?(index) && table[index].has_key?(value) 
     188      return table[index][value] 
     189    else 
    190190      return "?" 
    191     else 
    192       return table[index][value] 
    193191    end 
    194192 
     
    201199  def display_yn(value) 
    202200 
    203     if value.nil? 
    204       return "No" 
    205     end 
    206  
    207     if value == 1 || value.true? || !value.blank? || !value.nil? 
     201    if value == :true 
    208202      return "Yes" 
    209203    else 
  • nethorus/test/unit/application_helper_test.rb

    r216 r234  
    3232  include ApplicationHelper 
    3333 
     34  fixtures :devices, :ipv4_networks, :l2_interfaces, :l3_interfaces 
     35 
     36 
     37  # Test the link_to_controller function 
     38   
     39  def test_link_to_controller 
     40 
     41    # TODO: Write tests for link_to_controller 
     42   
     43  end 
     44 
     45 
     46  # Test the device_information_box function 
     47   
     48  def test_device_information_box 
     49   
     50    # TODO: Write tests for device_information_box 
     51   
     52  end 
     53 
     54 
     55  # Test the ipv4_network_name function 
     56   
     57  def test_ipv4_network_name 
     58   
     59    assert(ipv4_network_name("10.0.0.0") == "RFC1918 network 10.0.0.0/8") 
     60    assert(ipv4_network_name(nil) == "Unknown") 
     61    assert(ipv4_network_name("255.255.255.255") == "Unknown") 
     62   
     63  end 
     64   
     65   
     66  # Test the generate_title_line_1 function 
     67   
     68  def test_generate_title_line_1 
     69 
     70    assert_equal "NetHorus Test", generate_title_line_1 
     71     
     72  end 
     73 
     74 
     75  # Test the generate_title_line_2 function 
     76   
     77  def test_generate_title_line_2 
     78 
     79    # TODO: Write a test for @subtitle = nil 
     80 
     81    @subtitle = "foobar" 
     82    assert_equal "foobar", generate_title_line_2 
     83     
     84    @subtitle = "" 
     85    assert_equal "", generate_title_line_2 
     86 
     87  end 
     88 
     89 
     90  # Test the generate_title_tag function 
     91   
     92  def test_generate_title_tag 
     93 
     94    @subtitle = "foobar" 
     95     
     96    assert_equal "NetHorus Test: foobar", generate_title_tag 
     97   
     98  end 
     99 
     100 
     101  # Test the display_snmptable_value function 
     102   
     103  def test_display_snmptable_value 
     104   
     105    table = Hash.new 
     106    table_data = Hash.new 
     107    table_data[1] = "1:1" 
     108    table[1] = table_data 
     109 
     110    assert(display_snmptable_value(table, 1, 1) == "1:1") 
     111    assert(display_snmptable_value(table, 1, 2) == "?") 
     112    assert(display_snmptable_value(table, 2, 2) == "?") 
     113     
     114    assert(display_snmptable_value(table, 1, nil) == "?") 
     115    assert(display_snmptable_value(table, nil, 1) == "?") 
     116    assert(display_snmptable_value(table, nil, nil) == "?") 
     117 
     118    assert(display_snmptable_value(nil, 1, 1) == "?") 
     119    assert(display_snmptable_value(nil, nil, 1) == "?") 
     120    assert(display_snmptable_value(nil, 1, nil) == "?") 
     121    assert(display_snmptable_value(nil, nil, nil) == "?") 
     122   
     123  end 
     124 
     125 
     126  # Test the display_yn function 
     127 
     128  def test_display_yn 
     129   
     130    assert(display_yn(nil) == "No") 
     131    assert(display_yn("") == "No") 
     132    assert(display_yn("foobar") == "No") 
     133    assert(display_yn(:false) == "No") 
     134 
     135    assert(display_yn(:true) == "Yes") 
     136   
     137  end 
     138 
     139 
     140  # Test the format_interface_name function 
     141   
     142  def test_format_interface_name 
     143 
     144    assert(format_interface_name("Foo", "Bar") == "Foo (Bar)") 
     145    assert(format_interface_name("Foo", "") == "Foo") 
     146     
     147    assert(format_interface_name("", "Bar") == "") 
     148    assert(format_interface_name("", "") == "") 
     149 
     150    assert(format_interface_name("Foo", nil) == "Foo") 
     151    assert(format_interface_name(nil, "Bar") == "") 
     152 
     153    assert(format_interface_name(nil, nil) == "") 
     154 
     155  end 
     156 
     157 
     158  # Test the format_l2_interface function 
     159   
     160  def test_format_l2_interface 
     161   
     162    assert(format_l2_interface("something") == "something") 
     163    assert(format_l2_interface("") == "none") 
     164    assert(format_l2_interface(nil) == "none") 
     165   
     166  end 
     167   
     168   
     169  # Test the format_l2_interface_type function 
     170   
     171  def test_format_l2_interface_type 
     172   
     173    assert(format_l2_interface_type("something") == "something") 
     174    assert(format_l2_interface_type("") == "Unknown") 
     175    assert(format_l2_interface_type(nil) == "Unknown") 
     176   
     177  end 
     178 
     179 
     180  # Test the format_l3_interface function 
     181   
     182  def test_format_l3_interface 
     183   
     184    example_interface = l2_interfaces(:test_l2interface_1) 
     185    assert(format_l3_interface(example_interface) == "10.0.0.1 (RFC1918 network 10.0.0.0/8)") 
     186 
     187    example_interface_without_children = l2_interfaces(:l2_interface_without_any_l3_interfaces) 
     188    assert(format_l3_interface(example_interface_without_children) == "none") 
     189   
     190  end 
     191 
    34192 
    35193  # Test the variable_or_unavailable function 
     
    59217  end 
    60218 
     219 
     220  # Test the format_site function 
     221   
     222  def test_format_site 
     223   
     224    site = Site.new 
     225    assert(format_site(site) == "Unknown") 
     226     
     227    site.description = "" 
     228    assert(format_site(site) == "Unknown") 
     229 
     230    site.description = "Foobar" 
     231    assert(format_site(site) == "Foobar") 
     232   
     233  end 
     234 
    61235end