| 1 |
package element |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/mitch000001/go-hbci/charset" |
| 9 |
) |
| 10 |
|
| 11 |
type testDataElementData struct { |
| 12 |
inValue interface{} |
| 13 |
inType DataElementType |
| 14 |
inMaxLength int |
| 15 |
valid bool |
| 16 |
outValue interface{} |
| 17 |
outType DataElementType |
| 18 |
outLength int |
| 19 |
outString string |
| 20 |
} |
| 21 |
|
| 22 |
func TestNewDataElement(t *testing.T) { |
| 23 |
tests := []testDataElementData{ |
| 24 |
{1, numberDE, 3, true, 1, numberDE, 1, "1"}, |
| 25 |
{1234, numberDE, 3, false, 1234, numberDE, 4, "1234"}, |
| 26 |
} |
| 27 |
for _, test := range tests { |
| 28 |
d := New(test.inType, test.inValue, test.inMaxLength) |
| 29 |
|
| 30 |
expectedOut := test.outValue |
| 31 |
|
| 32 |
actualOut := d.Value() |
| 33 |
|
| 34 |
if !reflect.DeepEqual(expectedOut, actualOut) { |
| 35 |
t.Logf("Input: %+#v\n", test) |
| 36 |
t.Logf("Expected Value() to return %v, got %v\n", expectedOut, actualOut) |
| 37 |
t.Fail() |
| 38 |
} |
| 39 |
|
| 40 |
expectedLength := test.outLength |
| 41 |
|
| 42 |
actualLength := d.Length() |
| 43 |
if actualLength != expectedLength { |
| 44 |
t.Logf("Input: %+#v\n", test) |
| 45 |
t.Logf("Expected Length() to return %d, got %d\n", expectedLength, actualLength) |
| 46 |
t.Fail() |
| 47 |
} |
| 48 |
|
| 49 |
expectedString := test.outString |
| 50 |
|
| 51 |
actualString := d.String() |
| 52 |
|
| 53 |
if actualString != expectedString { |
| 54 |
t.Logf("Input: %+#v\n", test) |
| 55 |
t.Logf("Expected String() to return %q, got %q\n", expectedString, actualString) |
| 56 |
t.Fail() |
| 57 |
} |
| 58 |
|
| 59 |
valid := d.IsValid() |
| 60 |
|
| 61 |
if valid != test.valid { |
| 62 |
t.Logf("Input: %+#v\n", test) |
| 63 |
if test.valid { |
| 64 |
t.Logf("Expected DataElement to be valid, was not\n") |
| 65 |
} else { |
| 66 |
t.Logf("Expected DataElement to be invalid, was valid\n") |
| 67 |
} |
| 68 |
t.Logf("Expected DataElement to be valid, was not\n") |
| 69 |
t.Fail() |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestNewAlphaNumericDataElement(t *testing.T) { |
| 75 |
dataElement := NewAlphaNumeric("abc", 5) |
| 76 |
|
| 77 |
expectedType := alphaNumericDE |
| 78 |
|
| 79 |
actualType := dataElement.Type() |
| 80 |
|
| 81 |
if expectedType != actualType { |
| 82 |
t.Logf("Expected Type to equal %v, got %v\n", expectedType, actualType) |
| 83 |
t.Fail() |
| 84 |
} |
| 85 |
|
| 86 |
expectedLength := len("abc") |
| 87 |
|
| 88 |
actualLength := dataElement.Length() |
| 89 |
|
| 90 |
if expectedLength != actualLength { |
| 91 |
t.Logf("Expected Length() to return %d, got %d\n", expectedLength, actualLength) |
| 92 |
t.Fail() |
| 93 |
} |
| 94 |
|
| 95 |
expectedString := "abc" |
| 96 |
|
| 97 |
actualString := dataElement.String() |
| 98 |
|
| 99 |
if actualString != expectedString { |
| 100 |
t.Logf("Expected String() to return %q, got %q\n", expectedString, actualString) |
| 101 |
t.Fail() |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestAlphaNumericDataElementMarshalHBCI(t *testing.T) { |
| 106 |
type testData struct { |
| 107 |
unmarshaled string |
| 108 |
marshaled []byte |
| 109 |
err error |
| 110 |
} |
| 111 |
tests := []testData{ |
| 112 |
{ |
| 113 |
"abc", |
| 114 |
charset.ToISO8859_1("abc"), |
| 115 |
nil, |
| 116 |
}, |
| 117 |
{ |
| 118 |
"ab:c", |
| 119 |
charset.ToISO8859_1("ab?:c"), |
| 120 |
nil, |
| 121 |
}, |
| 122 |
{ |
| 123 |
"abüc", |
| 124 |
charset.ToISO8859_1("abüc"), |
| 125 |
nil, |
| 126 |
}, |
| 127 |
} |
| 128 |
for _, test := range tests { |
| 129 |
element := NewAlphaNumeric(test.unmarshaled, len(test.unmarshaled)) |
| 130 |
|
| 131 |
marshaled, err := element.MarshalHBCI() |
| 132 |
|
| 133 |
if !reflect.DeepEqual(test.err, err) { |
| 134 |
t.Logf("Expected error to equal\n%v\n\tgot\n%v\n", test.err, err) |
| 135 |
t.Fail() |
| 136 |
} |
| 137 |
|
| 138 |
if !bytes.Equal(test.marshaled, marshaled) { |
| 139 |
t.Logf("Expected unmarshaled value to equal\n%q\n\tgot\n%q\n", test.marshaled, marshaled) |
| 140 |
t.Fail() |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestAlphaNumericDataElementUnmarshalHBCI(t *testing.T) { |
| 146 |
type testData struct { |
| 147 |
marshaled []byte |
| 148 |
unmarshaled string |
| 149 |
err error |
| 150 |
} |
| 151 |
tests := []testData{ |
| 152 |
{ |
| 153 |
charset.ToISO8859_1("abc"), |
| 154 |
NewAlphaNumeric("abc", 3).Val(), |
| 155 |
nil, |
| 156 |
}, |
| 157 |
{ |
| 158 |
charset.ToISO8859_1("ab?:c"), |
| 159 |
NewAlphaNumeric("ab:c", 3).Val(), |
| 160 |
nil, |
| 161 |
}, |
| 162 |
{ |
| 163 |
charset.ToISO8859_1("abüc"), |
| 164 |
NewAlphaNumeric("abüc", 3).Val(), |
| 165 |
nil, |
| 166 |
}, |
| 167 |
} |
| 168 |
for _, test := range tests { |
| 169 |
element := &AlphaNumericDataElement{} |
| 170 |
|
| 171 |
err := element.UnmarshalHBCI(test.marshaled) |
| 172 |
|
| 173 |
if !reflect.DeepEqual(test.err, err) { |
| 174 |
t.Logf("Expected error to equal\n%v\n\tgot\n%v\n", test.err, err) |
| 175 |
t.Fail() |
| 176 |
} |
| 177 |
|
| 178 |
actual := element.Val() |
| 179 |
|
| 180 |
if test.unmarshaled != actual { |
| 181 |
t.Logf("Expected unmarshaled value to equal\n%q\n\tgot\n%q\n", test.unmarshaled, actual) |
| 182 |
t.Fail() |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
type testDigitDataElementData struct { |
| 188 |
in int |
| 189 |
inMaxLength int |
| 190 |
valid bool |
| 191 |
outLength int |
| 192 |
outString string |
| 193 |
} |
| 194 |
|
| 195 |
func TestNewDigitDataElement(t *testing.T) { |
| 196 |
tests := []testDigitDataElementData{ |
| 197 |
{1, 4, true, 1, "0001"}, |
| 198 |
{10, 4, true, 2, "0010"}, |
| 199 |
{1000, 4, true, 4, "1000"}, |
| 200 |
{10000, 4, false, 5, "10000"}, |
| 201 |
} |
| 202 |
|
| 203 |
for _, test := range tests { |
| 204 |
d := NewDigit(test.in, test.inMaxLength) |
| 205 |
expectedLength := test.outLength |
| 206 |
|
| 207 |
actualLength := d.Length() |
| 208 |
|
| 209 |
if actualLength != expectedLength { |
| 210 |
t.Logf("Input: %+#v\n", test) |
| 211 |
t.Logf("Expected Length() to return %d, got %d\n", expectedLength, actualLength) |
| 212 |
t.Fail() |
| 213 |
} |
| 214 |
|
| 215 |
expectedString := test.outString |
| 216 |
|
| 217 |
actualString := d.String() |
| 218 |
|
| 219 |
if actualString != expectedString { |
| 220 |
t.Logf("Input: %+#v\n", test) |
| 221 |
t.Logf("Expected String() to return %q, got %q\n", expectedString, actualString) |
| 222 |
t.Fail() |
| 223 |
} |
| 224 |
|
| 225 |
valid := d.IsValid() |
| 226 |
|
| 227 |
if valid != test.valid { |
| 228 |
t.Logf("Input: %+#v\n", test) |
| 229 |
if test.valid { |
| 230 |
t.Logf("Expected DataElement to be valid, was not\n") |
| 231 |
} else { |
| 232 |
t.Logf("Expected DataElement to be invalid, was valid\n") |
| 233 |
} |
| 234 |
t.Logf("Expected DataElement to be valid, was not\n") |
| 235 |
t.Fail() |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func TestDigitDataElementValue(t *testing.T) { |
| 241 |
d := NewDigit(1, 2) |
| 242 |
|
| 243 |
var expected interface{} = 1 |
| 244 |
|
| 245 |
actual := d.Value() |
| 246 |
|
| 247 |
if !reflect.DeepEqual(expected, actual) { |
| 248 |
t.Logf("Expected Value() to return %v, got %v\n", expected, actual) |
| 249 |
t.Fail() |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
func TestDigitDataElementType(t *testing.T) { |
| 254 |
d := NewDigit(1, 2) |
| 255 |
|
| 256 |
expected := digitDE |
| 257 |
|
| 258 |
actual := d.Type() |
| 259 |
|
| 260 |
if !reflect.DeepEqual(expected, actual) { |
| 261 |
t.Logf("Expected Value() to return %v, got %v\n", expected, actual) |
| 262 |
t.Fail() |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestNewNumberDataElement(t *testing.T) { |
| 267 |
tests := []testDigitDataElementData{ |
| 268 |
{1, 4, true, 1, "1"}, |
| 269 |
{10, 4, true, 2, "10"}, |
| 270 |
{1000, 4, true, 4, "1000"}, |
| 271 |
{10000, 4, false, 5, "10000"}, |
| 272 |
} |
| 273 |
|
| 274 |
for _, test := range tests { |
| 275 |
d := NewNumber(test.in, test.inMaxLength) |
| 276 |
expectedLength := test.outLength |
| 277 |
|
| 278 |
actualLength := d.Length() |
| 279 |
|
| 280 |
if actualLength != expectedLength { |
| 281 |
t.Logf("Input: %+#v\n", test) |
| 282 |
t.Logf("Expected Length() to return %d, got %d\n", expectedLength, actualLength) |
| 283 |
t.Fail() |
| 284 |
} |
| 285 |
|
| 286 |
expectedString := test.outString |
| 287 |
|
| 288 |
actualString := d.String() |
| 289 |
|
| 290 |
if actualString != expectedString { |
| 291 |
t.Logf("Input: %+#v\n", test) |
| 292 |
t.Logf("Expected String() to return %q, got %q\n", expectedString, actualString) |
| 293 |
t.Fail() |
| 294 |
} |
| 295 |
|
| 296 |
valid := d.IsValid() |
| 297 |
|
| 298 |
if valid != test.valid { |
| 299 |
t.Logf("Input: %+#v\n", test) |
| 300 |
if test.valid { |
| 301 |
t.Logf("Expected DataElement to be valid, was not\n") |
| 302 |
} else { |
| 303 |
t.Logf("Expected DataElement to be invalid, was valid\n") |
| 304 |
} |
| 305 |
t.Logf("Expected DataElement to be valid, was not\n") |
| 306 |
t.Fail() |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
func TestNumberDataElementValue(t *testing.T) { |
| 312 |
d := NewNumber(1, 2) |
| 313 |
|
| 314 |
var expected interface{} = 1 |
| 315 |
|
| 316 |
actual := d.Value() |
| 317 |
|
| 318 |
if !reflect.DeepEqual(expected, actual) { |
| 319 |
t.Logf("Expected Value() to return %v, got %v\n", expected, actual) |
| 320 |
t.Fail() |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func TestNumberDataElementType(t *testing.T) { |
| 325 |
d := NewNumber(1, 2) |
| 326 |
|
| 327 |
expected := numberDE |
| 328 |
|
| 329 |
actual := d.Type() |
| 330 |
|
| 331 |
if !reflect.DeepEqual(expected, actual) { |
| 332 |
t.Logf("Expected Value() to return %v, got %v\n", expected, actual) |
| 333 |
t.Fail() |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func TestBinaryDataElementString(t *testing.T) { |
| 338 |
b := NewBinary([]byte("test123"), 7) |
| 339 |
|
| 340 |
expected := "@7@test123" |
| 341 |
|
| 342 |
actual := b.String() |
| 343 |
|
| 344 |
if expected != actual { |
| 345 |
t.Logf("Expected BinaryDataElement to serialize to %q, got %q\n", expected, actual) |
| 346 |
t.Fail() |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func TestBinaryDataElementUnmarshalHBCI(t *testing.T) { |
| 351 |
var b BinaryDataElement |
| 352 |
|
| 353 |
err := b.UnmarshalHBCI([]byte("@7@test123")) |
| 354 |
|
| 355 |
if err != nil { |
| 356 |
t.Logf("Expected no error, got %T:%v\n", err, err) |
| 357 |
t.Fail() |
| 358 |
} |
| 359 |
|
| 360 |
val := b.Val() |
| 361 |
expectedVal := []byte("test123") |
| 362 |
|
| 363 |
if !bytes.Equal(val, expectedVal) { |
| 364 |
t.Logf("Expected Val() to return %q, got %q\n", expectedVal, val) |
| 365 |
t.Fail() |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
type testDataElement struct { |
| 370 |
alpha *AlphaNumericDataElement |
| 371 |
num *NumberDataElement |
| 372 |
} |
| 373 |
|
| 374 |
func (t *testDataElement) groupDataElements() []DataElement { |
| 375 |
return []DataElement{t.alpha, t.num} |
| 376 |
} |
| 377 |
|
| 378 |
func (t *testDataElement) Elements() []DataElement { |
| 379 |
return []DataElement{t.alpha, t.num} |
| 380 |
} |
| 381 |
|
| 382 |
type testDataElementGroupData struct { |
| 383 |
alphaIn *AlphaNumericDataElement |
| 384 |
numIn *NumberDataElement |
| 385 |
out string |
| 386 |
} |
| 387 |
|
| 388 |
func TestGroupDataElementGroupString(t *testing.T) { |
| 389 |
tests := []testDataElementGroupData{ |
| 390 |
{ |
| 391 |
NewAlphaNumeric("abc", 3), |
| 392 |
NewNumber(123, 3), |
| 393 |
"abc:123", |
| 394 |
}, |
| 395 |
{ |
| 396 |
NewAlphaNumeric("abc", 3), |
| 397 |
nil, |
| 398 |
"abc:", |
| 399 |
}, |
| 400 |
{ |
| 401 |
nil, |
| 402 |
NewNumber(123, 3), |
| 403 |
":123", |
| 404 |
}, |
| 405 |
{ |
| 406 |
nil, |
| 407 |
nil, |
| 408 |
":", |
| 409 |
}, |
| 410 |
} |
| 411 |
|
| 412 |
for _, test := range tests { |
| 413 |
testData := &testDataElement{ |
| 414 |
alpha: test.alphaIn, |
| 415 |
num: test.numIn, |
| 416 |
} |
| 417 |
|
| 418 |
group := NewGroupDataElementGroup(0, 2, testData) |
| 419 |
|
| 420 |
actualString := group.String() |
| 421 |
|
| 422 |
if test.out != actualString { |
| 423 |
t.Logf("Input: %#v\n", testData) |
| 424 |
t.Logf("Expected String() to return %q, got %q\n", test.out, actualString) |
| 425 |
t.Fail() |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
func TestGroupDataElementGroupUnmarshalHBCI(t *testing.T) { |
| 431 |
t.Skip("This test is broken due to necessary implementation changes.") |
| 432 |
type testDataElementGroupUnmarshalData struct { |
| 433 |
in string |
| 434 |
alphaOut *AlphaNumericDataElement |
| 435 |
numOut *NumberDataElement |
| 436 |
} |
| 437 |
|
| 438 |
tests := []testDataElementGroupUnmarshalData{ |
| 439 |
{ |
| 440 |
"abc:123", |
| 441 |
NewAlphaNumeric("abc", 3), |
| 442 |
NewNumber(123, 3), |
| 443 |
}, |
| 444 |
{ |
| 445 |
"abc:", |
| 446 |
NewAlphaNumeric("abc", 3), |
| 447 |
nil, |
| 448 |
}, |
| 449 |
{ |
| 450 |
":123", |
| 451 |
nil, |
| 452 |
NewNumber(123, 3), |
| 453 |
}, |
| 454 |
{ |
| 455 |
":", |
| 456 |
nil, |
| 457 |
nil, |
| 458 |
}, |
| 459 |
} |
| 460 |
|
| 461 |
for _, test := range tests { |
| 462 |
tde := testDataElement{} |
| 463 |
group := new(elementGroup) |
| 464 |
group.elements = tde.Elements |
| 465 |
|
| 466 |
err := group.UnmarshalHBCI([]byte(test.in)) |
| 467 |
|
| 468 |
if err != nil { |
| 469 |
t.Logf("Input: %q\n", test.in) |
| 470 |
t.Logf("Expected no error, got %T:%v\n", err, err) |
| 471 |
t.Fail() |
| 472 |
} |
| 473 |
|
| 474 |
expectedArray := []DataElement{test.alphaOut, test.numOut} |
| 475 |
actualArray := group.elements() |
| 476 |
|
| 477 |
if !reflect.DeepEqual(expectedArray, actualArray) { |
| 478 |
t.Logf("Input: %q\n", test.in) |
| 479 |
t.Logf("Expected UnmarshalHBCI() to return \n%+#v\n\tgot \n%+#v\n", expectedArray, actualArray) |
| 480 |
t.Fail() |
| 481 |
} |
| 482 |
} |
| 483 |
} |