1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2010 Martin Owens
4 #
5 # Written to test the coding of generating barcodes.
6 #
8 import sys
9 import random
10 import unittest
12 # Allow import of the extension code and modules
13 sys.path.append('..')
15 from render_barcode import *
17 import Barcode.EAN5
18 import Barcode.EAN8
19 import Barcode.EAN13
20 import Barcode.UPCA
21 import Barcode.UPCE
23 digits = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ]
25 class InsertBarcodeBasicTest(unittest.TestCase):
26 """Render Barcode"""
27 def setUp(self):
28 self.data = {}
29 fhl = open('render_barcode.data', 'r')
30 for line in fhl:
31 line = line.replace('\n', '').replace('\r', '')
32 (btype, text, code) = line.split(':')
33 if not self.data.has_key(btype):
34 self.data[btype] = []
35 self.data[btype].append( [ text, code ] )
36 fhl.close()
38 def test_run_without_parameters(self):
39 args = [ 'minimal-blank.svg' ]
40 e = InsertBarcode()
41 e.affect( args, False )
42 #self.assertEqual( e.something, 'some value', 'A commentary about that.' )
44 def test_render_barcode_ian5(self):
45 """Barcode IAN5"""
46 self.barcode_test( 'ean5', Barcode.EAN5 )
48 def test_render_barcode_ian8(self):
49 """Barcode IAN5"""
50 self.barcode_test( 'ean8', Barcode.EAN8 )
52 def test_render_barcode_ian13(self):
53 """Barcode IAN5"""
54 self.barcode_test( 'ean13', Barcode.EAN13 )
56 def test_render_barcode_upca(self):
57 """Barcode IAN5"""
58 self.barcode_test( 'upca', Barcode.UPCA )
60 def test_render_barcode_upce(self):
61 """Barcode UPCE"""
62 self.barcode_test( 'upce', Barcode.UPCE )
64 def barcode_test(self, name, module):
65 """Base module for all barcode testing"""
66 for datum in self.data[name]:
67 (text, code) = datum
68 if not text or not code:
69 continue
70 code2 = module.Object( {'text': text} ).encode(text)
71 self.assertEqual(code, code2)
74 if __name__ == '__main__':
75 unittest.main()