/spec/support/shared/examples/options.rb

https://github.com/debbiemezyene/metasploit-framework · Ruby · 61 lines · 50 code · 10 blank · 1 comment · 6 complexity · 28f2636b4f4ad75bd8f0569218bc5cde MD5 · raw file

  1. # -*- coding:binary -*-
  2. shared_examples_for "an option" do |valid_values, invalid_values, type|
  3. subject do
  4. described_class.new("name")
  5. end
  6. let(:required) { described_class.new('name', [true, 'A description here'])}
  7. let(:optional) { described_class.new('name', [false, 'A description here'])}
  8. it "should return a type of #{type}" do
  9. subject.type.should == type
  10. end
  11. context 'when required' do
  12. it 'should not be valid for nil' do
  13. required.valid?(nil).should == false
  14. end
  15. end
  16. context 'when not required' do
  17. it 'it should be valid for nil' do
  18. optional.valid?(nil).should == true
  19. end
  20. end
  21. context "with valid values" do
  22. valid_values.each do |vhash|
  23. valid_value = vhash[:value]
  24. normalized_value = vhash[:normalized]
  25. it "should be valid and normalize appropriately: #{valid_value}" do
  26. block = Proc.new {
  27. subject.normalize(valid_value).should == normalized_value
  28. subject.valid?(valid_value).should be_true
  29. }
  30. if vhash[:pending]
  31. pending(vhash[:pending], &block)
  32. else
  33. block.call
  34. end
  35. end
  36. end
  37. end
  38. context "with invalid values" do
  39. invalid_values.each do |vhash|
  40. invalid_value = vhash[:value]
  41. it "should not be valid: #{invalid_value}" do
  42. block = Proc.new { subject.valid?(invalid_value).should be_false }
  43. if vhash[:pending]
  44. pending(vhash[:pending], &block)
  45. else
  46. block.call
  47. end
  48. end
  49. end
  50. end
  51. end