cpf_cnpj 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +3 -0
- data/.github/dependabot.yml +15 -0
- data/.github/workflows/ruby-tests.yml +47 -0
- data/.rubocop.yml +10 -56
- data/README.md +24 -14
- data/Rakefile +4 -1
- data/bin/cnpj +1 -1
- data/bin/cpf +1 -1
- data/cpf_cnpj.gemspec +21 -14
- data/lib/cnpj/formatter.rb +4 -3
- data/lib/cnpj/verifier_digit.rb +1 -1
- data/lib/cnpj.rb +22 -13
- data/lib/cpf/formatter.rb +2 -2
- data/lib/cpf/verifier_digit.rb +1 -1
- data/lib/cpf.rb +22 -14
- data/lib/cpf_cnpj/cli.rb +1 -2
- data/lib/cpf_cnpj/generator.rb +2 -2
- data/lib/cpf_cnpj/version.rb +1 -1
- data/test/cnpj/cli_test.rb +112 -0
- data/test/{unit/cnpj → cnpj}/formatter_test.rb +2 -0
- data/test/{unit/cnpj_test.rb → cnpj_test.rb} +21 -2
- data/test/cpf/cli_test.rb +112 -0
- data/test/{unit/cpf → cpf}/formatter_test.rb +6 -4
- data/test/{unit/cpf_test.rb → cpf_test.rb} +15 -2
- data/test/support/run_command.rb +30 -0
- data/test/test_helper.rb +1 -1
- metadata +54 -32
- data/.travis.yml +0 -8
- data/test/support/capture_syscall.rb +0 -18
- data/test/unit/cnpj/cli_test.rb +0 -134
- data/test/unit/cpf/cli_test.rb +0 -134
- /data/test/{unit/cnpj → cnpj}/generator_test.rb +0 -0
- /data/test/{unit/cpf → cpf}/generator_test.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7b0bd493ddd22ed0ebe7128fd0e8dc17c8ff15bfb1209300e1ce60ae03b2c6b
|
4
|
+
data.tar.gz: c2f7a21d000907e8779790d8b72ce6177b58ddaa2fb0f369941d3e3d1989eae1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b58f3e8547dda2131b290a4db50c0c84da0ccaf266ebd241cef0d34e43eb935d0e31d1310185749f9a929d19db01136484d1b54bbb1abb9c41d351409c6b685b
|
7
|
+
data.tar.gz: 3d62c15ab2f3b0be301a55a5328368bcca1ccf1d2b453c3e28c01fa868dc10c403690b2ee2d33cd0bba375271a8a20b2415a77f477d3c3d2b941fad4049f8417
|
data/.github/FUNDING.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
# Documentation:
|
3
|
+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
4
|
+
|
5
|
+
version: 2
|
6
|
+
updates:
|
7
|
+
- package-ecosystem: "github-actions"
|
8
|
+
directory: "/"
|
9
|
+
schedule:
|
10
|
+
interval: "daily"
|
11
|
+
|
12
|
+
- package-ecosystem: bundler
|
13
|
+
directory: "/"
|
14
|
+
schedule:
|
15
|
+
interval: "daily"
|
@@ -0,0 +1,47 @@
|
|
1
|
+
---
|
2
|
+
name: ruby-tests
|
3
|
+
|
4
|
+
on:
|
5
|
+
push:
|
6
|
+
branches:
|
7
|
+
- main
|
8
|
+
pull_request_target:
|
9
|
+
workflow_dispatch:
|
10
|
+
inputs: {}
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
build:
|
14
|
+
name: Tests with Ruby ${{ matrix.ruby }}
|
15
|
+
runs-on: "ubuntu-latest"
|
16
|
+
if: |
|
17
|
+
github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target' ||
|
18
|
+
github.actor != 'dependabot[bot]'
|
19
|
+
strategy:
|
20
|
+
fail-fast: false
|
21
|
+
matrix:
|
22
|
+
ruby: ["3.2", "3.3"]
|
23
|
+
|
24
|
+
steps:
|
25
|
+
- uses: actions/checkout@v4
|
26
|
+
|
27
|
+
- uses: actions/cache@v4
|
28
|
+
with:
|
29
|
+
path: vendor/bundle
|
30
|
+
key: >
|
31
|
+
${{ runner.os }}-${{ matrix.ruby }}-gems-${{
|
32
|
+
hashFiles(matrix.gemfile) }}
|
33
|
+
|
34
|
+
- name: Set up Ruby
|
35
|
+
uses: ruby/setup-ruby@v1
|
36
|
+
with:
|
37
|
+
ruby-version: ${{ matrix.ruby }}
|
38
|
+
|
39
|
+
- name: Install gem dependencies
|
40
|
+
run: |
|
41
|
+
gem install bundler
|
42
|
+
bundle config path vendor/bundle
|
43
|
+
bundle update --jobs 4 --retry 3
|
44
|
+
|
45
|
+
- name: Run Tests
|
46
|
+
run: |
|
47
|
+
bundle exec rake
|
data/.rubocop.yml
CHANGED
@@ -1,59 +1,13 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
---
|
2
|
+
inherit_gem:
|
3
|
+
rubocop-fnando: .rubocop.yml
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
-
|
9
|
-
-
|
10
|
-
- '**/config.ru'
|
11
|
-
- '**/Gemfile'
|
12
|
-
- '**/Rakefile'
|
13
|
-
- '**/Capfile'
|
14
|
-
- '**/Guardfile'
|
15
|
-
- '**/Podfile'
|
16
|
-
- '**/Thorfile'
|
17
|
-
- '**/Vagrantfile'
|
18
|
-
- '**/Berksfile'
|
19
|
-
- '**/Cheffile'
|
20
|
-
- '**/Vagabondfile'
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 2.7
|
7
|
+
NewCops: enable
|
8
|
+
Exclude:
|
9
|
+
- vendor/**/*
|
10
|
+
- gemfiles/**/*
|
21
11
|
|
22
|
-
Style/
|
12
|
+
Style/OptionalBooleanParameter:
|
23
13
|
Enabled: false
|
24
|
-
|
25
|
-
Style/StringLiterals:
|
26
|
-
EnforcedStyle: double_quotes
|
27
|
-
|
28
|
-
Layout/SpaceInsideBlockBraces:
|
29
|
-
EnforcedStyle: space
|
30
|
-
EnforcedStyleForEmptyBraces: space
|
31
|
-
SpaceBeforeBlockParameters: false
|
32
|
-
|
33
|
-
Layout/SpaceInsideHashLiteralBraces:
|
34
|
-
EnforcedStyle: no_space
|
35
|
-
EnforcedStyleForEmptyBraces: no_space
|
36
|
-
|
37
|
-
Layout/FirstArrayElementLineBreak:
|
38
|
-
Enabled: true
|
39
|
-
|
40
|
-
Layout/FirstHashElementLineBreak:
|
41
|
-
Enabled: true
|
42
|
-
|
43
|
-
Style/SymbolArray:
|
44
|
-
Enabled: true
|
45
|
-
|
46
|
-
Style/PercentLiteralDelimiters:
|
47
|
-
PreferredDelimiters:
|
48
|
-
'%': '[]'
|
49
|
-
'%i': '[]'
|
50
|
-
'%q': '[]'
|
51
|
-
'%Q': '[]'
|
52
|
-
'%r': '[]'
|
53
|
-
'%s': '[]'
|
54
|
-
'%w': '[]'
|
55
|
-
'%W': '[]'
|
56
|
-
'%x': '[]'
|
57
|
-
|
58
|
-
Metrics/LineLength:
|
59
|
-
Max: 100
|
data/README.md
CHANGED
@@ -1,14 +1,19 @@
|
|
1
1
|
# CPF/CNPJ
|
2
2
|
|
3
|
-
[![
|
4
|
-
[![
|
5
|
-
[![Gem
|
3
|
+
[![Tests](https://github.com/fnando/cpf_cnpj/workflows/ruby-tests/badge.svg)](https://github.com/fnando/cpf_cnpj)
|
4
|
+
[![Gem](https://img.shields.io/gem/v/cpf_cnpj.svg)](https://rubygems.org/gems/cpf_cnpj)
|
5
|
+
[![Gem](https://img.shields.io/gem/dt/cpf_cnpj.svg)](https://rubygems.org/gems/cpf_cnpj)
|
6
6
|
|
7
|
-
This gem does some
|
7
|
+
This gem does some
|
8
|
+
[CPF](http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas)/[CNPJ](http://en.wikipedia.org/wiki/CNPJ)
|
9
|
+
magic. It allows you to create, validate and format CPF/CNPJ, even through the
|
10
|
+
command-line.
|
8
11
|
|
9
|
-
Just making my life easier when filling these damn numbers on internet bankings
|
12
|
+
Just making my life easier when filling these damn numbers on internet bankings
|
13
|
+
and government sites.
|
10
14
|
|
11
|
-
For ActiveModel/ActiveRecord validations, please check
|
15
|
+
For ActiveModel/ActiveRecord validations, please check
|
16
|
+
<https://github.com/fnando/validators>.
|
12
17
|
|
13
18
|
## Installation
|
14
19
|
|
@@ -28,24 +33,29 @@ Or install it yourself as:
|
|
28
33
|
|
29
34
|
### Ruby API
|
30
35
|
|
31
|
-
This library has the same API for both CNPJ/CPF, so only one of them is
|
36
|
+
This library has the same API for both CNPJ/CPF, so only one of them is
|
37
|
+
documented below.
|
32
38
|
|
33
39
|
```ruby
|
34
40
|
require "cpf_cnpj"
|
35
41
|
|
36
|
-
CPF.
|
37
|
-
CPF.
|
38
|
-
CPF.generate
|
42
|
+
CPF.format(number) # Format CPF (xxx.xxx.xxx-xx)
|
43
|
+
CPF.valid?(number) # Check if a CPF is valid
|
44
|
+
CPF.generate # Generate a random CPF number
|
45
|
+
CPF.generate(true) # Generate a formatted number
|
39
46
|
|
40
47
|
cpf = CPF.new(number)
|
41
|
-
cpf.formatted
|
42
|
-
cpf.stripped
|
43
|
-
cpf.valid?
|
48
|
+
cpf.formatted # Return formatted CPF (xxx.xxx.xxx-xx)
|
49
|
+
cpf.stripped # Return stripped CPF (xxxxxxxxxxx)
|
50
|
+
cpf.valid? # Check if CPF is valid
|
51
|
+
cpf.number_without_verifier # Return CPF without verifier digits
|
44
52
|
```
|
45
53
|
|
46
54
|
#### Strict Validation
|
47
55
|
|
48
|
-
By default, validations will strip any characters that aren't numbers. This
|
56
|
+
By default, validations will strip any characters that aren't numbers. This
|
57
|
+
means that `532#####820------857\n96` is considered a valid number. To perform a
|
58
|
+
strict validation use `strict: true`.
|
49
59
|
|
50
60
|
```ruby
|
51
61
|
CPF.valid?(number, strict: true)
|
data/Rakefile
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "bundler/gem_tasks"
|
4
4
|
require "rake/testtask"
|
5
|
+
require "rubocop/rake_task"
|
5
6
|
|
6
7
|
Rake::TestTask.new(:test) do |t|
|
7
8
|
t.libs << "test"
|
@@ -10,4 +11,6 @@ Rake::TestTask.new(:test) do |t|
|
|
10
11
|
t.warning = false
|
11
12
|
end
|
12
13
|
|
13
|
-
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
data/bin/cnpj
CHANGED
data/bin/cpf
CHANGED
data/cpf_cnpj.gemspec
CHANGED
@@ -2,20 +2,27 @@
|
|
2
2
|
|
3
3
|
require "./lib/cpf_cnpj/version"
|
4
4
|
|
5
|
-
Gem::Specification.new do |
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.authors = ["Nando Vieira"]
|
7
|
+
spec.email = ["fnando.vieira@gmail.com"]
|
8
|
+
spec.description = "Validate, generate and format CPF/CNPJ numbers. " \
|
9
|
+
"Include command-line tools."
|
10
|
+
spec.summary = spec.description
|
11
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
12
|
+
spec.homepage = "https://github.com/fnando/cpf_cnpj"
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
15
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
10
16
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
gem.version = CPF::VERSION
|
17
|
+
spec.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}).map {|f| File.basename(f) }
|
19
|
+
spec.name = "cpf_cnpj"
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
spec.version = CPF::VERSION
|
17
22
|
|
18
|
-
|
19
|
-
|
20
|
-
|
23
|
+
spec.add_development_dependency "minitest-utils"
|
24
|
+
spec.add_development_dependency "pry-meta"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rubocop"
|
27
|
+
spec.add_development_dependency "rubocop-fnando"
|
21
28
|
end
|
data/lib/cnpj/formatter.rb
CHANGED
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
class CNPJ
|
4
4
|
class Formatter
|
5
|
-
STRICT_REGEX = %r[
|
6
|
-
LOOSE_REGEX = /[^\d]
|
5
|
+
STRICT_REGEX = %r{[/.-]}.freeze
|
6
|
+
LOOSE_REGEX = /[^\d]/.freeze
|
7
7
|
|
8
8
|
def self.format(number)
|
9
|
-
strip(number)
|
9
|
+
strip(number)
|
10
|
+
.gsub(/\A(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})\Z/, "\\1.\\2.\\3/\\4-\\5")
|
10
11
|
end
|
11
12
|
|
12
13
|
def self.strip(number, strict = false)
|
data/lib/cnpj/verifier_digit.rb
CHANGED
data/lib/cnpj.rb
CHANGED
@@ -5,14 +5,13 @@ class CNPJ
|
|
5
5
|
require "cnpj/formatter"
|
6
6
|
require "cnpj/verifier_digit"
|
7
7
|
|
8
|
-
attr_reader :number
|
9
|
-
attr_reader :strict
|
8
|
+
attr_reader :number, :strict
|
10
9
|
|
11
|
-
REGEX = %r[\A\d{2}\.\d{3}.\d{3}/\d{4}-\d{2}\Z]
|
12
|
-
VALIDATION_SIZE_REGEX = /^\d{14}
|
10
|
+
REGEX = %r[\A\d{2}\.\d{3}.\d{3}/\d{4}-\d{2}\Z].freeze
|
11
|
+
VALIDATION_SIZE_REGEX = /^\d{14}$/.freeze
|
13
12
|
NUMBER_SIZE = 12
|
14
13
|
|
15
|
-
|
14
|
+
DENYLIST = %w[
|
16
15
|
00000000000000
|
17
16
|
11111111111111
|
18
17
|
22222222222222
|
@@ -25,6 +24,10 @@ class CNPJ
|
|
25
24
|
99999999999999
|
26
25
|
].freeze
|
27
26
|
|
27
|
+
def self.format(number)
|
28
|
+
new(number).formatted
|
29
|
+
end
|
30
|
+
|
28
31
|
def self.valid?(number, strict: false)
|
29
32
|
new(number, strict).valid?
|
30
33
|
end
|
@@ -56,24 +59,30 @@ class CNPJ
|
|
56
59
|
end
|
57
60
|
|
58
61
|
def valid?
|
59
|
-
|
60
|
-
|
62
|
+
if strict && !(number.match?(REGEX) || number.match?(VALIDATION_SIZE_REGEX))
|
63
|
+
return false
|
64
|
+
end
|
61
65
|
|
62
|
-
|
66
|
+
return false unless stripped.match?(VALIDATION_SIZE_REGEX)
|
67
|
+
return false if DENYLIST.include?(stripped)
|
68
|
+
|
69
|
+
digits = numbers[0...NUMBER_SIZE]
|
63
70
|
digits << VerifierDigit.generate(digits)
|
64
71
|
digits << VerifierDigit.generate(digits)
|
65
72
|
|
66
73
|
digits[-2, 2] == numbers[-2, 2]
|
67
74
|
end
|
68
75
|
|
69
|
-
def ==(
|
70
|
-
super ||
|
76
|
+
def ==(other)
|
77
|
+
super || (other.instance_of?(self.class) && other.stripped == stripped)
|
71
78
|
end
|
72
|
-
alias
|
79
|
+
alias eql? ==
|
73
80
|
|
74
|
-
|
81
|
+
def number_without_verifier
|
82
|
+
numbers[0...NUMBER_SIZE].join
|
83
|
+
end
|
75
84
|
|
76
|
-
def numbers
|
85
|
+
private def numbers
|
77
86
|
@numbers ||= stripped.each_char.to_a.map(&:to_i)
|
78
87
|
end
|
79
88
|
end
|
data/lib/cpf/formatter.rb
CHANGED
data/lib/cpf/verifier_digit.rb
CHANGED
data/lib/cpf.rb
CHANGED
@@ -5,14 +5,13 @@ class CPF
|
|
5
5
|
require "cpf/formatter"
|
6
6
|
require "cpf/verifier_digit"
|
7
7
|
|
8
|
-
attr_reader :number
|
9
|
-
attr_reader :strict
|
8
|
+
attr_reader :number, :strict
|
10
9
|
|
11
|
-
REGEX = /\A\d{3}\.\d{3}\.\d{3}-\d{2}\Z
|
12
|
-
VALIDATION_SIZE_REGEX =
|
10
|
+
REGEX = /\A\d{3}\.\d{3}\.\d{3}-\d{2}\Z/.freeze
|
11
|
+
VALIDATION_SIZE_REGEX = /^\d{11}$/.freeze
|
13
12
|
NUMBER_SIZE = 9
|
14
13
|
|
15
|
-
|
14
|
+
DENYLIST = %w[
|
16
15
|
00000000000
|
17
16
|
11111111111
|
18
17
|
22222222222
|
@@ -24,9 +23,12 @@ class CPF
|
|
24
23
|
88888888888
|
25
24
|
99999999999
|
26
25
|
12345678909
|
27
|
-
01234567890
|
28
26
|
].freeze
|
29
27
|
|
28
|
+
def self.format(number)
|
29
|
+
new(number).formatted
|
30
|
+
end
|
31
|
+
|
30
32
|
def self.valid?(number, strict: false)
|
31
33
|
new(number, strict).valid?
|
32
34
|
end
|
@@ -58,24 +60,30 @@ class CPF
|
|
58
60
|
end
|
59
61
|
|
60
62
|
def valid?
|
61
|
-
|
62
|
-
|
63
|
+
if strict && !(number.match?(REGEX) || number.match?(VALIDATION_SIZE_REGEX))
|
64
|
+
return false
|
65
|
+
end
|
63
66
|
|
64
|
-
|
67
|
+
return false unless stripped.match?(VALIDATION_SIZE_REGEX)
|
68
|
+
return false if DENYLIST.include?(stripped)
|
69
|
+
|
70
|
+
digits = numbers[0...NUMBER_SIZE]
|
65
71
|
digits << VerifierDigit.generate(digits)
|
66
72
|
digits << VerifierDigit.generate(digits)
|
67
73
|
|
68
74
|
digits[-2, 2] == numbers[-2, 2]
|
69
75
|
end
|
70
76
|
|
71
|
-
def
|
72
|
-
|
77
|
+
def number_without_verifier
|
78
|
+
numbers[0...NUMBER_SIZE].join
|
73
79
|
end
|
74
|
-
alias :eql? :==
|
75
80
|
|
76
|
-
|
81
|
+
def ==(other)
|
82
|
+
super || (other.instance_of?(self.class) && other.stripped == stripped)
|
83
|
+
end
|
84
|
+
alias eql? ==
|
77
85
|
|
78
|
-
def numbers
|
86
|
+
private def numbers
|
79
87
|
@numbers ||= stripped.each_char.to_a.map(&:to_i)
|
80
88
|
end
|
81
89
|
end
|
data/lib/cpf_cnpj/cli.rb
CHANGED
@@ -24,7 +24,6 @@ module CpfCnpj
|
|
24
24
|
@options ||= {}
|
25
25
|
end
|
26
26
|
|
27
|
-
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
28
27
|
def process_command
|
29
28
|
opts.banner = "Usage: #{bin_name} [options] [#{document_name} number]"
|
30
29
|
opts.separator ""
|
@@ -58,7 +57,6 @@ module CpfCnpj
|
|
58
57
|
opts.parse!(arguments)
|
59
58
|
opts.permute!(arguments)
|
60
59
|
end
|
61
|
-
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
62
60
|
|
63
61
|
def input
|
64
62
|
stdin.tty? ? arguments.first : stdin.read
|
@@ -77,6 +75,7 @@ module CpfCnpj
|
|
77
75
|
|
78
76
|
def validate(document)
|
79
77
|
return if document.valid?
|
78
|
+
|
80
79
|
stderr << "Error: Invalid number\n"
|
81
80
|
exit 1
|
82
81
|
end
|
data/lib/cpf_cnpj/generator.rb
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
module CpfCnpj
|
4
4
|
class Generator
|
5
|
-
NUMBERS =
|
5
|
+
NUMBERS = Array(0..9).freeze
|
6
6
|
|
7
7
|
def self.generate(size, verifier_digit_generator)
|
8
8
|
numbers = Array.new(size) { NUMBERS.sample }
|
9
9
|
numbers << verifier_digit_generator.generate(numbers)
|
10
10
|
numbers << verifier_digit_generator.generate(numbers)
|
11
|
-
numbers.join
|
11
|
+
numbers.join
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
data/lib/cpf_cnpj/version.rb
CHANGED
@@ -0,0 +1,112 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
module CNPJCli
|
6
|
+
class CheckTest < Minitest::Test
|
7
|
+
include RunCommand
|
8
|
+
|
9
|
+
%w[-c --check].each do |switch|
|
10
|
+
test "checks if provided number is valid [using #{switch}]" do
|
11
|
+
exit_status, stdout = run_command([switch, "54550752000155"])
|
12
|
+
|
13
|
+
assert_equal 0, exit_status
|
14
|
+
assert_equal "", stdout
|
15
|
+
end
|
16
|
+
|
17
|
+
test "outputs error if provided number is invalid [using #{switch}]" do
|
18
|
+
exit_status, _, stderr = run_command([switch, "invalid"])
|
19
|
+
|
20
|
+
assert_equal 1, exit_status
|
21
|
+
assert_includes stderr, "Error: Invalid number"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class HelpTest < Minitest::Test
|
27
|
+
include RunCommand
|
28
|
+
|
29
|
+
%w[-h --help].each do |switch|
|
30
|
+
test "outputs help [using #{switch}]" do
|
31
|
+
exit_status, _, stderr = run_command([switch])
|
32
|
+
|
33
|
+
assert_equal 1, exit_status
|
34
|
+
assert_includes stderr, "Usage: cnpj"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
test "outputs help on tail" do
|
39
|
+
exit_status, _, stderr = run_command([])
|
40
|
+
|
41
|
+
assert_equal 1, exit_status
|
42
|
+
assert_includes stderr, "Usage: cnpj"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class VersionTest < Minitest::Test
|
47
|
+
include RunCommand
|
48
|
+
|
49
|
+
%w[-v --version].each do |switch|
|
50
|
+
test "outputs version [using #{switch}]" do
|
51
|
+
exit_status, stdout = run_command([switch])
|
52
|
+
|
53
|
+
assert_equal 0, exit_status
|
54
|
+
assert_includes stdout, CNPJ::VERSION.to_s
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class GenerateTest < Minitest::Test
|
60
|
+
include RunCommand
|
61
|
+
|
62
|
+
%w[-g --generate].each do |switch|
|
63
|
+
test "generates number [using #{switch}]" do
|
64
|
+
exit_status, stdout = run_command([switch])
|
65
|
+
|
66
|
+
assert_equal 0, exit_status
|
67
|
+
assert_match CNPJ::REGEX, stdout
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
test "generates stripped number" do
|
72
|
+
exit_status, stdout = run_command(["-gs"])
|
73
|
+
|
74
|
+
assert_equal 0, exit_status
|
75
|
+
assert_match(/\A\d{14}\Z/, stdout)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class FormatTest < Minitest::Test
|
80
|
+
include RunCommand
|
81
|
+
|
82
|
+
%w[-f --format].each do |switch|
|
83
|
+
test "formats argument [using #{switch}]" do
|
84
|
+
exit_status, stdout = run_command([switch, "54550752000155"])
|
85
|
+
|
86
|
+
assert_equal 0, exit_status
|
87
|
+
assert_includes stdout, "54.550.752/0001-55"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
test "formats argument using stdin" do
|
92
|
+
exit_status, stdout = run_command(["--format"], input: "54550752000155")
|
93
|
+
|
94
|
+
assert_equal 0, exit_status
|
95
|
+
assert_includes stdout, "54.550.752/0001-55"
|
96
|
+
end
|
97
|
+
|
98
|
+
test "fails when providing invalid number" do
|
99
|
+
exit_status, _, stderr = run_command(["--format", "invalid"])
|
100
|
+
|
101
|
+
assert_equal 1, exit_status
|
102
|
+
assert_includes stderr, "Error: Invalid number"
|
103
|
+
end
|
104
|
+
|
105
|
+
test "fails when not providing a number" do
|
106
|
+
exit_status, _, stderr = run_command(["--format"])
|
107
|
+
|
108
|
+
assert_equal 1, exit_status
|
109
|
+
assert_includes stderr, "Error: Invalid number"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -5,11 +5,13 @@ require "test_helper"
|
|
5
5
|
class CNPJFormatterTest < Minitest::Test
|
6
6
|
test "formats strings without separators" do
|
7
7
|
number = "12345678901234"
|
8
|
+
|
8
9
|
assert_equal "12.345.678/9012-34", CNPJ::Formatter.format(number)
|
9
10
|
end
|
10
11
|
|
11
12
|
test "removes any non-numeric characters" do
|
12
13
|
number = "\n12$345[678/9012-34"
|
14
|
+
|
13
15
|
assert_equal "12345678901234", CNPJ::Formatter.strip(number)
|
14
16
|
end
|
15
17
|
end
|