PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/fleakr/api/option_test.rb

https://github.com/wakeless/fleakr
Ruby | 179 lines | 130 code | 49 blank | 0 comment | 33 complexity | fe6ae17ee01fff182f7fc2d7d2a0aaf0 MD5 | raw file
  1. require File.dirname(__FILE__) + '/../../../test_helper'
  2. module Fleakr::Api
  3. class OptionTest < Test::Unit::TestCase
  4. def self.should_know_the_class_for(type, options)
  5. should "know the class for the :#{type} type" do
  6. Option.class_for(type).should == options[:is]
  7. end
  8. end
  9. context "The Option class" do
  10. should_know_the_class_for :title, :is => Fleakr::Api::SimpleOption
  11. should_know_the_class_for :description, :is => Fleakr::Api::SimpleOption
  12. should_know_the_class_for :tags, :is => Fleakr::Api::TagOption
  13. should_know_the_class_for :viewable_by, :is => Fleakr::Api::ViewOption
  14. should_know_the_class_for :level, :is => Fleakr::Api::LevelOption
  15. should_know_the_class_for :type, :is => Fleakr::Api::TypeOption
  16. should_know_the_class_for :hide?, :is => Fleakr::Api::HiddenOption
  17. should "be able to create an option for a type" do
  18. option = stub()
  19. Option.expects(:class_for).with(:title).returns(Fleakr::Api::SimpleOption)
  20. Fleakr::Api::SimpleOption.expects(:new).with(:title, 'blip').returns(option)
  21. Option.for(:title, 'blip').should == option
  22. end
  23. end
  24. end
  25. class SimpleOptionTest < Test::Unit::TestCase
  26. context "An instance of the SimpleOption class" do
  27. should "have a type" do
  28. so = SimpleOption.new(:title, 'blip')
  29. so.type.should == :title
  30. end
  31. should "have a value" do
  32. so = SimpleOption.new(:title, 'blip')
  33. so.value.should == 'blip'
  34. end
  35. should "be able to generate a hash representation of itself" do
  36. so = SimpleOption.new(:title, 'blip')
  37. so.to_hash.should == {:title => 'blip'}
  38. end
  39. end
  40. end
  41. class TagOptionTest < Test::Unit::TestCase
  42. context "An instance of the TagOption class" do
  43. should "normalize the input value to an array" do
  44. to = TagOption.new(:tags, 'blip')
  45. to.value.should == ['blip']
  46. end
  47. should "be able to generate a hash representation of itself with tags joined on spaces" do
  48. to = TagOption.new(:tags, %w(bop bip))
  49. to.to_hash.should == {:tags => '"bop" "bip"'}
  50. end
  51. should "quote tag values with spaces" do
  52. to = TagOption.new(:tags, ['tag', 'one with spaces'])
  53. to.to_hash.should == {:tags => '"tag" "one with spaces"'}
  54. end
  55. end
  56. end
  57. class ViewOptionTest < Test::Unit::TestCase
  58. context "An instance of the ViewOption class" do
  59. should "be able to generate a hash representation for viewing by :everyone" do
  60. vo = ViewOption.new(:viewable_by, :everyone)
  61. vo.to_hash.should == {:is_public => 1, :is_family => 0, :is_friend => 0}
  62. end
  63. should "be able to generate a hash representation for viewing by :family" do
  64. vo = ViewOption.new(:viewable_by, :family)
  65. vo.to_hash.should == {:is_public => 0, :is_family => 1, :is_friend => 0}
  66. end
  67. should "be able to generate a hash representation for viewing by :friends" do
  68. vo = ViewOption.new(:viewable_by, :friends)
  69. vo.to_hash.should == {:is_public => 0, :is_family => 0, :is_friend => 1}
  70. end
  71. should "know the visibility is public if value is set to :everyone" do
  72. vo = ViewOption.new(:viewable_by, :everyone)
  73. vo.public?.should be(true)
  74. end
  75. should "know the visibility is not public if :everyone is not the only value" do
  76. vo = ViewOption.new(:viewable_by, [:everyone, :family])
  77. vo.public?.should be(false)
  78. end
  79. should "know that its visible to friends and family if specified as such" do
  80. vo = ViewOption.new(:viewable_by, [:friends, :family])
  81. vo.friends?.should be(true)
  82. vo.family?.should be(true)
  83. end
  84. end
  85. end
  86. class LevelOptionTest < Test::Unit::TestCase
  87. context "An instance of the LevelOption class" do
  88. should "be able to generate a hash representation for the :safe level" do
  89. lo = LevelOption.new(:level, :safe)
  90. lo.to_hash.should == {:safety_level => 1}
  91. end
  92. should "be able to generate a hash representation for the :moderate level" do
  93. lo = LevelOption.new(:level, :moderate)
  94. lo.to_hash.should == {:safety_level => 2}
  95. end
  96. should "be able to generate a hash representation for the :restricted level" do
  97. lo = LevelOption.new(:level, :restricted)
  98. lo.to_hash.should == {:safety_level => 3}
  99. end
  100. end
  101. end
  102. class TypeOptionTest < Test::Unit::TestCase
  103. context "An instance of the TypeOption class" do
  104. should "be able to generate a hash representation for the :photo type" do
  105. to = TypeOption.new(:type, :photo)
  106. to.to_hash.should == {:content_type => 1}
  107. end
  108. should "be able to generate a hash representation for the :screenshot type" do
  109. to = TypeOption.new(:type, :screenshot)
  110. to.to_hash.should == {:content_type => 2}
  111. end
  112. should "be able to generate a hash representation for the :other type" do
  113. to = TypeOption.new(:type, :other)
  114. to.to_hash.should == {:content_type => 3}
  115. end
  116. end
  117. end
  118. class HiddenOptionTest < Test::Unit::TestCase
  119. context "An instance of the HiddenOption class" do
  120. should "be able to generate a hash representation when set to true" do
  121. ho = HiddenOption.new(:hide?, true)
  122. ho.to_hash.should == {:hidden => 2}
  123. end
  124. should "be able to generate a hash representation when set to false" do
  125. ho = HiddenOption.new(:hide?, false)
  126. ho.to_hash.should == {:hidden => 1}
  127. end
  128. end
  129. end
  130. end