|  | 
|  | 1 | +package pix | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"strings" | 
|  | 5 | +	"testing" | 
|  | 6 | +) | 
|  | 7 | + | 
|  | 8 | +func TestNew(t *testing.T) { | 
|  | 9 | + | 
|  | 10 | +	opts := []Options{ | 
|  | 11 | +		OptQRCodeSize(256), | 
|  | 12 | +		OptUrl("https://example.com"), | 
|  | 13 | +		OptAditionalInfo("Aditional Info"), | 
|  | 14 | +		OptKind(STATIC), | 
|  | 15 | +		OptMerchantName("Test Merchant"), | 
|  | 16 | +		OptMerchantCity("Test City"), | 
|  | 17 | +		OptAmount("100.00"), | 
|  | 18 | +		OptDescription("Test Description"), | 
|  | 19 | +		OptPixKey("123456"), | 
|  | 20 | +	} | 
|  | 21 | + | 
|  | 22 | +	pix, err := New(opts...) | 
|  | 23 | +	if err != nil { | 
|  | 24 | +		t.Fatalf("failed to create Pix instance: %v", err) | 
|  | 25 | +	} | 
|  | 26 | + | 
|  | 27 | +	if pix.params.pixKey != "123456" { | 
|  | 28 | +		t.Errorf("expected pixKey to be '123456', got %v", pix.params.pixKey) | 
|  | 29 | +	} | 
|  | 30 | + | 
|  | 31 | +	if pix.params.merchant.name != "Test Merchant" { | 
|  | 32 | +		t.Errorf("expected merchant name to be 'Test Merchant', got %v", pix.params.merchant.name) | 
|  | 33 | +	} | 
|  | 34 | +} | 
|  | 35 | + | 
|  | 36 | +func TestGenPayload(t *testing.T) { | 
|  | 37 | +	opts := []Options{ | 
|  | 38 | +		OptQRCodeSize(256), | 
|  | 39 | +		OptUrl("https://example.com"), | 
|  | 40 | +		OptAditionalInfo("Aditional Info"), | 
|  | 41 | +		OptKind(STATIC), | 
|  | 42 | +		OptMerchantName("Test Merchant"), | 
|  | 43 | +		OptMerchantCity("Test City"), | 
|  | 44 | +		OptAmount("100.00"), | 
|  | 45 | +		OptDescription("Test Description"), | 
|  | 46 | +		OptPixKey("11912345678"), | 
|  | 47 | +	} | 
|  | 48 | + | 
|  | 49 | +	pix, err := New(opts...) | 
|  | 50 | +	if err != nil { | 
|  | 51 | +		t.Fatalf("failed to create Pix instance: %v", err) | 
|  | 52 | +	} | 
|  | 53 | + | 
|  | 54 | +	payload := pix.GenPayload() | 
|  | 55 | +	if payload == "" { | 
|  | 56 | +		t.Errorf("expected payload to be non-empty") | 
|  | 57 | +	} | 
|  | 58 | + | 
|  | 59 | +} | 
|  | 60 | + | 
|  | 61 | +func TestValidates(t *testing.T) { | 
|  | 62 | +	tests := []struct { | 
|  | 63 | +		opts     []Options | 
|  | 64 | +		hasError bool | 
|  | 65 | +	}{ | 
|  | 66 | +		{ | 
|  | 67 | +			opts: []Options{ | 
|  | 68 | +				OptMerchantName("Test Merchant"), | 
|  | 69 | +				OptMerchantCity("Test City"), | 
|  | 70 | +			}, | 
|  | 71 | +			hasError: true, // pixKey is missing | 
|  | 72 | +		}, | 
|  | 73 | +		{ | 
|  | 74 | +			opts: []Options{ | 
|  | 75 | +				OptPixKey("123456"), | 
|  | 76 | +				OptMerchantCity("Test City"), | 
|  | 77 | +			}, | 
|  | 78 | +			hasError: true, // merchant name is missing | 
|  | 79 | +		}, | 
|  | 80 | +		{ | 
|  | 81 | +			opts: []Options{ | 
|  | 82 | +				OptPixKey("123456"), | 
|  | 83 | +				OptMerchantName("Test Merchant"), | 
|  | 84 | +			}, | 
|  | 85 | +			hasError: true, // merchant city is missing | 
|  | 86 | +		}, | 
|  | 87 | +		{ | 
|  | 88 | +			opts: []Options{ | 
|  | 89 | +				OptPixKey("123456"), | 
|  | 90 | +				OptMerchantName("Test Merchant"), | 
|  | 91 | +				OptMerchantCity("Test City"), | 
|  | 92 | +			}, | 
|  | 93 | +			hasError: false, // all required fields are present | 
|  | 94 | +		}, | 
|  | 95 | +	} | 
|  | 96 | + | 
|  | 97 | +	for _, test := range tests { | 
|  | 98 | +		pix, err := New(test.opts...) | 
|  | 99 | +		if err != nil { | 
|  | 100 | +			t.Fatalf("failed to create Pix instance: %v", err) | 
|  | 101 | +		} | 
|  | 102 | + | 
|  | 103 | +		err = pix.Validates() | 
|  | 104 | +		if (err != nil) != test.hasError { | 
|  | 105 | +			t.Errorf("expected error to be %v, got %v", test.hasError, err != nil) | 
|  | 106 | +		} | 
|  | 107 | +	} | 
|  | 108 | +} | 
|  | 109 | + | 
|  | 110 | +func TestGenQRCode(t *testing.T) { | 
|  | 111 | +	opts := []Options{ | 
|  | 112 | +		OptQRCodeSize(256), | 
|  | 113 | +		OptUrl("https://example.com"), | 
|  | 114 | +		OptAditionalInfo("Aditional Info"), | 
|  | 115 | +		OptKind(STATIC), | 
|  | 116 | +		OptMerchantName("Test Merchant"), | 
|  | 117 | +		OptMerchantCity("Test City"), | 
|  | 118 | +		OptAmount("100.00"), | 
|  | 119 | +		OptDescription("Test Description"), | 
|  | 120 | +		OptPixKey("11912345678"), | 
|  | 121 | +	} | 
|  | 122 | + | 
|  | 123 | +	pix, err := New(opts...) | 
|  | 124 | +	if err != nil { | 
|  | 125 | +		t.Fatalf("failed to create Pix instance: %v", err) | 
|  | 126 | +	} | 
|  | 127 | + | 
|  | 128 | +	if err := pix.Validates(); err != nil { | 
|  | 129 | +		t.Fatalf("failed to validate Pix instance: %v", err) | 
|  | 130 | +	} | 
|  | 131 | + | 
|  | 132 | +	qrCode, err := pix.GenQRCode() | 
|  | 133 | +	if err != nil { | 
|  | 134 | +		t.Errorf("failed to generate QR code: %v", err) | 
|  | 135 | +	} | 
|  | 136 | + | 
|  | 137 | +	t.Logf("QR code: %v", qrCode) | 
|  | 138 | + | 
|  | 139 | +	if len(qrCode) == 0 { | 
|  | 140 | +		t.Errorf("expected QR code to be non-empty") | 
|  | 141 | +	} | 
|  | 142 | +} | 
|  | 143 | + | 
|  | 144 | +func TestGenerateMAI(t *testing.T) { | 
|  | 145 | +	opts := []Options{ | 
|  | 146 | +		OptPixKey("11999821234"), | 
|  | 147 | +		OptMerchantName("Thiago Zilli Sarmento"), | 
|  | 148 | +		OptMerchantCity("Ararangua"), | 
|  | 149 | +		OptKind(STATIC), | 
|  | 150 | +		OptAmount("100.00"), | 
|  | 151 | +		OptDescription("Test Description"), | 
|  | 152 | +		OptAditionalInfo("https://example.com"), | 
|  | 153 | +	} | 
|  | 154 | + | 
|  | 155 | +	pix, err := New(opts...) | 
|  | 156 | +	if err != nil { | 
|  | 157 | +		t.Fatalf("failed to create Pix instance: %v", err) | 
|  | 158 | +	} | 
|  | 159 | + | 
|  | 160 | +	if err := pix.Validates(); err != nil { | 
|  | 161 | +		t.Fatalf("failed to validate Pix instance: %v", err) | 
|  | 162 | +	} | 
|  | 163 | + | 
|  | 164 | +	payload := pix.GenPayload() | 
|  | 165 | + | 
|  | 166 | +	t.Logf("payload: %v", payload) | 
|  | 167 | + | 
|  | 168 | +	if !strings.Contains(payload, "11999821234") || !strings.Contains(payload, "https://example.com") { | 
|  | 169 | +		t.Errorf("generateMAI function did not work as expected") | 
|  | 170 | +	} | 
|  | 171 | +} | 
|  | 172 | + | 
|  | 173 | +func TestGenerateAdditionalData(t *testing.T) { | 
|  | 174 | +	opts := []Options{ | 
|  | 175 | +		OptPixKey("123456"), | 
|  | 176 | +		OptTxId("Transaction123"), | 
|  | 177 | +	} | 
|  | 178 | + | 
|  | 179 | +	pix, err := New(opts...) | 
|  | 180 | +	if err != nil { | 
|  | 181 | +		t.Fatalf("failed to create Pix instance: %v", err) | 
|  | 182 | +	} | 
|  | 183 | + | 
|  | 184 | +	payload := pix.GenPayload() | 
|  | 185 | +	if !strings.Contains(payload, "Transaction123") { | 
|  | 186 | +		t.Errorf("generateAdditionalData function did not work as expected") | 
|  | 187 | +	} | 
|  | 188 | +} | 
|  | 189 | + | 
|  | 190 | +func TestGetCRC16AndFindAndReplaceCRC(t *testing.T) { | 
|  | 191 | +	opts := []Options{ | 
|  | 192 | +		OptPixKey("123456"), | 
|  | 193 | +	} | 
|  | 194 | + | 
|  | 195 | +	pix, err := New(opts...) | 
|  | 196 | +	if err != nil { | 
|  | 197 | +		t.Fatalf("failed to create Pix instance: %v", err) | 
|  | 198 | +	} | 
|  | 199 | + | 
|  | 200 | +	payload := pix.GenPayload() | 
|  | 201 | +	if !strings.HasSuffix(payload, pix.getCRC16(payload[:len(payload)-4])) { | 
|  | 202 | +		t.Errorf("getCRC16 or FindAndReplaceCRC function did not work as expected") | 
|  | 203 | +	} | 
|  | 204 | +} | 
0 commit comments