cpf_cnpj 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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[A-Z\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
@@ -3,7 +3,7 @@
3
3
  require "test_helper"
4
4
 
5
5
  class CnpjTest < Minitest::Test
6
- test "blacklists common numbers" do
6
+ test "rejects common numbers" do
7
7
  refute CNPJ.valid?("00000000000000")
8
8
  refute CNPJ.valid?("11111111111111")
9
9
  refute CNPJ.valid?("22222222222222")
@@ -26,33 +26,63 @@ class CnpjTest < Minitest::Test
26
26
 
27
27
  test "validates formatted strings" do
28
28
  number = "54.550.752/0001-55"
29
+
30
+ assert CNPJ.valid?(number)
31
+ end
32
+
33
+ test "validates formatted strings with letters" do
34
+ number = "12.ABC.345/01DE-35"
35
+
36
+ assert CNPJ.valid?(number)
37
+ end
38
+
39
+ test "validates formatted strings with letters (lowercase)" do
40
+ number = "12.abc.345/01de-35"
41
+
29
42
  assert CNPJ.valid?(number)
30
43
  end
31
44
 
45
+ test "formats number" do
46
+ cnpj = CNPJ.new("54550752000155")
47
+
48
+ assert_equal "54.550.752/0001-55", cnpj.formatted
49
+ assert_equal "54.550.752/0001-55", CNPJ.format("54550752000155")
50
+ end
51
+
32
52
  test "validates unformatted strings" do
33
53
  number = "54550752000155"
54
+
34
55
  assert CNPJ.valid?(number)
35
56
  end
36
57
 
37
58
  test "validates messed strings" do
38
59
  number = "54550[752#0001..$55"
60
+
39
61
  assert CNPJ.valid?(number)
40
62
  end
41
63
 
42
64
  test "generates formatted number" do
43
- assert_match %r[\A\d{2}\.\d{3}\.\d{3}/\d{4}-\d{2}\z], CNPJ.generate(true)
65
+ assert_match(
66
+ %r[\A[A-Z\d]{2}\.[A-Z\d]{3}\.[A-Z\d]{3}/[A-Z\d]{4}-[A-Z\d]{2}\z],
67
+ CNPJ.generate(true)
68
+ )
44
69
  end
45
70
 
46
71
  test "generates stripped number" do
47
- assert_match(/\A\d{14}\z/, CNPJ.generate)
72
+ assert_match(/\A[A-Z\d]{14}\z/, CNPJ.generate)
48
73
  end
49
74
 
50
- test "rejects strings" do
75
+ test "rejects invalid strings" do
51
76
  refute CNPJ.valid?("aa.bb.ccc/dddd-ee")
52
77
  end
53
78
 
54
- test "rejects strings (strict)" do
79
+ test "strictly validates strings" do
55
80
  refute CNPJ.valid?("aa.bb.ccc/dddd-ee", strict: true)
81
+ refute CNPJ.valid?("54....550....752///0001---55", strict: true)
82
+ assert CNPJ.valid?("54.550.752/0001-55", strict: true)
83
+ assert CNPJ.valid?("54550752000155", strict: true)
84
+ assert CNPJ.valid?("12.ABC.345/01DE-35", strict: true)
85
+ assert CNPJ.valid?("12ABC34501DE35", strict: true)
56
86
  end
57
87
 
58
88
  test "compare objects by their numeric value" do
@@ -65,4 +95,11 @@ class CnpjTest < Minitest::Test
65
95
  refute_equal one, different
66
96
  refute_equal other, different
67
97
  end
98
+
99
+ test "returns number without verifier" do
100
+ assert_equal "545507520001",
101
+ CNPJ.new("54550752000155").number_without_verifier
102
+ assert_equal "12ABC34501DE",
103
+ CNPJ.new("12.ABC.345/01DE-35").number_without_verifier
104
+ end
68
105
  end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ module CPFCli
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, "76616598837"])
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: cpf"
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: cpf"
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, _stderr = run_command([switch])
52
+
53
+ assert_equal 0, exit_status
54
+ assert_includes stdout, CPF::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 CPF::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{11}\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, "76616598837"])
85
+
86
+ assert_equal 0, exit_status
87
+ assert_includes stdout, "766.165.988-37"
88
+ end
89
+ end
90
+
91
+ test "formats argument using stdin" do
92
+ exit_status, stdout = run_command(["--format"], input: "76616598837")
93
+
94
+ assert_equal 0, exit_status
95
+ assert_includes stdout, "766.165.988-37"
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
@@ -4,12 +4,14 @@ require "test_helper"
4
4
 
5
5
  class CPFFormatterTest < Minitest::Test
6
6
  test "formats strings without separators" do
7
- number = "12345678901234"
8
- assert_equal "12.345.678/9012-34", CNPJ::Formatter.format(number)
7
+ number = "12345678901"
8
+
9
+ assert_equal "123.456.789-01", CPF::Formatter.format(number)
9
10
  end
10
11
 
11
12
  test "removes any non-numeric characters" do
12
- number = "\n12$345[678/9012-34"
13
- assert_equal "12345678901234", CNPJ::Formatter.strip(number)
13
+ number = "\n12$345[678/901-"
14
+
15
+ assert_equal "12345678901", CPF::Formatter.strip(number)
14
16
  end
15
17
  end
@@ -3,8 +3,7 @@
3
3
  require "test_helper"
4
4
 
5
5
  class CpfTest < Minitest::Test
6
- test "blacklists common numbers" do
7
- refute CPF.valid?("01234567890")
6
+ test "rejects common numbers" do
8
7
  refute CPF.valid?("11111111111")
9
8
  refute CPF.valid?("22222222222")
10
9
  refute CPF.valid?("33333333333")
@@ -14,6 +13,7 @@ class CpfTest < Minitest::Test
14
13
  refute CPF.valid?("77777777777")
15
14
  refute CPF.valid?("88888888888")
16
15
  refute CPF.valid?("99999999999")
16
+ refute CPF.valid?("00000000000")
17
17
  refute CPF.valid?("12345678909")
18
18
  end
19
19
 
@@ -31,21 +31,25 @@ class CpfTest < Minitest::Test
31
31
 
32
32
  test "validates formatted strings" do
33
33
  number = "295.379.955-93"
34
+
34
35
  assert CPF.valid?(number)
35
36
  end
36
37
 
37
38
  test "validates unformatted strings" do
38
39
  number = "29537995593"
40
+
39
41
  assert CPF.valid?(number)
40
42
  end
41
43
 
42
44
  test "validates messed strings" do
43
45
  number = "295$379\n955...93"
46
+
44
47
  assert CPF.valid?(number)
45
48
  end
46
49
 
47
50
  test "strictly validates strings" do
48
51
  refute CPF.valid?("295$379\n955...93", strict: true)
52
+ refute CPF.valid?("295.......379.......955-----93", strict: true)
49
53
  assert CPF.valid?("295.379.955-93", strict: true)
50
54
  assert CPF.valid?("29537995593", strict: true)
51
55
  end
@@ -56,12 +60,15 @@ class CpfTest < Minitest::Test
56
60
 
57
61
  test "returns stripped number" do
58
62
  cpf = CPF.new("295.379.955-93")
63
+
59
64
  assert_equal "29537995593", cpf.stripped
60
65
  end
61
66
 
62
67
  test "formats number" do
63
68
  cpf = CPF.new("29537995593")
69
+
64
70
  assert_equal "295.379.955-93", cpf.formatted
71
+ assert_equal "295.379.955-93", CPF.format("29537995593")
65
72
  end
66
73
 
67
74
  test "generates formatted number" do
@@ -90,4 +97,10 @@ class CpfTest < Minitest::Test
90
97
  refute_equal one, different
91
98
  refute_equal other, different
92
99
  end
100
+
101
+ test "returns number without verifier" do
102
+ cpf = CPF.new("29537995593")
103
+
104
+ assert_equal "295379955", cpf.number_without_verifier
105
+ end
93
106
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RunCommand
4
+ class TTYStringIO < StringIO
5
+ def tty?
6
+ true
7
+ end
8
+ end
9
+
10
+ def run_command(args, input: nil)
11
+ stdout = StringIO.new
12
+ stderr = StringIO.new
13
+ stdin = input ? StringIO.new(input) : TTYStringIO.new
14
+ exit_status = 0
15
+
16
+ mod = if self.class.name.start_with?("CPF")
17
+ CPF
18
+ else
19
+ CNPJ
20
+ end
21
+
22
+ begin
23
+ CpfCnpj::CLI.new(mod, args, stdin, stdout, stderr).start
24
+ rescue SystemExit => error
25
+ exit_status = error.status
26
+ end
27
+
28
+ [exit_status, stdout.tap(&:rewind).read, stderr.tap(&:rewind).read]
29
+ end
30
+ end
data/test/test_helper.rb CHANGED
@@ -6,4 +6,4 @@ Bundler.require
6
6
  require "minitest/utils"
7
7
  require "minitest/autorun"
8
8
 
9
- require_relative "./support/capture_syscall"
9
+ require_relative "support/run_command"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpf_cnpj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-24 00:00:00.000000000 Z
11
+ date: 2024-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest-utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: pry-meta
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +53,21 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: minitest-utils
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-fnando
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
73
  - - ">="
@@ -62,9 +90,11 @@ executables:
62
90
  extensions: []
63
91
  extra_rdoc_files: []
64
92
  files:
93
+ - ".github/FUNDING.yml"
94
+ - ".github/dependabot.yml"
95
+ - ".github/workflows/ruby-tests.yml"
65
96
  - ".gitignore"
66
97
  - ".rubocop.yml"
67
- - ".travis.yml"
68
98
  - Gemfile
69
99
  - LICENSE
70
100
  - README.md
@@ -80,22 +110,24 @@ files:
80
110
  - lib/cpf/verifier_digit.rb
81
111
  - lib/cpf_cnpj.rb
82
112
  - lib/cpf_cnpj/cli.rb
83
- - lib/cpf_cnpj/generator.rb
84
113
  - lib/cpf_cnpj/version.rb
85
- - test/support/capture_syscall.rb
114
+ - test/cnpj/cli_test.rb
115
+ - test/cnpj/formatter_test.rb
116
+ - test/cnpj/generator_test.rb
117
+ - test/cnpj_test.rb
118
+ - test/cpf/cli_test.rb
119
+ - test/cpf/formatter_test.rb
120
+ - test/cpf/generator_test.rb
121
+ - test/cpf_test.rb
122
+ - test/support/run_command.rb
86
123
  - test/test_helper.rb
87
- - test/unit/cnpj/cli_test.rb
88
- - test/unit/cnpj/formatter_test.rb
89
- - test/unit/cnpj/generator_test.rb
90
- - test/unit/cnpj_test.rb
91
- - test/unit/cpf/cli_test.rb
92
- - test/unit/cpf/formatter_test.rb
93
- - test/unit/cpf/generator_test.rb
94
- - test/unit/cpf_test.rb
95
- homepage:
124
+ homepage: https://github.com/fnando/cpf_cnpj
96
125
  licenses: []
97
- metadata: {}
98
- post_install_message:
126
+ metadata:
127
+ homepage_uri: https://github.com/fnando/cpf_cnpj
128
+ source_code_uri: https://github.com/fnando/cpf_cnpj
129
+ rubygems_mfa_required: 'true'
130
+ post_install_message:
99
131
  rdoc_options: []
100
132
  require_paths:
101
133
  - lib
@@ -103,26 +135,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
135
  requirements:
104
136
  - - ">="
105
137
  - !ruby/object:Gem::Version
106
- version: '0'
138
+ version: 2.7.0
107
139
  required_rubygems_version: !ruby/object:Gem::Requirement
108
140
  requirements:
109
141
  - - ">="
110
142
  - !ruby/object:Gem::Version
111
143
  version: '0'
112
144
  requirements: []
113
- rubyforge_project:
114
- rubygems_version: 2.7.6
115
- signing_key:
145
+ rubygems_version: 3.5.16
146
+ signing_key:
116
147
  specification_version: 4
117
148
  summary: Validate, generate and format CPF/CNPJ numbers. Include command-line tools.
118
- test_files:
119
- - test/support/capture_syscall.rb
120
- - test/test_helper.rb
121
- - test/unit/cnpj/cli_test.rb
122
- - test/unit/cnpj/formatter_test.rb
123
- - test/unit/cnpj/generator_test.rb
124
- - test/unit/cnpj_test.rb
125
- - test/unit/cpf/cli_test.rb
126
- - test/unit/cpf/formatter_test.rb
127
- - test/unit/cpf/generator_test.rb
128
- - test/unit/cpf_test.rb
149
+ test_files: []
data/.travis.yml DELETED
@@ -1,8 +0,0 @@
1
- language: ruby
2
- sudo: false
3
- cache: bundler
4
- notifications:
5
- email: false
6
- rvm:
7
- - '2.4.2'
8
- - '2.3.5'
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CpfCnpj
4
- class Generator
5
- NUMBERS = [*0..9].freeze
6
-
7
- def self.generate(size, verifier_digit_generator)
8
- numbers = Array.new(size) { NUMBERS.sample }
9
- numbers << verifier_digit_generator.generate(numbers)
10
- numbers << verifier_digit_generator.generate(numbers)
11
- numbers.join("")
12
- end
13
- end
14
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "timeout"
4
-
5
- module CaptureSyscall
6
- def capture_syscall
7
- exit_status = nil
8
-
9
- stdout, stderr = Timeout.timeout(1) do
10
- capture_subprocess_io do
11
- yield
12
- exit_status = $CHILD_STATUS.exitstatus
13
- end
14
- end
15
-
16
- [exit_status, stdout, stderr]
17
- end
18
- end