Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions assessment_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
package phass

import (
"fmt"
"math"
"strings"
"testing"
"time"
)

func TestPersonMeasurerInterface(t *testing.T) {
cases := []struct {
name string
dob string
gender int
err error
}{
{
name: "Person 1",
dob: "1978-Dec-15",
gender: Male,
err: nil,
},
{
name: "Person 2",
dob: "1988-Mar-08",
gender: Female,
err: nil,
},
}

for _, data := range cases {
p, err := NewPerson(data.name, data.dob, data.gender)
if err != data.err {
t.Error("This is a valid person")
}

if p.GetName() != "Person" {
t.Error("Wrong measurement name")
}

rs, err := p.Result()
if err != nil {
t.Error("This is a valid result")
} else if len(rs) != 4 {
t.Error("There should be 4 information about person")
}
for index, item := range rs {
switch index {
case 0:
if !strings.Contains(item, p.FullName) {
t.Error("Full name should be present")
}
case 1:
if !strings.Contains(item, p.genderRepr()) {
t.Error("Gender representation should be present")
}
case 2:
if !strings.Contains(item, fmt.Sprintf("%.0f", p.Age())) {
t.Error("Age in years should be present")
}
case 3:
if !strings.Contains(item, fmt.Sprintf("%.1f", p.AgeInMonths())) {
t.Error("Age in months should be present")
}
}
}
}
}

func TestPersonBirth(t *testing.T) {
_, err := NewPerson("Someone", "1900-Dec-40", Male)
if err == nil {
Expand Down
11 changes: 5 additions & 6 deletions bodyfat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package phass

import (
"math"
"strings"
"testing"
)

Expand Down Expand Up @@ -34,31 +33,31 @@ func TestBodyFatCompositionValidation(t *testing.T) {
map[int]float64{SKFTriceps: 10.5, SKFSuprailiac: 26.9, SKFThigh: 21.2},
"Too young",
0.0,
"Valid for age",
ErrInvalidAge.Error(),
),
newCaseBodyFat(
female,
"2048-Mar-15",
map[int]float64{SKFTriceps: 10.5, SKFSuprailiac: 26.9, SKFThigh: 21.2},
"Too old",
0.0,
"Valid for age",
ErrInvalidAge.Error(),
),
newCaseBodyFat(
male,
"1998-Dec-15",
map[int]float64{SKFTriceps: 10.5, SKFSuprailiac: 26.9, SKFThigh: 21.2},
"Wrong gender",
0.0,
"Valid for gender",
ErrInvalidGender.Error(),
),
newCaseBodyFat(
female,
"2008-Mar-15",
map[int]float64{SKFTriceps: 10.5},
"Without skinfolds",
0.0,
"Missing skinfold",
"Missing skinfold suprailiac",
),
newCaseBodyFat(
female,
Expand All @@ -74,7 +73,7 @@ func TestBodyFatCompositionValidation(t *testing.T) {
bc := newEquation(data.person, data.assessment, data.skinfold)
if calc, err := bc.Calc(); data.err != "" && err == nil {
t.Errorf("Case _%s_ failed, should show a validation error", data.name)
} else if data.err != "" && !strings.Contains(err.Error(), data.err) {
} else if data.err != "" && err.Error() != data.err {
t.Errorf("Case _%s_ failed, should show proper error message", data.name)
} else if data.calc > 0.0 && !floatEqual(calc, data.calc, 0.009) {
t.Errorf("Case _%s_ failed, should have value %.4f, instead got %.4f", data.name, data.calc, calc)
Expand Down
23 changes: 17 additions & 6 deletions common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package phass

import "fmt"
import (
"errors"
)

/**
* Equation
Expand Down Expand Up @@ -99,6 +101,15 @@ type Equationer interface {
* Validation
*/

// Validation error messages.
var (
ErrMissingGender = errors.New("Missing gender")
ErrInvalidGender = errors.New("Invalid gender")
ErrMissingAge = errors.New("Missing age")
ErrInvalidAge = errors.New("Invalid age")
ErrMissingMeasure = errors.New("Missing measure")
)

// ValidateGender returns a Validator function, that ensure gender is equal to
// the one expected.
func ValidateGender(expect int) Validator {
Expand All @@ -110,9 +121,9 @@ func ValidateGender(expect int) Validator {
// validateGender ensure that gender is set and matches the expected value.
func validateGender(expect int, e *Equation) (bool, error) {
if g, ok := e.In("gender"); !ok {
return false, fmt.Errorf("Missing gender")
return false, ErrMissingGender
} else if int(g) != expect {
return false, fmt.Errorf("Valid for gender %d", expect)
return false, ErrInvalidGender
}
return true, nil
}
Expand All @@ -128,9 +139,9 @@ func ValidateAge(lower, upper float64) Validator {
// validateAge ensure that age is set and is between limits.
func validateAge(lower, upper float64, e *Equation) (bool, error) {
if age, ok := e.In("age"); !ok {
return false, fmt.Errorf("Missing age measure")
return false, ErrMissingAge
} else if age < lower || age > upper {
return false, fmt.Errorf("Valid for ages between %.0f and %.0f", lower, upper)
return false, ErrInvalidAge
}
return true, nil
}
Expand All @@ -147,7 +158,7 @@ func ValidateMeasures(expect []string) Validator {
func validateMeasures(expect []string, e *Equation) (bool, error) {
for _, k := range expect {
if _, ok := e.In(k); !ok {
return false, fmt.Errorf("Missing %s measure", k)
return false, ErrMissingMeasure
}
}
return true, nil
Expand Down
Loading