Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelbarrow committed Apr 3, 2015
2 parents bc5125f + 5226166 commit e13504d
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ braintree-go.test
braintree.sublime-workspace
braintree.sublime-project
.ruby-version
.env
6 changes: 3 additions & 3 deletions add_on_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func TestAddOn(t *testing.T) {
addOns, err := testGateway.AddOn().All()

if err != nil {
t.Error(err)
t.Fatal(err)
} else if len(addOns) != 1 {
t.Fatalf("expected to retrieve one add-on, but retrieved %d", len(addOns))
}
Expand All @@ -19,8 +19,8 @@ func TestAddOn(t *testing.T) {

if addOn.Id != "test_add_on" {
t.Fatalf("expected Id to be %s, was %s", "test_add_on", addOn.Id)
} else if addOn.Amount != 10 {
t.Fatalf("expected Amount to be %v, was %v", NewDecimal(1000, 2), addOn.Amount)
} else if addOn.Amount.Cmp(NewDecimal(1000, 2)) != 0 {
t.Fatalf("expected Amount to be %s, was %s", NewDecimal(1000, 2), addOn.Amount)
} else if addOn.Kind != ModificationKindAddOn {
t.Fatalf("expected Kind to be %s, was %s", ModificationKindAddOn, addOn.Kind)
} else if addOn.Name != "test_add_on_name" {
Expand Down
43 changes: 43 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (d *Decimal) UnmarshalText(text []byte) (err error) {
scale int = 0
)

if str == "" {
return nil
}

if i := strings.Index(str, "."); i != -1 {
scale = len(str) - i - 1
str = strings.Replace(str, ".", "", 1)
Expand All @@ -58,3 +62,42 @@ func (d *Decimal) UnmarshalText(text []byte) (err error) {

return nil
}

// Cmp compares x and y and returns:
//
// -1 if x < y
// 0 if x == y
// +1 if x > y
//
func (x *Decimal) Cmp(y *Decimal) int {
xUnscaled, yUnscaled := x.Unscaled, y.Unscaled
xScale, yScale := x.Scale, y.Scale

for ; xScale > yScale; xScale-- {
yUnscaled = yUnscaled * 10
}

for ; yScale > xScale; yScale-- {
xUnscaled = xUnscaled * 10
}

switch {
case xUnscaled < yUnscaled:
return -1
case xUnscaled > yUnscaled:
return 1
default:
return 0
}
}

// String returns string representation of Decimal
func (d *Decimal) String() string {
b, err := d.MarshalText()

if err != nil {
panic(err) //should never happen (see: MarshalText)
}

return string(b)
}
19 changes: 19 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,22 @@ func TestDecimalMarshalText(t *testing.T) {
}
}
}

func TestDecimalCmp(t *testing.T) {
tests := []struct {
x, y *Decimal
out int
}{
{NewDecimal(250, -2), NewDecimal(250, -2), 0},
{NewDecimal(2, 0), NewDecimal(250, -2), -1},
{NewDecimal(500, 2), NewDecimal(50, 1), 0},
{NewDecimal(2500, -2), NewDecimal(250, -2), 1},
{NewDecimal(100, 2), NewDecimal(1, 0), 0},
}

for i, tt := range tests {
if out := tt.x.Cmp(tt.y); out != tt.out {
t.Errorf("%d: %+v.Cmp(%+v) => %d, want %d", i, tt.x, tt.y, out, tt.out)
}
}
}
2 changes: 1 addition & 1 deletion disbursement.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Disbursement struct {
FollowUpAction string `xml:"follow-up-action"`
Success bool `xml:"success"`
Retry bool `xml:"retry"`
Amount float64 `xml:"amount"`
Amount *Decimal `xml:"amount"`
MerchantAccount *MerchantAccount `xml:"merchant-account"`
CurrencyIsoCode string `xml:"currency-iso-code"`
SubmerchantAccount bool `xml:"sub-merchant-account"`
Expand Down
8 changes: 4 additions & 4 deletions disbursement_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
type DisbursementDetails struct {
XMLName xml.Name `xml:"disbursement-details"`
DisbursementDate string `xml:"disbursement-date"`
SettlementAmount string `xml:"settlement-amount"` // float64
SettlementAmount *Decimal `xml:"settlement-amount"`
SettlementCurrencyIsoCode string `xml:"settlement-currency-iso-code"`
SettlementCurrencyExchangeRate string `xml:"settlement-currency-exchange-rate"` // float64
FundsHeld string `xml:"funds-held"` // bool
Success string `xml:"success"` // bool
SettlementCurrencyExchangeRate *Decimal `xml:"settlement-currency-exchange-rate"`
FundsHeld string `xml:"funds-held"` // bool
Success string `xml:"success"` // bool
}
4 changes: 2 additions & 2 deletions discount_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestDiscounts(t *testing.T) {

if discount.Id != "test_discount" {
t.Fatalf("expected Id to be %s, was %s", "test_discount", discount.Id)
} else if discount.Amount != 10 {
t.Fatalf("expected Amount to be %v, was %v", NewDecimal(1000, 2), discount.Amount)
} else if discount.Amount.Cmp(NewDecimal(1000, 2)) != 0 {
t.Fatalf("expected Amount to be %s, was %s", NewDecimal(1000, 2), discount.Amount)
} else if discount.Kind != ModificationKindDiscount {
t.Fatalf("expected Kind to be %s, was %s", ModificationKindDiscount, discount.Kind)
} else if discount.Name != "test_discount_name" {
Expand Down
2 changes: 1 addition & 1 deletion modification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const (

type Modification struct {
Id string `xml:"id,omitempty"`
Amount float64 `xml:"amount,omitempty"`
Amount *Decimal `xml:"amount,omitempty"`
Description string `xml:"description,omitempty"`
Kind string `xml:"kind,omitempty"`
Name string `xml:"name,omitempty"`
Expand Down
4 changes: 1 addition & 3 deletions plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ type Plan struct {
Description string `xml:"description"`
Name string `xml:"name"`
NumberOfBillingCycles string `xml:"number-of-billing-cycles"` // int
Price float64 `xml:"price"`
Price *Decimal `xml:"price"`
TrialDuration string `xml:"trial-duration"` // int
TrialDurationUnit string `xml:"trial-duration-unit"`
TrialPeriod string `xml:"trial-period"` // bool
CreatedAt *time.Time `xml:"created-at"`
UpdatedAt *time.Time `xml:"updated-at"`
// AddOns []interface{} `xml:"add-ons"`
// Discounts []interface{} `xml:"discounts"`
}

// TODO(eaigner): it is suboptimal that we use string instead of int/bool types here,
Expand Down
2 changes: 1 addition & 1 deletion plan_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestPlan(t *testing.T) {
if x := plan.NumberOfBillingCycles; x != "2" {
t.Fatal(x)
}
if x := plan.Price; x != 10.0 {
if x := plan.Price; x.Cmp(NewDecimal(1000, 2)) != 0 {
t.Fatal(x)
}
if x := plan.TrialDuration; x != "14" {
Expand Down
8 changes: 4 additions & 4 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
type Subscription struct {
XMLName string `xml:"subscription"`
Id string `xml:"id,omitempty"`
Balance float64 `xml:"balance,omitempty"`
Balance *Decimal `xml:"balance,omitempty"`
BillingDayOfMonth string `xml:"billing-day-of-month,omitempty"`
BillingPeriodEndDate string `xml:"billing-period-end-date,omitempty"`
BillingPeriodStartDate string `xml:"billing-period-start-date,omitempty"`
Expand All @@ -23,14 +23,14 @@ type Subscription struct {
FirstBillingDate string `xml:"first-billing-date,omitempty"`
MerchantAccountId string `xml:"merchant-account-id,omitempty"`
NeverExpires string `xml:"never-expires,omitempty"` // bool
NextBillAmount float64 `xml:"next-bill-amount,omitempty"`
NextBillingPeriodAmount float64 `xml:"next-billing-period-amount,omitempty"`
NextBillAmount *Decimal `xml:"next-bill-amount,omitempty"`
NextBillingPeriodAmount *Decimal `xml:"next-billing-period-amount,omitempty"`
NextBillingDate string `xml:"next-billing-date,omitempty"`
NumberOfBillingCycles string `xml:"number-of-billing-cycles,omitempty"` // int
PaidThroughDate string `xml:"paid-through-date,omitempty"`
PaymentMethodToken string `xml:"payment-method-token,omitempty"`
PlanId string `xml:"plan-id,omitempty"`
Price float64 `xml:"price,omitempty"`
Price *Decimal `xml:"price,omitempty"`
Status string `xml:"status,omitempty"`
TrialDuration string `xml:"trial-duration,omitempty"`
TrialDurationUnit string `xml:"trial-duration-unit,omitempty"`
Expand Down
25 changes: 12 additions & 13 deletions transaction_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package braintree

import (
"math/rand"
"reflect"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -46,8 +45,8 @@ func TestTransactionCreateSubmitForSettlementAndVoid(t *testing.T) {
if x := tx2.Status; x != "submitted_for_settlement" {
t.Fatal(x)
}
if amount := tx2.Amount; !reflect.DeepEqual(amount, ten) {
t.Fatalf("transaction settlement amount (%v) did not equal amount requested (%v)", amount, ten)
if amount := tx2.Amount; amount.Cmp(ten) != 0 {
t.Fatalf("transaction settlement amount (%s) did not equal amount requested (%s)", amount, ten)
}

// Void
Expand Down Expand Up @@ -203,8 +202,8 @@ func TestAllTransactionFields(t *testing.T) {
if tx2.Type != tx.Type {
t.Fatalf("expected Type to be equal, but %s was not %s", tx2.Type, tx.Type)
}
if !reflect.DeepEqual(tx2.Amount, tx.Amount) {
t.Fatalf("expected Amount to be equal, but %v was not %v", tx2.Amount, tx.Amount)
if tx2.Amount.Cmp(tx.Amount) != 0 {
t.Fatalf("expected Amount to be equal, but %s was not %s", tx2.Amount, tx.Amount)
}
if tx2.OrderId != tx.OrderId {
t.Fatalf("expected OrderId to be equal, but %s was not %s", tx2.OrderId, tx.OrderId)
Expand Down Expand Up @@ -255,22 +254,22 @@ func TestTransactionDisbursementDetails(t *testing.T) {
}

if txn.DisbursementDetails.DisbursementDate != "2013-06-27" {
t.Fail()
t.Fatalf("expected disbursement date to be %s, was %s", "2013-06-27", txn.DisbursementDetails.DisbursementDate)
}
if txn.DisbursementDetails.SettlementAmount != "100.00" {
t.Fail()
if txn.DisbursementDetails.SettlementAmount.Cmp(NewDecimal(10000, 2)) != 0 {
t.Fatalf("expected settlement amount to be %s, was %s", NewDecimal(10000, 2), txn.DisbursementDetails.SettlementAmount)
}
if txn.DisbursementDetails.SettlementCurrencyIsoCode != "USD" {
t.Fail()
t.Fatalf("expected settlement currency code to be %s, was %s", "USD", txn.DisbursementDetails.SettlementCurrencyIsoCode)
}
if txn.DisbursementDetails.SettlementCurrencyExchangeRate != "1" {
t.Fail()
if txn.DisbursementDetails.SettlementCurrencyExchangeRate.Cmp(NewDecimal(100, 2)) != 0 {
t.Fatalf("expected settlement currency exchange rate to be %s, was %s", NewDecimal(100, 2), txn.DisbursementDetails.SettlementCurrencyExchangeRate)
}
if txn.DisbursementDetails.FundsHeld == "true" {
t.Fail()
t.Fatalf("expected funds held to be %s, was %s", "true", txn.DisbursementDetails.FundsHeld)
}
if txn.DisbursementDetails.Success != "true" {
t.Fail()
t.Fatalf("expected success to be %s, was %s", "true", txn.DisbursementDetails.Success)
}
}

Expand Down

0 comments on commit e13504d

Please sign in to comment.