Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blood): Added faker for blood types #40

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 54 additions & 0 deletions blood.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package faker

import (
"fmt"
"reflect"

"github.com/go-faker/faker/v4/pkg/options"
)

var bloodTypes = []string{"O", "A", "B", "AB"}
var bloodRhFactors = []string{"+", "-"}

func GetBlood(opts ...options.OptionFunc) Blooder {
opt := options.BuildOptions(opts)
db := &Blood{
fakerOption: *opt,
}
return db
}

type Blooder interface {
BloodType(v reflect.Value) (interface{}, error)
BloodRHFactor(v reflect.Value) (interface{}, error)
BloodGroup(v reflect.Value) (interface{}, error)
}

// Internet struct
type Blood struct {
fakerOption options.Options
}

func (b Blood) bloodType() string {
return randomElementFromSliceString(bloodTypes)
}

func (b Blood) BloodType(v reflect.Value) (interface{}, error) {
return b.bloodType(), nil
}

func (b Blood) bloodRhFactor() string {
return randomElementFromSliceString(bloodRhFactors)
}

func (b Blood) BloodRHFactor(v reflect.Value) (interface{}, error) {
return b.bloodRhFactor(), nil
}

func (b Blood) bloodGroup() string {
return fmt.Sprintf("%s%s", b.bloodType(), b.bloodRhFactor())
}

func (b Blood) BloodGroup(v reflect.Value) (interface{}, error) {
return b.bloodGroup(), nil
}
41 changes: 41 additions & 0 deletions blood_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package faker

import (
"reflect"
"strings"
"testing"

"github.com/go-faker/faker/v4/pkg/slice"
)

func TestBloodType(t *testing.T) {
bloodType, err := GetBlood().BloodType(reflect.Value{})
if err != nil {
t.Error("Expected not error, got err", err)
}
if !slice.Contains(bloodTypes, bloodType.(string)) {
t.Error("Expected value from variable bloodType in function BloodType")
}
}

func TestBloodRhFactor(t *testing.T) {
bloodRhFactor, err := GetBlood().BloodRHFactor(reflect.Value{})
if err != nil {
t.Error("Expected not error, got err", err)
}
if !slice.Contains(bloodRhFactors, bloodRhFactor.(string)) {
t.Error("Expected value from variable bloodRhFactor in function BloodType")
}
}

func TestBloodGroup(t *testing.T) {
bloodTypes = []string{"O"}
bloodRhFactors = []string{"+"}
bloodGroup, err := GetBlood().BloodGroup(reflect.Value{})
if err != nil {
t.Error("Expected not error, got err", err)
}
if !strings.Contains(bloodGroup.(string), "O+") {
t.Error("Expected get url")
}
}