PageRenderTime 38ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/test/handset_test.rb

https://github.com/bluecat76/wurfl
Ruby | 110 lines | 85 code | 25 blank | 0 comment | 2 complexity | 44db5cec0fb12a7f3ee5574f481e33b0 MD5 | raw file
  1. require File.join(File.dirname(__FILE__), 'test_helper')
  2. require 'wurfl/handset'
  3. class HandsetTest < Test::Unit::TestCase
  4. def setup
  5. @fallback = Wurfl::Handset.new("fallback_id", "f", nil)
  6. @handset = Wurfl::Handset.new("handset_id", "h", @fallback)
  7. end
  8. should("not be equal to nil") { assert_not_equal @handset, nil }
  9. should("not be equal to 1") { assert_not_equal @handset, 1 }
  10. should("not be equal to fallback") { assert_not_equal @handset, @fallback }
  11. should("equal self") { assert_equal @handset, @handset }
  12. should("not have differences") { assert @handset.differences(@fallback).empty? }
  13. context "key not set" do
  14. should("not have value") { assert_nil @handset["k"] }
  15. should("not have owner") { assert_equal nil, @handset.owner("k") }
  16. end
  17. context "fallback key set" do
  18. setup { @fallback["k"] = "v" }
  19. should("fetch value from fallback") { assert_equal "v", @handset["k"] }
  20. should("have fallback as owner") { assert_equal "fallback_id", @handset.owner("k") }
  21. context "and handset overwrites key" do
  22. setup { @handset["k"] = nil }
  23. should("fetch value from handset") { assert_nil @handset["k"] }
  24. should("have handset as owner") { assert_equal "handset_id", @handset.owner("k") }
  25. end
  26. end
  27. context "fallback and handset set different keys" do
  28. setup do
  29. @handset["k1"] = "v1"
  30. @fallback["k2"] = "v2"
  31. end
  32. should("have keys from handset and fallback") { assert_equal(["k1", "k2"], @handset.keys) }
  33. end
  34. context "another handset with same wurfl_id and fallback" do
  35. setup { @another_handset = Wurfl::Handset.new("handset_id","h", @fallback) }
  36. should("equal handset") { assert_equal @handset, @another_handset}
  37. context "and the other handset sets a key" do
  38. setup { @another_handset["k"] = "v" }
  39. should("not equal handset") { assert_not_equal @handset, @another_handset }
  40. context "and the fallback sets identical key" do
  41. setup { @fallback["k"] = "v" }
  42. should("equal handset") { assert_equal @handset, @another_handset }
  43. end
  44. end
  45. end
  46. context "another handset with different wurfl_id and fallback" do
  47. setup do
  48. @another_fallback = Wurfl::Handset.new("f2", "f2_ua", nil)
  49. @another_handset = Wurfl::Handset.new("h2","h2_ua", @another_fallback)
  50. end
  51. context "and no keys set" do
  52. should('have no differences') {assert @handset.differences(@another_fallback).empty? }
  53. end
  54. context "and the other handset has a key set" do
  55. setup { @another_handset["k"] = "v" }
  56. should('have the key as a difference') do
  57. assert_equal ["k"], @handset.differences(@another_handset)
  58. end
  59. end
  60. context "and handset has a key set" do
  61. setup { @handset["k"] = "v" }
  62. should('have the key as a difference') do
  63. assert_equal ["k"], @handset.differences(@another_handset)
  64. end
  65. end
  66. context "and both handsets have different values for same key" do
  67. setup do
  68. @handset["k"] = "v"
  69. @another_handset["k"] = "v2"
  70. end
  71. should 'have the key as a difference' do
  72. assert_equal ["k"], @handset.differences(@another_handset)
  73. end
  74. end
  75. context "and fallbacks have different values for same key" do
  76. setup do
  77. @fallback["j"] = "1"
  78. @another_fallback["j"] = "2"
  79. end
  80. should 'have the key as a difference' do
  81. assert_equal ["j"], @handset.differences(@another_handset)
  82. end
  83. end
  84. end
  85. end