Issue 508
[zxing.git] / jruby / test / zxing_test.rb
1 #!/usr/bin/env jruby --headless -rubygems
2
3 require File.expand_path( File.dirname(__FILE__) + '/test_helper')
4 require 'zxing'
5
6 class ZXingTest < Test::Unit::TestCase
7   context "A QR decoder singleton" do
8
9     class Foo < Struct.new(:v); def to_s; self.v; end; end
10
11     setup do
12       @decoder = ZXing
13       @uri = "http://2d-code.co.uk/images/bbc-logo-in-qr-code.gif"
14       @path = File.expand_path( File.dirname(__FILE__) + '/qrcode.png')
15       @file = File.new(@path)
16       @google_logo = "http://www.google.com/logos/grandparentsday10.gif"
17       @uri_result = "http://bbc.co.uk/programmes"
18       @path_result = "http://rubyflow.com"
19     end
20
21     should "decode a URL" do
22       assert_equal @decoder.decode(@uri), @uri_result
23     end
24
25     should "decode a file path" do
26       assert_equal @decoder.decode(@path), @path_result
27     end
28
29     should "return nil if #decode fails" do
30       assert_nil @decoder.decode(@google_logo)
31     end
32
33     should "raise an exception if #decode! fails" do
34       assert_raise(NativeException) { @decoder.decode!(@google_logo) }
35     end
36
37     should "decode objects that respond to #path" do
38       assert_equal @decoder.decode(@file), @path_result
39     end
40
41     should "call #to_s to argument passed in as a last resort" do
42       assert_equal @decoder.decode(Foo.new(@path)), @path_result
43     end
44   end
45
46   context "A QR decoder module" do
47     
48     setup do
49       class SpyRing; include ZXing end
50       @ring = SpyRing.new
51     end
52
53     should "include #decode and #decode! into classes" do
54       assert_equal defined?(@ring.decode), "method"
55       assert_equal defined?(@ring.decode!), "method"
56     end
57
58   end
59 end