Skip to content

Commit

Permalink
feat: add email procedures of internet provider #17 (#68)
Browse files Browse the repository at this point in the history
* add internet module template

* update providers

* update

* add test cases

* update

* fix

* Revert "fix"

This reverts commit e2832a7.

* Revert "Revert "fix""

This reverts commit 17f0f94.

* update tool

* update

* update

* add test cases
  • Loading branch information
jiro4989 authored Jan 12, 2021
1 parent 998e213 commit c80674c
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/faker/provider.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# See 'tools/generator'. #
# ----------------------------------------------- #

import provider/[address, automotive, bank, company, currency, file, isbn, job, lorem, misc, person, phone_number, user_agent]
export address, automotive, bank, company, currency, file, isbn, job, lorem, misc, person, phone_number, user_agent
import provider/[address, automotive, bank, company, currency, file, internet, isbn, job, lorem, misc, person, phone_number, user_agent]
export address, automotive, bank, company, currency, file, internet, isbn, job, lorem, misc, person, phone_number, user_agent
74 changes: 74 additions & 0 deletions src/faker/provider/internet.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# ----------------------------------------------- #
# This module was generated by 'generator' tool . #
# See 'tools/generator'. #
# ----------------------------------------------- #

import ../base
import internet/[internet_en_US, internet_ja_JP]
export base

proc safeDomainName*(f: Faker): string =
## Generates random safeDomainName.
runnableExamples:
let f = newFaker()
echo f.safeDomainName()

case f.locale
of "en_US": internet_en_US.safeDomainName(f)
of "ja_JP": internet_ja_JP.safeDomainName(f)
else: internet_en_US.safeDomainName(f)

proc freeDomainName*(f: Faker): string =
## Generates random freeDomainName.
runnableExamples:
let f = newFaker()
echo f.freeDomainName()

case f.locale
of "en_US": internet_en_US.freeDomainName(f)
of "ja_JP": internet_ja_JP.freeDomainName(f)
else: internet_en_US.freeDomainName(f)

proc topLevelDomain*(f: Faker): string =
## Generates random topLevelDomain.
runnableExamples:
let f = newFaker()
echo f.topLevelDomain()

case f.locale
of "en_US": internet_en_US.topLevelDomain(f)
of "ja_JP": internet_ja_JP.topLevelDomain(f)
else: internet_en_US.topLevelDomain(f)

proc safeEmail*(f: Faker): string =
## Generates random safeEmail.
runnableExamples:
let f = newFaker()
echo f.safeEmail()

case f.locale
of "en_US": internet_en_US.safeEmail(f)
of "ja_JP": internet_ja_JP.safeEmail(f)
else: internet_en_US.safeEmail(f)

proc freeEmail*(f: Faker): string =
## Generates random freeEmail.
runnableExamples:
let f = newFaker()
echo f.freeEmail()

case f.locale
of "en_US": internet_en_US.freeEmail(f)
of "ja_JP": internet_ja_JP.freeEmail(f)
else: internet_en_US.freeEmail(f)

proc email*(f: Faker): string =
## Generates random email.
runnableExamples:
let f = newFaker()
echo f.email()

case f.locale
of "en_US": internet_en_US.email(f)
of "ja_JP": internet_ja_JP.email(f)
else: internet_en_US.email(f)
27 changes: 27 additions & 0 deletions src/faker/provider/internet/interfaces.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# interfaces はプロシージャの定義のみをまとめたモジュール。
# C言語でいうヘッダファイルのように使用する。
#
# このモジュールをincludeしたモジュールは、このモジュールに定義しているプロシー
# ジャを全て実装しないと、コンパイルが通らない。

import random
import ../../base
export base

const
safeDomainNames = @["example.org", "example.com", "example.net"]
freeDomainNames = @["gmail.com", "yahoo.co.jp", "hotmail.com"]
topLevelDomains = @["com", "biz", "info", "net", "org"]

proc safeDomainName*(f: Faker): string =
f.rand.sample(safeDomainNames)

proc freeDomainName*(f: Faker): string =
f.rand.sample(freeDomainNames)

proc topLevelDomain*(f: Faker): string =
f.rand.sample(topLevelDomains)

proc safeEmail*(f: Faker): string
proc freeEmail*(f: Faker): string
proc email*(f: Faker): string
17 changes: 17 additions & 0 deletions src/faker/provider/internet/internet_en_US.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
include interfaces
import strutils
import ../person

proc safeEmail*(f: Faker): string =
f.firstName.toLower & "@" & f.safeDomainName

proc freeEmail*(f: Faker): string =
f.firstName.toLower & "@" & f.freeDomainName

proc email*(f: Faker): string =
let domain =
if rand(1) == 0:
f.freeDomainName
else:
f.safeDomainName
f.firstName.toLower & "@" & domain
17 changes: 17 additions & 0 deletions src/faker/provider/internet/internet_ja_JP.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
include interfaces
import strutils
import ../person/person_ja_JP

proc safeEmail*(f: Faker): string =
f.firstRomanizedName.toLower & "@" & f.safeDomainName

proc freeEmail*(f: Faker): string =
f.firstRomanizedName.toLower & "@" & f.freeDomainName

proc email*(f: Faker): string =
let domain =
if rand(1) == 0:
f.freeDomainName
else:
f.safeDomainName
f.firstRomanizedName.toLower & "@" & domain
8 changes: 7 additions & 1 deletion tests/test1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ suite "basic usage":
let locales = ["en_US", "ja_JP"]
for locale in locales:
let fake = newFaker(locale)
for i in 1..10:
for i in 1..100:
echo fake.address()
echo fake.phoneNumber()
echo fake.job()
Expand Down Expand Up @@ -42,4 +42,10 @@ suite "basic usage":
echo fake.binary(1024)
echo fake.md5
echo fake.sha1
echo fake.safeDomainName
echo fake.freeDomainName
echo fake.safeEmail
echo fake.freeEmail
echo fake.email
echo fake.topLevelDomain
echo "-----"
2 changes: 2 additions & 0 deletions tools/generator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ proc parseArgs(line: string): seq[string] =

proc parseReturnType(line: string): string =
result = line.split("):")[1].strip
if result.endsWith(" ="):
return result[0..^3]

proc parseProcDef(code: string): seq[ProcDef] =
## コードをパースしてプロシージャ定義を抽出する
Expand Down

0 comments on commit c80674c

Please sign in to comment.