cpf_cnpj 0.4.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 060f6cfefb56f74570849d75281ab0f5d00fe500
4
- data.tar.gz: 52d7c441db65b19ce5175aa82c168a670e708f06
2
+ SHA256:
3
+ metadata.gz: d7b0bd493ddd22ed0ebe7128fd0e8dc17c8ff15bfb1209300e1ce60ae03b2c6b
4
+ data.tar.gz: c2f7a21d000907e8779790d8b72ce6177b58ddaa2fb0f369941d3e3d1989eae1
5
5
  SHA512:
6
- metadata.gz: 21a524afa4993d82d12b1abe9ee15f957a786acf5ba89704dc5916f35946e475d50842ce40f78e56bd979c4ec66afdd90376da8d011b28e41e5203a31e8a5530
7
- data.tar.gz: 9c25eb4c0c480eb29b040b8595d1b1a9b9e768f21dd689f4c24adc0b4f22be4051624013398a4259c400985c1f843e7a35859681409b0b3a9205f0f8f53866e5
6
+ metadata.gz: b58f3e8547dda2131b290a4db50c0c84da0ccaf266ebd241cef0d34e43eb935d0e31d1310185749f9a929d19db01136484d1b54bbb1abb9c41d351409c6b685b
7
+ data.tar.gz: 3d62c15ab2f3b0be301a55a5328368bcca1ccf1d2b453c3e28c01fa868dc10c403690b2ee2d33cd0bba375271a8a20b2415a77f477d3c3d2b941fad4049f8417
@@ -0,0 +1,3 @@
1
+ ---
2
+ other: ["https://paypal.me/nandovieira/🍕"]
3
+ github: [fnando]
@@ -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
- AllCops:
2
- TargetRubyVersion: 2.3
1
+ ---
2
+ inherit_gem:
3
+ rubocop-fnando: .rubocop.yml
3
4
 
4
- Include:
5
- - '**/*.gemspec'
6
- - '**/*.podspec'
7
- - '**/*.jbuilder'
8
- - '**/*.rake'
9
- - '**/*.opal'
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/Documentation:
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
- [![Build Status](https://travis-ci.org/fnando/cpf_cnpj.svg)](https://travis-ci.org/fnando/cpf_cnpj)
4
- [![Code Climate](https://codeclimate.com/github/fnando/cpf_cnpj/badges/gpa.svg)](https://codeclimate.com/github/fnando/cpf_cnpj)
5
- [![Gem Version](https://badge.fury.io/rb/cpf_cnpj.svg)](http://badge.fury.io/rb/cpf_cnpj)
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 [CPF](http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas)/[CNPJ](http://en.wikipedia.org/wiki/CNPJ) magic. It allows you to create, validate and format CPF/CNPJ, even through the command-line.
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 and government sites.
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 <https://github.com/fnando/validators>.
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 documented below.
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.valid?(number) # Check if a CPF is valid
37
- CPF.generate # Generate a random CPF number
38
- CPF.generate(true) # Generate a formatted number
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 # Return formatted CPF (xxx.xxx.xxx-xx)
42
- cpf.stripped # Return stripped CPF (xxxxxxxxxxx)
43
- cpf.valid? # Check if CPF is 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 means that `532#####820------857\n96` is considered a valid number. To perform a strict validation use `strict: true`.
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
- task default: :test
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/cnpj CHANGED
@@ -3,4 +3,4 @@
3
3
 
4
4
  $LOAD_PATH.unshift "#{__dir__}/../lib"
5
5
  require "cpf_cnpj"
6
- CpfCnpj::CLI.new(CNPJ, ARGV, STDIN, STDOUT, STDERR).start
6
+ CpfCnpj::CLI.new(CNPJ, ARGV, $stdin, $stdout, $stderr).start
data/bin/cpf CHANGED
@@ -3,4 +3,4 @@
3
3
 
4
4
  $LOAD_PATH.unshift "#{__dir__}/../lib"
5
5
  require "cpf_cnpj"
6
- CpfCnpj::CLI.new(CPF, ARGV, STDIN, STDOUT, STDERR).start
6
+ CpfCnpj::CLI.new(CPF, ARGV, $stdin, $stdout, $stderr).start
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 |gem|
6
- gem.authors = ["Nando Vieira"]
7
- gem.email = ["fnando.vieira@gmail.com"]
8
- gem.description = "Validate, generate and format CPF/CNPJ numbers. Include command-line tools."
9
- gem.summary = gem.description
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
- gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
12
- gem.executables = gem.files.grep(%r[^bin/]).map {|f| File.basename(f) }
13
- gem.test_files = gem.files.grep(%r[^(test|spec|features)/])
14
- gem.name = "cpf_cnpj"
15
- gem.require_paths = ["lib"]
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
- gem.add_development_dependency "pry-meta"
19
- gem.add_development_dependency "rake"
20
- gem.add_development_dependency "minitest-utils"
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
@@ -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).gsub(/\A(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})\Z/, "\\1.\\2.\\3/\\4-\\5")
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)
@@ -6,7 +6,7 @@ class CNPJ
6
6
  index = 2
7
7
 
8
8
  sum = numbers.reverse.reduce(0) do |buffer, number|
9
- (buffer + number * index).tap do
9
+ (buffer + (number * index)).tap do
10
10
  index = index == 9 ? 2 : index + 1
11
11
  end
12
12
  end
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
- BLACKLIST = %w[
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,19 +59,30 @@ class CNPJ
56
59
  end
57
60
 
58
61
  def valid?
59
- return unless stripped =~ VALIDATION_SIZE_REGEX
60
- return if BLACKLIST.include?(stripped)
62
+ if strict && !(number.match?(REGEX) || number.match?(VALIDATION_SIZE_REGEX))
63
+ return false
64
+ end
65
+
66
+ return false unless stripped.match?(VALIDATION_SIZE_REGEX)
67
+ return false if DENYLIST.include?(stripped)
61
68
 
62
- digits = numbers[0...12]
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
- private
76
+ def ==(other)
77
+ super || (other.instance_of?(self.class) && other.stripped == stripped)
78
+ end
79
+ alias eql? ==
80
+
81
+ def number_without_verifier
82
+ numbers[0...NUMBER_SIZE].join
83
+ end
70
84
 
71
- def numbers
85
+ private def numbers
72
86
  @numbers ||= stripped.each_char.to_a.map(&:to_i)
73
87
  end
74
88
  end
data/lib/cpf/formatter.rb CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  class CPF
4
4
  class Formatter
5
- STRICT_REGEX = /[.-]/
6
- LOOSE_REGEX = /[^\d]/
5
+ STRICT_REGEX = /[.-]/.freeze
6
+ LOOSE_REGEX = /[^\d]/.freeze
7
7
 
8
8
  def self.format(number)
9
9
  strip(number).gsub(/\A(\d{3})(\d{3})(\d{3})(\d{2})\Z/, "\\1.\\2.\\3-\\4")
@@ -9,7 +9,7 @@ class CPF
9
9
  number * (modulus - index)
10
10
  end
11
11
 
12
- mod = multiplied.reduce(:+) % 11
12
+ mod = multiplied.sum % 11
13
13
  mod < 2 ? 0 : 11 - mod
14
14
  end
15
15
  end
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 = /^[0-9]{11}$/
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
- BLACKLIST = %w[
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,19 +60,30 @@ class CPF
58
60
  end
59
61
 
60
62
  def valid?
61
- return unless stripped =~ VALIDATION_SIZE_REGEX
62
- return if BLACKLIST.include?(stripped)
63
+ if strict && !(number.match?(REGEX) || number.match?(VALIDATION_SIZE_REGEX))
64
+ return false
65
+ end
66
+
67
+ return false unless stripped.match?(VALIDATION_SIZE_REGEX)
68
+ return false if DENYLIST.include?(stripped)
63
69
 
64
- digits = numbers[0...9]
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
- private
77
+ def number_without_verifier
78
+ numbers[0...NUMBER_SIZE].join
79
+ end
80
+
81
+ def ==(other)
82
+ super || (other.instance_of?(self.class) && other.stripped == stripped)
83
+ end
84
+ alias eql? ==
72
85
 
73
- def numbers
86
+ private def numbers
74
87
  @numbers ||= stripped.each_char.to_a.map(&:to_i)
75
88
  end
76
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
@@ -2,13 +2,13 @@
2
2
 
3
3
  module CpfCnpj
4
4
  class Generator
5
- NUMBERS = [*0..9].freeze
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
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CpfCnpj
4
- VERSION = "0.4.1"
4
+ VERSION = "0.6.0"
5
5
  end
6
6
 
7
7
  class CPF
@@ -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