2020-03-31 00:19:18 -04:00
package ofxgo
2017-03-13 21:09:15 -04:00
import (
"fmt"
2017-11-18 05:58:32 -05:00
"github.com/aclindsa/xml"
2017-03-13 21:09:15 -04:00
"reflect"
"testing"
"time"
)
func getTypeName ( i interface { } ) string {
val := reflect . ValueOf ( i )
// Do the same thing that encoding/xml does to get the name
for val . Kind ( ) == reflect . Interface || val . Kind ( ) == reflect . Ptr {
if val . IsNil ( ) {
return ""
}
val = val . Elem ( )
}
return val . Type ( ) . Name ( )
}
func marshalHelper ( t * testing . T , expected string , i interface { } ) {
2017-10-06 05:53:19 -04:00
t . Helper ( )
2017-03-13 21:09:15 -04:00
typename := getTypeName ( i )
expectedstring := fmt . Sprintf ( "<%s>%s</%s>" , typename , expected , typename )
b , err := xml . Marshal ( i )
if err != nil {
t . Fatalf ( "Unexpected error on xml.Marshal(%T): %s\n" , i , err )
}
if string ( b ) != expectedstring {
t . Fatalf ( "Expected '%s', got '%s'\n" , expectedstring , string ( b ) )
}
}
func unmarshalHelper2 ( t * testing . T , input string , expected interface { } , overwritten interface { } , eq func ( a , b interface { } ) bool ) {
2017-10-06 05:53:19 -04:00
t . Helper ( )
2017-03-13 21:09:15 -04:00
typename := getTypeName ( expected )
inputstring := fmt . Sprintf ( "<%s>%s</%s>" , typename , input , typename )
err := xml . Unmarshal ( [ ] byte ( inputstring ) , & overwritten )
if err != nil {
t . Fatalf ( "Unexpected error on xml.Unmarshal(%T): %s\n" , expected , err )
}
if ! eq ( overwritten , expected ) {
t . Fatalf ( "Expected '%s', got '%s'\n" , expected , overwritten )
}
}
func unmarshalHelper ( t * testing . T , input string , expected interface { } , overwritten interface { } ) {
2017-10-06 05:53:19 -04:00
t . Helper ( )
2017-03-13 21:09:15 -04:00
eq := func ( a , b interface { } ) bool {
return reflect . DeepEqual ( a , b )
}
unmarshalHelper2 ( t , input , expected , overwritten , eq )
}
func TestMarshalInt ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var i Int = 927
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "927" , & i )
i = 0
marshalHelper ( t , "0" , & i )
i = - 768276587425
marshalHelper ( t , "-768276587425" , & i )
}
func TestUnmarshalInt ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var i , overwritten Int = - 48394 , 0
2017-03-13 21:09:15 -04:00
unmarshalHelper ( t , "-48394" , & i , & overwritten )
i = 0
unmarshalHelper ( t , "0" , & i , & overwritten )
i = 198237198
unmarshalHelper ( t , "198237198" , & i , & overwritten )
2017-03-19 20:46:01 -04:00
// Make sure stray newlines are handled properly
unmarshalHelper ( t , "198237198\n" , & i , & overwritten )
2017-04-04 20:22:12 -04:00
unmarshalHelper ( t , "198237198\n\t" , & i , & overwritten )
2017-03-13 21:09:15 -04:00
}
func TestMarshalAmount ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var a Amount
2017-03-13 21:09:15 -04:00
2017-04-03 21:15:08 -04:00
a . SetFrac64 ( 8 , 1 )
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "8" , & a )
2017-04-03 21:15:08 -04:00
a . SetFrac64 ( 1 , 8 )
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "0.125" , & a )
2017-04-03 21:15:08 -04:00
a . SetFrac64 ( - 1 , 200 )
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "-0.005" , & a )
2017-04-03 21:15:08 -04:00
a . SetInt64 ( 0 )
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "0" , & a )
2017-04-03 21:15:08 -04:00
a . SetInt64 ( - 768276587425 )
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "-768276587425" , & a )
2017-04-03 21:15:08 -04:00
a . SetFrac64 ( 1 , 12 )
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "0.0833333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333" , & a )
}
func TestUnmarshalAmount ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var a , overwritten Amount
2017-03-13 21:09:15 -04:00
// Amount/big.Rat needs a special equality test because reflect.DeepEqual
// doesn't always return equal for two values that big.Rat.Cmp() does
eq := func ( a , b interface { } ) bool {
2020-03-31 00:19:18 -04:00
if amountA , ok := a . ( * Amount ) ; ok {
if amountB , ok2 := b . ( * Amount ) ; ok2 {
2017-04-03 21:15:08 -04:00
return amountA . Cmp ( & amountB . Rat ) == 0
2017-03-13 21:09:15 -04:00
}
}
return false
}
2017-04-03 21:15:08 -04:00
a . SetFrac64 ( 12 , 1 )
2017-03-13 21:09:15 -04:00
unmarshalHelper2 ( t , "12" , & a , & overwritten , eq )
2017-04-03 21:15:08 -04:00
a . SetFrac64 ( - 21309 , 100 )
2017-03-13 21:09:15 -04:00
unmarshalHelper2 ( t , "-213.09" , & a , & overwritten , eq )
2017-04-03 21:15:08 -04:00
a . SetFrac64 ( 8192 , 1000 )
2017-03-13 21:09:15 -04:00
unmarshalHelper2 ( t , "8.192" , & a , & overwritten , eq )
unmarshalHelper2 ( t , "+8.192" , & a , & overwritten , eq )
2017-04-03 21:15:08 -04:00
a . SetInt64 ( 0 )
2017-03-13 21:09:15 -04:00
unmarshalHelper2 ( t , "0" , & a , & overwritten , eq )
unmarshalHelper2 ( t , "+0" , & a , & overwritten , eq )
unmarshalHelper2 ( t , "-0" , & a , & overwritten , eq )
2017-04-03 21:15:08 -04:00
a . SetInt64 ( - 19487135 )
2017-03-13 21:09:15 -04:00
unmarshalHelper2 ( t , "-19487135" , & a , & overwritten , eq )
2017-03-19 20:46:01 -04:00
// Make sure stray newlines are handled properly
unmarshalHelper2 ( t , "-19487135\n" , & a , & overwritten , eq )
2017-04-04 20:22:12 -04:00
unmarshalHelper2 ( t , "-19487135\n \t " , & a , & overwritten , eq )
2017-03-13 21:09:15 -04:00
}
2017-04-04 20:20:31 -04:00
func TestAmountEqual ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
assertEq := func ( a , b Amount ) {
2017-04-04 20:20:31 -04:00
if ! a . Equal ( b ) {
t . Fatalf ( "Amounts should be equal but Equal returned false: %s and %s\n" , a , b )
}
}
2020-03-31 00:19:18 -04:00
assertNEq := func ( a , b Amount ) {
2017-04-04 20:20:31 -04:00
if a . Equal ( b ) {
t . Fatalf ( "Amounts should not be equal but Equal returned true: %s and %s\n" , a , b )
}
}
2020-03-31 00:19:18 -04:00
var a , b Amount
2017-04-04 20:20:31 -04:00
a . SetInt64 ( - 19487135 )
b . SetInt64 ( - 19487135 )
assertEq ( a , b )
b . SetInt64 ( 19487135 )
assertNEq ( a , b )
b . SetInt64 ( 0 )
assertNEq ( a , b )
a . SetInt64 ( - 0 )
assertEq ( a , b )
a . SetFrac64 ( 1 , 1000000000000000000 )
b . SetFrac64 ( 1 , 1000000000000000001 )
assertNEq ( a , b )
}
2017-03-13 21:09:15 -04:00
func TestMarshalDate ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var d * Date
2017-03-13 21:09:15 -04:00
UTC := time . FixedZone ( "UTC" , 0 )
2020-10-06 23:09:18 -04:00
GMTNodesc := time . FixedZone ( "" , 0 )
2017-03-13 21:09:15 -04:00
EST := time . FixedZone ( "EST" , - 5 * 60 * 60 )
NPT := time . FixedZone ( "NPT" , ( 5 * 60 + 45 ) * 60 )
IST := time . FixedZone ( "IST" , ( 5 * 60 + 30 ) * 60 )
NST := time . FixedZone ( "NST" , - ( 3 * 60 + 30 ) * 60 )
2020-03-31 00:19:18 -04:00
d = NewDateGMT ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "20170314150926.053[0:GMT]" , d )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , NPT )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "20170314150926.053[5.75:NPT]" , d )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , EST )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "20170314150926.053[-5:EST]" , d )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , UTC )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "20170314150926.053[0:UTC]" , d )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , IST )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "20170314150926.053[5.50:IST]" , d )
2020-03-31 00:19:18 -04:00
d = NewDate ( 9999 , 11 , 1 , 23 , 59 , 59 , 1000 , EST )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "99991101235959.000[-5:EST]" , d )
2020-03-31 00:19:18 -04:00
d = NewDate ( 0 , 1 , 1 , 0 , 0 , 0 , 0 , IST )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "00000101000000.000[5.50:IST]" , d )
2020-03-31 00:19:18 -04:00
d = & Date { Time : time . Unix ( 0 , 0 ) . In ( UTC ) }
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "19700101000000.000[0:UTC]" , d )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 0 , 0 , 26 , 53 * 1000 * 1000 , EST )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "20170314000026.053[-5:EST]" , d )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 0 , 0 , 26 , 53 * 1000 * 1000 , NST )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "20170314000026.053[-3.50:NST]" , d )
2017-03-13 21:09:15 -04:00
// Time zone without textual description
2020-10-06 23:09:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , GMTNodesc )
2017-04-04 05:45:19 -04:00
marshalHelper ( t , "20170314150926.053[0]" , d )
2017-03-13 21:09:15 -04:00
}
func TestUnmarshalDate ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var d * Date
var overwritten Date
2017-03-13 21:09:15 -04:00
GMT := time . FixedZone ( "GMT" , 0 )
EST := time . FixedZone ( "EST" , - 5 * 60 * 60 )
NPT := time . FixedZone ( "NPT" , ( 5 * 60 + 45 ) * 60 )
IST := time . FixedZone ( "IST" , ( 5 * 60 + 30 ) * 60 )
NST := time . FixedZone ( "NST" , - ( 3 * 60 + 30 ) * 60 )
2020-10-06 23:09:18 -04:00
NSTNodesc := time . FixedZone ( "" , - ( 3 * 60 + 30 ) * 60 )
2017-03-13 21:09:15 -04:00
2017-03-19 20:46:01 -04:00
eq := func ( a , b interface { } ) bool {
2020-03-31 00:19:18 -04:00
if dateA , ok := a . ( * Date ) ; ok {
if dateB , ok2 := b . ( * Date ) ; ok2 {
2017-04-04 05:45:19 -04:00
return dateA . Equal ( * dateB )
2017-03-19 20:46:01 -04:00
}
}
return false
}
2017-03-13 21:09:15 -04:00
// Ensure omitted fields default to the correct values
2020-03-31 00:19:18 -04:00
d = NewDateGMT ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314150926.053[0]" , d , & overwritten , eq )
unmarshalHelper2 ( t , "20170314150926.053" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 0 , 0 , 0 , 0 , GMT )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314" , d , & overwritten , eq )
2017-03-13 21:09:15 -04:00
// Ensure all signs on time zone offsets are properly handled
2020-03-31 00:19:18 -04:00
d = NewDateGMT ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314150926.053[0:GMT]" , d , & overwritten , eq )
unmarshalHelper2 ( t , "20170314150926.053[+0:GMT]" , d , & overwritten , eq )
unmarshalHelper2 ( t , "20170314150926.053[-0:GMT]" , d , & overwritten , eq )
unmarshalHelper2 ( t , "20170314150926.053[0]" , d , & overwritten , eq )
unmarshalHelper2 ( t , "20170314150926.053[+0]" , d , & overwritten , eq )
unmarshalHelper2 ( t , "20170314150926.053[-0]" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , NPT )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314150926.053[5.75:NPT]" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , EST )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314150926.053[-5:EST]" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , GMT )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314150926.053[0:GMT]" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , IST )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314150926.053[5.50:IST]" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2018 , 11 , 1 , 23 , 59 , 58 , 0 , EST )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20181101235958.000[-5:EST]" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = NewDate ( 0 , 1 , 1 , 0 , 0 , 0 , 0 , IST )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "00000101000000.000[5.50:IST]" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = & Date { Time : time . Unix ( 0 , 0 ) . In ( GMT ) }
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "19700101000000.000[0:GMT]" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 0 , 0 , 26 , 53 * 1000 * 1000 , EST )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314000026.053[-5:EST]" , d , & overwritten , eq )
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 0 , 0 , 26 , 53 * 1000 * 1000 , NST )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314000026.053[-3.50:NST]" , d , & overwritten , eq )
2017-03-13 21:09:15 -04:00
// Autopopulate zone without textual description for GMT
2020-03-31 00:19:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , GMT )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314150926.053[0]" , d , & overwritten , eq )
2017-03-13 21:09:15 -04:00
// but not for others:
2020-10-06 23:09:18 -04:00
d = NewDate ( 2017 , 3 , 14 , 0 , 0 , 26 , 53 * 1000 * 1000 , NSTNodesc )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20170314000026.053[-3.50]" , d , & overwritten , eq )
2017-03-19 20:46:01 -04:00
// Make sure we handle poorly-formatted dates (from Vanguard)
2020-03-31 00:19:18 -04:00
d = NewDate ( 2016 , 12 , 7 , 16 , 0 , 0 , 0 , EST )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20161207160000.000[-5:EST]610900.500[-9:BST]" , d , & overwritten , eq ) // extra part intentionally different to ensure the first timezone is parsed
2017-03-19 20:46:01 -04:00
// Make sure we properly handle ending newlines
2020-03-31 00:19:18 -04:00
d = NewDate ( 2018 , 11 , 1 , 23 , 59 , 58 , 0 , EST )
2017-04-04 05:45:19 -04:00
unmarshalHelper2 ( t , "20181101235958.000[-5:EST]\n" , d , & overwritten , eq )
2017-04-04 20:22:12 -04:00
unmarshalHelper2 ( t , "20181101235958.000[-5:EST]\n\t" , d , & overwritten , eq )
2017-03-13 21:09:15 -04:00
}
2017-04-04 19:51:35 -04:00
func TestDateEqual ( t * testing . T ) {
GMT := time . FixedZone ( "GMT" , 0 )
EST := time . FixedZone ( "EST" , - 5 * 60 * 60 )
2020-03-31 00:19:18 -04:00
assertEq := func ( a , b * Date ) {
2017-04-04 19:51:35 -04:00
if ! a . Equal ( * b ) {
t . Fatalf ( "Dates should be equal but Equal returned false: %s and %s\n" , * a , * b )
}
}
2020-03-31 00:19:18 -04:00
assertNEq := func ( a , b * Date ) {
2017-04-04 19:51:35 -04:00
if a . Equal ( * b ) {
t . Fatalf ( "Dates should not be equal but Equal returned true: %s and %s\n" , * a , * b )
}
}
// Ensure omitted fields default to the correct values
2020-03-31 00:19:18 -04:00
gmt1 := NewDateGMT ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 )
gmt2 := NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , GMT )
est1 := NewDate ( 2017 , 3 , 14 , 10 , 9 , 26 , 53 * 1000 * 1000 , EST )
est2 := NewDate ( 2017 , 3 , 14 , 10 , 9 , 26 , 53 * 1000 * 1000 + 1 , EST )
est3 := NewDate ( 2017 , 3 , 14 , 15 , 9 , 26 , 53 * 1000 * 1000 , EST )
2017-04-04 19:51:35 -04:00
assertEq ( gmt1 , gmt2 )
assertEq ( gmt2 , gmt1 )
assertEq ( gmt1 , est1 )
assertNEq ( gmt1 , est2 )
assertNEq ( est1 , est2 )
assertNEq ( gmt1 , est3 )
}
2017-03-13 21:09:15 -04:00
func TestMarshalString ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var s String = ""
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "" , & s )
s = "foo&bar"
marshalHelper ( t , "foo&bar" , & s )
s = "\n"
marshalHelper ( t , "
" , & s )
s = "Some Name"
marshalHelper ( t , "Some Name" , & s )
}
func TestUnmarshalString ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var s , overwritten String = "" , ""
2017-03-13 21:09:15 -04:00
unmarshalHelper ( t , "" , & s , & overwritten )
s = "foo&bar"
unmarshalHelper ( t , "foo&bar" , & s , & overwritten )
// whitespace intentionally stripped because some OFX servers add newlines
// inside tags
s = "new\nline"
unmarshalHelper ( t , " new
line
" , & s , & overwritten )
s = "Some Name"
unmarshalHelper ( t , "Some Name" , & s , & overwritten )
2017-04-03 19:44:25 -04:00
// Make sure stray newlines are handled properly
unmarshalHelper ( t , "Some Name\n" , & s , & overwritten )
2017-04-04 20:22:12 -04:00
unmarshalHelper ( t , "Some Name\n " , & s , & overwritten )
2017-03-13 21:09:15 -04:00
}
func TestMarshalBoolean ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var b Boolean = true
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "Y" , & b )
b = false
marshalHelper ( t , "N" , & b )
}
func TestUnmarshalBoolean ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var b , overwritten Boolean = true , false
2017-03-13 21:09:15 -04:00
unmarshalHelper ( t , "Y" , & b , & overwritten )
b = false
unmarshalHelper ( t , "N" , & b , & overwritten )
2017-04-03 19:44:25 -04:00
// Make sure stray newlines are handled properly
unmarshalHelper ( t , "N\n" , & b , & overwritten )
2017-04-04 20:22:12 -04:00
unmarshalHelper ( t , "N\n \t" , & b , & overwritten )
2017-03-13 21:09:15 -04:00
}
func TestMarshalUID ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var u UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04"
2017-03-13 21:09:15 -04:00
marshalHelper ( t , "d1cf3d3d-9ef9-4a97-b180-81706829cb04" , & u )
}
func TestUnmarshalUID ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var u , overwritten UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04" , ""
2017-03-13 21:09:15 -04:00
unmarshalHelper ( t , "d1cf3d3d-9ef9-4a97-b180-81706829cb04" , & u , & overwritten )
2017-04-03 19:44:25 -04:00
// Make sure stray newlines are handled properly
u = "0f94ce83-13b7-7568-e4fc-c02c7b47e7ab"
unmarshalHelper ( t , "0f94ce83-13b7-7568-e4fc-c02c7b47e7ab\n" , & u , & overwritten )
2017-04-04 20:22:12 -04:00
unmarshalHelper ( t , "0f94ce83-13b7-7568-e4fc-c02c7b47e7ab\n\t" , & u , & overwritten )
2017-03-13 21:09:15 -04:00
}
2017-03-29 05:41:28 -04:00
func TestUIDRecommendedFormat ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var u UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04"
2017-03-29 05:41:28 -04:00
if ok , err := u . RecommendedFormat ( ) ; ! ok || err != nil {
2017-03-13 21:09:15 -04:00
t . Fatalf ( "UID unexpectedly failed validation\n" )
}
u = "d1cf3d3d-9ef9-4a97-b180-81706829cb0"
2017-03-29 05:41:28 -04:00
if ok , err := u . RecommendedFormat ( ) ; ok || err == nil {
2017-03-13 21:09:15 -04:00
t . Fatalf ( "UID should have failed validation because it's too short\n" )
}
u = "d1cf3d3d-9ef94a97-b180-81706829cb04"
2017-03-29 05:41:28 -04:00
if ok , err := u . RecommendedFormat ( ) ; ok || err == nil {
2017-03-13 21:09:15 -04:00
t . Fatalf ( "UID should have failed validation because it's missing hyphens\n" )
}
u = "d1cf3d3d-9ef9-4a97-b180981706829cb04"
2017-03-29 05:41:28 -04:00
if ok , err := u . RecommendedFormat ( ) ; ok || err == nil {
2017-03-13 21:09:15 -04:00
t . Fatalf ( "UID should have failed validation because its hyphens have been replaced\n" )
}
}
2017-04-08 10:55:11 -04:00
2017-04-18 19:46:23 -04:00
func TestUIDValid ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var u UID = ""
2017-04-18 19:46:23 -04:00
if ok , err := u . Valid ( ) ; ok || err == nil {
t . Fatalf ( "Empty UID unexpectedly valid\n" )
}
u = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
if ok , err := u . Valid ( ) ; ok || err == nil {
t . Fatalf ( "Too-long UID unexpectedly valid\n" )
}
u = "7be37076-623a-425f-ae6b-a5465b7e93b0"
if ok , err := u . Valid ( ) ; ! ok || err != nil {
t . Fatalf ( "Good UID unexpectedly invalid: %s\n" , err . Error ( ) )
}
}
2017-04-08 10:55:11 -04:00
func TestRandomUID ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
uid , err := RandomUID ( )
2017-04-08 10:55:11 -04:00
if err != nil {
t . Fatalf ( "Unexpected error when calling RandomUID: %s\n" , err )
}
if ok , err := uid . RecommendedFormat ( ) ; ! ok || err != nil {
t . Fatalf ( "UID generated with RandomUID() doesn't match recommended format: %s\n" , err )
}
}
2017-04-17 20:11:53 -04:00
func TestMarshalCurrSymbol ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
c , _ := NewCurrSymbol ( "USD" )
2017-04-17 20:11:53 -04:00
marshalHelper ( t , "USD" , & c )
}
func TestUnmarshalCurrSymbol ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var overwritten CurrSymbol
c , _ := NewCurrSymbol ( "USD" )
2017-04-17 20:11:53 -04:00
unmarshalHelper ( t , "USD" , c , & overwritten )
// Make sure stray newlines are handled properly
2020-03-31 00:19:18 -04:00
c , _ = NewCurrSymbol ( "EUR" )
2017-04-17 20:11:53 -04:00
unmarshalHelper ( t , "EUR\n" , c , & overwritten )
unmarshalHelper ( t , "EUR\n\t" , c , & overwritten )
}
func TestCurrSymbolEqual ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
usd1 , _ := NewCurrSymbol ( "USD" )
usd2 , _ := NewCurrSymbol ( "USD" )
2017-04-17 20:11:53 -04:00
if ! usd1 . Equal ( * usd2 ) {
t . Fatalf ( "Two \"USD\" CurrSymbols returned !Equal()\n" )
}
2020-03-31 00:19:18 -04:00
xxx , _ := NewCurrSymbol ( "XXX" )
2017-04-17 20:11:53 -04:00
if usd1 . Equal ( * xxx ) {
t . Fatalf ( "\"USD\" and \"XXX\" CurrSymbols returned Equal()\n" )
}
}
func TestCurrSymbolValid ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
var initial CurrSymbol
2017-04-17 20:11:53 -04:00
ok , err := initial . Valid ( )
if ok || err == nil {
t . Fatalf ( "CurrSymbol unexpectedly returned Valid() for initial value\n" )
}
2020-03-31 00:19:18 -04:00
ars , _ := NewCurrSymbol ( "ARS" )
2017-04-17 20:11:53 -04:00
ok , err = ars . Valid ( )
if ! ok || err != nil {
t . Fatalf ( "CurrSymbol unexpectedly returned !Valid() for \"ARS\": %s\n" , err . Error ( ) )
}
2020-03-31 00:19:18 -04:00
xxx , _ := NewCurrSymbol ( "XXX" )
2017-04-17 20:11:53 -04:00
ok , err = xxx . Valid ( )
if ok || err == nil {
t . Fatalf ( "CurrSymbol unexpectedly returned Valid() for \"XXX\"\n" )
}
}
func TestNewCurrSymbol ( t * testing . T ) {
2020-03-31 00:19:18 -04:00
curr , err := NewCurrSymbol ( "GBP" )
2017-04-17 20:11:53 -04:00
if err != nil {
t . Fatalf ( "Unexpected error calling NewCurrSymbol: %s\n" , err )
}
if curr . String ( ) != "GBP" {
t . Fatalf ( "Created CurrSymbol doesn't print \"GBP\" as string representation\n" )
}
2020-03-31 00:19:18 -04:00
curr , err = NewCurrSymbol ( "AFN" )
2017-04-17 20:11:53 -04:00
if err != nil {
t . Fatalf ( "Unexpected error calling NewCurrSymbol: %s\n" , err )
}
if curr . String ( ) != "AFN" {
t . Fatalf ( "Created CurrSymbol doesn't print \"AFN\" as string representation\n" )
}
2020-03-31 00:19:18 -04:00
curr , err = NewCurrSymbol ( "BLAH" )
2017-04-17 20:11:53 -04:00
if err == nil {
t . Fatalf ( "NewCurrSymbol didn't error on invalid currency identifier\n" )
}
}