forked from bxcodec/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Test coverage up add tests for coverage up * errors in variables * return variable in start function * add internet fake * refactoring code * credit cart refactoring add new type and new file for credit cart * add test internet and add helpers function, which check slice string type to has element * coverage up test internet file * add payment and add test done concept * typo fix * fix variable internet in struct Internet * fix * delete old code * change init mutex * merge * add phone provider and test * change format in stdout structure * change format in stdout structure test * add TollFreePhoneNumber function * add e_164_phone_number * fix test * fix and done phone provider * test coverage up
- Loading branch information
1 parent
60d8932
commit 34279ec
Showing
5 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package faker | ||
|
||
import ( | ||
"fmt" | ||
"github.com/agoalofalife/faker/support/slice" | ||
"math/rand" | ||
"strings" | ||
) | ||
|
||
var phone Phoner | ||
|
||
// Constructor | ||
func getPhoner() Phoner { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
if phone == nil { | ||
phone = &Phone{} | ||
} | ||
return phone | ||
} | ||
|
||
// this set custom Phoner | ||
func SetPhoner(p Phoner) { | ||
phone = p | ||
} | ||
|
||
type Phoner interface { | ||
PhoneNumber() string | ||
TollFreePhoneNumber() string | ||
E164PhoneNumber() string | ||
} | ||
|
||
type Phone struct{} | ||
|
||
// 201-886-0269 | ||
func (p Phone) PhoneNumber() string { | ||
randInt, _ := RandomInt(1, 10) | ||
str:= strings.Join(slice.SliceIntToString(randInt), "") | ||
return fmt.Sprintf("%s-%s-%s", str[:3],str[3:6], str[6:10]) | ||
} | ||
|
||
// example : (888) 937-7238 | ||
func (p Phone) TollFreePhoneNumber() string { | ||
out := "" | ||
boxDigitsStart := []string{"777", "888"} | ||
|
||
ints, _ := RandomInt(1, 9) | ||
for index, v := range slice.SliceIntToString(ints) { | ||
if index == 3 { | ||
out += "-" | ||
} | ||
out += string(v) | ||
} | ||
return fmt.Sprintf("(%s) %s", boxDigitsStart[rand.Intn(1)], out) | ||
} | ||
|
||
// '+27113456789' | ||
func (p Phone) E164PhoneNumber() string { | ||
out := "" | ||
boxDigitsStart := []string{"7", "8"} | ||
ints, _ := RandomInt(1, 10) | ||
|
||
for _, v := range slice.SliceIntToString(ints) { | ||
out += string(v) | ||
} | ||
return fmt.Sprintf("+%s%s", boxDigitsStart[rand.Intn(1)], strings.Join(slice.SliceIntToString(ints), "")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package faker | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestPhoneNumber(t *testing.T) { | ||
ph := getPhoner() | ||
if strings.Count(ph.PhoneNumber(), "-") != 2 { | ||
t.Error("Expected no more than two characters '-'") | ||
} | ||
} | ||
|
||
func TestTollFreePhoneNumber(t *testing.T) { | ||
ph := getPhoner() | ||
|
||
if !strings.HasPrefix(ph.TollFreePhoneNumber(), "(888)") && !strings.HasPrefix(ph.TollFreePhoneNumber(), "(777)") { | ||
t.Error("Expected character '(888)' or (777), in function TollFreePhoneNumber") | ||
} | ||
} | ||
|
||
func TestE164PhoneNumber(t *testing.T) { | ||
ph := getPhoner() | ||
if !strings.HasPrefix(ph.E164PhoneNumber(), "+") { | ||
t.Error("Expected character '(888)', in function TollFreePhoneNumber") | ||
} | ||
} | ||
|
||
func TestSetPhoner(t *testing.T) { | ||
SetPhoner(Phone{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters