File: qr4r_test.rb

package info (click to toggle)
ruby-qr4r 0.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 184 kB
  • sloc: ruby: 230; makefile: 9
file content (120 lines) | stat: -rw-r--r-- 3,409 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require "minitest/autorun"
require "qr4r"
require "tempfile"
class Qr4rTest < Minitest::Test
  def test_encode
    # do something
    f = Tempfile.new(["qr4r", ".png"])
    Qr4r.encode("whatever yo", f.path)
    # assert that it worked
    assert File.exist?(f.path)
    r = MojoMagick.get_image_size(f.path)
    assert r[:height] == 25 * 3
    assert r[:width] == 25 * 3
  end

  def test_encode_with_size
    # do something
    f = Tempfile.new(["qr4r", ".png"])
    Qr4r.encode("whatever yo", f.path, size: 4)
    # assert that it worked
    assert File.exist?(f)
    r = MojoMagick.get_image_size(f.path)
    assert r[:height] == 33 * 3
    assert r[:width] == 33 * 3
  end

  def test_encode_with_size_and_border
    # do something
    f = Tempfile.new(["qr4r", ".png"])
    Qr4r.encode("whatever yo", f.path, size: 4, border: 10)
    # assert that it worked
    assert File.exist?(f)
    r = MojoMagick.get_image_size(f.path)
    assert r[:height] == (33 * 3) + 20
    assert r[:width] == (33 * 3) + 20
  end

  def test_encode_with_pixel_size
    # do something
    f = Tempfile.new(["qr4r", ".png"])
    Qr4r.encode("whatever yo", f.path, pixel_size: 5)
    # assert that it worked
    assert File.exist?(f)
    r = MojoMagick.get_image_size(f.path)
    assert r[:height] == 25 * 5
    assert r[:width] == 25 * 5
  end

  def test_encode_with_pixel_size_as_string
    # do something
    f = Tempfile.new(["qr4r", ".png"])
    Qr4r.encode("whatever yo", f.path, pixel_size: "5")
    # assert that it worked
    assert File.exist?(f)
    r = MojoMagick.get_image_size(f.path)
    assert r[:height] == 25 * 5
    assert r[:width] == 25 * 5
  end

  def test_encode_with_size_and_level
    # do something
    f = Tempfile.new(["qr4r", ".png"])
    Qr4r.encode("whatever yo", f.path, size: 4, level: :m)
    # assert that it worked
    assert File.exist?(f)
    r = MojoMagick.get_image_size(f.path)
    assert r[:height] == 33 * 3
    assert r[:width] == 33 * 3
  end

  def test_a_long_string_with_size_thats_too_small
    caught = false
    begin
      f = Tempfile.new(["qr4r", ".png"])
      Qr4r.encode("this string should also be encodable. don't ya think", f.path, size: 4)
    rescue StandardError
      caught = true
    end
    assert caught, "Expected an error"
  end

  def test_a_long_string_with_size_thats_right
    f = Tempfile.new(["qr4r", ".png"])
    Qr4r.encode("this string should also be encodable. don't ya think", f.path, size: 10)
    assert File.exist?(f)
    r = MojoMagick.get_image_size(f.path)
    assert r[:height] == 57 * 3
    assert r[:width] == 57 * 3
  end

  def test_a_long_string_without_size
    f = Tempfile.new(["qr4r", ".png"])
    Qr4r.encode("this string should also be encodable. don't ya think", f.path)
    assert File.exist?(f)
    r = MojoMagick.get_image_size(f.path)
    assert r[:height] = 41 * 3
    assert r[:width] = 41 * 3
  end

  def test_compute_size
    test_sizes = [7, 14, 24, 34, 44, 58, 64, 84, 98]
    test_sizes.each_with_index do |sz, idx|
      str = "a" * (sz - 1)
      assert Qr4r.send(:compute_size, str) == idx + 1
      str = "a" * sz
      assert Qr4r.send(:compute_size, str) == idx + 2
    end
  end

  def test_compute_size_too_big
    str = "a" * 120
    caught = false
    begin
      Qr4r.send(:compute_size, str)
    rescue StandardError => _e
      caught = true
    end
    assert caught, "Expected exception"
  end
end