import * as assert from 'assert' ; import { QRCodeDecodedBitStreamParser } from '@zxing/library' ; import BitSourceBuilder from '../../../core/common/BitSourceBuilder' ; import { QRCodeVersion } from '@zxing/library' ; import Random from '../../../core/util/Random' ; import { TextDecoder } from '@zxing/text-encoding' ; import { ZXingStringEncoding } from '@zxing/library' ; ZXingStringEncoding . customDecoder = ( b , e ) => new TextDecoder ( e ) . decode ( b ) ; /**
 * Tests  QRCodeDecodedBitStreamParser .
 *
 *  author Sean Owen
  describe ( 'QRCodeDecodedBitStreamParser' , ( ) => { it ( 'testSimpleByteMode' , ( ) => { const builder = new BitSourceBuilder ( ) ; builder . write ( 0x04 , 4 ) ; builder . write ( 0x03 , 8 ) ; builder . write ( 0xF1 , 8 ) ; builder . write ( 0xF2 , 8 ) ; builder . write ( 0xF3 , 8 ) ; const result = QRCodeDecodedBitStreamParser . decode ( builder . toByteArray ( ) , QRCodeVersion . getVersionForNumber ( 1 ) , null , null ) . getText ( ) ; assert . strictEqual ( result , '\u00f1\u00f2\u00f3' ) ; } ) ; it ( 'testSimpleSJIS' , ( ) => { const builder = new BitSourceBuilder ( ) ; builder . write ( 0x04 , 4 ) ; builder . write ( 0x04 , 8 ) ; builder . write ( 0xA1 , 8 ) ; builder . write ( 0xA2 , 8 ) ; builder . write ( 0xA3 , 8 ) ; builder . write ( 0xD0 , 8 ) ; const result = QRCodeDecodedBitStreamParser . decode ( builder . toByteArray ( ) , QRCodeVersion . getVersionForNumber ( 1 ) , null , null ) . getText ( ) ; assert . strictEqual ( result , '\uff61\uff62\uff63\uff90' ) ; } ) ; it . skip ( 'testECI' , ( ) => { const builder = new BitSourceBuilder ( ) ; builder . write ( 0x07 , 4 ) ; builder . write ( 0x02 , 8 ) ; builder . write ( 0x04 , 4 ) ; builder . write ( 0x03 , 8 ) ; builder . write ( 0xA1 , 8 ) ; builder . write ( 0xA2 , 8 ) ; builder . write ( 0xA3 , 8 ) ; const byteArray = builder . toByteArray ( ) ; const result = QRCodeDecodedBitStreamParser . decode ( byteArray , QRCodeVersion . getVersionForNumber ( 1 ) , null , null ) . getText ( ) ; assert . strictEqual ( result , '\u00ed\u00f3\u00fa' ) ; } ) ; const eciTestData = [ [ 'ISO8859_1' , 0x03 , 0xA1 , 0xA2 , 0xA3 , '\u00A1\u00A2\u00A3' ] , [ 'ISO8859_2' , 0x04 , 0xA1 , 0xA2 , 0xA3 , '\u0104\u02D8\u0141' ] , [ 'ISO8859_3' , 0x05 , 0xA1 , 0xA2 , 0xA3 , '\u0126\u02D8\u00A3' ] , [ 'ISO8859_4' , 0x06 , 0xA1 , 0xA2 , 0xA3 , '\u0104\u0138\u0156' ] , [ 'ISO8859_5' , 0x07 , 0xA1 , 0xA2 , 0xA3 , '\u0401\u0402\u0403' ] , [ 'ISO8859_6' , 0x08 , 0xE1 , 0xE2 , 0xE3 , '\u0641\u0642\u0643' ] , [ 'ISO8859_7' , 0x09 , 0xA1 , 0xA2 , 0xA3 , '\u2018\u2019\u00A3' ] , [ 'ISO8859_8' , 0x0A , 0xE1 , 0xE2 , 0xE3 , '\u05D1\u05D2\u05D3' ] , [ 'ISO8859_9' , 0x0B , 0xD0 , 0xDD , 0xDE , '\u011E\u0130\u015E' ] , [ 'ISO8859_10' , 0x0C , 0xA1 , 0xA2 , 0xA3 , '\u0104\u0112\u0122' ] , [ 'ISO8859_11' , 0x0D , 0xA1 , 0xA2 , 0xA3 , '\u0E01\u0E02\u0E03' ] , [ 'ISO8859_13' , 0x0F , 0xD1 , 0xD2 , 0xD3 , '\u0143\u0145\u00D3' ] , [ 'ISO8859_14' , 0x10 , 0xA1 , 0xA2 , 0xA3 , '\u1E02\u1E03\u00A3' ] , [ 'ISO8859_15' , 0x11 , 0xBC , 0xBD , 0xBE , '\u0152\u0153\u0178' ] , [ 'ISO8859_16' , 0x12 , 0xA1 , 0xA2 , 0xA3 , '\u0104\u0105\u0141' ] , [ 'windows-1250' , 0x15 , 0xA1 , 0xA2 , 0xA3 , '\u02C7\u02D8\u0141' ] , [ 'windows-1251' , 0x16 , 0xA1 , 0xA2 , 0xA3 , '\u040E\u045E\u0408' ] , [ 'windows-1252' , 0x17 , 0x91 , 0x92 , 0x93 , '\u2018\u2019\u201C' ] , [ 'windows-1256' , 0x18 , 0xE1 , 0xE2 , 0xE3 , '\u0644\u00E2\u0645' ] , ] ; describe ( 'testECIISOEach' , ( ) => { for ( const d of eciTestData ) { it ( 'testECIISOEach ' + < string > d [ 0 ] , ( ) => { testEciOneEncoding ( < string > d [ 0 ] , < number > d [ 1 ] , < number > d [ 2 ] , < number > d [ 3 ] , < number > d [ 4 ] , < string > d [ 5 ] ) ; } ) ; } } ) ; function testEciOneEncoding ( encodingLabel , eciBits , b1 , b2 , b3 , expected ) { const builder = new BitSourceBuilder ( ) ; builder . write ( 0x07 , 4 ) ; builder . write ( eciBits , 8 ) ; builder . write ( 0x04 , 4 ) ; builder . write ( 0x03 , 8 ) ; builder . write ( b1 , 8 ) ; builder . write ( b2 , 8 ) ; builder . write ( b3 , 8 ) ; const byteArray = builder . toByteArray ( ) ; const result = QRCodeDecodedBitStreamParser . decode ( byteArray , QRCodeVersion . getVersionForNumber ( 1 ) , null , null ) . getText ( ) ; assert . strictEqual ( result , expected , encodingLabel ) ; } describe ( 'testECIISOCombine' , ( ) => { const r = new Random ( 'ECIISO' ) ; for ( let i = 0 ; i !== 10 ; i ++ ) { let id1 = r . next ( eciTestData . length ) ; let id2 = r . next ( eciTestData . length ) ; let d1 = eciTestData [ id1 ] ; let d2 = eciTestData [ id2 ] ; it ( 'testECIISOCombine ' + < string > d1 [ 0 ] + ' & ' + < string > d2 [ 0 ] , ( ) => { testEciComboned ( < string > d1 [ 0 ] , < number > d1 [ 1 ] , < number > d1 [ 2 ] , < number > d1 [ 3 ] , < number > d1 [ 4 ] , < string > d1 [ 5 ] , < string > d2 [ 0 ] , < number > d2 [ 1 ] , < number > d2 [ 2 ] , < number > d2 [ 3 ] , < number > d2 [ 4 ] , < string > d2 [ 5 ] ) ; } ) ; } } ) ; function testEciComboned ( encodingLabel1 , eciBits1 , b1 , b2 , b3 , expected1 , encodingLabel2 , eciBits2 , b4 , b5 , b6 , expected2 ) { const builder = new BitSourceBuilder ( ) ; builder . write ( 0x07 , 4 ) ; builder . write ( eciBits1 , 8 ) ; builder . write ( 0x04 , 4 ) ; builder . write ( 0x03 , 8 ) ; builder . write ( b1 , 8 ) ; builder . write ( b2 , 8 ) ; builder . write ( b3 , 8 ) ; builder . write ( 0x07 , 4 ) ; builder . write ( eciBits2 , 8 ) ; builder . write ( 0x04 , 4 ) ; builder . write ( 0x03 , 8 ) ; builder . write ( b4 , 8 ) ; builder . write ( b5 , 8 ) ; builder . write ( b6 , 8 ) ; const byteArray = builder . toByteArray ( ) ; const result = QRCodeDecodedBitStreamParser . decode ( byteArray , QRCodeVersion . getVersionForNumber ( 1 ) , null , null ) . getText ( ) ; assert . strictEqual ( result , expected1 + expected2 , encodingLabel1 + ' & ' + encodingLabel2 ) ; } it ( 'testHanzi' , ( ) => { const builder = new BitSourceBuilder ( ) ; builder . write ( 0x0D , 4 ) ; builder . write ( 0x01 , 4 ) ; builder . write ( 0x01 , 8 ) ; builder . write ( 0x03C1 , 13 ) ; const result = QRCodeDecodedBitStreamParser . decode ( builder . toByteArray ( ) , QRCodeVersion . getVersionForNumber ( 1 ) , null , null ) . getText ( ) ; assert . strictEqual ( result , '\u963f' ) ; } ) ; } ) ;